From 12fe9371f649aad363b13828156d36941907479f Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 3 May 2022 23:05:55 -0700 Subject: [PATCH] Generate book from readme (#1155) --- .github/workflows/ci.yaml | 22 ++++++++++++++- .gitignore | 2 ++ Cargo.lock | 47 +++++++++++++++++++++++++++++++ Cargo.toml | 2 +- README.md | 2 ++ bin/generate-book/Cargo.toml | 9 ++++++ bin/generate-book/src/main.rs | 51 ++++++++++++++++++++++++++++++++++ book/book.toml | 9 ++++++ src/function.rs | 2 +- src/settings.rs | 4 +-- {docs => www}/.nojekyll | 0 {docs => www}/CNAME | 0 {docs => www}/favicon.ico | Bin {docs => www}/index.css | 0 {docs => www}/index.html | 0 {docs => www}/install.sh | 0 16 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 bin/generate-book/Cargo.toml create mode 100644 bin/generate-book/src/main.rs create mode 100644 book/book.toml rename {docs => www}/.nojekyll (100%) rename {docs => www}/CNAME (100%) rename {docs => www}/favicon.ico (100%) rename {docs => www}/index.css (100%) rename {docs => www}/index.html (100%) rename {docs => www}/install.sh (100%) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1ed2b1f..fc92cea 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,7 +44,7 @@ jobs: with: components: clippy, rustfmt override: true - toolchain: 1.47.0 + toolchain: 1.53.0 - uses: Swatinem/rust-cache@v1 @@ -73,3 +73,23 @@ jobs: sudo apt-get update sudo apt-get install ripgrep ./bin/forbid + + - name: Install `mdbook` + if: ${{ matrix.os == 'ubuntu-latest' }} + uses: peaceiris/actions-mdbook@v1 + with: + mdbook-version: latest + + - name: Build book + if: ${{ matrix.os == 'ubuntu-latest' }} + run: | + cargo run --package generate-book + mdbook build book + + - name: Deploy Pages + uses: peaceiris/actions-gh-pages@v3 + if: github.ref == 'refs/heads/master' && matrix.os == 'ubuntu-latest' + with: + github_token: ${{secrets.GITHUB_TOKEN}} + publish_branch: gh-pages + publish_dir: www diff --git a/.gitignore b/.gitignore index 9f62f02..6e98dda 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ /.vagrant /README.html +/book/src +/www/book /fuzz/artifacts /fuzz/corpus /fuzz/target diff --git a/Cargo.lock b/Cargo.lock index d8ab86c..f7e0e4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -174,6 +174,23 @@ dependencies = [ "instant", ] +[[package]] +name = "generate-book" +version = "0.0.0" +dependencies = [ + "pulldown-cmark", + "pulldown-cmark-to-cmark", +] + +[[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + [[package]] name = "heck" version = "0.3.3" @@ -358,6 +375,27 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "pulldown-cmark" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34f197a544b0c9ab3ae46c359a7ec9cbbb5c7bf97054266fecb7ead794a181d6" +dependencies = [ + "bitflags", + "getopts", + "memchr", + "unicase", +] + +[[package]] +name = "pulldown-cmark-to-cmark" +version = "10.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eafb76310f7dc895b5d83d24f2a00e244fc8a68ef94f4257eb4060984d0385f" +dependencies = [ + "pulldown-cmark", +] + [[package]] name = "quote" version = "1.0.18" @@ -619,6 +657,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae" +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-segmentation" version = "1.9.0" diff --git a/Cargo.toml b/Cargo.toml index ef38c19..0e343ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ categories = ["command-line-utilities", "development-tools"] keywords = ["command-line", "task", "runner", "development", "utility"] [workspace] -members = [".", "bin/ref-type"] +members = [".", "bin/ref-type", "bin/generate-book"] [dependencies] ansi_term = "0.12.0" diff --git a/README.md b/README.md index 134656e..2d0c184 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ `just` is a handy way to save and run project-specific commands. +This readme is also available as a [book](https://just.systems/book/); + (非官方中文文档,[这里](https://github.com/chinanf-boy/just-zh),快看过来!) Commands, called recipes, are stored in a file called `justfile` with syntax inspired by `make`: diff --git a/bin/generate-book/Cargo.toml b/bin/generate-book/Cargo.toml new file mode 100644 index 0000000..1a3bd2b --- /dev/null +++ b/bin/generate-book/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "generate-book" +version = "0.0.0" +edition = "2018" +publish = false + +[dependencies] +pulldown-cmark = "0.9.1" +pulldown-cmark-to-cmark = "10.0.1" diff --git a/bin/generate-book/src/main.rs b/bin/generate-book/src/main.rs new file mode 100644 index 0000000..b870cae --- /dev/null +++ b/bin/generate-book/src/main.rs @@ -0,0 +1,51 @@ +use { + pulldown_cmark::{ + Event, + HeadingLevel::{H2, H3}, + Options, Parser, Tag, + }, + pulldown_cmark_to_cmark::cmark, + std::{error::Error, fs}, +}; + +fn main() -> Result<(), Box> { + fs::remove_dir_all("book/src").ok(); + fs::create_dir("book/src")?; + + let txt = fs::read_to_string("README.md")?; + + let mut chapters = vec![(1usize, Vec::new())]; + + for event in Parser::new_ext(&txt, Options::all()) { + if let Event::Start(Tag::Heading(level @ (H2 | H3), ..)) = event { + chapters.push((if level == H2 { 2 } else { 3 }, Vec::new())); + } + chapters.last_mut().unwrap().1.push(event); + } + + let mut summary = String::new(); + + for (i, (level, chapter)) in chapters.into_iter().enumerate() { + let mut txt = String::new(); + cmark(chapter.iter(), &mut txt)?; + let title = if i == 0 { + txt = txt.split_inclusive('\n').skip(1).collect::(); + "Introduction" + } else { + txt.lines().next().unwrap().split_once(' ').unwrap().1 + }; + + let path = format!("book/src/chapter_{}.md", i + 1); + fs::write(&path, &txt)?; + summary.push_str(&format!( + "{}- [{}](chapter_{}.md)\n", + " ".repeat((level.saturating_sub(1)) * 4), + title, + i + 1 + )); + } + + fs::write("book/src/SUMMARY.md", summary)?; + + Ok(()) +} diff --git a/book/book.toml b/book/book.toml new file mode 100644 index 0000000..7dc4cac --- /dev/null +++ b/book/book.toml @@ -0,0 +1,9 @@ +[book] +authors = ["Casey Rodarmor"] +language = "en" +multilingual = false +src = "src" +title = "Just Programmer's Manual" + +[build] +build-dir = "../www/book" diff --git a/src/function.rs b/src/function.rs index 96dc6ed..ccdb8a5 100644 --- a/src/function.rs +++ b/src/function.rs @@ -1,4 +1,4 @@ -#![allow(clippy::unknown_clippy_lints)] +#![allow(unknown_lints)] #![allow(clippy::unnecessary_wraps)] use crate::common::*; diff --git a/src/settings.rs b/src/settings.rs index 9ba196d..ed5432e 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -192,10 +192,10 @@ mod tests { if cfg!(windows) { assert_eq!(settings.shell_binary(&config), "powershell.exe"); - assert_eq!(settings.shell_arguments(&config), vec!["-nice"]); } else { assert_eq!(settings.shell_binary(&config), "sh"); - assert_eq!(settings.shell_arguments(&config), vec!["-nice"]); } + + assert_eq!(settings.shell_arguments(&config), vec!["-nice"]); } } diff --git a/docs/.nojekyll b/www/.nojekyll similarity index 100% rename from docs/.nojekyll rename to www/.nojekyll diff --git a/docs/CNAME b/www/CNAME similarity index 100% rename from docs/CNAME rename to www/CNAME diff --git a/docs/favicon.ico b/www/favicon.ico similarity index 100% rename from docs/favicon.ico rename to www/favicon.ico diff --git a/docs/index.css b/www/index.css similarity index 100% rename from docs/index.css rename to www/index.css diff --git a/docs/index.html b/www/index.html similarity index 100% rename from docs/index.html rename to www/index.html diff --git a/docs/install.sh b/www/install.sh similarity index 100% rename from docs/install.sh rename to www/install.sh