Include code without hidden lines for nostarch files

This commit is contained in:
Carol (Nichols || Goulding) 2019-09-29 20:14:22 -04:00
parent 8b0c2161f5
commit 6579c05cb9
No known key found for this signature in database
GPG Key ID: D04B39A6CA243902
3 changed files with 98 additions and 6 deletions

View File

@ -25,6 +25,10 @@ path = "tools/src/bin/link2print.rs"
name = "release_listings"
path = "tools/src/bin/release_listings.rs"
[[bin]]
name = "remove_hidden_lines"
path = "tools/src/bin/remove_hidden_lines.rs"
[[bin]]
name = "remove_links"
path = "tools/src/bin/remove_links.rs"

View File

@ -6,19 +6,22 @@ cargo build --release
mkdir -p tmp
rm -rf tmp/*.md
rm -rf tmp/markdown
# Get all the Markdown files in the src dir,
ls src/${1:-""}*.md | \
# except for `SUMMARY.md`.
grep -v SUMMARY.md | \
# Render the book as Markdown to include all the code listings
MDBOOK_OUTPUT__MARKDOWN=1 mdbook build -d tmp
# Get all the Markdown files
ls tmp/markdown/${1:-""}*.md | \
# Extract just the filename so we can reuse it easily.
xargs -n 1 basename | \
# Remove all links followed by `<!-- ignore -->``, then
# Change all remaining links from Markdown to italicized inline text.
while IFS= read -r filename; do
< "src/$filename" ./target/release/remove_links \
< "tmp/markdown/$filename" ./target/release/remove_links \
| ./target/release/link2print \
| ./target/release/remove_markup > "tmp/$filename"
| ./target/release/remove_markup \
| ./target/release/remove_hidden_lines > "tmp/$filename"
done
# Concatenate the files into the `nostarch` dir.
./target/release/concat_chapters tmp nostarch

View File

@ -0,0 +1,85 @@
use std::io;
use std::io::prelude::*;
fn main() {
write_md(remove_hidden_lines(&read_md()));
}
fn read_md() -> String {
let mut buffer = String::new();
match io::stdin().read_to_string(&mut buffer) {
Ok(_) => buffer,
Err(error) => panic!(error),
}
}
fn write_md(output: String) {
write!(io::stdout(), "{}", output).unwrap();
}
fn remove_hidden_lines(input: &str) -> String {
let mut resulting_lines = vec![];
let mut within_codeblock = false;
for line in input.lines() {
if line.starts_with("```") {
within_codeblock = !within_codeblock;
}
if !within_codeblock || (!line.starts_with("# ") && line != "#") {
resulting_lines.push(line)
}
}
resulting_lines.join("\n")
}
#[cfg(test)]
mod tests {
use crate::remove_hidden_lines;
#[test]
fn hidden_line_in_code_block_is_removed() {
let input = r#"
In this listing:
```
fn main() {
# secret
}
```
you can see that...
"#;
let output = remove_hidden_lines(input);
let desired_output = r#"
In this listing:
```
fn main() {
}
```
you can see that...
"#;
assert_eq!(output, desired_output);
}
#[test]
fn headings_arent_removed() {
let input = r#"
# Heading 1
"#;
let output = remove_hidden_lines(input);
let desired_output = r#"
# Heading 1
"#;
assert_eq!(output, desired_output);
}
}