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
use failure::Error;
use oatie::doc::*;
use oatie::rtf::*;
use oatie::writer::DocWriter;
use pulldown_cmark::{
    Event::{
        self,
        End,
        FootnoteReference,
        HardBreak,
        Html,
        InlineHtml,
        SoftBreak,
        Start,
        Text,
    },
    Parser,
    Tag,
};

struct Ctx<'b, I> {
    iter: I,
    body: &'b mut DocWriter<RtfSchema>,
    styles: StyleSet,
    bare_text: bool,
}

impl<'a, 'b, I: Iterator<Item = Event<'a>>> Ctx<'b, I> {
    pub fn run(&mut self) {
        while let Some(event) = self.iter.next() {
            match event {
                Start(tag) => {
                    self.start_tag(tag);
                }
                End(tag) => {
                    self.end_tag(tag);
                }
                Text(text) => {
                    // TODO wrapping bare txt in a paragraph makes the result
                    // validate, but 1) the wrapping element should be a div,
                    // since it lacks any margin and 2) it should be contiguous
                    // with all other elements that follow it, so text<b>with</b>bold
                    // doesn't have three block elements generated, 1 for each span.
                    if self.bare_text {
                        self.body.begin();
                    }
                    self.body.place(&DocText(
                        self.styles.clone(),
                        DocString::from_str(text.as_ref()),
                    ));
                    if self.bare_text {
                        self.body.close(Attrs::Para);
                    }
                }
                SoftBreak => {
                    // TODO this should actually use some heuristics to know
                    // if we should soft-space like HTML does. whitespace is
                    // significant in the document model so we can't always
                    // just add a space
                    if self.bare_text {
                        self.body.begin();
                    }
                    self.body
                        .place(&DocText(self.styles.clone(), DocString::from_str(" ")));
                    if self.bare_text {
                        self.body.close(Attrs::Para);
                    }
                }
                HardBreak => {
                    self.body
                        .place(&DocText(self.styles.clone(), DocString::from_str("\n")));
                }
                Html(html) => {
                    self.body.begin();
                    self.body
                        .place(&DocText(StyleSet::new(), DocString::from_str(&html)));
                    self.body.close(Attrs::Html);
                }

                InlineHtml(..) | FootnoteReference(..) => {}
            }
        }
    }

    fn start_tag(&mut self, tag: Tag<'a>) {
        match tag {
            // Blocks
            Tag::Paragraph => {
                self.body.begin();
                self.bare_text = false;
            }
            Tag::Header(_level) => {
                self.body.begin();
                self.bare_text = false;
            }
            Tag::CodeBlock(_info) => {
                self.body.begin();
                self.bare_text = false;
            }

            // List items
            Tag::Item => {
                self.body.begin();
                self.bare_text = true;
            }

            // Block objects
            Tag::Rule => {
                self.body.begin();
            }

            // Spans
            Tag::Link(dest, _title) => {
                // TODO link styles
                // self.styles.insert(Style::Link, Some(dest.to_string()));
            }
            Tag::Strong => {
                self.styles.insert(RtfStyle::Bold);
            }
            Tag::Emphasis => {
                self.styles.insert(RtfStyle::Italic);
            }

            Tag::Table(..)
            | Tag::TableHead
            | Tag::TableRow
            | Tag::TableCell
            | Tag::BlockQuote
            | Tag::Code
            | Tag::List(_)
            | Tag::Image(..)
            | Tag::FootnoteDefinition(_) => {}
        }
    }

    fn end_tag(&mut self, tag: Tag<'_>) {
        match tag {
            // Blocks
            Tag::Paragraph => {
                self.body.close(Attrs::Para);
                self.bare_text = true;
            }
            Tag::Header(level) => {
                self.body.close(Attrs::Header(level as u8));
                self.bare_text = true;
            }
            Tag::CodeBlock(_) => {
                self.body.close(Attrs::Code);
                self.bare_text = true;
            }

            // List items
            Tag::Item => {
                self.body.close(Attrs::ListItem);
                self.bare_text = true;
            }

            // Block objects
            Tag::Rule => {
                self.body.close(Attrs::Rule);
            }
            Tag::Image(_, _) => (), // shouldn't happen, handled in start

            // Spans
            Tag::Link(..) => {
                // TODO add link styles
                // self.styles.remove(&RtfStyle::Link);
            }
            Tag::Strong => {
                self.styles.remove(&RtfStyle::Bold);
            }
            Tag::Emphasis => {
                self.styles.remove(&RtfStyle::Italic);
            }

            Tag::FootnoteDefinition(_)
            | Tag::Code
            | Tag::TableCell
            | Tag::Table(_)
            | Tag::TableHead
            | Tag::TableRow
            | Tag::List(_)
            | Tag::BlockQuote => {}
        }
    }
}

pub fn markdown_to_doc(input: &str) -> Result<DocSpan<RtfSchema>, Error> {
    let parser = Parser::new(input);
    let mut doc_writer = DocWriter::new();
    {
        let mut ctx = Ctx {
            iter: parser,
            body: &mut doc_writer,
            styles: StyleSet::new(),
            bare_text: true,
        };
        ctx.run();
    }
    doc_writer.result()
}