xrml 0.1.0

eXtensible Rust Markup Language — recursive acronym: HRML (HRML Markup Language) and TRML (TOML-like Markup Language)
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
use std::collections::BTreeMap;
use std::ops::{Add, AddAssign};

// ============================================================================
// OXML — Oxidized Markup Language
//
// A closed algebraic system for constructing valid HTML in Rust.
//
// Design invariants:
//   1. Void elements are represented as a distinct type and cannot have children.
//   2. Content elements always render with matching open/close tags.
//   3. Composition (cat) of valid nodes always yields a valid node.
//   4. All attribute maps are ordered (BTreeMap) for deterministic output.
//   5. The language is closed under all public operations.
// ============================================================================

// --- Element classification ---

/// Classification of HTML elements by their content model.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ElemKind {
    /// Void elements that must not have children or closing tags.
    Void,
    /// Elements that can contain child nodes.
    Content,
}

impl ElemKind {
    pub const fn is_void(&self) -> bool {
        matches!(self, Self::Void)
    }
}

/// Typed HTML element tag with known classification.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ElemTag {
    pub name: &'static str,
    pub kind: ElemKind,
}

impl ElemTag {
    pub const fn void(name: &'static str) -> Self {
        Self {
            name,
            kind: ElemKind::Void,
        }
    }

    pub const fn content(name: &'static str) -> Self {
        Self {
            name,
            kind: ElemKind::Content,
        }
    }

    pub const fn is_void(&self) -> bool {
        matches!(self.kind, ElemKind::Void)
    }
}

// --- Canonical tag registry ---

pub mod tags {
    use super::ElemTag;

    // Document structure
    pub const HTML: ElemTag = ElemTag::content("html");
    pub const HEAD: ElemTag = ElemTag::content("head");
    pub const BODY: ElemTag = ElemTag::content("body");
    pub const TITLE: ElemTag = ElemTag::content("title");
    pub const STYLE: ElemTag = ElemTag::content("style");
    pub const SCRIPT: ElemTag = ElemTag::content("script");

    // Sectioning
    pub const MAIN: ElemTag = ElemTag::content("main");
    pub const SECTION: ElemTag = ElemTag::content("section");
    pub const ARTICLE: ElemTag = ElemTag::content("article");
    pub const NAV: ElemTag = ElemTag::content("nav");
    pub const ASIDE: ElemTag = ElemTag::content("aside");
    pub const HEADER: ElemTag = ElemTag::content("header");
    pub const FOOTER: ElemTag = ElemTag::content("footer");

    // Grouping
    pub const DIV: ElemTag = ElemTag::content("div");
    pub const P: ElemTag = ElemTag::content("p");
    pub const PRE: ElemTag = ElemTag::content("pre");
    pub const BLOCKQUOTE: ElemTag = ElemTag::content("blockquote");
    pub const FIGURE: ElemTag = ElemTag::content("figure");
    pub const FIGCAPTION: ElemTag = ElemTag::content("figcaption");
    pub const UL: ElemTag = ElemTag::content("ul");
    pub const OL: ElemTag = ElemTag::content("ol");
    pub const LI: ElemTag = ElemTag::content("li");
    pub const DL: ElemTag = ElemTag::content("dl");
    pub const DT: ElemTag = ElemTag::content("dt");
    pub const DD: ElemTag = ElemTag::content("dd");

    // Text
    pub const H1: ElemTag = ElemTag::content("h1");
    pub const H2: ElemTag = ElemTag::content("h2");
    pub const H3: ElemTag = ElemTag::content("h3");
    pub const H4: ElemTag = ElemTag::content("h4");
    pub const H5: ElemTag = ElemTag::content("h5");
    pub const H6: ElemTag = ElemTag::content("h6");
    pub const SPAN: ElemTag = ElemTag::content("span");
    pub const A: ElemTag = ElemTag::content("a");
    pub const STRONG: ElemTag = ElemTag::content("strong");
    pub const EM: ElemTag = ElemTag::content("em");
    pub const CODE: ElemTag = ElemTag::content("code");
    pub const BR: ElemTag = ElemTag::void("br");
    pub const HR: ElemTag = ElemTag::void("hr");

