#build-dependencies #documentation #build

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

#143 in Build Utils

Download history 6170/week @ 2024-07-27 7542/week @ 2024-08-03 7878/week @ 2024-08-10 9196/week @ 2024-08-17 15725/week @ 2024-08-24 7072/week @ 2024-08-31 5714/week @ 2024-09-07 7578/week @ 2024-09-14 8495/week @ 2024-09-21 6686/week @ 2024-09-28 7680/week @ 2024-10-05 7794/week @ 2024-10-12 6900/week @ 2024-10-19 9933/week @ 2024-10-26 9626/week @ 2024-11-02 6480/week @ 2024-11-09

33,959 downloads per month
Used in 44 crates (11 directly)

MIT license

33KB
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`].


Above README.md is rustdocified to:


A foo library.

## Usage

Create [`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