Make a handlebars helper that uses prlink to generate a link from a file
This commit is contained in:
@@ -7,13 +7,15 @@
|
||||
// README.md.
|
||||
|
||||
extern crate handlebars;
|
||||
extern crate prlink;
|
||||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
|
||||
use handlebars::Handlebars;
|
||||
use handlebars::{Handlebars, Helper, RenderContext, RenderError};
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
let template = include_str!("../../README-template.hbs");
|
||||
@@ -23,7 +25,9 @@ order to make changes here rather than committing the changes directly.";
|
||||
|
||||
let mut generated_readme = File::create("README.md").unwrap();
|
||||
|
||||
let hbs = Handlebars::new();
|
||||
let mut hbs = Handlebars::new();
|
||||
hbs.register_helper("playground_link", Box::new(playground_link_helper));
|
||||
|
||||
write!(
|
||||
generated_readme,
|
||||
"{}",
|
||||
@@ -33,3 +37,10 @@ order to make changes here rather than committing the changes directly.";
|
||||
).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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user