    // Inline
    pub const IMG: ElemTag = ElemTag::void("img");
    pub const IFRAME: ElemTag = ElemTag::content("iframe");
    pub const CANVAS: ElemTag = ElemTag::content("canvas");

    // Form
    pub const FORM: ElemTag = ElemTag::content("form");
    pub const INPUT: ElemTag = ElemTag::void("input");
    pub const BUTTON: ElemTag = ElemTag::content("button");
    pub const TEXTAREA: ElemTag = ElemTag::content("textarea");
    pub const SELECT: ElemTag = ElemTag::content("select");
    pub const OPTION: ElemTag = ElemTag::content("option");
    pub const LABEL: ElemTag = ElemTag::content("label");

    // Table
    pub const TABLE: ElemTag = ElemTag::content("table");
    pub const THEAD: ElemTag = ElemTag::content("thead");
    pub const TBODY: ElemTag = ElemTag::content("tbody");
    pub const TFOOT: ElemTag = ElemTag::content("tfoot");
    pub const TR: ElemTag = ElemTag::content("tr");
    pub const TH: ElemTag = ElemTag::content("th");
    pub const TD: ElemTag = ElemTag::content("td");

    // Head / metadata (void)
    pub const META: ElemTag = ElemTag::void("meta");
    pub const LINK: ElemTag = ElemTag::void("link");
    pub const BASE: ElemTag = ElemTag::void("base");
}

// --- Core node algebra ---

/// An OXML node. The type system guarantees that:
///   - Void elements never carry children.
///   - Content elements always render with matching open/close tags.
///   - Composition of valid nodes is always valid.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ONode {
    /// Identity element for cat. Renders to empty string.
    Empty,
    /// Escaped text node.
    Text(String),
    /// Raw HTML fragment (use with care).
    Raw(String),
    /// A typed content element with children.
    Content(OContent),
    /// A typed void element (no children possible).
    Void(OVoid),
    /// Ordered sequence of nodes.
    Cat(Vec<ONode>),
}

/// A content element: tag + attrs + children.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OContent {
    pub tag: ElemTag,
    pub attrs: BTreeMap<String, String>,
    pub children: Vec<ONode>,
}

/// A void element: tag + attrs. No children by construction.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OVoid {
    pub tag: ElemTag,
    pub attrs: BTreeMap<String, String>,
}

// --- Builders ---

/// Builder for content elements. Consumed on `.build()`.
pub struct ContentBuilder {
    tag: ElemTag,
    attrs: BTreeMap<String, String>,
    children: Vec<ONode>,
}

/// Builder for void elements. Consumed on `.build()`.
pub struct VoidBuilder {
    tag: ElemTag,
    attrs: BTreeMap<String, String>,
}

impl ContentBuilder {
    pub fn new(tag: ElemTag) -> Self {
        debug_assert!(!tag.is_void(), "ContentBuilder requires a non-void tag");
        Self {
            tag,
            attrs: BTreeMap::new(),
            children: Vec::new(),
        }
    }

    pub fn attr(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.attrs.insert(key.into(), value.into());
        self
    }

    pub fn attr_if(self, cond: bool, key: impl Into<String>, value: impl Into<String>) -> Self {
        if cond {
            self.attr(key, value)
        } else {
            self
        }
    }

    pub fn class(self, value: impl Into<String>) -> Self {
        self.attr("class", value)
    }

    pub fn id(self, value: impl Into<String>) -> Self {
        self.attr("id", value)
    }

    pub fn data(self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.attr(format!("data-{}", key.into()), value)
    }

    pub fn child(mut self, node: ONode) -> Self {
        self.children.push(node);
        self
    }

    pub fn text(self, value: impl Into<String>) -> Self {
        self.child(ONode::text(value))
    }

    pub fn raw(self, value: impl Into<String>) -> Self {
        self.child(ONode::raw(value))
    }

    pub fn children(mut self, nodes: Vec<ONode>) -> Self {
        self.children.extend(nodes);
        self
    }

