adoctra-core 0.0.2

Core parser and document model for the adoctra AsciiDoc toolchain
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Ruby's derived backend and doctype attributes (`Document#
//! update_backend_attributes` / `update_doctype_attributes`): `backend-html5`,
//! `basebackend-html`, `doctype-article`, their combinations, `filetype`,
//! `htmlsyntax`, `outfilesuffix` and `pagewidth`.
//!
//! Like Ruby, the current backend and doctype are tracked separately from the
//! attributes (`@backend`/`@doctype`): unsetting `doctype` in the header
//! removes the attribute only, and later updates still clean up the derived
//! attributes of the remembered value.

use crate::Attributes;

/// Ruby's `DEFAULT_BACKEND`.
pub(crate) const DEFAULT_BACKEND: &str = "html5";
/// Ruby's `DEFAULT_DOCTYPE`.
pub(crate) const DEFAULT_DOCTYPE: &str = "article";

/// The traits of a backend's converter (`init_backend_traits`).
struct Traits {
    basebackend: String,
    filetype: String,
    htmlsyntax: Option<&'static str>,
    outfilesuffix: String,
}

/// The built-in converters' traits; any other backend gets Ruby's
/// `Converter.derive_backend_traits` (Ruby itself aborts without a converter
/// for it, while adoctra keeps parsing).
fn traits(backend: &str, htmlsyntax: Option<&str>) -> Traits {
    let known = |basebackend: &str, filetype: &str, suffix: &str| Traits {
        basebackend: basebackend.to_owned(),
        filetype: filetype.to_owned(),
        htmlsyntax: None,
        outfilesuffix: suffix.to_owned(),
    };
    match backend {
        "html5" => Traits {
            htmlsyntax: Some(if htmlsyntax == Some("xml") {
                "xml"
            } else {
                "html"
            }),
            ..known("html", "html", ".html")
        },
        "docbook5" => known("docbook", "xml", ".xml"),
        "manpage" => known("manpage", "man", ".man"),
        _ => {
            let basebackend = backend.trim_end_matches(|c: char| c.is_ascii_digit());
            let suffix = match basebackend {
                "html" => Some(".html"),
                "docbook" => Some(".xml"),
                "pdf" => Some(".pdf"),
                "epub" => Some(".epub"),
                "manpage" => Some(".man"),
                "asciidoc" => Some(".adoc"),
                _ => None,
            };
            let outfilesuffix = suffix.map_or_else(|| format!(".{basebackend}"), str::to_owned);
            Traits {
                filetype: outfilesuffix[1..].to_owned(),
                htmlsyntax: None,
                basebackend: basebackend.to_owned(),
                outfilesuffix,
            }
        }
    }
}

/// Ruby's `@backend` and `@doctype` with the derived attributes they own.
#[derive(Debug, Clone, Default)]
pub(crate) struct Profile {
    pub backend: Option<String>,
    pub doctype: Option<String>,
    /// The `htmlsyntax` of the converter Ruby (re)creates for the backend
    /// (`create_converter` with `htmlsyntax: @attributes['htmlsyntax']`):
    /// `html`/`xml` for html5, none for other backends. A later
    /// `htmlsyntax` entry changes the attribute, not the converter.
    pub htmlsyntax: Option<&'static str>,
}

