diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-09 14:40:01 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-09 14:40:01 +0000 |
commit | 41e1968d4f546360c3f19b87a311cd7f6b8ebadc (patch) | |
tree | 1242c7cc8fb7aeab7dab0455859e2ebc982a5e20 /src/codegen.rs | |
parent | 8a1a3baba5f7c7bc42de3b67a021cdb9fb8e1bbc (diff) | |
download | subplot-41e1968d4f546360c3f19b87a311cd7f6b8ebadc.tar.gz |
resource: Step one of VFS support, redirect all opens
This redirects all file reading via the new resource module
which will be used to control where files come from.
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/codegen.rs')
-rw-r--r-- | src/codegen.rs | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/src/codegen.rs b/src/codegen.rs index 19a84e7..f436a34 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,4 +1,4 @@ -use crate::{Document, SubplotError, TemplateSpec}; +use crate::{resource, Document, SubplotError, TemplateSpec}; use std::collections::HashMap; use std::fs::File; use std::io::prelude::{Read, Write}; @@ -35,7 +35,9 @@ pub fn generate_test_program( let dirname = spec.template_filename().parent().unwrap(); for helper in spec.helpers() { let helper_path = dirname.join(helper); - tera.add_template_file(helper_path, helper.to_str())?; + let helper_content = resource::read_as_string(helper_path)?; + let helper_name = helper.display().to_string(); + tera.add_raw_template(&helper_name, &helper_content)?; } let code = tera.render("template", &context).expect("render"); write(filename, &code)?; @@ -62,16 +64,17 @@ fn context(doc: &mut Document) -> Result<Context> { fn tera(tmplspec: &TemplateSpec) -> Result<Tera> { // Tera insists on a glob, but we want to load a specific template // only, so we use a glob that doesn't match anything. - let mut tera = Tera::new(&"*notexist").expect("new"); + let mut tera = Tera::new(&"/..IGNORE-THIS../..SUBPLOT-TERA-NOT-EXIST../*").expect("new"); tera.register_filter("base64", base64); tera.register_filter("nameslug", nameslug); tera.register_filter("commentsafe", commentsafe); - tera.add_template_file(tmplspec.template_filename(), Some("template"))?; + let template = resource::read_as_string(tmplspec.template_filename())?; + tera.add_raw_template("template", &template)?; Ok(tera) } fn cat<P: AsRef<Path>>(filename: P) -> Result<String> { - let mut f = File::open(filename)?; + let mut f = resource::open(filename)?; let mut buf = String::new(); f.read_to_string(&mut buf)?; Ok(buf) |