remove treehouse-format crate and collapse everything into src
This commit is contained in:
parent
ca127a9411
commit
b792688776
66 changed files with 145 additions and 112 deletions
37
src/generate/dir_helper.rs
Normal file
37
src/generate/dir_helper.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use handlebars::{Context, Handlebars, Helper, HelperDef, RenderContext, RenderError, ScopedJson};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::vfs::{self, DynDir, VPath};
|
||||
|
||||
pub struct DirHelper {
|
||||
site: String,
|
||||
dir: DynDir,
|
||||
}
|
||||
|
||||
impl DirHelper {
|
||||
pub fn new(site: &str, dir: DynDir) -> Self {
|
||||
Self {
|
||||
site: site.to_owned(),
|
||||
dir,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HelperDef for DirHelper {
|
||||
fn call_inner<'reg: 'rc, 'rc>(
|
||||
&self,
|
||||
h: &Helper<'reg, 'rc>,
|
||||
_: &'reg Handlebars<'reg>,
|
||||
_: &'rc Context,
|
||||
_: &mut RenderContext<'reg, 'rc>,
|
||||
) -> Result<ScopedJson<'reg, 'rc>, RenderError> {
|
||||
if let Some(path) = h.param(0).and_then(|v| v.value().as_str()) {
|
||||
let vpath = VPath::try_new(path).map_err(|e| RenderError::new(e.to_string()))?;
|
||||
let url = vfs::url(&self.site, &self.dir, vpath)
|
||||
.ok_or_else(|| RenderError::new("path is not anchored anywhere"))?;
|
||||
Ok(ScopedJson::Derived(Value::String(url)))
|
||||
} else {
|
||||
Err(RenderError::new("missing path string"))
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue