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
use crate::*;
use std::collections::HashMap;

pub trait Emitter {
    fn emit_block(&self, block: Block, out: &mut String);

    fn emit_elements(&self, elements: Vec<Element>, out: &mut String) {
        for element in elements {
            self.emit_element(element, out);
        }
    }

    fn emit_element(&self, element: Element, out: &mut String) {
        match element {
            Element::Text(t) => self.emit_text(t, out),
            Element::ExtensionElement(tag, e) => self.emit_extension_element(tag, *e, out),
        }
    }

    fn emit_text(&self, text: String, out: &mut String) {
        out.push_str(text.as_ref());
    }

    fn emit_extension_element(&self, _tag: ExtensionTag, element: Element, out: &mut String) {
        self.emit_element(element, out)
    }
}

impl TryInto<Box<dyn Emitter>> for &Path {
    type Error = ();

    fn try_into(self) -> Result<Box<dyn Emitter>, ()> {
        match self.extension() {
            Some(html) if html == "html" => Ok(Box::new(HtmlEmitter::new())),
            Some(txt) if txt == "txt" => Ok(Box::new(TextEmitter::new())),
            _ => Err(()),
        }
    }
}

pub struct HtmlEmitter {
    extension_element_map: HashMap<ExtensionTag, Box<dyn Fn(&Element) -> (String, Vec<(String, String)>)>>,
    extension_block_map: HashMap<ExtensionTag, Box<dyn Fn(&Vec<Element>) -> (String, Vec<(String, String)>)>>,
    extension_blocks_map: HashMap<ExtensionTag, Box<dyn Fn(&Vec<Block>) -> (String, Vec<(String, String)>)>>,
}

impl HtmlEmitter {
    pub fn new() -> HtmlEmitter {
        HtmlEmitter {
            extension_element_map: HashMap::new(),
            extension_block_map: HashMap::new(),
            extension_blocks_map: HashMap::new(),
        }
    }
}

impl HtmlEmitter {
    pub fn tagged_element<F: 'static + Fn(&Element) -> (String, Vec<(String, String)>)>(&mut self, tag: ExtensionTag, f: F) {
        self.extension_element_map.insert(tag, Box::new(f));
    }

    pub fn tagged_block<F: 'static + Fn(&Vec<Element>) -> (String, Vec<(String, String)>)>(&mut self, tag: ExtensionTag, f: F) {
        self.extension_block_map.insert(tag, Box::new(f));
    }

    pub fn tagged_blocks<F: 'static + Fn(&Vec<Block>) -> (String, Vec<(String, String)>)>(&mut self, tag: ExtensionTag, f: F) {
        self.extension_blocks_map.insert(tag, Box::new(f));
    }
}

impl Emitter for HtmlEmitter {
    fn emit_block(&self, block: Block, out: &mut String) {
        match block {
            Block::Paragraph(e) => {
                out.push_str("<p>\n  ");
                self.emit_elements(e, out);
                out.push_str("\n</p>\n");
            }
            Block::ExtensionBlock(extensions::LIST_ITEM, elements) => {
                out.push_str("  <li>\n    ");
                self.emit_elements(elements, out);
                out.push_str("\n  </li>\n");
            }
            Block::ExtensionBlock(tag, elements) => {
                let ext = self.extension_block_map.get(&tag).map(|f| f(&elements));
                if let Some((element, attrs)) = &ext {
                    out.push_str(format!("<{}", element).as_ref());
                    for (key, value) in attrs.iter() {
                        out.push_str(format!(" {}={:?}", key, value).as_ref());
                    }
                    out.push('>');
                } else {
                    out.push_str(format!("<div data-publ-tag={:?}>\n  ", tag).as_ref());
                }

                self.emit_elements(elements, out);

                if let Some((element, _)) = ext {
                    out.push_str(format!("</{}>", element).as_ref());
                } else {
                    out.push_str("\n</div>\n");
                }
            }
            Block::ExtensionBlocks(extensions::LIST, blocks) => {
                out.push_str("<ul>\n");
                for block in blocks {
                    self.emit_block(block, out);
                }
                out.push_str("</ul>\n");
            }
            Block::ExtensionBlocks(tag, blocks) => {
                let ext = self.extension_blocks_map.get(&tag).map(|f| f(&blocks));
                if let Some((element, attrs)) = &ext {
                    out.push_str(format!("<{}", element).as_ref());
                    for (key, value) in attrs.iter() {
                        out.push_str(format!(" {}={:?}", key, value).as_ref());
                    }
                    out.push('>');
                }
                for block in blocks {
                    self.emit_block(block, out);
                }
                if let Some((element, _)) = ext {
                    out.push_str(format!("</{}>", element).as_ref());
                }
            }
        }
    }

    fn emit_text(&self, text: String, out: &mut String) {
        for c in text.chars() {
            match c {
                '\'' => out.push_str("&apos;"),
                '\"' => out.push_str("&quot;"),
                '<' => out.push_str("&lt;"),
                '>' => out.push_str("&gt;"),
                '&' => out.push_str("&amp;"),
                c => out.push(c),
            }
        }
    }

    fn emit_extension_element(&self, tag: ExtensionTag, element: Element, out: &mut String) {
        match tag {
            extensions::BOLD => {
                out.push_str("<strong>");
                self.emit_element(element, out);
                out.push_str("</strong>");
            }
            extensions::ITALICS => {
                out.push_str("<em>");
                self.emit_element(element, out);
                out.push_str("</em>");
            }
            tag if self.extension_element_map.contains_key(&tag) => {
                let (el, attrs) = self.extension_element_map.get(&tag).unwrap()(&element);
                out.push_str(format!("<{}", el).as_ref());
                for (key, value) in attrs.iter() {
                    out.push_str(format!(" {}={:?}", key, value).as_ref());
                }
                out.push('>');
                self.emit_element(element, out);
                out.push_str(format!("</{}>", el).as_ref());
            }
            _ => self.emit_element(element, out),
        }
    }
}

pub struct TextEmitter {
}

impl TextEmitter {
    pub fn new() -> TextEmitter {
        TextEmitter {}
    }
}

impl Emitter for TextEmitter {
    fn emit_block(&self, block: Block, out: &mut String) {
        match block {
            Block::Paragraph(e) => {
                if !out.is_empty() {
                    out.push('\n');
                }
                self.emit_elements(e, out);
                out.push('\n');
            }
            _ => {}
        }
    }
}