Skip to content

Converting to mdbook #53

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

Closed
wants to merge 9 commits into from
Closed
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: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.log
book
target
src
!src/main.rs
!src/SUMMARY.md
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "building_script"
version = "0.1.0"
authors = ["Luxko <[email protected]>"]

[dependencies]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ language.
Contributions are very welcome!

You should start with [the template](template.md). Copy it into the appropriate
directory, edit it, and submit a PR. You might not want every section, and you
might want to add extra sections.
directory, edit it, test it with `cargo run`, and submit a PR. You might not want
every section, and you might want to add extra sections.

We suggest leaving a comment on the [issue tracker](https://github.com/rust-unofficial/patterns/issues)
so that other people don't start working on the same topic.
Expand Down
Empty file added anti_patterns/README.md
Empty file.
Empty file added idioms/README.md
Empty file.
Empty file added patterns/README.md
Empty file.
25 changes: 25 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Summary

- [Introduction](./intro.md)
- [Idioms](./idioms/README.md)
* [Constructor](./idioms/ctor.md)
* [Concatenating Strings with `format!`](./idioms/concat-format.md)
* [Privacy For Extensibility](./idioms/priv-extend.md)
* [Collections Are Smart Pointers](./idioms/deref.md)
* [Finalisation in Sestructors](./idioms/dtor-finally.md)
* [Iterating over an `Option`](./idioms/option-iter.md)
* [Pass Variables to Closure](./idioms/pass-var-to-closure.md)
* [`mem::replace(_)`](./idioms/mem-replace.md)
- [Design Patterns](./patterns/README.md)
* [Builder](./patterns/builder.md)
* [RAII Guards](./patterns/RAII.md)
* [Newtype](./patterns/newtype.md)
* [Entry API](./patterns/entry.md)
* [Visitor](./patterns/visitor.md)
* [Fold](./patterns/fold.md)
* [Prefer Small Crates](./patterns/small-crates.md)
* [Late Bound Bounds](./patterns/late-bounds.md)
* [Compose Structs](./patterns/compose-structs.md)
- [Anti-patterns](./anti_patterns/README.md)
* [Deref Polymorphism](./anti_patterns/deref.md)
* [`#[deny(warnings)]`](./anti_patterns/deny-warnings.md)
114 changes: 114 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//! Building scripts for mdbook
use std::process::Command;
use std::path::{Path, PathBuf};

const HELP_MSG: &'static str = r"USAGE:
cargo run <SUBCOMMAND>

SUBCOMMANDS:
render Build the book from the markdown files
test Test that code samples compile";

fn main() {
let mut args = std::env::args();
let _ = args.next();
let _guard = preprocess();
match args.next() {
None => default(),
Some(ref cmd) if cmd == "render" => rendering(),
Some(ref cmd) if cmd == "test" => testing(),
_ => {
println!("{}", HELP_MSG);
}
}
}

fn default() {
println!("Building start...");
testing();
rendering();
println!("Building complete.");
}

fn testing() {
print!("Testing...");
let mut test_proc = Command::new("mdbook")
.arg("test")
.spawn()
.expect("Failed to start the testing process");
let ecode = test_proc.wait().expect("Failed to finish the testing process");
assert!(ecode.success());
println!("Done.");
}

fn rendering() {
print!("Rendering...");
let mut render_proc = Command::new("mdbook")
.arg("build")
.spawn()
.expect("Failed to start the rendering process");
let ecode = render_proc.wait().expect("Failed to finish the rendering process");
assert!(ecode.success());
println!("Done.");
}

struct Guard {
olds: Vec<PathBuf>,
news: Vec<PathBuf>,
}

impl Drop for Guard {
fn drop(&mut self) {
postprocess(&self.olds, &self.news);
}
}

fn preprocess() -> Guard {
let root = std::env::current_dir().expect("WTF current directory does not exist");
let file_paths = {
fn file_paths(spath: &Path) -> Vec<String> {
use std::io::Read;
let scontent = {
let sfile = std::fs::File::open(spath).expect("Failed to open SUMMARY.md");
let mut buf = String::with_capacity(sfile.metadata().unwrap().len() as usize);
(&sfile).read_to_string(&mut buf).expect("Failed to read from SUMMARY.md");
buf
};

let mut paths = Vec::new();
for line in scontent.lines() {
if let Some(index) = line.find("(./") {
let (_, path) = line.split_at(index + 3);
paths.push(path[..path.len() - 1].to_owned());
}
}
paths
}
let mut summary_path = root.to_path_buf();
summary_path.push("src");
summary_path.push("SUMMARY.md");
file_paths(summary_path.as_path())
};
let mut ret = Guard {
olds: Vec::new(),
news: Vec::new(),
};
for path in &file_paths {
let mut old = root.to_path_buf();
old.push(&path);
let mut new = root.to_path_buf();
new.push("src");
new.push(&path);
std::fs::create_dir_all(new.parent().unwrap()).expect("Failed to move a file");
std::fs::rename(old.as_path(), new.as_path()).expect("Failed to move a file");
ret.olds.push(old);
ret.news.push(new);
}
ret
}
fn postprocess(olds: &Vec<PathBuf>, news: &Vec<PathBuf>) {
assert_eq!(olds.len(), news.len());
for i in 0..olds.len() {
std::fs::rename(news[i].as_path(), olds[i].as_path()).expect("Failed to move back!");
}
}