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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use crate::format::Template;
use crate::parsing::elements::*;
use htmlescape::{encode_attribute, encode_minimal};
use minify::html::minify;
use std::cell::RefCell;
use syntect::highlighting::ThemeSet;
use syntect::html::highlighted_html_for_string;
use syntect::parsing::SyntaxSet;

macro_rules! combine_with_lb {
    ($a:expr, $b:expr) => {
        if $a.len() > 0 {
            format!("{}<br>{}", $a, $b.to_html())
        } else {
            $b.to_html()
        }
    };
}

pub trait ToHtml {
    fn to_html(&self) -> String;
}

impl ToHtml for Element {
    fn to_html(&self) -> String {
        match self {
            Element::Block(block) => block.to_html(),
            Element::Inline(inline) => inline.to_html(),
            Element::Line(line) => line.to_html(),
        }
    }
}

impl ToHtml for Line {
    fn to_html(&self) -> String {
        match self {
            Line::Text(text) => text.to_html(),
            Line::Ruler(ruler) => ruler.to_html(),
            Line::Anchor(anchor) => anchor.to_html(),
            Line::Centered(centered) => centered.to_html(),
            Line::ReferenceEntry(ref_entry) => ref_entry.to_html(),
        }
    }
}

impl ToHtml for Inline {
    fn to_html(&self) -> String {
        match self {
            Inline::Url(url) => url.to_html(),
            Inline::Monospace(mono) => mono.to_html(),
            Inline::Striked(striked) => striked.to_html(),
            Inline::Plain(plain) => plain.to_html(),
            Inline::Italic(italic) => italic.to_html(),
            Inline::Underlined(under) => under.to_html(),
            Inline::Bold(bold) => bold.to_html(),
            Inline::Image(img) => img.to_html(),
            Inline::Placeholder(placeholder) => placeholder.lock().unwrap().to_html(),
            Inline::Reference(reference) => reference.to_html(),
            Inline::Superscript(superscript) => superscript.to_html(),
            Inline::Checkbox(checkbox) => checkbox.to_html(),
            Inline::Emoji(emoji) => emoji.to_html(),
        }
    }
}

impl ToHtml for Block {
    fn to_html(&self) -> String {
        match self {
            Block::Paragraph(para) => para.to_html(),
            Block::List(list) => list.to_html(),
            Block::Table(table) => table.to_html(),
            Block::CodeBlock(code) => code.to_html(),
            Block::Quote(quote) => quote.to_html(),
            Block::Section(section) => section.to_html(),
            Block::Import(import) => import.to_html(),
            Block::Placeholder(placeholder) => placeholder.lock().unwrap().to_html(),
        }
    }
}

impl ToHtml for MetadataValue {
    fn to_html(&self) -> String {
        match self {
            MetadataValue::String(string) => encode_minimal(string),
            MetadataValue::Integer(num) => format!("{}", num),
            MetadataValue::Placeholder(ph) => ph.lock().unwrap().to_html(),
            MetadataValue::Bool(b) => format!("{}", b),
            MetadataValue::Float(f) => format!("{}", f),
        }
    }
}

impl ToHtml for Document {
    fn to_html(&self) -> String {
        let inner = self
            .elements
            .iter()
            .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
        let path = if let Some(path) = &self.path {
            format!("path='{}'", encode_attribute(path.as_str()))
        } else {
            "".to_string()
        };
        if self.is_root {
            let style = minify(std::include_str!("assets/style.css"));
            format!(
                "<!DOCTYPE html>\n<html><head {}><style>{}</style></head><body><div class='content'>{}</div></body></html>",
                path, style, inner
            )
        } else {
            format!(
                "<div class='documentImport' document-import=true {}>{}</div>",
                path, inner
            )
        }
    }
}

impl ToHtml for Import {
    fn to_html(&self) -> String {
        let anchor = self.anchor.lock().unwrap();
        if let Some(document) = &anchor.document {
            document.to_html()
        } else {
            "".to_string()
        }
    }
}

impl ToHtml for Section {
    fn to_html(&self) -> String {
        let inner = self
            .elements
            .iter()
            .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
        format!("<section>{}{}</section>", self.header.to_html(), inner)
    }
}

impl ToHtml for Header {
    fn to_html(&self) -> String {
        format!(
            "<h{0} id='{1}'>{2}</h{0}>",
            self.size,
            encode_attribute(self.anchor.as_str()),
            self.line.to_html()
        )
    }
}