    pub fn build(self) -> ONode {
        ONode::Content(OContent {
            tag: self.tag,
            attrs: self.attrs,
            children: self.children,
        })
    }
}

impl VoidBuilder {
    pub fn new(tag: ElemTag) -> Self {
        debug_assert!(tag.is_void(), "VoidBuilder requires a void tag");
        Self {
            tag,
            attrs: BTreeMap::new(),
        }
    }

    pub fn attr(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.attrs.insert(key.into(), value.into());
        self
    }

    pub fn attr_if(self, cond: bool, key: impl Into<String>, value: impl Into<String>) -> Self {
        if cond {
            self.attr(key, value)
        } else {
            self
        }
    }

    pub fn build(self) -> ONode {
        ONode::Void(OVoid {
            tag: self.tag,
            attrs: self.attrs,
        })
    }
}

// --- ONode constructors and algebra ---

impl ONode {
    pub fn empty() -> Self {
        Self::Empty
    }

    pub fn text(value: impl Into<String>) -> Self {
        Self::Text(value.into())
    }

    pub fn raw(value: impl Into<String>) -> Self {
        Self::Raw(value.into())
    }

    pub fn cat(nodes: Vec<ONode>) -> Self {
        Self::Cat(flatten_vec(nodes))
    }

    pub fn content(tag: ElemTag) -> ContentBuilder {
        ContentBuilder::new(tag)
    }

    pub fn void(tag: ElemTag) -> VoidBuilder {
        VoidBuilder::new(tag)
    }

    /// Cat (concatenation): append two nodes into a flat sequence.
    /// This is the monoidal append for ONode.
    pub fn cat2(self, rhs: ONode) -> Self {
        match (self, rhs) {
            (ONode::Empty, b) => b,
            (a, ONode::Empty) => a,
            (ONode::Cat(mut a), ONode::Cat(b)) => {
                a.extend(b);
                ONode::Cat(a)
            }
            (ONode::Cat(mut a), b) => {
                a.push(b);
                ONode::Cat(a)
            }
            (a, ONode::Cat(mut b)) => {
                let mut out = Vec::with_capacity(1 + b.len());
                out.push(a);
                out.append(&mut b);
                ONode::Cat(out)
            }
            (a, b) => ONode::Cat(vec![a, b]),
        }
    }

    /// Wrap this node inside a new content element.
    pub fn wrap(self, tag: ElemTag) -> ONode {
        ONode::content(tag).child(self).build()
    }

    /// Insert a node at a given index in a Cat sequence.
    pub fn insert_at(self, index: usize, node: ONode) -> ONode {
        match self {
            ONode::Cat(mut nodes) => {
                let idx = index.min(nodes.len());
                nodes.insert(idx, node);
                ONode::Cat(nodes)
            }
            other => {
                if index == 0 {
                    ONode::Cat(vec![node, other])
                } else {
                    ONode::Cat(vec![other, node])
                }
            }
        }
    }

    /// Map over all text nodes in the tree.
    pub fn map_text(self, f: impl Fn(&str) -> String + Copy) -> ONode {
        match self {
            ONode::Text(t) => ONode::Text(f(&t)),
            ONode::Raw(t) => ONode::Raw(t),
            ONode::Empty => ONode::Empty,
            ONode::Content(c) => ONode::Content(OContent {
                tag: c.tag,
                attrs: c.attrs,
                children: c.children.into_iter().map(|n| n.map_text(f)).collect(),
            }),
            ONode::Void(v) => ONode::Void(v),
            ONode::Cat(nodes) => ONode::Cat(nodes.into_iter().map(|n| n.map_text(f)).collect()),
        }
    }

    /// Filter out Empty nodes from Cat sequences.
    pub fn compact(self) -> ONode {
        match self {
            ONode::Cat(nodes) => {
                let compacted: Vec<ONode> = nodes
                    .into_iter()
                    .map(|n| n.compact())
                    .filter(|n| !matches!(n, ONode::Empty))
                    .collect();
                match compacted.len() {
                    0 => ONode::Empty,
                    1 => compacted.into_iter().next().unwrap(),
                    _ => ONode::Cat(compacted),
                }
            }
            ONode::Content(c) => ONode::Content(OContent {
                tag: c.tag,
                attrs: c.attrs,
                children: c.children.into_iter().map(|n| n.compact()).collect(),
            }),
            other => other,
        }
    }

