From 97fde19a93a59d8c0f8401c5520a396eec5deccb Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 8 Jun 2022 20:04:06 -0500 Subject: [PATCH] Build Chinese language user manual (#1219) --- .github/workflows/ci.yaml | 6 ++- .gitignore | 3 +- README.md | 2 +- README.中文.md | 2 +- bin/generate-book/src/main.rs | 88 ++++++++++++++++++++++------------- book/{ => en}/book.toml | 3 +- book/zh/book.toml | 8 ++++ www/index.html | 6 ++- 8 files changed, 78 insertions(+), 40 deletions(-) rename book/{ => en}/book.toml (70%) create mode 100644 book/zh/book.toml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3950c11..bb527d3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -84,8 +84,10 @@ jobs: if: ${{ matrix.os == 'ubuntu-latest' }} run: | cargo run --package generate-book - mdbook build book - cp screenshot.png www/man + mdbook build book/en + mdbook build book/zh + cp screenshot.png www/man/en + cp screenshot.png www/man/zh - name: Deploy Pages uses: peaceiris/actions-gh-pages@v3 diff --git a/.gitignore b/.gitignore index fae4012..410265d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /.vagrant /README.html -/book/src +/book/en/src +/book/zh/src /fuzz/artifacts /fuzz/corpus /fuzz/target diff --git a/README.md b/README.md index 6e650e0..ddaf0c6 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ `just` is a handy way to save and run project-specific commands. -This readme is also available as a [book](https://just.systems/man/); +This readme is also available as a [book](https://just.systems/man/en/); (中文文档在 [这里](README.中文.md), 快看过来!) diff --git a/README.中文.md b/README.中文.md index 2e381b6..1795ae1 100644 --- a/README.中文.md +++ b/README.中文.md @@ -23,7 +23,7 @@ `just` 为您提供一种保存和运行项目特有命令的便捷方式。 -本指南同时也可以以 [书](https://just.systems/man/) 的形式提供在线阅读; +本指南同时也可以以 [书](https://just.systems/man/zh/) 的形式提供在线阅读; 命令,在此也称为配方,存储在一个名为 `justfile` 的文件中,其语法受 `make` 启发: diff --git a/bin/generate-book/src/main.rs b/bin/generate-book/src/main.rs index b870cae..645f26c 100644 --- a/bin/generate-book/src/main.rs +++ b/bin/generate-book/src/main.rs @@ -8,44 +8,68 @@ use { std::{error::Error, fs}, }; -fn main() -> Result<(), Box> { - fs::remove_dir_all("book/src").ok(); - fs::create_dir("book/src")?; +enum Language { + English, + Chinese, +} - 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())); +impl Language { + fn code(&self) -> &'static str { + match self { + Self::English => "en", + Self::Chinese => "zh", } - 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 - )); + fn suffix(&self) -> &'static str { + match self { + Self::English => "", + Self::Chinese => ".中文", + } } +} - fs::write("book/src/SUMMARY.md", summary)?; +fn main() -> Result<(), Box> { + for language in [Language::English, Language::Chinese] { + let src = format!("book/{}/src", language.code()); + fs::remove_dir_all(&src).ok(); + fs::create_dir(&src)?; + + let txt = fs::read_to_string(format!("README{}.md", language.suffix()))?; + + 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!("{}/chapter_{}.md", src, i + 1); + fs::write(&path, &txt)?; + summary.push_str(&format!( + "{}- [{}](chapter_{}.md)\n", + " ".repeat((level.saturating_sub(1)) * 4), + title, + i + 1 + )); + } + + fs::write(format!("{}/SUMMARY.md", src), summary).unwrap(); + } Ok(()) } diff --git a/book/book.toml b/book/en/book.toml similarity index 70% rename from book/book.toml rename to book/en/book.toml index 39ee791..22d0435 100644 --- a/book/book.toml +++ b/book/en/book.toml @@ -1,9 +1,8 @@ [book] authors = ["Casey Rodarmor"] language = "en" -multilingual = false src = "src" title = "Just Programmer's Manual" [build] -build-dir = "../www/man" +build-dir = "../../www/man/en" diff --git a/book/zh/book.toml b/book/zh/book.toml new file mode 100644 index 0000000..e0d677e --- /dev/null +++ b/book/zh/book.toml @@ -0,0 +1,8 @@ +[book] +authors = ["Casey Rodarmor"] +language = "zh" +src = "src" +title = "Just Programmer's Manual" + +[build] +build-dir = "../../www/man/zh" diff --git a/www/index.html b/www/index.html index 2a0c24b..a8be532 100644 --- a/www/index.html +++ b/www/index.html @@ -12,9 +12,13 @@
github - manual + manual discord crates.io +
+ 手冊 +
+