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
pub use self::{frag_map::*, lay_img::*, marked::*, media::*, sized::*};
use super::*;
use yaml_peg::serde::InlineList;
mod frag_map;
mod lay_img;
mod marked;
mod media;
mod sized;
#[derive(Default, serde::Deserialize)]
#[serde(default)]
pub struct Content {
pub fit: Vec<String>,
pub doc: String,
pub include: String,
#[serde(rename = "include-html")]
pub include_html: bool,
pub math: String,
pub img: InlineList<Img>,
pub video: InlineList<Video>,
pub iframe: InlineList<IFrame>,
#[serde(rename = "lay-img")]
pub lay_img: InlineList<LayImg>,
#[serde(flatten)]
pub frag: FragMap,
#[serde(rename = "h-stack")]
pub h_stack: Vec<Self>,
#[serde(rename = "v-stack")]
pub v_stack: Vec<Self>,
#[serde(rename = "h-stack-border")]
pub h_stack_border: Vec<Self>,
#[serde(rename = "v-stack-border")]
pub v_stack_border: Vec<Self>,
}
impl ToHtml for Content {
fn to_html(self, ctx: &Ctx) -> String {
let Self {
fit,
doc,
include,
include_html,
math,
img,
video,
iframe,
lay_img,
mut frag,
h_stack,
v_stack,
h_stack_border,
v_stack_border,
} = self;
frag.with_counter(ctx.frag.clone());
let mut s = String::new();
for t in fit {
if t == "---" {
s += "<hr/>";
} else {
s += &frag
.wrap("fit", &t)
.wrap("<h2 class=\"r-fit-text\">", "</h2>\n");
}
}
s += &frag.wrap("doc", &md2html(&doc));
if !include.is_empty() {
let doc = std::fs::read_to_string(include).unwrap();
let doc = if include_html { doc } else { md2html(&doc) };
s += &frag.wrap("include", &doc);
}
s += &frag.wrap("math", &math.wrap("\\[", "\\]"));
for media in [img.to_html(ctx), video.to_html(ctx), iframe.to_html(ctx)] {
s += &media.wrap("<div class=\"h-stack\">\n", "</div>\n");
}
s += &lay_img
.to_html(ctx)
.wrap("<div class=\"r-stack\">", "</div>\n");
if !h_stack.is_empty() {
let width = 100. / h_stack.len() as f32;
let pre = format!("<div style=\"width:{:.04}%\">", width);
s += &h_stack
.into_iter()
.map(|c| c.to_html(ctx).wrap(&pre, "</div>\n"))
.collect::<String>()
.wrap("<div class=\"h-stack\">", "</div>\n");
}
s += &v_stack
.into_iter()
.map(|c| c.to_html(ctx).wrap("<div>", "</div>\n"))
.collect::<String>()
.wrap("<div class=\"v-stack\">", "</div>\n");
if !h_stack_border.is_empty() {
let width = 100. / h_stack_border.len() as f32;
let pre = format!(
"<div class=\"h-stack-border\" style=\"width:{:.04}%\">",
width
);
s += &h_stack_border
.into_iter()
.enumerate()
.map(|(i, c)| {
let text = c.to_html(ctx);
if i == 0 {
text.wrap(&format!("<div style=\"width:{:.04}%\">", width), "</div>")
} else {
text.wrap(&pre, "</div>")
}
})
.collect::<String>()
.wrap("<div class=\"h-stack\">", "</div>");
}
s += &v_stack_border
.into_iter()
.enumerate()
.map(|(i, c)| {
let text = c.to_html(ctx);
if i == 0 {
text.wrap("<div class=\"v-stack\">", "</div>\n")
} else {
text.wrap("<div class=\"v-stack-border\">", "</div>\n")
}
})
.collect::<String>()
.wrap("<div class=\"v-stack\">", "</div>\n");
s
}
}