rustlings-solutions/old_curriculum/src/bin/generate_readme.rs
2018-04-26 21:29:11 +02:00

50 lines
1.7 KiB
Rust

// This script reads README-template.md and generates the playground links
// from the Rust source files in the various directories.
// To add a new exercise, add it to the appropriate place in README-template.md
// and then make sure to recompile this script (because the template gets
// included at compile time and then run it to generate a new version of
// README.md.
extern crate handlebars;
extern crate prlink;
#[macro_use]
extern crate serde_json;
use handlebars::{Handlebars, Helper, RenderContext, RenderError};
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
fn main() {
let mut template_file = File::open("README-template.hbs").unwrap();
let mut template = String::new();
template_file.read_to_string(&mut template).unwrap();
let autogenerated_notice = "This file was autogenerated by the script in src/bin/generate_readme.rs.
Please edit either the script or the template in README-template.md in
order to make changes here rather than committing the changes directly.";
let mut generated_readme = File::create("README.md").unwrap();
let mut hbs = Handlebars::new();
hbs.register_helper("playground_link", Box::new(playground_link_helper));
write!(
generated_readme,
"{}",
hbs.render_template(
&template,
&json!({ "autogenerated_notice": autogenerated_notice }),
).unwrap()
).unwrap();
}
fn playground_link_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
let filename = PathBuf::from(h.param(0).unwrap().value().as_str().unwrap());
let link = prlink::linkify_file(&filename);
rc.writer.write(link.into_bytes().as_ref())?;
Ok(())
}