From f4966ab70b4580a87ad84ba3b756689cb7f9bd76 Mon Sep 17 00:00:00 2001 From: Tiffany Bennett Date: Sat, 8 Oct 2016 11:20:46 -0400 Subject: [PATCH] web: Add some links to units --- src/eval.rs | 3 +++ web/src/main.rs | 37 +++++++++++++++++++++++++++++++++++-- web/templates/number.hbs | 14 +++++++++----- web/templates/query.hbs | 2 +- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index 0cb68a9..15321cd 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -764,8 +764,11 @@ impl Context { None }) .expect("Search returned non-existent result"); + let mut raw = BTreeMap::new(); + raw.insert(Dim::new(x), 1); NumberParts { unit: Some(x.to_owned()), + raw_unit: Some(raw), quantity: parts.quantity, ..Default::default() } diff --git a/web/src/main.rs b/web/src/main.rs index ffa36f3..857ea89 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -30,6 +30,7 @@ use router::Router; use iron::AfterMiddleware; use iron::headers; use iron::modifiers::Header; +use handlebars::Handlebars; use handlebars_iron::{HandlebarsEngine, DirectorySource, Template}; use mount::Mount; use staticfile::Static; @@ -95,6 +96,36 @@ fn api(req: &mut Request) -> IronResult { Ok(Response::with((acao, status::Ok, reply))) } +fn ifnot1helper( + c: &handlebars::Context, + h: &handlebars::Helper, + r: &Handlebars, + rc: &mut handlebars::RenderContext +) -> Result<(), handlebars::RenderError> { + use handlebars::RenderError; + use handlebars::Renderable; + + let param = try!(h.param(0) + .ok_or_else(|| RenderError::new("Param not found for helper \"ifnot1\""))); + let param = param.value(); + + let value = + param.as_string().map(|x| x != "1").unwrap_or(true) && + param.as_i64().map(|x| x != 1).unwrap_or(true) && + param.as_u64().map(|x| x != 1).unwrap_or(true) && + param.as_f64().map(|x| x != 1.0).unwrap_or(true); + + let tmpl = if value { + h.template() + } else { + h.inverse() + }; + match tmpl { + Some(ref t) => t.render(c, r, rc), + None => Ok(()), + } +} + fn main() { let mut args = env::args(); args.next(); @@ -117,9 +148,11 @@ fn main() { mount.mount("/static", Static::new("./static/")); let mut chain = Chain::new(mount); - let mut hbse = HandlebarsEngine::new(); - hbse.add(Box::new(DirectorySource::new("./templates/", ".hbs"))); + let mut hb = Handlebars::new(); + hb.register_helper("ifnot1", Box::new(ifnot1helper)); + let mut hbse = HandlebarsEngine::from(hb); + hbse.add(Box::new(DirectorySource::new("./templates/", ".hbs"))); // load templates from all registered sources if let Err(r) = hbse.reload() { panic!("{}", r); diff --git a/web/templates/number.hbs b/web/templates/number.hbs index ded4d57..dfbd015 100644 --- a/web/templates/number.hbs +++ b/web/templates/number.hbs @@ -2,11 +2,15 @@ {{#if approx_value}} approx. {{approx_value}} {{/if}} -{{#if unit}} - {{unit}} -{{else}} - {{dimensions}} -{{/if}} +{{#each raw_unit}} + {{@key}}{{#ifnot1 this}}{{this}}{{/ifnot1}} + {{else}} + {{#if unit}} + {{unit}} + {{else}} + {{dimensions}} + {{/if}} +{{/each}} {{insert}} {{#if quantity}} ({{quantity}}) diff --git a/web/templates/query.hbs b/web/templates/query.hbs index 340c977..09c7aa5 100644 --- a/web/templates/query.hbs +++ b/web/templates/query.hbs @@ -79,7 +79,7 @@ {{#if units}} {{else}}