-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.rs
264 lines (249 loc) · 8.11 KB
/
views.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use ammonia;
use iron::prelude::*;
use maud::{Markup, PreEscaped, Render};
use pulldown_cmark::{self, Event, Parser, Tag};
use models::User;
fn layout(r: &Request, title: Option<&str>, body: Markup) -> Markup {
layout_inner(r, title, title, body)
}
fn layout_inner(r: &Request, head_title: Option<&str>, body_title: Option<&str>, body: Markup) -> Markup {
html! {
(PreEscaped("<!DOCTYPE html>"))
html {
meta charset="utf-8" /
title {
@if let Some(head_title) = head_title {
(head_title) " - "
}
"Karkinos"
}
meta name="viewport" content="width=device-width" /
link rel="stylesheet" href=(url_for!(r, "static", "path" => "styles.css")) /
link rel="icon" type="image/png" href=(url_for!(r, "static", "path" => "icon.png")) /
body {
h1 a href="/" {
span.thecrab "🦀"
"Karkinos"
}
@if let Some(body_title) = body_title {
h2 a href=(r.url) title="Link to this page" (body_title)
}
(body)
}
}
}
}
pub fn home(r: &Request) -> Markup {
layout(r, None, html! {
(search_form(r, ""))
p {
"… or view a "
a href=(url_for!(r, "random")) "random Rustacean"
"."
}
p {
span.karkinos "KARKINOS"
" is a database of people interested in the "
a href="https://www.rust-lang.org" "Rust programming language"
". It uses the same data as "
a href="http://rustaceans.org" "rustaceans.org"
", but presents it through a different interface."
}
p "I created Karkinos for these reasons:"
ul {
li "To provide access for users who browse with JavaScript disabled;"
li "To rewrite the backend in Rust (instead of Node.js);"
li {
"As a proving ground for my template engine, "
a href="https://github.com/lfairy/maud" "Maud"
";"
}
li "To screw around with CSS (this is the most important reason)."
}
p {
"Karkinos is named after a very special "
a href="https://en.wikipedia.org/wiki/Cancer_(constellation)#Names" "giant crab"
"."
}
p {
"The source code for this site can be found on "
a href="https://github.com/lfairy/karkinos" "GitHub"
"."
}
})
}
fn search_form(r: &Request, value: &str) -> Markup {
html! {
form action=(url_for!(r, "search")) {
input name="q" id="q" type="search" placeholder="Search"
autocomplete="off" value=(value) /
}
script (PreEscaped(r#"
var searchBox = document.getElementById('q')
if (!searchBox.value) searchBox.select()
"#))
}
}
pub fn not_found(r: &Request) -> Markup {
layout(r, Some("Not found"), html! {
p {
"The page at "
strong (r.url)
" could not be found."
}
p a href="/" "‹ Back to home page"
})
}
pub fn search(r: &Request) -> Markup {
layout(r, Some("Search"), html! {
(search_form(r, ""))
})
}
pub fn search_results<'u, I>(
r: &Request, query: &str, results: I, correction: Option<String>) -> Markup where
I: Iterator<Item=(Result<&'u User, &'u str>, String, u64)>,
{
let title = format!("Search results for “{}”", query);
let mut results = results.peekable();
layout_inner(r, Some(&title), None, html! {
(search_form(r, query))
@if results.peek().is_none() {
p "No results found."
} @else if let Some(correction) = correction {
p {
"Showing results for "
a href=(url_for!(r, "search", "q" => &correction[..])) strong (correction)
}
}
@for (user, id, weight) in results {
h3 title={ "Weight: " (weight) } {
a href=(url_for!(r, "user", "id" => &id[..])) {
(user_title(&id, user.ok()))
}
}
@if let Ok(user) = user {
(user_box(&id, user, 3))
}
hr /
}
})
}
pub fn user(r: &Request, id: &str, user: &User) -> Markup {
layout(r, Some(&user_title(id, Some(user))), user_box(id, user, 2))
}
fn user_title(id: &str, user: Option<&User>) -> String {
if let Some(name) = user.and_then(|user| user.name.as_ref()) {
format!("{} ({})", name, id)
} else {
id.to_string()
}
}
fn user_box(id: &str, user: &User, demote_headers: u32) -> Markup {
html! {
table {
tr {
th "GitHub"
td a href={ "https://github.com/" (id) } (id)
}
@if let Some(ref nick) = user.irc {
tr {
th "IRC"
td {
(nick)
@if !user.irc_channels.is_empty() {
" on "
@for (i, channel) in user.irc_channels.iter().enumerate() {
@if i > 0 { ", " }
a href={ "irc://irc.mozilla.org/" (channel) } {
"#" (channel)
}
}
}
}
}
}
@if let Some(ref x) = user.discourse {
tr {
th "Discourse"
td a href={ "https://users.rust-lang.org/users/" (x) } (x)
}
}
@if let Some(ref x) = user.reddit {
tr {
th "Reddit"
td a href={ "https://reddit.com/user/" (x) } (x)
}
}
@if let Some(ref x) = user.twitter {
tr {
th "Twitter"
td a href={ "https://twitter.com/" (x) } (x)
}
}
@if let Some(ref x) = user.website {
tr {
th "Website"
td a href=(x) (x)
}
}
@if let Some(ref x) = user.blog {
tr {
th "Blog"
td a href=(x) (x)
}
}
@if let Some(ref x) = user.email {
tr {
th "Email"
td a href={ "mailto:" (x) } (x)
}
}
}
@if let Some(ref x) = user.notes {
div.notes (Markdown { text: x, demote_headers: demote_headers })
}
}
}
pub fn user_error(r: &Request, id: &str, error: &str) -> Markup {
layout(r, Some(id), html! {
p {
"The user "
strong (id)
" exists, but their entry could not be parsed."
}
p {
"(Error: " (error) ")"
}
})
}
pub fn user_not_found(r: &Request, id: &str) -> Markup {
layout(r, Some(id), html! {
p {
"The user "
strong (id)
" could not be found."
}
p a href="/" "‹ Back to home page"
})
}
struct Markdown<'a> {
text: &'a str,
demote_headers: u32,
}
impl<'a> Render for Markdown<'a> {
fn render(&self) -> Markup {
let parser = Parser::new_ext(self.text, pulldown_cmark::OPTION_ENABLE_TABLES);
// Demote headers
let parser = parser.map(|event| match event {
Event::Start(Tag::Header(level)) =>
Event::Start(Tag::Header(level + self.demote_headers as i32)),
Event::End(Tag::Header(level)) =>
Event::End(Tag::Header(level + self.demote_headers as i32)),
_ => event,
});
let mut unsafe_html = String::new();
pulldown_cmark::html::push_html(&mut unsafe_html, parser);
let safe_html = ammonia::clean(&unsafe_html);
PreEscaped(safe_html)
}
}