summaryrefslogtreecommitdiff
path: root/old_curriculum/src/bin/generate_readme.rs
blob: a502feae554aedd99e4c0d38538eacaee8db9491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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(())
}