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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
use regex::Regex;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::io::Write;
use std::path::Path;

pub struct Parser {
    // For each destination file, when was the last time one its
    // youngest source was modified?
    pub tangle_sources: HashMap<String, std::time::SystemTime>,

    // Each source, with its last modification time.
    pub sources: HashMap<String, std::time::SystemTime>,
    stack: Vec<Tree>,
    antistack: Vec<Tree>,
    indent: usize,
    id: usize,
}

#[derive(Debug, Clone)]
pub enum Property {
    CustomId { id: String },
    Title { title: String },
    Author { author: String },
    HtmlHead { html_head: String },
    HtmlFoot { html_foot: String },
}

#[derive(Debug, Clone)]
pub enum Id {
    String(String),
    Int(usize),
}

#[derive(Debug, Clone)]
pub enum Tree {
    Root {
        children: Vec<Tree>,
        title: String,
        level: usize,
        properties: Vec<Property>,
        properties_open: bool,
        toc: Vec<usize>,
        id: Id,
    },
    Block {
        contents: String,
    },
    Item {
        children: Vec<Vec<Tree>>,
        indent: usize,
    },
    Src {
        indent: usize,
        contents: String,
        style: String,
        options: Vec<(String, String)>,
        id: String,
    },
}

impl Parser {
    pub fn new() -> Self {
        Parser {
            tangle_sources: HashMap::new(),
            sources: HashMap::new(),
            stack: vec![Tree::Root {
                children: Vec::new(),
                level: 0,
                title: String::new(),
                properties: Vec::new(),
                properties_open: false,
                toc: Vec::new(),
                id: Id::Int(0),
            }],
            antistack: Vec::new(),
            indent: 0,
            id: 0,
        }
    }

    // Reduce to the highest root of level <= to_level
    fn reduce_to(&mut self, to_level: usize) -> bool {
        loop {
            let pop = self.stack.pop().unwrap();
            if let Tree::Root {
                mut children,
                title,
                level,
                properties,
                properties_open,
                toc,
                id,
            } = pop
            {
                while let Some(anti) = self.antistack.pop() {
                    children.push(anti)
                }
                if level <= to_level {
                    self.stack.push(Tree::Root {
                        children,
                        title,
                        level,
                        properties,
                        properties_open,
                        toc,
                        id,
                    });
                    break;
                } else {
                    self.antistack.push(Tree::Root {
                        children,
                        title,
                        level,
                        properties,
                        properties_open,
                        toc,
                        id,
                    });
                }
            } else {
                self.antistack.push(pop)
            }
        }
        self.stack.len() > 1
    }

    // Reduce to the highest root on the stack
    fn reduce(&mut self) -> bool {
        loop {
            let pop = self.stack.pop().unwrap();
            if let Tree::Root {
                mut children,
                title,
                level,
                properties,
                properties_open,
                toc,
                id,
            } = pop
            {
                while let Some(anti) = self.antistack.pop() {
                    children.push(anti)
                }
                self.stack.push(Tree::Root {
                    children,
                    title,
                    level,
                    properties,
                    properties_open,
                    toc,
                    id,
                });
                break;
            }
            self.antistack.push(pop)
        }
        self.stack.len() > 1
    }

    // Reduce to the highest item of a level on the stack.
    fn reduce_item(&mut self, level: usize) -> bool {
        while let Some(pop) = self.stack.pop() {
            if let Tree::Item {
                mut children,
                indent,
            } = pop
            {
                if indent >= level {
                    let mut child = Vec::new();
                    while let Some(anti) = self.antistack.pop() {
                        child.push(anti)
                    }
                    if !child.is_empty() {
                        children.push(child);
                    }
                    if indent == level {
                        self.stack.push(Tree::Item { children, indent });
                        return true;
                    } else {
                        self.antistack.push(Tree::Item { children, indent });
                    }
                } else {
                    // Indent > level, keep collecting.
                    self.antistack.push(Tree::Item { children, indent })
                }
            } else {
                self.antistack.push(pop)
            }
        }
        // There was no other Item on the stack.
        while let Some(elt) = self.antistack.pop() {
            self.stack.push(elt)
        }
        false
    }

    pub fn finish(&mut self) -> Tree {
        while self.reduce_item(0) {}
        while self.reduce_to(0) {}
        self.stack.pop().unwrap()
    }