impl ToHtml for Paragraph {
    fn to_html(&self) -> String {
        let inner = self
            .elements
            .iter()
            .fold("".to_string(), |a, b| combine_with_lb!(a, b));
        format!("<div class='paragraph'>{}</div>", inner)
    }
}

impl ToHtml for List {
    fn to_html(&self) -> String {
        let inner = self
            .items
            .iter()
            .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
        if self.ordered {
            format!("<ol>{}</ol>", inner)
        } else {
            format!("<ul>{}</ul>", inner)
        }
    }
}

impl ToHtml for ListItem {
    fn to_html(&self) -> String {
        let inner = self
            .children
            .iter()
            .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
        if let Some(first) = self.children.first() {
            if first.ordered {
                format!("<li>{}<ol>{}</ol></li>", self.text.to_html(), inner)
            } else {
                format!("<li>{}<ul>{}</ul></li>", self.text.to_html(), inner)
            }
        } else {
            format!("<li>{}</li>", self.text.to_html())
        }
    }
}

impl ToHtml for Table {
    fn to_html(&self) -> String {
        let head = self.header.cells.iter().fold("".to_string(), |a, b| {
            format!("{}<th>{}</th>", a, b.text.to_html())
        });
        let body = self
            .rows
            .iter()
            .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
        format!(
            "<div class='tableWrapper'><table><tr>{}<tr>{}</table></div>",
            head, body
        )
    }
}

impl ToHtml for Row {
    fn to_html(&self) -> String {
        let inner = self
            .cells
            .iter()
            .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
        format!("<tr>{}</tr>", inner)
    }
}

impl ToHtml for Cell {
    fn to_html(&self) -> String {
        format!("<td>{}</td>", self.text.to_html())
    }
}

thread_local! {static PS: RefCell<SyntaxSet> = RefCell::new(SyntaxSet::load_defaults_nonewlines());}
thread_local! {static TS: RefCell<ThemeSet> = RefCell::new(ThemeSet::load_defaults());}

impl ToHtml for CodeBlock {
    fn to_html(&self) -> String {
        if self.language.len() > 0 {
            PS.with(|ps_cell| {
                let ps = ps_cell.borrow();
                if let Some(syntax) = ps.find_syntax_by_token(self.language.as_str()) {
                    TS.with(|ts_cell| {
                        let ts = ts_cell.borrow();
                        format!(
                            "<div><code lang='{}'>{}</code></div>",
                            encode_attribute(self.language.clone().as_str()),
                            highlighted_html_for_string(
                                self.code.as_str(),
                                &ps,
                                syntax,
                                &ts.themes["InspiredGitHub"]
                            )
                        )
                    })
                } else {
                    format!(
                        "<div><code lang='{}'><pre>{}</pre></code></div>",
                        encode_attribute(self.language.clone().as_str()),
                        encode_minimal(self.code.as_str())
                    )
                }
            })
        } else {
            format!(
                "<div><code><pre>{}</pre></code></div>",
                encode_minimal(self.code.as_str())
            )
        }
    }
}

impl ToHtml for Quote {
    fn to_html(&self) -> String {
        let text = self
            .text
            .iter()
            .fold("".to_string(), |a, b| combine_with_lb!(a, b));
        if let Some(meta) = self.metadata.clone() {
            format!(
                "<div class='quote'><blockquote>{}</blockquote><span class='metadata'>{}</span></div>",
                text, meta.to_html()
            )
        } else {
            format!("<div class='quote'><blockquote>{}</blockquote></div>", text)
        }
    }
}

impl ToHtml for Ruler {
    fn to_html(&self) -> String {
        "<hr>".to_string()
    }
}

impl ToHtml for TextLine {
    fn to_html(&self) -> String {
        self.subtext
            .iter()
            .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()))
    }
}

impl ToHtml for Image {
    fn to_html(&self) -> String {
        let mut style = String::new();
        if let Some(meta) = &self.metadata {
            if let Some(width) = meta.data.get("width") {
                style = format!("{}width: {};", style, width.to_html())
            }
            if let Some(height) = meta.data.get("height") {
                style = format!("{}height: {};", style, height.to_html())
            }
        }
        if let Some(description) = self.url.description.clone() {
            minify(
                format!(
                    "<div class='figure'>\
                     <a href={0}>\
                     <img src='{0}' alt='{1}' style='{2}'/>\
                     </a>\
                     <label class='imageDescription'>{1}</label>\
                     </div>",
                    encode_attribute(self.url.url.clone().as_str()),
                    encode_attribute(description.as_str()),
                    style
                )
                .as_str(),
            )
        } else {
            format!(
                "<a href={0}><img src='{0}' style='{1}'/></a>",
                self.url.url.clone(),
                style
            )
        }
    }
}

