Generate book from readme (#1155)

This commit is contained in:
Casey Rodarmor 2022-05-03 23:05:55 -07:00 committed by GitHub
parent e5ce303845
commit 12fe9371f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 145 additions and 5 deletions

View File

@ -44,7 +44,7 @@ jobs:
with: with:
components: clippy, rustfmt components: clippy, rustfmt
override: true override: true
toolchain: 1.47.0 toolchain: 1.53.0
- uses: Swatinem/rust-cache@v1 - uses: Swatinem/rust-cache@v1
@ -73,3 +73,23 @@ jobs:
sudo apt-get update sudo apt-get update
sudo apt-get install ripgrep sudo apt-get install ripgrep
./bin/forbid ./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

2
.gitignore vendored
View File

@ -1,5 +1,7 @@
/.vagrant /.vagrant
/README.html /README.html
/book/src
/www/book
/fuzz/artifacts /fuzz/artifacts
/fuzz/corpus /fuzz/corpus
/fuzz/target /fuzz/target

47
Cargo.lock generated
View File

@ -174,6 +174,23 @@ dependencies = [
"instant", "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]] [[package]]
name = "heck" name = "heck"
version = "0.3.3" version = "0.3.3"
@ -358,6 +375,27 @@ dependencies = [
"unicode-xid", "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]] [[package]]
name = "quote" name = "quote"
version = "1.0.18" version = "1.0.18"
@ -619,6 +657,15 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae" 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]] [[package]]
name = "unicode-segmentation" name = "unicode-segmentation"
version = "1.9.0" version = "1.9.0"

View File

@ -13,7 +13,7 @@ categories = ["command-line-utilities", "development-tools"]
keywords = ["command-line", "task", "runner", "development", "utility"] keywords = ["command-line", "task", "runner", "development", "utility"]
[workspace] [workspace]
members = [".", "bin/ref-type"] members = [".", "bin/ref-type", "bin/generate-book"]
[dependencies] [dependencies]
ansi_term = "0.12.0" ansi_term = "0.12.0"

View File

@ -11,6 +11,8 @@
`just` is a handy way to save and run project-specific commands. `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),快看过来!) (非官方中文文档,[这里](https://github.com/chinanf-boy/just-zh),快看过来!)
Commands, called recipes, are stored in a file called `justfile` with syntax inspired by `make`: Commands, called recipes, are stored in a file called `justfile` with syntax inspired by `make`:

View File

@ -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"

View File

@ -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<dyn Error>> {
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::<String>();
"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(())
}

9
book/book.toml Normal file
View File

@ -0,0 +1,9 @@
[book]
authors = ["Casey Rodarmor"]
language = "en"
multilingual = false
src = "src"
title = "Just Programmer's Manual"
[build]
build-dir = "../www/book"

View File

@ -1,4 +1,4 @@
#![allow(clippy::unknown_clippy_lints)] #![allow(unknown_lints)]
#![allow(clippy::unnecessary_wraps)] #![allow(clippy::unnecessary_wraps)]
use crate::common::*; use crate::common::*;

View File

@ -192,10 +192,10 @@ mod tests {
if cfg!(windows) { if cfg!(windows) {
assert_eq!(settings.shell_binary(&config), "powershell.exe"); assert_eq!(settings.shell_binary(&config), "powershell.exe");
assert_eq!(settings.shell_arguments(&config), vec!["-nice"]);
} else { } else {
assert_eq!(settings.shell_binary(&config), "sh"); assert_eq!(settings.shell_binary(&config), "sh");
assert_eq!(settings.shell_arguments(&config), vec!["-nice"]);
} }
assert_eq!(settings.shell_arguments(&config), vec!["-nice"]);
} }
} }

View File

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 53 KiB