impl Profile {
    /// Ruby's `update_backend_attributes`. `init` keeps an existing
    /// `outfilesuffix` (`||=`), otherwise it is replaced unless locked.
    pub fn update_backend(
        &mut self,
        attrs: &mut Attributes,
        new_backend: &str,
        init: bool,
        outfilesuffix_locked: bool,
    ) {
        if !init && self.backend.as_deref() == Some(new_backend) {
            return;
        }
        let current_basebackend = attrs.get("basebackend").map(str::to_owned);
        let (actual, mut backend) = match new_backend.split_once(':') {
            Some((actual, delegate)) => (Some(actual), delegate),
            None => (None, new_backend),
        };
        if let Some(rest) = backend.strip_prefix('x').filter(|b| b.starts_with("html")) {
            attrs.set("htmlsyntax", "xml");
            backend = rest;
        } else if backend.starts_with("html") && attrs.get("htmlsyntax").is_none() {
            attrs.set("htmlsyntax", "html");
        }
        let backend = match backend {
            "html" => "html5",
            "docbook" => "docbook5",
            other => other,
        };
        let backend = actual.unwrap_or(backend).to_owned();
        if let Some(doctype) = &self.doctype {
            if let Some(current) = &self.backend {
                attrs.remove(&format!("backend-{current}"));
                attrs.remove(&format!("backend-{current}-doctype-{doctype}"));
            }
            attrs.set(format!("backend-{backend}-doctype-{doctype}"), "");
            attrs.set(format!("doctype-{doctype}"), "");
        } else if let Some(current) = &self.backend {
            attrs.remove(&format!("backend-{current}"));
        }
        attrs.set(format!("backend-{backend}"), "");
        attrs.set("backend", backend.clone());
        let traits = traits(&backend, attrs.get("htmlsyntax"));
        self.backend = Some(backend);
        self.htmlsyntax = traits.htmlsyntax;
        if let Some(syntax) = traits.htmlsyntax {
            attrs.set("htmlsyntax", syntax);
        }
        if if init {
            attrs.get("outfilesuffix").is_none()
        } else {
            !outfilesuffix_locked
        } {
            attrs.set("outfilesuffix", traits.outfilesuffix);
        }
        if let Some(current) = attrs.get("filetype").map(str::to_owned) {
            attrs.remove(&format!("filetype-{current}"));
        }
        attrs.set(format!("filetype-{}", traits.filetype), "");
        attrs.set("filetype", traits.filetype);
        // Ruby's DEFAULT_PAGE_WIDTHS.
        if traits.basebackend == "docbook" {
            attrs.set("pagewidth", "425");
        } else {
            attrs.remove("pagewidth");
        }
        let basebackend = traits.basebackend;
        if current_basebackend.as_deref() != Some(basebackend.as_str()) {
            if let Some(doctype) = &self.doctype {
                if let Some(current) = &current_basebackend {
                    attrs.remove(&format!("basebackend-{current}"));
                    attrs.remove(&format!("basebackend-{current}-doctype-{doctype}"));
                }
                attrs.set(format!("basebackend-{basebackend}-doctype-{doctype}"), "");
            } else if let Some(current) = &current_basebackend {
                attrs.remove(&format!("basebackend-{current}"));
            }
            attrs.set(format!("basebackend-{basebackend}"), "");
            attrs.set("basebackend", basebackend);
        }
    }

    /// Ruby's `update_doctype_attributes`.
    pub fn update_doctype(&mut self, attrs: &mut Attributes, new_doctype: &str) {
        if self.doctype.as_deref() == Some(new_doctype) {
            return;
        }
        let basebackend = attrs.get("basebackend").map(str::to_owned);
        if let Some(current) = &self.doctype {
            attrs.remove(&format!("doctype-{current}"));
            if let Some(backend) = &self.backend {
                attrs.remove(&format!("backend-{backend}-doctype-{current}"));
            }
            if let Some(base) = &basebackend {
                attrs.remove(&format!("basebackend-{base}-doctype-{current}"));
            }
        }
        if let Some(backend) = &self.backend {
            attrs.set(format!("backend-{backend}-doctype-{new_doctype}"), "");
        }
        if let Some(base) = &basebackend {
            attrs.set(format!("basebackend-{base}-doctype-{new_doctype}"), "");
        }
        attrs.set(format!("doctype-{new_doctype}"), "");
        attrs.set("doctype", new_doctype);
        self.doctype = Some(new_doctype.to_owned());
    }

