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
//! Defines the marks used in OpMark.

/// How the element aligns. Currently work for `Image` only.
#[derive(Debug)]
pub enum AlignHorizontal {
    Auto,
    Left,
    Right,
    Center,
}

impl Default for AlignHorizontal {
    fn default() -> Self {
        AlignHorizontal::Auto
    }
}

/// The heading level of the text element.
#[derive(Clone, Debug)]
pub enum Heading {
    None,
    H1,
    H2,
    H3,
    H4,
    H5,
}

impl Default for Heading {
    fn default() -> Self {
        Heading::None
    }
}

impl From<u8> for Heading {
    #[inline]
    fn from(n: u8) -> Self {
        match n {
            0 => Heading::None,
            1 => Heading::H1,
            2 => Heading::H2,
            3 => Heading::H3,
            4 => Heading::H4,
            _ => Heading::H5,
        }
    }
}

impl Heading {
    #[inline]
    pub fn to_int(&self) -> u8 {
        match *self {
            Heading::None => 0,
            Heading::H1 => 1,
            Heading::H2 => 2,
            Heading::H3 => 3,
            Heading::H4 => 4,
            Heading::H5 => 5,
        }
    }
}

/// The intent level of the text element.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum IndentLevel {
    None,
    I1,
    I2,
    I3,
    I4,
    I5,
}

impl Default for IndentLevel {
    fn default() -> Self {
        IndentLevel::None
    }
}

impl From<u8> for IndentLevel {
    #[inline]
    fn from(n: u8) -> Self {
        match n {
            0 => IndentLevel::None,
            1 => IndentLevel::I1,
            2 => IndentLevel::I2,
            3 => IndentLevel::I3,
            4 => IndentLevel::I4,
            _ => IndentLevel::I5,
        }
    }
}

impl IndentLevel {
    #[inline]
    pub fn to_int(&self) -> u8 {
        match *self {
            IndentLevel::None => 0,
            IndentLevel::I1 => 1,
            IndentLevel::I2 => 2,
            IndentLevel::I3 => 3,
            IndentLevel::I4 => 4,
            IndentLevel::I5 => 5,
        }
    }
}

/// Whether the text element is within a list.
#[derive(Clone, Debug)]
pub enum Listing {
    /// Text is not in a list.
    None,
    /// Text is in an ordered list.
    Ordered(u8, IndentLevel),
    /// Text is in an unordered list.
    Unordered(IndentLevel),
}

impl Default for Listing {
    fn default() -> Self {
        Listing::None
    }
}

/// The marks used in OpMark.
#[derive(Debug)]
pub enum Mark {
    /// A code block element:
    /// ````text
    /// ```language
    /// code
    /// ```
    CodeBlock(String, Option<String>),
    /// An image element:
    /// ```
    /// ![title](src)<options>
    /// ```
    /// You can specify the size and the alignment of the image in options:
    /// ```
    /// // Image with width of 50.
    /// ![test](test.png)<w50>
    /// // Image with height of 50.
    /// ![test](test.png)<h50>
    /// // Image with center alignment. Available values: auto, left, right, center.
    /// ![test](test.png)<center>
    /// ```
    /// You can combine options together, and each option is separated by `|`.
    ///
    /// `options` is optional.
    Image(String, String, StyleImage),
    /// A new line element.
    NewLine,
    /// A transition element:
    /// ```
    /// ---t
    /// ```
    /// A transition is a group of elements which show up together after interaction (usually mouse click or keyboard input).
    ///
    /// A transition starts at a transition mark (`---t`), and ends at either the next transition mark, next page mark, or a transition end mark.
    ///
    /// Number can be appended to a transition mark, indicates that the order of the appearence of this transition group (noted that the index starts from 0), otherwise the transitions show up from top to bottom:
    /// ```
    /// ---t
    /// This line will show up after the first interaction.
    /// ---t3
    /// This line will show up after the fourth interaction.
    /// ---t1
    /// This line will show up after the second interaction.
    /// ```
    Transition(usize, Vec<Mark>),
    /// An element marks where the previous transition ends:
    /// ```
    /// t----
    /// ```
    TransitionEnd,
    /// A page mark:
    /// ```
    /// ---
    /// ```
    ///
    /// A page is a group of transitions. Transitions from different pages would never appear in the window at the same time.
    Page(Vec<Mark>),
    /// A separator element:
    /// ```
    /// ---- // A horizontal separator.
    /// ----v // A vertical separator.
    /// ```
    Separator(SeparatorDir),
    /// A text element:
    /// ```text
    /// normal text
    /// ## heading 1
    /// ### heading 2
    /// #### heading 3
    /// ##### heading 4
    /// ###### heading 5
    ///
    /// *bold*
    /// `code`
    /// /italics/
    /// $small$
    /// ~strikethrough~
    /// _underline_
    ///
    /// <hyperlink>
    /// [hyperlink title](hyperlink)
    ///
    /// - unordered list
    ///
    /// 1. ordered list
    Text(String, StyleText),
}

