liwe 0.12.0

IWE core library
Documentation
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
use pulldown_cmark_to_cmark::Options as CmarkOptions;

use serde::{Deserialize, Serialize};

use crate::graph::GraphContext;

use super::node::NodeIter;
use super::NodeId;

pub const DEFAULT_KEY_DATE_FORMAT: &str = "%Y-%m-%d";

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct MarkdownOptions {
    #[serde(default)]
    pub refs_extension: String,
    #[serde(default)]
    pub refs_path: RefsPath,
    #[serde(default)]
    pub refs_text: RefsText,
    pub date_format: Option<String>,
    pub time_format: Option<String>,
    pub locale: Option<String>,
    #[serde(default)]
    pub wiki_link_path: WikiLinkPath,
    #[serde(default)]
    pub formatting: FormattingOptions,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Format {
    #[default]
    Markdown,
    Djot,
}

impl Format {
    pub fn extension(&self) -> &'static str {
        match self {
            Format::Markdown => "md",
            Format::Djot => "dj",
        }
    }

    pub fn from_extension(extension: &str) -> Option<Format> {
        match extension {
            "md" => Some(Format::Markdown),
            "dj" => Some(Format::Djot),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct DjotOptions {
    #[serde(default)]
    pub refs_extension: String,
    #[serde(default)]
    pub refs_path: RefsPath,
    #[serde(default)]
    pub refs_text: RefsText,
    pub date_format: Option<String>,
    pub time_format: Option<String>,
    pub locale: Option<String>,
    #[serde(default)]
    pub formatting: FormattingOptions,
}

impl Default for DjotOptions {
    fn default() -> Self {
        Self {
            refs_extension: String::new(),
            refs_path: RefsPath::default(),
            refs_text: RefsText::default(),
            date_format: Some("%b %d, %Y".into()),
            time_format: None,
            locale: None,
            formatting: FormattingOptions::default(),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum FormatOptions {
    Markdown(MarkdownOptions),
    Djot(DjotOptions),
}

impl Default for FormatOptions {
    fn default() -> Self {
        FormatOptions::Markdown(MarkdownOptions::default())
    }
}

impl From<MarkdownOptions> for FormatOptions {
    fn from(options: MarkdownOptions) -> Self {
        FormatOptions::Markdown(options)
    }
}

impl From<DjotOptions> for FormatOptions {
    fn from(options: DjotOptions) -> Self {
        FormatOptions::Djot(options)
    }
}

impl FormatOptions {
    pub fn format(&self) -> Format {
        match self {
            FormatOptions::Markdown(_) => Format::Markdown,
            FormatOptions::Djot(_) => Format::Djot,
        }
    }

    pub fn extension(&self) -> &'static str {
        self.format().extension()
    }

    pub fn markdown_options(&self) -> MarkdownOptions {
        match self {
            FormatOptions::Markdown(options) => options.clone(),
            FormatOptions::Djot(_) => MarkdownOptions::default(),
        }
    }

    pub fn refs_extension(&self) -> &str {
        match self {
            FormatOptions::Markdown(options) => &options.refs_extension,
            FormatOptions::Djot(options) => &options.refs_extension,
        }
    }

    pub fn refs_path(&self) -> RefsPath {
        match self {
            FormatOptions::Markdown(options) => options.refs_path,
            FormatOptions::Djot(options) => options.refs_path,
        }
    }

    pub fn refs_text(&self) -> RefsText {
        match self {
            FormatOptions::Markdown(options) => options.refs_text,
            FormatOptions::Djot(options) => options.refs_text,
        }
    }

    pub fn date_format(&self) -> Option<&str> {
        match self {
            FormatOptions::Markdown(options) => options.date_format.as_deref(),
            FormatOptions::Djot(options) => options.date_format.as_deref(),
        }
    }

    pub fn time_format(&self) -> Option<&str> {
        match self {
            FormatOptions::Markdown(options) => options.time_format.as_deref(),
            FormatOptions::Djot(options) => options.time_format.as_deref(),
        }
    }

    pub fn locale(&self) -> Option<&str> {
        match self {
            FormatOptions::Markdown(options) => options.locale.as_deref(),
            FormatOptions::Djot(options) => options.locale.as_deref(),
        }
    }

    pub fn formatting(&self) -> &FormattingOptions {
        match self {
            FormatOptions::Markdown(options) => &options.formatting,
            FormatOptions::Djot(options) => &options.formatting,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum WikiLinkPath {
    Full,
    Short,
    #[default]
    Preserve,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum RefsPath {
    #[default]
    Relative,
    Absolute,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum RefsText {
    #[default]
    Preserve,
    Normalize,
}

impl RefsText {
    pub fn normalize(&self) -> bool {
        matches!(self, RefsText::Normalize)
    }
}

impl Default for MarkdownOptions {
    fn default() -> Self {
        Self {
            refs_extension: String::new(),
            refs_path: RefsPath::default(),
            refs_text: RefsText::default(),
            date_format: Some("%b %d, %Y".into()),
            time_format: None,
            locale: None,
            wiki_link_path: WikiLinkPath::Preserve,
            formatting: FormattingOptions::default(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum LineBreakStyle {
    Backslash,
    Spaces,
}

#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
pub struct FormattingOptions {
    pub emphasis_token: Option<String>,
    pub strong_token: Option<String>,
    pub list_token: Option<String>,
    pub ordered_list_token: Option<String>,
    pub code_block_token: Option<String>,
    pub code_block_token_count: Option<usize>,
    pub increment_ordered_list_bullets: Option<bool>,
    pub ordered_list_content_indent: Option<usize>,
    pub bullet_list_content_indent: Option<usize>,
    pub rule_token: Option<String>,
    pub rule_token_count: Option<usize>,
    pub wrap_column: Option<usize>,
    pub preserve_line_breaks: Option<bool>,
    pub line_break_style: Option<LineBreakStyle>,
    pub preserve_newlines: Option<bool>,
}

impl FormattingOptions {
    pub fn validated(self) -> Self {
        Self {
            emphasis_token: self
                .emphasis_token
                .filter(|v| matches!(v.as_str(), "*" | "_")),
            strong_token: self
                .strong_token
                .filter(|v| matches!(v.as_str(), "**" | "__")),
            list_token: self
                .list_token
                .filter(|v| matches!(v.as_str(), "*" | "-" | "+")),
            ordered_list_token: self
                .ordered_list_token
                .filter(|v| matches!(v.as_str(), "." | ")")),
            code_block_token: self
                .code_block_token
                .filter(|v| matches!(v.as_str(), "`" | "~")),
            code_block_token_count: self.code_block_token_count.filter(|&v| v >= 3),
            increment_ordered_list_bullets: self.increment_ordered_list_bullets,
            ordered_list_content_indent: self
                .ordered_list_content_indent
                .filter(|&v| (2..=4).contains(&v)),
            bullet_list_content_indent: self
                .bullet_list_content_indent
                .filter(|&v| (2..=4).contains(&v)),
            rule_token: self
                .rule_token
                .filter(|v| matches!(v.as_str(), "-" | "*" | "_")),
            rule_token_count: self.rule_token_count.filter(|&v| v >= 3),
            wrap_column: self.wrap_column.filter(|&v| v >= 20),
            preserve_line_breaks: self.preserve_line_breaks,
            line_break_style: self.line_break_style,
            preserve_newlines: self.preserve_newlines,
        }
    }

    pub fn emphasis_token(&self) -> &str {
        self.emphasis_token.as_deref().unwrap_or("*")
    }

    pub fn strong_token(&self) -> &str {
        self.strong_token.as_deref().unwrap_or("**")
    }

    pub fn list_token(&self) -> &str {
        self.list_token.as_deref().unwrap_or("-")
    }

    pub fn list_token_char(&self) -> char {
        self.list_token().chars().next().unwrap_or('-')
    }

    pub fn ordered_list_token(&self) -> &str {
        self.ordered_list_token.as_deref().unwrap_or(".")
    }

    pub fn ordered_list_token_char(&self) -> char {
        self.ordered_list_token().chars().next().unwrap_or('.')
    }

    pub fn code_block_token(&self) -> &str {
        self.code_block_token.as_deref().unwrap_or("`")
    }

    pub fn code_block_token_char(&self) -> char {
        self.code_block_token().chars().next().unwrap_or('`')
    }

    pub fn code_block_token_count(&self) -> usize {
        self.code_block_token_count.unwrap_or(3)
    }

    pub fn increment_ordered_list_bullets(&self) -> bool {
        self.increment_ordered_list_bullets.unwrap_or(true)
    }

    pub fn ordered_list_content_indent(&self) -> Option<usize> {
        self.ordered_list_content_indent
    }

    pub fn bullet_list_content_indent(&self) -> Option<usize> {
        self.bullet_list_content_indent
    }

    pub fn rule_token(&self) -> &str {
        self.rule_token.as_deref().unwrap_or("-")
    }

    pub fn rule_token_count(&self) -> usize {
        self.rule_token_count.unwrap_or(72)
    }

    pub fn wrap_column(&self) -> Option<usize> {
        self.wrap_column
    }

    pub fn preserve_line_breaks(&self) -> bool {
        self.preserve_line_breaks.unwrap_or(false)
    }

    pub fn preserve_newlines(&self) -> bool {
        self.preserve_newlines.unwrap_or(false)
    }

    pub fn line_break_style(&self) -> LineBreakStyle {
        self.line_break_style.unwrap_or(LineBreakStyle::Backslash)
    }

    pub fn line_break_marker(&self) -> &'static str {
        match self.line_break_style() {
            LineBreakStyle::Backslash => "\\\n",
            LineBreakStyle::Spaces => "  \n",
        }
    }

    pub fn to_cmark_options(&self) -> CmarkOptions<'_> {
        CmarkOptions {
            newlines_after_headline: 2,
            newlines_after_paragraph: 2,
            newlines_after_codeblock: 2,
            newlines_after_htmlblock: 1,
            newlines_after_table: 2,
            newlines_after_rule: 2,
            newlines_after_list: 2,
            newlines_after_blockquote: 2,
            newlines_after_rest: 1,
            newlines_after_metadata: 1,
            code_block_token_count: self.code_block_token_count(),
            code_block_token: self.code_block_token_char(),
            list_token: self.list_token_char(),
            ordered_list_token: self.ordered_list_token_char(),
            increment_ordered_list_bullets: self.increment_ordered_list_bullets(),
            emphasis_token: self.emphasis_token_char(),
            strong_token: self.strong_token(),
            use_html_for_super_sub_script: false,
        }
    }

    fn emphasis_token_char(&self) -> char {
        self.emphasis_token().chars().next().unwrap_or('*')
    }
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum LinkType {
    #[serde(rename = "markdown")]
    Markdown,
    #[serde(rename = "wiki")]
    WikiLink,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum InlineType {
    #[serde(rename = "section")]
    Section,
    #[serde(rename = "quote")]
    Quote,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum Operation {
    Replace,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum TargetType {
    Block,
    Paragraph,
    List,
}

impl TargetType {
    pub fn acceptable_target(&self, id: NodeId, context: impl GraphContext) -> Option<NodeId> {
        match self {
            TargetType::Block => Some(id).filter(|id| !context.node(*id).is_section()),
            TargetType::Paragraph => Some(id).filter(|id| context.node(*id).is_leaf()),
            TargetType::List => Some(id).filter(|id| context.node(*id).is_leaf()),
        }
    }
}