From a9c1b23705fe609bb88adff2103ad5849c9b3f1f Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sat, 29 Oct 2016 18:23:04 -0700 Subject: show example models in webface --- src/bin/mt-webface.rs | 40 +++++++++++++++++++++++++++++---------- webface/templates/model_list.html | 11 +++++++++++ webface/templates/model_view.html | 18 ++++++++++-------- 3 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 webface/templates/model_list.html diff --git a/src/bin/mt-webface.rs b/src/bin/mt-webface.rs index 6809f57..a11a038 100644 --- a/src/bin/mt-webface.rs +++ b/src/bin/mt-webface.rs @@ -1,15 +1,19 @@ extern crate getopts; extern crate pencil; -#[macro_use] extern crate log; +extern crate modelthing; extern crate env_logger; +#[macro_use] +extern crate log; + use std::env; use std::collections::BTreeMap; use std::process::exit; +use std::path::Path; use getopts::Options; use pencil::Pencil; -use pencil::{Request, PencilResult, Response, HTTPError}; +use pencil::{Request, PencilResult, Response, HTTPError, PencilError}; fn home(r: &mut Request) -> PencilResult { @@ -24,20 +28,35 @@ fn readme(r: &mut Request) -> PencilResult { return r.app.render_template("raw.html", &context); } -fn model_list(_: &mut Request) -> PencilResult { - Ok(Response::from("List of Models")) +fn model_list(r: &mut Request) -> PencilResult { + let paths = modelthing::search_models(Path::new("examples")); + let l: Vec = paths.iter().map(|x| Path::new(x).strip_prefix("examples").unwrap().to_string_lossy().to_string()).collect(); + let mut context = BTreeMap::new(); + context.insert("model_slug_list".to_string(), l); + return r.app.render_template("model_list.html", &context); } fn model_view(r: &mut Request) -> PencilResult { - let model_id = r.view_args.get("model_id").unwrap(); - let mut context = BTreeMap::new(); - context.insert("model_id".to_string(), model_id.to_string()); - return r.app.render_template("model_view.html", &context); + let model_slug = r.view_args.get("model_slug").unwrap(); + let model_path = Path::new("examples").join(model_slug); + match modelthing::load_model_entry(model_path.as_path()) { + Ok(me) => { + let mut context = BTreeMap::new(); + context.insert("model_slug".to_string(), model_slug.to_string()); + context.insert("model_name".to_string(), me.metadata.name_en); + context.insert("model_description".to_string(), me.metadata.description_en.unwrap_or("".to_string())); + //context.insert("metadata".to_string(), me.metadata); + context.insert("markdown".to_string(), me.markdown); + context.insert("modelica".to_string(), format!("{:?}", me.ast)); + r.app.render_template("model_view.html", &context) + }, + Err(_) => Err(PencilError::PenHTTPError(pencil::HTTPError::NotFound)), + } } fn page_not_found(_: HTTPError) -> PencilResult { let mut response = Response::from("404: Not Found"); - response.status_code = 500; + response.status_code = 404; Ok(response) } @@ -97,9 +116,10 @@ fn main() { app.register_template("raw.html"); app.get("/", "home", home); app.get("/readme/", "readme", readme); + app.register_template("model_list.html"); app.get("/model_list/", "model_list", model_list); app.register_template("model_view.html"); - app.get("/model/", "model", model_view); + app.get("/model//", "model", model_view); let bind = matches.opt_str("bind").unwrap_or("127.0.0.1:5000".to_string()); let bind_str: &str = &bind; diff --git a/webface/templates/model_list.html b/webface/templates/model_list.html new file mode 100644 index 0000000..db11bdc --- /dev/null +++ b/webface/templates/model_list.html @@ -0,0 +1,11 @@ +{{# partial content}} +

Example Models

+ +
    +{{#each model_slug_list slug}} +
  • {{ this }} +{{/each}} +
+ +{{/partial}} +{{~> base.html~}} diff --git a/webface/templates/model_view.html b/webface/templates/model_view.html index 4dba406..4add92b 100644 --- a/webface/templates/model_view.html +++ b/webface/templates/model_view.html @@ -1,17 +1,19 @@ {{# partial content}} -

{{ model_id }}

+

{{ model_slug }}

-Full Name: {{ model.name }} +

Metadata

+
+
  • Full Name: {{ model_name }} +
  • Description: {{ model_description }} +
  • -Raw Model: +

    Modelica

    -{{ raw_model }}
    +{{ modelica }}
     
    - -

    Other Info

    -{{ page_text }} - +

    Wiki Page

    +{{ markdown }} {{/partial}} {{~> base.html~}} -- cgit v1.2.3