#build #build-dependencies #documentation #readme

build readme-rustdocifier

A library for rustdocifying README.md for inclusion in lib.rs

2 releases

0.1.1 Feb 17, 2022
0.1.0 Jan 1, 2022

#111 in Build Utils

Download history 24/week @ 2022-10-13 47/week @ 2022-10-20 72/week @ 2022-10-27 83/week @ 2022-11-03 72/week @ 2022-11-10 73/week @ 2022-11-17 153/week @ 2022-11-24 30/week @ 2022-12-01 100/week @ 2022-12-08 32/week @ 2022-12-15 57/week @ 2022-12-22 78/week @ 2022-12-29 191/week @ 2023-01-05 86/week @ 2023-01-12 101/week @ 2023-01-19 138/week @ 2023-01-26

521 downloads per month
Used in 10 crates (9 directly)

MIT license

32KB
746 lines

readme-rustdocifier

A library for rustdocifying README.md for inclusion in lib.rs.

  • Removes top-level header.
  • Changes other headers to be one level higher.
  • Converts package-internal docs.rs links to rustdoc format.
  • Doesn't change anything within code blocks.
  • (optional) Checks that converted links have correct version and crate name.
  • No unsafe.
  • No dependencies.

Usage

  • Add this to Cargo.toml:
[build-dependencies]
readme-rustdocifier = "0.1.0"
  • Create README.md.
  • Create build.rs with following content:
use std::{env, error::Error, fs, path::PathBuf};

const CRATE_NAME: &str = "your_crate_name_here";

fn main() -> Result<(), Box<dyn Error>> {
    println!("cargo:rerun-if-changed=README.md");
    fs::write(
        PathBuf::from(env::var("OUT_DIR")?).join("README-rustdocified.md"),
        readme_rustdocifier::rustdocify(
            &fs::read_to_string("README.md")?,
            &env::var("CARGO_PKG_NAME")?,
            Some(&env::var("CARGO_PKG_VERSION")?),
            Some(CRATE_NAME),
        )?,
    )?;
    Ok(())
}
  • Add this to start of lib.rs:
#![doc = include_str!(concat!(env!("OUT_DIR"), "/README-rustdocified.md"))]
  • Run cargo doc and see the generated documentation of your library.

Example README.md

## foo

A foo library.

### Usage

Create [`Foo::new`].

[`Foo::new`]: https://docs.rs/foo/*/foo/struct.Foo.html#method.new

Above README.md is rustdocified to:


A foo library.

## Usage

Create [`Foo::new`].

[`Foo::new`]: crate::Foo::new

Lines like [...]: https://docs.rs/PACKAGE/... are converted to rustdoc format.

Following conversions are done:

  • https://docs.rs/PACKAGE (1)
    • crate
  • https://docs.rs/PACKAGE#fragment (1)
    • crate#fragment
  • https://docs.rs/PACKAGE/VERSION (2)
    • crate
  • https://docs.rs/PACKAGE/VERSION#fragment (2)
    • crate#fragment
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES (2)
    • crate::MODULES
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES#fragment (2)
    • crate::MODULES#fragment
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/enum.ENUM.html
    • crate::MODULES::ENUM
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/enum.ENUM.html#method.METHOD
    • crate::MODULES::ENUM::METHOD
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/enum.ENUM.html#variant.VARIANT
    • crate::MODULES::ENUM::VARIANT
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/enum.ENUM.html#fragment
    • crate::MODULES::ENUM#fragment
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/fn.FUNCTION.html
    • crate::MODULES::FUNCTION
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/fn.FUNCTION.html#fragment
    • crate::MODULES::FUNCTION#fragment
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/macro.MACRO.html
    • crate::MODULES::MACRO
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/macro.MACRO.html#fragment
    • crate::MODULES::MACRO#fragment
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/struct.STRUCT.html
    • crate::MODULES::STRUCT
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/struct.STRUCT.html#method.METHOD
    • crate::MODULES::STRUCT::METHOD
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/struct.STRUCT.html#fragment
    • crate::MODULES::STRUCT#fragment
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/trait.TRAIT.html
    • crate::MODULES::TRAIT
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/trait.TRAIT.html#tymethod.METHOD
    • crate::MODULES::TRAIT::METHOD
  • https://docs.rs/PACKAGE/VERSION/CRATE/MODULES/trait.TRAIT.html#fragment
    • crate::MODULES::TRAIT#fragment

Notes:

  • (1) Can have optional / at path end.
  • (2) Can have optional / or /index.html at path end.
  • /MODULES and corresponding ::MODULES can be empty.

Safety

This crate doesn't use any unsafe code. This is enforced by #![forbid(unsafe_code)].

No runtime deps