    /// Render to an HTML string.
    pub fn render(&self) -> String {
        match self {
            ONode::Empty => String::new(),
            ONode::Text(t) => escape_html(t),
            ONode::Raw(r) => r.clone(),
            ONode::Content(c) => c.render(),
            ONode::Void(v) => v.render(),
            ONode::Cat(nodes) => nodes
                .iter()
                .map(|n| n.render())
                .collect::<Vec<_>>()
                .join(""),
        }
    }

    /// Render with indentation for debugging.
    pub fn render_pretty(&self) -> String {
        self.render_indent(0)
    }

    fn render_indent(&self, depth: usize) -> String {
        let pad = "  ".repeat(depth);
        match self {
            ONode::Empty => String::new(),
            ONode::Text(t) => format!("{}{}", pad, escape_html(t)),
            ONode::Raw(r) => format!("{}{}", pad, r),
            ONode::Content(c) => c.render_indent(depth),
            ONode::Void(v) => format!("{}{}", pad, v.render()),
            ONode::Cat(nodes) => nodes
                .iter()
                .map(|n| n.render_indent(depth))
                .filter(|s| !s.is_empty())
                .collect::<Vec<_>>()
                .join("\n"),
        }
    }
}

// --- Monoid instance ---

impl Add for ONode {
    type Output = ONode;
    fn add(self, rhs: ONode) -> Self::Output {
        self.cat2(rhs)
    }
}

impl AddAssign for ONode {
    fn add_assign(&mut self, rhs: ONode) {
        let lhs = std::mem::replace(self, ONode::Empty);
        *self = lhs.cat2(rhs);
    }
}

/// Flatten a Vec<ONode>, collapsing nested Cat nodes.
fn flatten_vec(nodes: Vec<ONode>) -> Vec<ONode> {
    let mut out = Vec::with_capacity(nodes.len());
    for node in nodes {
        match node {
            ONode::Cat(inner) => out.extend(flatten_vec(inner)),
            other => out.push(other),
        }
    }
    out
}

// --- Element rendering ---

impl OContent {
    pub fn render(&self) -> String {
        let attrs = render_attrs(&self.attrs);
        let children = self
            .children
            .iter()
            .map(|n| n.render())
            .collect::<Vec<_>>()
            .join("");
        format!(
            "<{}{}>{}</{}>",
            self.tag.name, attrs, children, self.tag.name
        )
    }

    fn render_indent(&self, depth: usize) -> String {
        let pad = "  ".repeat(depth);
        let attrs = render_attrs(&self.attrs);
        if self.children.is_empty() {
            return format!("{}<{}{}></{}>", pad, self.tag.name, attrs, self.tag.name);
        }
        let inner = self
            .children
            .iter()
            .map(|n| n.render_indent(depth + 1))
            .collect::<Vec<_>>()
            .join("\n");
        format!(
            "{}<{}{}>\n{}\n{}</{}>",
            pad, self.tag.name, attrs, inner, pad, self.tag.name
        )
    }
}

impl OVoid {
    pub fn render(&self) -> String {
        let attrs = render_attrs(&self.attrs);
        format!("<{}{}>", self.tag.name, attrs)
    }
}

fn render_attrs(attrs: &BTreeMap<String, String>) -> String {
    attrs
        .iter()
        .map(|(k, v)| format!(" {}=\"{}\"", k, escape_attr(v)))
        .collect::<String>()
}

pub fn escape_html(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#39;")
}

pub fn escape_attr(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('"', "&quot;")
        .replace('\'', "&#39;")
}

// --- Typed convenience constructors ---

pub fn post_button(text: &str, url: &str, target: &str) -> String {
    button()
        .attr("class", "btn btn-primary")
        .attr("data-post", url)
        .attr("data-target", target)
        .attr("data-swap", "innerHTML")
        .text(text)
        .build()
        .render()
}

