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
use crate::generation::gen_types::Context;

pub type Range = std::ops::Range<usize>;

pub trait Ranged {
  fn range(&self) -> &Range;
  fn text<'a>(&self, context: &Context<'a>) -> &'a str;
}

pub struct SourceFile {
  pub range: Range,
  pub children: Vec<Node>,
  pub yaml_header: Option<YamlHeader>,
}

pub struct YamlHeader {
  pub range: Range,
  pub text: String,
}

pub struct Heading {
  pub range: Range,
  pub level: u32,
  pub children: Vec<Node>,
}

pub struct Paragraph {
  pub range: Range,
  pub children: Vec<Node>,
}

pub struct BlockQuote {
  pub range: Range,
  pub children: Vec<Node>,
}

pub struct Text {
  pub range: Range,
  pub text: String,
}

pub enum TextDecorationKind {
  Emphasis,
  Strong,
  Strikethrough,
}

pub struct TextDecoration {
  pub range: Range,
  pub kind: TextDecorationKind,
  pub children: Vec<Node>,
}

pub struct Html {
  pub range: Range,
  pub text: String,
}

pub struct FootnoteReference {
  pub range: Range,
  pub name: String,
}

pub struct FootnoteDefinition {
  pub range: Range,
  pub name: String,
  pub children: Vec<Node>,
}

pub struct InlineLink {
  pub range: Range,
  pub children: Vec<Node>,
  pub url: String,
  pub title: Option<String>,
}

pub struct ReferenceLink {
  pub range: Range,
  pub children: Vec<Node>,
  pub reference: String,
}

pub struct ShortcutLink {
  pub range: Range,
  pub children: Vec<Node>,
}

pub struct AutoLink {
  pub range: Range,
  pub children: Vec<Node>,
}

pub struct LinkReference {
  pub range: Range,
  pub name: String,
  pub link: String,
  pub title: Option<String>,
}

pub struct InlineImage {
  pub range: Range,
  pub text: String,
  pub url: String,
  pub title: Option<String>,
}

pub struct ReferenceImage {
  pub range: Range,
  pub text: String,
  pub reference: String,
}

impl Text {
  pub fn starts_with_list_word(&self) -> bool {
    return crate::generation::utils::is_list_word(&get_first_word(&self.text));

    fn get_first_word(text: &str) -> String {
      let mut result = String::new();
      for c in text.chars() {
        if c.is_whitespace() {
          break;
        }
        result.push(c);
      }
      result
    }
  }
}

pub struct SoftBreak {
  pub range: Range,
}

pub struct HardBreak {
  pub range: Range,
}

pub struct List {
  pub range: Range,
  pub start_index: Option<u64>,
  pub children: Vec<Node>,
}

pub struct Item {
  pub range: Range,
  pub marker: Option<TaskListMarker>,
  pub children: Vec<Node>,
  pub sub_lists: Vec<Node>,
}

pub struct TaskListMarker {
  pub range: Range,
  pub is_checked: bool,
}

/// Inline code.
pub struct Code {
  pub range: Range,
  pub code: String,
}

pub struct CodeBlock {
  pub range: Range,
  pub tag: Option<String>,
  pub is_fenced: bool,
  pub code: String,
}

pub struct HorizontalRule {
  pub range: Range,
}

pub struct Table {
  pub range: Range,
  pub header: TableHead,
  pub column_alignment: Vec<ColumnAlignment>,
  pub rows: Vec<TableRow>,
}

pub struct TableHead {
  pub range: Range,
  pub cells: Vec<TableCell>,
}

pub struct TableRow {
  pub range: Range,
  pub cells: Vec<TableCell>,
}

pub struct TableCell {
  pub range: Range,
  pub children: Vec<Node>,
}

#[derive(PartialEq, Debug, Copy, Clone)]
pub enum ColumnAlignment {
  None,
  Left,
  Center,
  Right,
}

pub struct NotImplemented {
  pub range: Range,
}

macro_rules! generate_node {
    ($($node_name:ident),*) => {
        pub enum Node {
            $($node_name($node_name)),*,
        }

        #[cfg(debug_assertions)]
        #[derive(Debug)]
        pub enum NodeKind {
            $($node_name),*,
        }

        #[cfg(debug_assertions)]
        impl Node {
            #[allow(dead_code)]
            pub fn kind(&self) -> NodeKind {
                match self {
                    $(Node::$node_name(_) => NodeKind::$node_name),*
                }
            }
        }

        impl Ranged for Node {
            fn range<'a>(&'a self) -> &'a Range {
                match self {
                    $(Node::$node_name(node) => node.range()),*
                }
            }

            fn text<'a>(&self, context: &Context<'a>) -> &'a str {
                match self {
                    $(Node::$node_name(node) => node.text(context)),*
                }
            }
        }

        $(
        impl Ranged for $node_name {
            fn range<'a>(&'a self) -> &'a Range {
                &self.range
            }

            fn text<'a>(&self, context: &Context<'a>) -> &'a str {
                &context.file_text[self.range.start..self.range.end]
            }
        }

        impl Into<Node> for $node_name {
            fn into(self) -> Node {
                Node::$node_name(self)
            }
        }
        )*
    };
}

impl Node {
  pub fn starts_with_list_word(&self) -> bool {
    if let Node::Text(text) = self {
      text.starts_with_list_word()
    } else {
      false
    }
  }

  pub fn has_preceeding_space(&self, file_text: &str) -> bool {
    let range = self.range();
    if range.start == 0 {
      false
    } else {
      file_text.as_bytes().get(range.start - 1) == " ".as_bytes().get(0)
    }
  }

  pub fn starts_with_punctuation(&self, file_text: &str) -> bool {
    let range = self.range();
    let text = &file_text[range.start..range.end];
    if let Some(first_char) = text.chars().next() {
      first_char.is_ascii_punctuation()
    } else {
      false
    }
  }

  pub fn ends_with_punctuation(&self, file_text: &str) -> bool {
    let range = self.range();
    let text = &file_text[range.start..range.end];
    if let Some(last_char) = text.chars().last() {
      last_char.is_ascii_punctuation()
    } else {
      false
    }
  }
}

generate_node![
  NotImplemented,
  SourceFile,
  Heading,
  Paragraph,
  BlockQuote,
  Text,
  TextDecoration,
  Html,
  FootnoteReference,
  FootnoteDefinition,
  InlineLink,
  ReferenceLink,
  ShortcutLink,
  AutoLink,
  LinkReference,
  InlineImage,
  ReferenceImage,
  List,
  Item,
  TaskListMarker,
  SoftBreak,
  HardBreak,
  Code,
  CodeBlock,
  HorizontalRule,
  Table,
  TableHead,
  TableRow,
  TableCell
];