diff options
author | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2017-07-09 01:23:54 -0400 |
---|---|---|
committer | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2017-07-09 01:23:54 -0400 |
commit | c6d6702cf838fa8d43a511d455f45a385b74e427 (patch) | |
tree | 354c19f1914af8e006bd6eefed1bcae95411dc43 /src | |
parent | 24fd328a34b6c9ffef29a8d4566c7d1065ee5682 (diff) |
add: Rendering of quotes.
Diffstat (limited to 'src')
-rw-r--r-- | src/data.rs | 27 | ||||
-rw-r--r-- | src/data/templates/quotes.hbs | 84 | ||||
-rw-r--r-- | src/lib.rs | 5 | ||||
-rw-r--r-- | src/server.rs | 23 |
4 files changed, 137 insertions, 2 deletions
diff --git a/src/data.rs b/src/data.rs new file mode 100644 index 0000000..ee39613 --- /dev/null +++ b/src/data.rs @@ -0,0 +1,27 @@ +use serde_json::{Value, Map}; +use handlebars_iron::handlebars::to_json; + +#[derive(Serialize, Debug)] +pub struct Quote { + id: u64, + author: String, + date: String, + score: String, + votes: u32, + content: String +} + +pub fn make_data() -> Map<String, Value> { + let mut data = Map::new(); + data.insert("quotes".to_string(), to_json(&vec!( + &Quote { + id: 1, + author: "panda_man".to_owned(), + date: "2017-07-01".to_owned(), + score: format!("{:.2}", 450.0 / 96.0), + votes: 99, + content: "<orbekk> hvor er jantho?".to_owned(), + } + ))); + data +} diff --git a/src/data/templates/quotes.hbs b/src/data/templates/quotes.hbs new file mode 100644 index 0000000..68f0a5b --- /dev/null +++ b/src/data/templates/quotes.hbs @@ -0,0 +1,84 @@ +<!doctype html> +<html lang="no"> + <head> + <meta name="robots" content="noindex, nofollow" /> + <link rel="shortcut icon" href="/favicon.ico" /> + <title>Quotes fra #linux.no på freenode</title> + <style type="text/css"> + body { + font-family: monospace; + background: #ffffff; + color: #000000; + } + a, a:visited { + color: #000000; + } + hr { + border-style: solid; + border-color: black; + border-width: 1px; + } + .ragebutton { + font-family: monospace; + text-align: left; + text-decoration: underline; + color: black; + background: none; + margin: 0; + padding: 0; + border: none; + cursor: pointer; + -moz-user-select: text; + } + </style> + </head> + <body> + +<pre> + _ _ + | (_)_ __ _ ___ __ _ __ ___ + _|_|_ | | | '_ \| | | \ \/ / | '_ \ / _ \ + _|_|_ | | | | | | |_| |) ( _| | | | (_) | + | | |_|_|_| |_|\__,_/_/\_(_)_| |_|\___/ + -=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=- GAMMA -=- + Quotes fra #linux.no på freenode + Klikk <a href="add.jsp">her</a> for å legge til en quote + + Sortér etter <a href="quotes.jsp?order=date">quote-dato</a> | <a href="quotes.jsp?order=score">score</a> | <a href="quotes.jsp?order=timestamp">timestamp</a> +</pre> +<hr> + +{{#each quotes}} + +<br> +<a href="/view_quote?id={{id}}">#{{id}}</a>, lagt til av {{author}}<br> +Dato: {{date}}, Score: +<span id="v{{id}}"> +{{score}} (fra {{votes}}), Vote: <font size="-1"> + <a href="javascript:vote({{id}},1)">1</a> + <a href="javascript:vote({{id}},2)">2</a> + <a href="javascript:vote({{id}},3)">3</a> + <a href="javascript:vote({{id}},4)">4</a> + <a href="javascript:vote({{id}},5)">5</a> + </font></span>, + +<form method="post" style="display: inline;"action="http://www.vidarholen.net/contents/rage/index.php"><input type="hidden" name="irc" value="{{content}}"/><input type="submit" class="ragebutton" value="Rage it"/></form><br> +<br> <br> + +{{content}} +<br> +<hr> +{{/each}} + + +<center> +<br> +<p>linoquotes v.3 © 2004-2017 Kjetil Ørbekk, Erlend Hamberg, Vidar Holen, John H. Anthony. +<p>Source code at <a href="https://git.orbekk.com/linoquotes-gamma.git">https://git.orbekk.com/linoquotes-gamma.git</a>. +<p>The quotes on this page are copyright their respective owners and submitters.</p> +</center> + + </body> +</html> + + @@ -3,5 +3,10 @@ extern crate log; extern crate iron; #[macro_use] extern crate router; +extern crate handlebars_iron; +extern crate serde_json; +#[macro_use] +extern crate serde_derive; pub mod server; +mod data; diff --git a/src/server.rs b/src/server.rs index 3a15e45..37c5713 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,18 +1,37 @@ use iron::headers::{ContentType}; use iron::modifiers::{Header}; use iron::{Iron, Chain, Request, Response, IronResult, status}; +use handlebars_iron::{HandlebarsEngine, MemorySource, Template}; +use std::collections::BTreeMap; +use data; + +fn renderer() -> HandlebarsEngine { + let mut e = HandlebarsEngine::new(); + + let mut templates = BTreeMap::new(); + templates.insert("quotes".to_string(), + include_str!("data/templates/quotes.hbs").to_string()); + + e.add(Box::new(MemorySource(templates))); + if let Err(r) = e.reload() { + panic!("Error loading templates: {}", r) + } + e +} fn info(_r: &mut Request) -> IronResult<Response> { + let data = data::make_data(); Ok(Response::with((status::Ok, Header(ContentType::html()), - "<p>Info"))) + Template::new("quotes", data)))) } pub fn serve(port: u16) { let router = router!( info: get "/" => info, ); - let chain = Chain::new(router); + let mut chain = Chain::new(router); + chain.link_after(renderer()); let bind_address = format!("{}:{}", "::", port); let _server = Iron::new(chain).http(bind_address.as_str()); info!("Serving on {}", bind_address); |