Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Chinese language book #1219

Merged
merged 7 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/.vagrant
/README.html
/book/src
/book/en/src
/book/zh/src
/fuzz/artifacts
/fuzz/corpus
/fuzz/target
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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), 快看过来!)

Expand Down
2 changes: 1 addition & 1 deletion README.中文.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

`just` 为您提供一种保存和运行项目特有命令的便捷方式。

本指南同时也可以以 [书](https://just.systems/man/) 的形式提供在线阅读;
本指南同时也可以以 [书](https://just.systems/man/zh/) 的形式提供在线阅读;

命令,在此也称为配方,存储在一个名为 `justfile` 的文件中,其语法受 `make` 启发:

Expand Down
86 changes: 55 additions & 31 deletions bin/generate-book/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,68 @@ use {
std::{error::Error, fs},
};

enum Language {
English,
Chinese,
}

impl Language {
fn code(&self) -> &'static str {
match self {
Self::English => "en",
Self::Chinese => "zh",
}
}

fn suffix(&self) -> &'static str {
match self {
Self::English => "",
Self::Chinese => ".中文",
}
}
}

fn main() -> Result<(), Box<dyn Error>> {
fs::remove_dir_all("book/src").ok();
fs::create_dir("book/src")?;
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("README.md")?;
let txt = fs::read_to_string(format!("README{}.md", language.suffix()))?;

let mut chapters = vec![(1usize, Vec::new())];
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()));
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);
}
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
));
}
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
};

fs::write("book/src/SUMMARY.md", summary)?;
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(())
}
3 changes: 1 addition & 2 deletions book/book.toml → book/en/book.toml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions book/zh/book.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[book]
authors = ["Casey Rodarmor"]
language = "zh"
src = "src"
title = "Just Programmer's Manual"

[build]
build-dir = "../../www/man/zh"
6 changes: 5 additions & 1 deletion www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
<div id="s"></div>
<div id="t"></div>
<a href="https://github.com/casey/just">github</a>
<a href="man/">manual</a>
<a href="man/en/">manual</a>
<a href="https://discord.gg/ezYScXR">discord</a>
<a href="https://crates.io/crates/just">crates.io</a>
<div></div>
<a href="man/zh/">手冊</a>
<div></div>
<div></div>
</body>
</html>
<!-- Love, Casey -->