pub fn get_link(text: &str, url: &str, target: &str) -> String {
    a().attr("href", "#")
        .attr("data-get", url)
        .attr("data-target", target)
        .attr("data-swap", "innerHTML")
        .text(text)
        .build()
        .render()
}

pub fn list(items: Vec<String>) -> String {
    ul().children(
        items
            .into_iter()
            .map(|item| li().child(ONode::text(item)).build())
            .collect(),
    )
    .build()
    .render()
}

/// Build a full HTML document from head and body nodes.
pub fn doc(head: ONode, body: ONode) -> String {
    let html = ONode::content(tags::HTML)
        .attr("lang", "en")
        .children(vec![
            ONode::content(tags::HEAD).child(head).build(),
            ONode::content(tags::BODY).child(body).build(),
        ])
        .build()
        .render();
    format!("<!DOCTYPE html>{}", html)
}

// --- Common element shorthands ---

pub fn div() -> ContentBuilder {
    ContentBuilder::new(tags::DIV)
}
pub fn span() -> ContentBuilder {
    ContentBuilder::new(tags::SPAN)
}
pub fn p() -> ContentBuilder {
    ContentBuilder::new(tags::P)
}
pub fn h1() -> ContentBuilder {
    ContentBuilder::new(tags::H1)
}
pub fn h2() -> ContentBuilder {
    ContentBuilder::new(tags::H2)
}
pub fn h3() -> ContentBuilder {
    ContentBuilder::new(tags::H3)
}
pub fn h4() -> ContentBuilder {
    ContentBuilder::new(tags::H4)
}
pub fn h5() -> ContentBuilder {
    ContentBuilder::new(tags::H5)
}
pub fn h6() -> ContentBuilder {
    ContentBuilder::new(tags::H6)
}
pub fn section() -> ContentBuilder {
    ContentBuilder::new(tags::SECTION)
}
pub fn article() -> ContentBuilder {
    ContentBuilder::new(tags::ARTICLE)
}
pub fn nav() -> ContentBuilder {
    ContentBuilder::new(tags::NAV)
}
pub fn header() -> ContentBuilder {
    ContentBuilder::new(tags::HEADER)
}
pub fn footer() -> ContentBuilder {
    ContentBuilder::new(tags::FOOTER)
}
pub fn main() -> ContentBuilder {
    ContentBuilder::new(tags::MAIN)
}
pub fn a() -> ContentBuilder {
    ContentBuilder::new(tags::A)
}
pub fn button() -> ContentBuilder {
    ContentBuilder::new(tags::BUTTON)
}
pub fn form() -> ContentBuilder {
    ContentBuilder::new(tags::FORM)
}
pub fn ul() -> ContentBuilder {
    ContentBuilder::new(tags::UL)
}
pub fn ol() -> ContentBuilder {
    ContentBuilder::new(tags::OL)
}
pub fn li() -> ContentBuilder {
    ContentBuilder::new(tags::LI)
}
pub fn table() -> ContentBuilder {
    ContentBuilder::new(tags::TABLE)
}
pub fn tr() -> ContentBuilder {
    ContentBuilder::new(tags::TR)
}
pub fn th() -> ContentBuilder {
    ContentBuilder::new(tags::TH)
}
pub fn td() -> ContentBuilder {
    ContentBuilder::new(tags::TD)
}
pub fn title() -> ContentBuilder {
    ContentBuilder::new(tags::TITLE)
}
pub fn style() -> ContentBuilder {
    ContentBuilder::new(tags::STYLE)
}
pub fn script() -> ContentBuilder {
    ContentBuilder::new(tags::SCRIPT)
}
pub fn meta() -> VoidBuilder {
    VoidBuilder::new(tags::META)
}
pub fn link() -> VoidBuilder {
    VoidBuilder::new(tags::LINK)
}
pub fn img() -> VoidBuilder {
    VoidBuilder::new(tags::IMG)
}
pub fn br() -> VoidBuilder {
    VoidBuilder::new(tags::BR)
}
pub fn hr() -> VoidBuilder {
    VoidBuilder::new(tags::HR)
}
pub fn input() -> VoidBuilder {
    VoidBuilder::new(tags::INPUT)
}