aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-10-29 18:23:04 -0700
committerbnewbold <bnewbold@robocracy.org>2016-10-29 18:23:04 -0700
commita9c1b23705fe609bb88adff2103ad5849c9b3f1f (patch)
treedb6549f5a5bb6915ad4fd457cfc2b442ce59e07a
parentbb907287a9f923615b9f52dbf3a37d7fea20f58e (diff)
downloadmodelthing-a9c1b23705fe609bb88adff2103ad5849c9b3f1f.tar.gz
modelthing-a9c1b23705fe609bb88adff2103ad5849c9b3f1f.zip
show example models in webface
-rw-r--r--src/bin/mt-webface.rs40
-rw-r--r--webface/templates/model_list.html11
-rw-r--r--webface/templates/model_view.html18
3 files changed, 51 insertions, 18 deletions
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<String> = 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_id:string>", "model", model_view);
+ app.get("/model/<model_slug:string>/", "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}}
+<h1>Example Models</h1>
+
+<ul>
+{{#each model_slug_list slug}}
+<li><a href="/model/{{this}}/">{{ this }}</a>
+{{/each}}
+</ul>
+
+{{/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}}
-<h1>{{ model_id }}</h1>
+<h1>{{ model_slug }}</h1>
-<b>Full Name</b>: {{ model.name }}
+<h2>Metadata</h2>
+<dl>
+ <li><b>Full Name</b>: {{ model_name }}
+ <li><b>Description</b>: {{ model_description }}
+</dl>
-<b>Raw Model:</b>
+<h2>Modelica</h2>
<pre>
-{{ raw_model }}
+{{ modelica }}
</pre>
-
-<h2>Other Info</h2>
-{{ page_text }}
-
+<h2>Wiki Page</h2>
+{{ markdown }}
{{/partial}}
{{~> base.html~}}