    pub fn parse<P: AsRef<Path>, F: FnMut(&str)>(
        &mut self,
        file: P,
        mut include_callback: F,
    ) -> Result<(), anyhow::Error> {
        self.parse_(file, &mut include_callback)
    }

    fn close_src(&mut self, l: &str) -> bool {
        lazy_static! {
            static ref END_SRC: Regex = Regex::new(r#"^\s*#\+END_SRC"#).unwrap();
        }
        if let Some(Tree::Src {
            ref mut contents,
            ref mut id,
            indent,
            ..
        }) = self.stack.last_mut()
        {
            if !END_SRC.is_match(&l) {
                let l = if l.chars().take_while(|c| c.is_whitespace()).count() >= *indent + 2 {
                    let (_indent, l) = l.split_at(*indent + 2);
                    l
                } else {
                    l
                };
                contents.push_str(l);
                contents.push('\n');

                *id = base64::encode(blake3::hash(contents.as_bytes()).as_bytes());
                return true;
            }
        } else {
            return false;
        };
        self.reduce();
        true
    }

    fn begin_src(&mut self, l: &str, time: std::time::SystemTime) -> bool {
        lazy_static! {
            static ref BEGIN_SRC: Regex = Regex::new(r#"^(\s*)#\+BEGIN_SRC(\s+.*)?"#).unwrap();
        }
        if let Some(caps) = BEGIN_SRC.captures(&l) {
            let mut style = String::new();
            let mut options = Vec::new();
            let indent = caps.get(1).unwrap().as_str().len();
            let mut it = caps.get(2).unwrap().as_str().split_whitespace();
            if let Some(style_) = it.next() {
                style.push_str(style_)
            }
            while let Some(param) = it.next() {
                if param.starts_with(":") {
                    if let Some(value) = it.next() {
                        if param == ":tangle" {
                            let e = self.tangle_sources.entry(value.to_string()).or_insert(time);
                            if *e < time {
                                *e = time
                            }
                        }
                        options.push((param.to_string(), value.to_string()));
                    }
                }
            }
            self.stack.push(Tree::Src {
                contents: String::new(),
                style,
                options,
                indent,
                id: String::new(),
            });
            true
        } else {
            false
        }
    }

    fn properties(&mut self, l: &str) -> bool {
        lazy_static! {
            static ref PROP: Regex = Regex::new(r#"^\s*:([^:]+):(.*)"#).unwrap();
        }
        if let Some(cap) = PROP.captures(l) {
            if &cap[1] == "PROPERTIES" {
                if let Some(Tree::Root {
                    ref mut properties_open,
                    ..
                }) = self.stack.last_mut()
                {
                    *properties_open = true;
                }
                true
            } else if &cap[1] == "END" {
                if let Some(Tree::Root {
                    ref mut properties_open,
                    ..
                }) = self.stack.last_mut()
                {
                    *properties_open = false;
                }
                true
            } else if let Some(Tree::Root {
                ref properties_open,
                ref mut id,
                ..
            }) = self.stack.last_mut()
            {
                if *properties_open {
                    if &cap[1] == "CUSTOM_ID" {
                        /*properties.push(Property::CustomId {
                            id: cap[2].trim().to_string()
                        });*/
                        *id = Id::String(cap[2].trim().to_string());
                    }
                }
                *properties_open
            } else {
                false
            }
        } else {
            false
        }
    }

    fn section(&mut self, l: &str) -> bool {
        lazy_static! {
            static ref SECTION: Regex = Regex::new(r#"^(\*+)\s+(.*)"#).unwrap();
        }
        if let Some(caps) = SECTION.captures(&l) {
            let level = caps.get(1).unwrap().as_str().len();
            let title = caps.get(2).unwrap().as_str().to_string();
            self.reduce_item(0);
            self.reduce_to(level - 1);
            self.id += 1;
            self.stack.push(Tree::Root {
                level,
                title,
                children: Vec::new(),
                properties: Vec::new(),
                properties_open: false,
                toc: Vec::new(),
                id: Id::Int(self.id),
            });
            true
        } else {
            false
        }
    }

    fn item(&mut self, l: &str) -> bool {
        lazy_static! {
            static ref ITEM: Regex = Regex::new(r#"^(\s*)-(\s*)(.*)"#).unwrap();
        }
        if let Some(caps) = ITEM.captures(&l) {
            let indent =
                caps.get(1).unwrap().as_str().len() + caps.get(2).unwrap().as_str().len() + 1;
            let contents = caps.get(3).unwrap().as_str().to_string();
            if !self.reduce_item(indent) {
                self.stack.push(Tree::Item {
                    indent,
                    children: Vec::new(),
                });
            }
            self.indent = indent;
            self.stack.push(Tree::Block { contents });
            true
        } else {
            false
        }
    }

    fn parse_<P: AsRef<Path>, F: FnMut(&str)>(
        &mut self,
        file: P,
        include_callback: &mut F,
    ) -> Result<(), anyhow::Error> {
        let f = std::fs::read(file.as_ref())?;
        let time = std::fs::metadata(file.as_ref())?.modified()?;
        self.sources
            .insert(file.as_ref().to_str().unwrap().to_string(), time);
        let f = String::from_utf8(f)?;
        lazy_static! {
            static ref PROP: Regex = Regex::new(r#"^\s*#\+([^\s:]*)\s*:\s*(.*)"#).unwrap();
        }

        for l in f.lines() {
            let indent = l
                .chars()
                .take_while(|c| c.is_whitespace() || *c == '-')
                .count();
            if indent < self.indent && !l.is_empty() {
                self.reduce_item(indent);
                self.reduce();
                self.indent = indent;
            }

            // If we're closing an SRC block, reduce.
            if self.close_src(l) {
                continue;
            }

            if self.properties(l) {
                continue;
            }

            // Else, if we're starting an SRC block…
            if self.begin_src(l, time) {
                continue;
            }

            if let Some(caps) = PROP.captures(&l) {
                let prop = caps.get(1).unwrap().as_str();
                let value = caps.get(2).unwrap().as_str();
                if prop == "INCLUDE" {
                    let value = value.trim_matches('"');
                    include_callback(&value);
                    let path = if let Some(p) = file.as_ref().parent() {
                        p.join(value)
                    } else {
                        Path::new(value).to_path_buf()
                    };
                    self.parse_(&path, include_callback)?;
                } else if prop == "HTML_HEAD" {
                    if let Some(Tree::Root {
                        ref mut properties, ..
                    }) = self.stack.last_mut()
                    {
                        properties.push(Property::HtmlHead {
                            html_head: value.to_string(),
                        });
                    }
                } else if prop == "HTML_FOOT" {
                    if let Some(Tree::Root {
                        ref mut properties, ..
                    }) = self.stack.last_mut()
                    {
                        properties.push(Property::HtmlFoot {
                            html_foot: value.to_string(),
                        });
                    }
                } else if prop == "TITLE" {
                    if let Some(Tree::Root {
                        ref mut properties, ..
                    }) = self.stack.last_mut()
                    {
                        properties.push(Property::Title {
                            title: value.to_string(),
                        });
                    }
                } else if prop == "AUTHOR" {
                    if let Some(Tree::Root {
                        ref mut properties, ..
                    }) = self.stack.last_mut()
                    {
                        properties.push(Property::Author {
                            author: value.to_string(),
                        });
                    }
                } else {
                }
                continue;
            }

            // Comment / unhandled Org-mode property.
            if l.starts_with("#") {
                continue;
            }

            if self.section(l) {
                continue;
            }
            if self.item(l) {
                continue;
            }

            match self.stack.last_mut().unwrap() {
                Tree::Root { ref title, .. }
                    if title.starts_with("DONE") && l.trim_start().starts_with("CLOSED:") => {}
                Tree::Root { .. } | Tree::Item { .. } if !l.is_empty() => {
                    let mut contents = String::new();
                    contents.push_str(l);
                    contents.push('\n');
                    self.stack.push(Tree::Block { contents })
                }
                Tree::Block { ref mut contents } => {
                    contents.push_str(l);
                    contents.push('\n');
                }
                Tree::Src { .. } => unreachable!(),
                _ => {}
            }
        }
        Ok(())
    }
}

pub fn tangle(
    tree: &Tree,
    files: &mut HashSet<String>,
    tangle_files: &HashMap<String, std::time::SystemTime>,
) -> Result<(), anyhow::Error> {
    match tree {
        Tree::Root { ref children, .. } => {
            for c in children.iter() {
                tangle(c, files, tangle_files)?
            }
            Ok(())
        }
        Tree::Item { ref children, .. } => {
            for c in children.iter() {
                for c in c.iter() {
                    tangle(c, files, tangle_files)?
                }
            }
            Ok(())
        }
        Tree::Src {
            ref contents,
            ref options,
            ref id,
            ..
        } => {
            if let Some((_, file)) = options.iter().find(|(k, _)| k == ":tangle") {
                let path: &Path = file.as_ref();
                let new_file = files.insert(file.to_string());
                if new_file {
                    if let Ok(meta) = std::fs::metadata(file) {
                        if meta.modified()? > *tangle_files.get(file).unwrap() {
                            files.remove(file);
                            return Ok(());
                        }
                    }
                }
                std::fs::create_dir_all(path.parent().unwrap())?;
                let mut file_ = std::fs::OpenOptions::new()
                    .write(true)
                    .create(true)
                    .append(!new_file)
                    .truncate(new_file)
                    .open(file)?;
                debug!("id = {:?}", id);
                writeln!(file_, "// org id {}", id)?;
                for line in contents.lines() {
                    file_.write_all(line.trim().as_bytes())?;
                    file_.write_all(b"\n")?;
                }
            }
            Ok(())
        }
        Tree::Block { .. } => Ok(()),
    }
}

pub fn html_output<W: Write>(
    tree: &Tree,
    use_syntect: bool,
    w: &mut W,
) -> Result<(), anyhow::Error> {
    writeln!(w, "<html><head><meta charset=\"utf-8\"/> ")?;
    if let Tree::Root { ref properties, .. } = tree {
        for head in properties {
            if let Property::HtmlHead { ref html_head } = head {
                writeln!(w, "{}", html_head)?
            }
        }
    }
    writeln!(w, "</head><body>")?;

    if let Tree::Root { ref properties, .. } = tree {
        let mut author_ = String::new();
        let mut title_ = String::new();
        for head in properties {
            match head {
                Property::Title { ref title } => {
                    title_.clear();
                    title_.push_str(title);
                }
                Property::Author { ref author } => {
                    author_.clear();
                    author_.push_str(author)
                }
                _ => {}
            }
        }
        if !title_.is_empty() {
            writeln!(w, "<h1 class=\"title\">{}</h1>", title_)?;
        }
        if !author_.is_empty() {
            writeln!(w, "<h1 class=\"subtitle\">{}</h1>", author_)?;
        }
    }

    table_of_contents(tree, w)?;
    std::process::Command::new("cargo")
        .args(&["fmt"])
        .output()
        .expect("failed to execute process");
    output_tree(
        tree,
        w,
        &mut vec![],
        &mut Output {
            use_syntect,
            syntax_set: syntect::parsing::SyntaxSet::load_defaults_newlines(),
            theme: syntect::highlighting::ThemeSet::load_defaults()
                .themes
                .remove("InspiredGitHub")
                .unwrap(),
            fmt: HashMap::new(),
        },
    )?;
    writeln!(w, "</body>")?;
    if let Tree::Root { ref properties, .. } = tree {
        for head in properties {
            if let Property::HtmlFoot { ref html_foot } = head {
                writeln!(w, "{}", html_foot)?
            }
        }
    }
    writeln!(w, "</html>")?;
    Ok(())
}

fn output_string<W: Write>(s: &str, is_title: bool, w: &mut W) -> Result<(), anyhow::Error> {
    lazy_static! {
        static ref BOLD: Regex = Regex::new(r#"\*(?P<bold>[^\*]*)\*"#).unwrap();
        static ref VERB: Regex = Regex::new(r#"~(?P<verb>[^~]*)~"#).unwrap();
        static ref LINK: Regex = Regex::new(r#"\[\[(?P<link>[^\]]*)\]\]"#).unwrap();
        static ref LINK2: Regex =
            Regex::new(r#"\[\[(?P<link>[^\]]*)\]\[(?P<target>[^\]]*)\]\]"#).unwrap();
        static ref ANCHOR: Regex =
            Regex::new(r#"(<<(?P<anchor>[^>\n]*)>>)|(<)|(>)|/([^/]*)/"#).unwrap();
        static ref AMP: Regex = Regex::new(r#"&"#).unwrap();
        static ref LT: Regex = Regex::new(r#"<"#).unwrap();
        static ref GT: Regex = Regex::new(r#">"#).unwrap();
        static ref PARBREAK: Regex = Regex::new("\n\n").unwrap();
        static ref LATEX: Regex = Regex::new(r#"\$([^$]*)\$"#).unwrap();
    }
    let s = AMP.replace_all(s, "&amp;");
    let s = ANCHOR.replace_all(&s, |cap: &regex::Captures| {
        if &cap[0] == "<" {
            Cow::Borrowed("&lt;")
        } else if &cap[0] == ">" {
            Cow::Borrowed("&gt;")
        } else if let Some(anchor) = cap.get(2) {
            Cow::Owned(format!("<a name=\"{}\"></a>", anchor.as_str()))
        } else {
            Cow::Owned(format!("<emph>{}</emph>", &cap[5]))
        }
    });
    let s = BOLD.replace_all(&s, "<strong>$bold</strong>");
    let s = VERB.replace_all(&s, "<code>$verb</code>");
    let s = LINK.replace_all(&s, "<a href=\"$link\">$link</a>");
    let s = LINK2.replace_all(&s, "<a href=\"$link\">$target</a>");
    let s = LINK2.replace_all(&s, "<a href=\"$link\">$target</a>");
    let s = LATEX.replace_all(&s, |cap: &regex::Captures| {
        if let Some(mml) = itex2mml::MML::parse(&cap[0]) {
            mml.as_cstr().to_str().unwrap().to_string()
        } else {
            s.to_string()
        }
    });

    if is_title && s.starts_with("TODO ") {
        let (_, b) = s.split_at(5);
        w.write_all(b"<span class=\"todo TODO\">TODO</span> ")?;
        w.write_all(b.as_bytes())?;
    } else if is_title && s.starts_with("DONE ") {
        let (_, b) = s.split_at(5);
        w.write_all(b"<span class=\"done DONE\">DONE</span> ")?;
        w.write_all(b.as_bytes())?;
    } else if is_title {
        w.write_all(s.as_bytes())?;
    } else {
        let s = PARBREAK.replace_all(&s, "</p><p>");
        w.write_all(s.as_bytes())?;
    }
    Ok(())
}

struct Output {
    use_syntect: bool,
    syntax_set: syntect::parsing::SyntaxSet,
    theme: syntect::highlighting::Theme,
    // rustfmt-formatted sources
    fmt: HashMap<String, String>,
}

fn output_tree<W: Write>(
    tree: &Tree,
    w: &mut W,
    num: &mut Vec<usize>,
    out: &mut Output,
) -> Result<(), anyhow::Error> {
    match tree {
        Tree::Root {
            ref children,
            ref title,
            ref level,
            ref properties,
            ref id,
            ..
        } => {
            for prop in properties {
                match prop {
                    Property::CustomId { .. } => {
                        // writeln!(w, "<a name=\"{}\"></a>", id)?;
                    }
                    _ => {}
                }
            }
            match id {
                Id::String(ref s) => writeln!(w, "<a name=\"{}\"></a>", s)?,
                Id::Int(s) => writeln!(w, "<a name=\"{}\"></a>", s)?,
            }
            if *level > 0 {
                writeln!(w, "<h{}>", level)?;
                for i in num.iter() {
                    write!(w, "{}.", i)?;
                }
                output_string(title, true, w)?;
                writeln!(w, "</h{}>", level)?;
            }
            num.push(1);
            for chi in children {
                output_tree(chi, w, num, out)?;
            }
            num.pop();
            if let Some(num) = num.last_mut() {
                *num += 1
            }
        }
        Tree::Item { ref children, .. } => {
            writeln!(w, "<ul>")?;
            for chi in children {
                writeln!(w, "<li>")?;
                // is there a single children, which also is a paragraph?
                let single_child = if chi.len() == 1 {
                    if let Tree::Block { ref contents, .. } = chi[0] {
                        output_string(contents, false, w)?;
                        true
                    } else {
                        false
                    }
                } else {
                    false
                };
                if !single_child {
                    for chi in chi {
                        output_tree(chi, w, num, out)?;
                    }
                }
                writeln!(w, "</li>")?;
            }
            writeln!(w, "</ul>")?;
        }
        Tree::Src {
            ref contents,
            ref style,
            ref options,
            ref id,
            ..
        } if out.use_syntect && style == "rust" => {
            writeln!(w, "")?;
            let syntax = out.syntax_set.find_syntax_by_name("Rust").unwrap();
            let mut highlighter = syntect::easy::HighlightLines::new(&syntax, &out.theme);
            let contents = if let Some((_, file)) = options.iter().find(|(k, _)| k == ":tangle") {
                debug!("file = {:?}", file);
                if let Some(contents) = out.fmt.get(id) {
                    contents
                } else {
                    let s = std::fs::read_to_string(file)?;
                    let mut current_line = String::new();
                    let mut current_id = None;
                    lazy_static! {
                        static ref ID: Regex = Regex::new(r#"(.*)// org id (.*)"#).unwrap();
                    }
                    for l in s.lines() {
                        if let Some(caps) = ID.captures(&l) {
                            let n = caps.get(2).unwrap().as_str().to_string();
                            current_line.push_str(&caps[1]);
                            current_line.push('\n');
                            if let Some(m) = current_id.take() {
                                out.fmt
                                    .insert(m, std::mem::replace(&mut current_line, String::new()));
                            }
                            current_id = Some(n);
                        } else {
                            current_line.push_str(l);
                            current_line.push('\n');
                        }
                    }
                    if let Some(m) = current_id.take() {
                        out.fmt
                            .insert(m, std::mem::replace(&mut current_line, String::new()));
                    }
                    out.fmt.get(id).unwrap_or(contents)
                }
            } else {
                contents
            };

            let mut output = String::new();

            for line in syntect::util::LinesWithEndings::from(contents) {
                let regions = highlighter.highlight(line, &out.syntax_set);
                syntect::html::append_highlighted_html_for_styled_line(
                    &regions[..],
                    syntect::html::IncludeBackground::No,
                    &mut output,
                );
            }

            if let Some((_, file)) = options.iter().find(|(k, _)| k == ":tangle") {
                writeln!(w, "<div class=\"org-src-container\"><div class=\"org-before-pre\">{}</div><pre>{}</pre></div>", file, output)?;
            } else {
                writeln!(w, "<div class=\"org-src-container\"><div class=\"org-before-pre\">Rust</div><pre>{}</pre></div>", output)?;
            }
        }
        Tree::Src {
            ref contents,
            ref style,
            ..
        } => {
            lazy_static! {
                static ref LT: Regex = Regex::new(r#"<"#).unwrap();
                static ref GT: Regex = Regex::new(r#">"#).unwrap();
            }
            let s = LT.replace_all(contents, "&lt;");
            let s = GT.replace_all(&s, "&gt;");

            writeln!(
                w,
                "<div class=\"org-src-container\"><pre class=\"src-{}\">{}</pre></div>",
                style, s
            )?;
        }
        Tree::Block { ref contents, .. } => {
            writeln!(w, "<p>")?;
            output_string(contents, false, w)?;
            writeln!(w, "</p>")?;
        }
    }
    Ok(())
}

fn table_of_contents<W: Write>(tree: &Tree, w: &mut W) -> Result<(), anyhow::Error> {
    writeln!(w, "<h1>Table of contents</h1>")?;
    if let Tree::Root { ref children, .. } = tree {
        table_of_contents_(children, w, &mut vec![1])?;
    }
    Ok(())
}

fn table_of_contents_<W: Write>(
    children: &[Tree],
    w: &mut W,
    num: &mut Vec<usize>,
) -> Result<(), anyhow::Error> {
    writeln!(w, "<ul>")?;
    for c in children.iter() {
        if let Tree::Root {
            ref children,
            ref title,
            ref id,
            ..
        } = c
        {
            match *id {
                Id::String(ref id) => write!(w, "<li><a href=\"#{}\">", id)?,
                Id::Int(ref id) => write!(w, "<li><a href=\"#{}\">", id)?,
            }
            for i in num.iter() {
                write!(w, "{}.", i)?;
            }
            output_string(title, true, w)?;
            writeln!(w, "</a>")?;
            num.push(1);
            table_of_contents_(children, w, num)?;
            num.pop();
            *num.last_mut().unwrap() += 1;
            writeln!(w, "</li>")?;
        }
    }
    writeln!(w, "</ul>")?;
    Ok(())
}