    /// The root document's initialization after the host attributes: the
    /// default doctype (`manpage` for the manpage backend, locking it) and
    /// the initial backend. Returns whether `doctype` must be locked.
    pub fn initialize(&mut self, attrs: &mut Attributes, outfilesuffix_locked: bool) -> bool {
        let backend = attrs.get("backend").unwrap_or(DEFAULT_BACKEND).to_owned();
        let lock = backend == "manpage";
        if lock {
            attrs.set("doctype", "manpage");
        } else if attrs.get("doctype").is_none() {
            attrs.set("doctype", DEFAULT_DOCTYPE);
        }
        self.doctype = attrs.get("doctype").map(str::to_owned);
        self.update_backend(attrs, &backend, true, outfilesuffix_locked);
        lock
    }

    /// A nested document (Ruby's `parent_doc` branch): the backend stays, the
    /// parent's doctype attribute is reset to the default.
    pub fn nested(attrs: &mut Attributes) -> Self {
        let mut profile = Self {
            backend: attrs.get("backend").map(str::to_owned),
            doctype: attrs.get("doctype").map(str::to_owned),
            htmlsyntax: None,
        };
        profile.update_doctype(attrs, DEFAULT_DOCTYPE);
        profile
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn keys(attrs: &Attributes) -> Vec<String> {
        attrs.iter().map(|(k, v)| format!("{k}={v}")).collect()
    }

    #[test]
    fn initial_html5_article() {
        let mut attrs = Attributes::new();
        let mut profile = Profile::default();
        assert!(!profile.initialize(&mut attrs, false));
        assert_eq!(
            keys(&attrs),
            [
                "backend=html5",
                "backend-html5=",
                "backend-html5-doctype-article=",
                "basebackend=html",
                "basebackend-html=",
                "basebackend-html-doctype-article=",
                "doctype=article",
                "doctype-article=",
                "filetype=html",
                "filetype-html=",
                "htmlsyntax=html",
                "outfilesuffix=.html",
            ]
        );
    }

    #[test]
    fn manpage_backend_forces_doctype() {
        let mut attrs = Attributes::new();
        attrs.set("backend", "manpage");
        attrs.set("doctype", "book");
        let mut profile = Profile::default();
        assert!(profile.initialize(&mut attrs, false));
        assert_eq!(attrs.get("doctype"), Some("manpage"));
        assert_eq!(attrs.get("outfilesuffix"), Some(".man"));
        assert_eq!(attrs.get("basebackend-manpage-doctype-manpage"), Some(""));
        assert_eq!(attrs.get("htmlsyntax"), None);
    }

    #[test]
    fn switching_backend_and_doctype_cleans_up() {
        let mut attrs = Attributes::new();
        let mut profile = Profile::default();
        profile.initialize(&mut attrs, false);
        profile.update_doctype(&mut attrs, "book");
        profile.update_backend(&mut attrs, "docbook", false, false);
        assert_eq!(attrs.get("backend"), Some("docbook5"));
        assert_eq!(attrs.get("backend-html5"), None);
        assert_eq!(attrs.get("backend-html5-doctype-book"), None);
        assert_eq!(attrs.get("basebackend-html"), None);
        assert_eq!(attrs.get("basebackend-html-doctype-book"), None);
        assert_eq!(attrs.get("doctype-article"), None);
        assert_eq!(attrs.get("basebackend-docbook-doctype-book"), Some(""));
        assert_eq!(attrs.get("filetype"), Some("xml"));
        assert_eq!(attrs.get("filetype-html"), None);
        assert_eq!(attrs.get("outfilesuffix"), Some(".xml"));
        assert_eq!(attrs.get("pagewidth"), Some("425"));
        // Ruby leaves htmlsyntax behind.
        assert_eq!(attrs.get("htmlsyntax"), Some("html"));
    }

    #[test]
    fn xhtml_and_derived_backends() {
        let mut attrs = Attributes::new();
        let mut profile = Profile::default();
        profile.initialize(&mut attrs, false);
        profile.update_backend(&mut attrs, "xhtml", false, true);
        assert_eq!(attrs.get("backend"), Some("html5"));
        assert_eq!(attrs.get("htmlsyntax"), Some("xml"));
        assert_eq!(attrs.get("outfilesuffix"), Some(".html"));
        profile.update_backend(&mut attrs, "pdf2", false, false);
        assert_eq!(attrs.get("basebackend"), Some("pdf"));
        assert_eq!(attrs.get("filetype-pdf"), Some(""));
        assert_eq!(attrs.get("outfilesuffix"), Some(".pdf"));
        assert_eq!(attrs.get("backend-html5"), None);
        assert_eq!(attrs.get("backend-pdf2"), Some(""));
    }

    #[test]
    fn nested_resets_doctype() {
        let mut attrs = Attributes::new();
        let mut profile = Profile::default();
        profile.initialize(&mut attrs, false);
        profile.update_doctype(&mut attrs, "book");
        let nested = Profile::nested(&mut attrs);
        assert_eq!(nested.doctype.as_deref(), Some("article"));
        assert_eq!(attrs.get("doctype-book"), None);
        assert_eq!(attrs.get("doctype-article"), Some(""));
        assert_eq!(attrs.get("backend-html5-doctype-article"), Some(""));
    }

    fn paragraphs(blocks: &[crate::Block], out: &mut Vec<String>) {
        for block in blocks {
            match &block.kind {
                crate::BlockKind::Paragraph { inlines } => {
                    out.push(crate::resolve::inline_text(inlines))
                }
                crate::BlockKind::Section { blocks, .. }
                | crate::BlockKind::Preamble { blocks } => paragraphs(blocks, out),
                crate::BlockKind::Table(table) => {
                    for cell in table.body.iter().flatten() {
                        if let crate::ast::CellContent::Blocks { blocks, .. } = &cell.content {
                            paragraphs(blocks, out);
                        }
                    }
                }
                _ => {}
            }
        }
    }

    fn texts(source: &str) -> Vec<String> {
        let mut out = Vec::new();
        paragraphs(&crate::parse(source).document.blocks, &mut out);
        out
    }

    #[test]
    fn parse_exposes_the_initial_profile() {
        let result = crate::parse("{backend} {doctype} {filetype} {last-update-label}\n");
        let attrs = &result.document.attributes;
        assert_eq!(attrs.get("basebackend-html-doctype-article"), Some(""));
        assert_eq!(attrs.get("attribute-missing"), Some("skip"));
        assert_eq!(attrs.get("attribute-undefined"), Some("drop-line"));
        assert_eq!(attrs.get("version-label"), Some("Version"));
        assert_eq!(attrs.get("untitled-label"), Some("Untitled"));
        assert_eq!(
            texts("{backend} {doctype} {filetype} {last-update-label}\n"),
            ["html5 article html Last updated"]
        );
    }

    #[test]
    fn header_doctype_updates_derived_attributes_body_does_not() {
        let result = crate::parse("= T\n:doctype: book\n\n:doctype: article\n\n{doctype}\n");
        let header = &result.document.attributes;
        assert_eq!(header.get("doctype-book"), Some(""));
        assert_eq!(header.get("backend-html5-doctype-book"), Some(""));
        assert_eq!(header.get("doctype-article"), None);
        let last = &result.document.final_attributes;
        assert_eq!(last.get("doctype"), Some("article"));
        assert_eq!(last.get("doctype-book"), Some(""));
        assert_eq!(last.get("doctype-article"), None);
    }

    #[test]
    fn unset_doctype_keeps_the_book_behaviour() {
        let result = crate::parse("= T\n:doctype: book\n:doctype!:\n\n= Part\n\n== Chapter\n");
        let attrs = &result.document.attributes;
        assert_eq!(attrs.get("doctype"), None);
        assert_eq!(attrs.get("doctype-book"), Some(""));
        assert!(
            !result
                .diagnostics
                .iter()
                .any(|d| d.message.contains("level 0")),
            "{:?}",
            result.diagnostics
        );
        // A later assignment cleans up after the remembered doctype.
        let result = crate::parse("= T\n:doctype: book\n:doctype!:\n:doctype: article\n");
        assert_eq!(result.document.attributes.get("doctype-book"), None);
    }

    /// Ruby's converter keeps the `htmlsyntax` of its creation: the xhtml
    /// backends and an `htmlsyntax` before the last `backend` entry make it
    /// XML; an `htmlsyntax` after it changes only the attribute.
    #[test]
    fn document_keeps_the_converter_htmlsyntax() {
        let syntax = |source: &str, host: &[(&str, &str)]| {
            let mut attributes = Attributes::new();
            for (name, value) in host {
                attributes.set(*name, *value);
            }
            let options = crate::ParseOptions {
                attributes,
                ..Default::default()
            };
            let doc = crate::parse_with_options(source, &options).document;
            (
                doc.htmlsyntax,
                doc.attributes.get("htmlsyntax").map(str::to_owned),
            )
        };
        let xml = (Some("xml"), Some("xml".to_owned()));
        assert_eq!(syntax("text", &[]), (Some("html"), Some("html".to_owned())));
        assert_eq!(syntax("text", &[("backend", "xhtml")]), xml);
        assert_eq!(syntax("text", &[("backend", "xhtml5")]), xml);
        assert_eq!(syntax("text", &[("htmlsyntax", "xml")]), xml);
        assert_eq!(syntax(":backend: xhtml\n\ntext", &[]), xml);
        assert_eq!(
            syntax(":htmlsyntax: xml\n:backend: html5\n\ntext", &[]),
            xml
        );
        assert_eq!(
            syntax(":backend: html5\n:htmlsyntax: xml\n\ntext", &[]),
            (Some("html"), Some("xml".to_owned()))
        );
        assert_eq!(syntax(":backend: docbook\n\ntext", &[]).0, None);
        // A body entry is a plain attribute assignment.
        assert_eq!(syntax("text\n\n:backend: xhtml\n", &[]).0, Some("html"));
    }

    #[test]
    fn header_backend_switches_traits() {
        let result = crate::parse(":backend: docbook\n\ntext\n");
        let attrs = &result.document.attributes;
        assert_eq!(attrs.get("backend"), Some("docbook5"));
        assert_eq!(attrs.get("backend-docbook5-doctype-article"), Some(""));
        assert_eq!(attrs.get("basebackend-docbook"), Some(""));
        assert_eq!(attrs.get("backend-html5"), None);
        assert_eq!(attrs.get("outfilesuffix"), Some(".xml"));
        assert_eq!(attrs.get("pagewidth"), Some("425"));
        // The same backend again is a no-op unless htmlsyntax changed.
        let result = crate::parse(":outfilesuffix!:\n:backend: html5\n");
        assert_eq!(result.document.attributes.get("outfilesuffix"), None);
        let result = crate::parse(":outfilesuffix!:\n:htmlsyntax: xml\n:backend: html5\n");
        let attrs = &result.document.attributes;
        assert_eq!(attrs.get("outfilesuffix"), Some(".html"));
        assert_eq!(attrs.get("htmlsyntax"), Some("xml"));
    }

    #[test]
    fn host_overrides_lock_backend_suffix_and_manpage_doctype() {
        let mut options = crate::ParseOptions::default();
        options.attributes.set("backend", "manpage");
        options.attributes.set("outfilesuffix", ".8");
        let source = "= prog(1)\n:doctype: book\n:backend: html5\n:outfilesuffix: .x\n\n== NAME\n\nprog - p\n";
        let result = crate::parse_with_options(source, &options);
        let attrs = &result.document.attributes;
        assert_eq!(attrs.get("doctype"), Some("manpage"));
        assert_eq!(attrs.get("backend"), Some("manpage"));
        assert_eq!(attrs.get("outfilesuffix"), Some(".8"));
        assert_eq!(attrs.get("filetype-man"), Some(""));
        assert!(result.document.manpage.is_some());
    }

    #[test]
    fn nested_documents_reset_the_doctype() {
        let source = "= T\n:doctype: book\n\n[cols=1a]\n|===\n\
                      |{doctype} {doctype-book} [{doctype-article}]\n\n\
                      :doctype-book: x\n\n{doctype-book}\n|===\n\n{doctype} [{doctype-book}]\n";
        assert_eq!(
            texts(source),
            ["article {doctype-book} []", "{doctype-book}", "book []"]
        );
    }
}