/// The direction of the seperator element.
#[derive(Debug)]
pub enum SeparatorDir {
    Horizontal,
    Vertical,
}

/// The configuration of the image element.
#[derive(Debug, Default)]
pub struct StyleImage {
    /// How the image should be aligned horizontally.
    pub align_h: AlignHorizontal,
    /// A string defines the url where the image should link to.
    pub hyperlink: String,
    /// The width of the image. If `None`, the ordinary width of the image will be used.
    pub width: Option<f32>,
    /// The height of the image. If `None`, the ordinary height of the image will be used.
    pub height: Option<f32>,
}

impl StyleImage {
    #[inline]
    pub fn new() -> Self {
        Default::default()
    }

    #[inline]
    pub fn with_align_h(mut self, align_h: AlignHorizontal) -> Self {
        self.align_h = align_h;
        self
    }

    #[inline]
    pub fn with_height(mut self, height: f32) -> Self {
        self.height = Some(height);
        self
    }

    #[inline]
    pub fn with_hyperlink(mut self, hyperlink: String) -> Self {
        self.hyperlink = hyperlink;
        self
    }

    #[inline]
    pub fn with_width(mut self, width: f32) -> Self {
        self.width = Some(width);
        self
    }
}

/// The configuration of the text element.
#[derive(Debug, Default)]
pub struct StyleText {
    /// Should the text be bold.
    pub bold: bool,
    /// Should the text be code-style.
    pub code: bool,
    /// The heading level of the text.
    pub heading: Heading,
    /// The hyperlink the text links to.
    pub hyperlink: String,
    /// Should the text be italics.
    pub italics: bool,
    /// Whether the text is within an ordered/unordered list.
    pub listing: Listing,
    /// Should the text be quote-style.
    pub quote: bool,
    /// Should the text be small.
    pub small: bool,
    /// Should the text be strikethroughed.
    pub strikethrough: bool,
    /// Should the text be underlined.
    pub underline: bool,
}

impl Clone for StyleText {
    fn clone(&self) -> Self {
        StyleText {
            bold: self.bold,
            code: self.code,
            heading: self.heading.clone(),
            hyperlink: self.hyperlink.clone(),
            italics: self.italics,
            listing: self.listing.clone(),
            quote: self.quote,
            small: self.small,
            strikethrough: self.strikethrough,
            underline: self.underline,
        }
    }
}

impl StyleText {
    #[inline]
    pub fn new() -> Self {
        Default::default()
    }

    #[inline]
    pub fn with_bold(mut self) -> Self {
        self.bold = true;
        self
    }

    #[inline]
    pub fn with_code(mut self) -> Self {
        self.code = true;
        self
    }

    #[inline]
    pub fn with_heading(mut self, heading: Heading) -> Self {
        self.heading = heading;
        self
    }

    #[inline]
    pub fn with_hyperlink(mut self, hyperlink: String) -> Self {
        self.hyperlink = hyperlink;
        self
    }

    #[inline]
    pub fn with_italics(mut self) -> Self {
        self.italics = true;
        self
    }

    #[inline]
    pub fn with_listing(mut self, listing: Listing) -> Self {
        self.listing = listing;
        self
    }

    #[inline]
    pub fn with_quote(mut self) -> Self {
        self.quote = true;
        self
    }

    #[inline]
    pub fn with_small(mut self) -> Self {
        self.small = true;
        self
    }

    #[inline]
    pub fn with_strikethrough(mut self) -> Self {
        self.strikethrough = true;
        self
    }

    #[inline]
    pub fn with_underline(mut self) -> Self {
        self.underline = true;
        self
    }
}