web: Add some links to units

This commit is contained in:
Tiffany Bennett 2016-10-08 11:20:46 -04:00
parent 190ac4b9f1
commit f4966ab70b
4 changed files with 48 additions and 8 deletions

View file

@ -764,8 +764,11 @@ impl Context {
None None
}) })
.expect("Search returned non-existent result"); .expect("Search returned non-existent result");
let mut raw = BTreeMap::new();
raw.insert(Dim::new(x), 1);
NumberParts { NumberParts {
unit: Some(x.to_owned()), unit: Some(x.to_owned()),
raw_unit: Some(raw),
quantity: parts.quantity, quantity: parts.quantity,
..Default::default() ..Default::default()
} }

View file

@ -30,6 +30,7 @@ use router::Router;
use iron::AfterMiddleware; use iron::AfterMiddleware;
use iron::headers; use iron::headers;
use iron::modifiers::Header; use iron::modifiers::Header;
use handlebars::Handlebars;
use handlebars_iron::{HandlebarsEngine, DirectorySource, Template}; use handlebars_iron::{HandlebarsEngine, DirectorySource, Template};
use mount::Mount; use mount::Mount;
use staticfile::Static; use staticfile::Static;
@ -95,6 +96,36 @@ fn api(req: &mut Request) -> IronResult<Response> {
Ok(Response::with((acao, status::Ok, reply))) 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() { fn main() {
let mut args = env::args(); let mut args = env::args();
args.next(); args.next();
@ -117,9 +148,11 @@ fn main() {
mount.mount("/static", Static::new("./static/")); mount.mount("/static", Static::new("./static/"));
let mut chain = Chain::new(mount); 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 // load templates from all registered sources
if let Err(r) = hbse.reload() { if let Err(r) = hbse.reload() {
panic!("{}", r); panic!("{}", r);

View file

@ -2,11 +2,15 @@
{{#if approx_value}} {{#if approx_value}}
approx. {{approx_value}} approx. {{approx_value}}
{{/if}} {{/if}}
{{#if unit}} {{#each raw_unit}}
{{unit}} <a href="/?q={{@key}}">{{@key}}</a>{{#ifnot1 this}}<sup>{{this}}</sup>{{/ifnot1}}
{{else}} {{else}}
{{dimensions}} {{#if unit}}
{{/if}} {{unit}}
{{else}}
{{dimensions}}
{{/if}}
{{/each}}
{{insert}} {{insert}}
{{#if quantity}} {{#if quantity}}
({{quantity}}) ({{quantity}})

View file

@ -79,7 +79,7 @@
{{#if units}} {{#if units}}
<ul> <ul>
{{#each units}} {{#each units}}
<li>{{this}}</li> <li><a href="/?q={{this}}">{{this}}</a></li>
{{/each}} {{/each}}
</ul> </ul>
{{else}} {{else}}