-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathviews.rs
136 lines (124 loc) · 3.57 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
use comrak::nodes::AstNode;
use maud::{html, Markup, PreEscaped, Render, DOCTYPE};
use std::str;
use crate::{
highlight::Highlighter,
page::{default_comrak_options, Page},
string_writer::StringWriter,
};
pub fn main<'a>(
slug: &str,
page: Page<'a>,
nav: &[(&str, &'a AstNode<'a>)],
version: &str,
hash: &str,
) -> Markup {
html! {
(DOCTYPE)
meta charset="utf-8";
title {
@if let Some(title) = page.title {
(ComrakText(title))
" \u{2013} "
}
"Maud, a macro for writing HTML"
}
link rel="stylesheet" href="styles.css";
meta name="theme-color" content="#808";
meta name="viewport" content="width=device-width";
header {
h1 {
a href="." {
"maud"
}
}
}
nav {
ul {
@for &(other_slug, other_title) in nav {
li {
@if other_slug == slug {
b {
(ComrakRemovePTags(other_title))
}
} @else {
a href={ (other_slug) ".html" } {
(ComrakRemovePTags(other_title))
}
}
}
}
}
ul {
li {
a href="https://docs.rs/maud/" {
"API documentation"
}
}
li {
a href="https://github.com/lambda-fairy/maud" {
"GitHub"
}
}
}
}
main {
@if let Some(title) = page.title {
h2 {
(ComrakRemovePTags(title))
}
}
(Comrak(page.content))
}
footer {
p {
a href={ "https://github.com/lambda-fairy/maud/tree/" (hash) } {
(version)
}
}
}
}
}
struct Comrak<'a>(&'a AstNode<'a>);
impl Render for Comrak<'_> {
fn render_to(&self, buffer: &mut String) {
let highlighter = Highlighter::get();
comrak::format_html_with_plugins(
self.0,
&default_comrak_options(),
&mut StringWriter(buffer),
&highlighter.as_plugins(),
)
.unwrap();
}
}
/// Hack! The page title is wrapped in a `Paragraph` node, which introduces an
/// extra `<p>` tag that we don't want most of the time.
struct ComrakRemovePTags<'a>(&'a AstNode<'a>);
impl Render for ComrakRemovePTags<'_> {
fn render(&self) -> Markup {
let mut buffer = String::new();
let highlighter = Highlighter::get();
comrak::format_html_with_plugins(
self.0,
&default_comrak_options(),
&mut StringWriter(&mut buffer),
&highlighter.as_plugins(),
)
.unwrap();
assert!(buffer.starts_with("<p>") && buffer.ends_with("</p>\n"));
PreEscaped(
buffer
.trim_start_matches("<p>")
.trim_end_matches("</p>\n")
.to_string(),
)
}
}
struct ComrakText<'a>(&'a AstNode<'a>);
impl Render for ComrakText<'_> {
fn render_to(&self, buffer: &mut String) {
comrak::format_commonmark(self.0, &default_comrak_options(), &mut StringWriter(buffer))
.unwrap();
}
}