impl ToHtml for BoldText {
    fn to_html(&self) -> String {
        format!("<b>{}</b>", self.value.to_html())
    }
}

impl ToHtml for UnderlinedText {
    fn to_html(&self) -> String {
        format!("<u>{}</u>", self.value.to_html())
    }
}

impl ToHtml for ItalicText {
    fn to_html(&self) -> String {
        format!("<i>{}</i>", self.value.to_html())
    }
}

impl ToHtml for StrikedText {
    fn to_html(&self) -> String {
        format!("<del>{}</del>", self.value.to_html())
    }
}

impl ToHtml for SuperscriptText {
    fn to_html(&self) -> String {
        format!("<sup>{}</sup>", self.value.to_html())
    }
}

impl ToHtml for MonospaceText {
    fn to_html(&self) -> String {
        format!(
            "<code class='inlineCode'>{}</code>",
            encode_minimal(self.value.as_str())
        )
    }
}

impl ToHtml for Url {
    fn to_html(&self) -> String {
        if let Some(description) = self.description.clone() {
            format!(
                "<a href='{}'>{}</a>",
                self.url.clone(),
                encode_minimal(description.as_str())
            )
        } else {
            format!(
                "<a href='{}'>{}</a>",
                self.url.clone(),
                encode_minimal(self.url.clone().as_str())
            )
        }
    }
}

impl ToHtml for PlainText {
    fn to_html(&self) -> String {
        encode_minimal(self.value.clone().as_str())
    }
}

impl ToHtml for Placeholder {
    fn to_html(&self) -> String {
        if let Some(value) = &self.value {
            value.to_html()
        } else {
            format!("Unknown placeholder '{}'!", encode_minimal(&self.name))
        }
    }
}

impl ToHtml for Anchor {
    fn to_html(&self) -> String {
        format!(
            "<a href='#{}'>{}</a>",
            encode_attribute(self.reference.as_str()),
            self.description.to_html()
        )
    }
}

impl ToHtml for InlineMetadata {
    fn to_html(&self) -> String {
        if let Some(MetadataValue::String(format)) = self.data.get("display") {
            let mut template = Template::new(format.clone());
            self.data
                .iter()
                .for_each(|(k, v)| template.add_replacement(k, v.to_html().as_str()));

            template.render()
        } else {
            self.data.iter().fold("".to_string(), |s, (k, v)| {
                format!("{} {}={},", s, k, v.to_html())
            })
        }
    }
}

impl ToHtml for Centered {
    fn to_html(&self) -> String {
        format!("<div class='centered'>{}</div>", self.line.to_html())
    }
}

impl ToHtml for Reference {
    fn to_html(&self) -> String {
        if let Some(value) = &self.value {
            let ref_id = value.get_ref_id();
            if let Some(display) = &self.display {
                match value {
                    RefValue::BibEntry(bib) => {
                        let bib = bib.lock().unwrap();
                        let mut template = bib.get_template();
                        template.set_value(display.lock().unwrap().value.to_html());

                        format!("<a href='#{}'>{}</a>", ref_id, template.render())
                    }
                }
            } else {
                format!("<a href='#{}'>{}</a>", ref_id, value.to_html())
            }
        } else {
            "Unknown reference".to_string()
        }
    }
}

impl ToHtml for RefValue {
    fn to_html(&self) -> String {
        match self {
            RefValue::BibEntry(bib) => encode_minimal(bib.lock().unwrap().get_formatted().as_str()),
        }
    }
}

impl ToHtml for ReferenceEntry {
    fn to_html(&self) -> String {
        if let Some(val) = &self.value {
            format!(
                "<div id='{}'>{}</div>",
                encode_attribute(val.get_ref_id().as_str()),
                val.to_html()
            )
        } else {
            "Unknown reference".to_string()
        }
    }
}

impl ToHtml for Checkbox {
    fn to_html(&self) -> String {
        if self.value {
            format!("<input type='checkbox' checked disabled>")
        } else {
            format!("<input type='checkbox'disabled>")
        }
    }
}

impl ToHtml for Emoji {
    fn to_html(&self) -> String {
        format!(
            "<span class='emoji' emoji-name='{}'>{}</span>",
            encode_attribute(self.name.as_str()),
            self.value
        )
    }
}