umd 0.2.0

Universal Markdown - A post-Markdown superset with semantic HTML and extensible syntax
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
//! Position/alignment notation shared across paragraphs, tables, and block
//! plugins: `START`/`CENTER`/`END`/`JUSTIFY` (inline-direction) and
//! `V-START`/`V-CENTER`/`V-END`/`BASELINE` (block-direction).
//!
//! These are logical-direction names (matching `scss/utilities/text.scss`),
//! not physical `LEFT`/`RIGHT`/`TOP`/`BOTTOM` — under `dir="rtl"` or vertical
//! writing modes, "start" and "end" (or "block-start"/"block-end") flip
//! automatically, whereas a hardcoded `LEFT`/`TOP` would not. See PLAN.md's
//! "CSS 論理プロパティ方針".
//!
//! This module covers alignment wherever it appears:
//! - Paragraph-level prefixes, composed together with `COLOR()`/`SIZE()`/
//!   `TRUNCATE:` by `block_decoration::apply_block_decorations_with_options`
//!   (this module only supplies the alignment slice of that prefix chain).
//! - `apply_block_placement`: the `START:`/`CENTER:`/`END:`/`JUSTIFY:`
//!   wrapper placed on its own line before a table, block plugin, or media
//!   element.
//! - GFM table cells (`process_table_cell_alignment`), via `V-START:` etc.
//!   prefixes inside `<td>`/`<th>` content.
//! - UMD table cells (`strip_umd_table_alignment_prefix`), called from
//!   `table::umd::decorations`.

use once_cell::sync::Lazy;
use regex::{Captures, Regex};

/// Inline-direction alignment keyword → `umd-*` text-align class. The single
/// source of truth for this mapping — `ALIGN_EXTRACT`, `map_text_align`, and
/// (chained with `VERTICAL_ALIGN`) `CELL_ALIGNMENT_PREFIXES` all derive from
/// this table rather than each hardcoding the keyword list separately.
///
/// Order matters here only insofar as it fixes `CELL_ALIGNMENT_PREFIXES`'s
/// derived order (see that constant's docs) — kept as END/CENTER/START/
/// JUSTIFY to match the original hand-written table cell prefix list.
pub(crate) const TEXT_ALIGN: &[(&str, &str)] = &[
    ("END", "umd-end"),
    ("CENTER", "umd-center"),
    ("START", "umd-start"),
    ("JUSTIFY", "umd-justify"),
];

/// Block-direction alignment keyword → `umd-v-*` class. There's no CSS
/// logical equivalent of `vertical-align` — `V-` is UMD's own naming
/// convention. Single source of truth for this mapping, same rationale as
/// `TEXT_ALIGN` above.
pub(crate) const VERTICAL_ALIGN: &[(&str, &str)] = &[
    ("V-START", "umd-v-start"),
    ("V-CENTER", "umd-v-center"),
    ("V-END", "umd-v-end"),
    ("BASELINE", "umd-v-baseline"),
];

fn align_keywords_pattern(table: &[(&str, &str)]) -> String {
    let keywords: Vec<&str> = table.iter().map(|(keyword, _)| *keyword).collect();
    format!("({}):", keywords.join("|"))
}

pub(crate) static ALIGN_EXTRACT: Lazy<Regex> =
    Lazy::new(|| Regex::new(&align_keywords_pattern(TEXT_ALIGN)).unwrap());
pub(crate) static VALIGN_EXTRACT: Lazy<Regex> =
    Lazy::new(|| Regex::new(&align_keywords_pattern(VERTICAL_ALIGN)).unwrap());

// Block placement pattern for tables and plugins (must start on new line).
// Placement classes (`umd-block-*`) are a distinct namespace from the
// text-align classes in `TEXT_ALIGN` above (block positioning vs. inline
// text-align), so this keeps its own literal keyword list rather than
// reusing that table.
static BLOCK_PLACEMENT: Lazy<Regex> = Lazy::new(|| {
    Regex::new(
        r"(?m)^(START|CENTER|END|JUSTIFY):\n((?:\|[^\n]*\|(?:\n|$))+|@\w+(?:\([^)]*\))?\{[^}]*\})",
    )
    .unwrap()
});

fn lookup_align_class(table: &[(&str, &str)], value: &str, default: &str) -> String {
    let upper = value.to_uppercase();
    table
        .iter()
        .find(|(keyword, _)| *keyword == upper)
        .map(|(_, class)| class.to_string())
        .unwrap_or_else(|| default.to_string())
}

/// Map inline-direction alignment (`START`/`CENTER`/`END`/`JUSTIFY`) to a
/// `umd-*` text-align class
pub(crate) fn map_text_align(value: &str) -> String {
    lookup_align_class(TEXT_ALIGN, value, "umd-start")
}

/// Map block-direction alignment (`V-START`/`V-CENTER`/`V-END`/`BASELINE`)
/// to a `umd-v-*` class.
pub(crate) fn map_vertical_align(value: &str) -> String {
    lookup_align_class(VERTICAL_ALIGN, value, "umd-v-baseline")
}

/// UMD table cell alignment prefixes, in match order: `VERTICAL_ALIGN` then
/// `TEXT_ALIGN`, colon-suffixed. Used by
/// `table::umd::decorations::parse_cell_content`, which scans this list
/// once against the (progressively stripped) cell content — a later-in-list
/// prefix can be caught only after an earlier one strips, so the order here
/// (not just the keyword set) is significant and must stay
/// V-START/V-CENTER/V-END/BASELINE/END/CENTER/START/JUSTIFY.
pub(crate) fn cell_alignment_prefixes() -> impl Iterator<Item = (&'static str, &'static str)> {
    VERTICAL_ALIGN.iter().chain(TEXT_ALIGN.iter()).copied()
}

/// Merge extra classes into a tag's existing `class="..."` attribute (or add
/// one if absent), de-duplicating against classes already present. Shared by
/// `apply_block_placement` and `apply_pending_code_block_placement` — both
/// attach placement classes to an already-rendered element rather than
/// wrapping it in a new one.
pub(crate) fn merge_class_into_tag(tag_html: &str, extra_classes: &str) -> String {
    let class_re = Regex::new(r#"class=\"([^\"]*)\""#).unwrap();

    if let Some(caps) = class_re.captures(tag_html) {
        let existing = caps.get(1).map_or("", |m| m.as_str());
        let mut merged: Vec<String> = if existing.trim().is_empty() {
            Vec::new()
        } else {
            existing.split_whitespace().map(|s| s.to_string()).collect()
        };

        for class_name in extra_classes.split_whitespace() {
            if !merged.iter().any(|c| c == class_name) {
                merged.push(class_name.to_string());
            }
        }

        class_re
            .replace(tag_html, format!(r#"class="{}""#, merged.join(" ")))
            .to_string()
    } else {
        tag_html.replacen('>', &format!(r#" class="{}">"#, extra_classes), 1)
    }
}

/// Placement class for an element that is naturally full-width by default
/// (tables, code block figures) — START/CENTER/END need an explicit
/// shrink-to-content (`umd-block-auto`) before a margin utility can align it,
/// unlike media elements which shrink to their intrinsic size on their own.
fn placement_class_for_block(placement: &str) -> &'static str {
    match placement {
        "START" => "umd-block-auto",
        "CENTER" => "umd-block-center",
        "END" => "umd-block-auto umd-block-end",
        "JUSTIFY" => "umd-block-justify",
        _ => "",
    }
}

// A fenced code block interrupts the paragraph before it (unlike UMD's raw
// pipe-table/plugin syntax, which comrak treats as plain paragraph text), so
// by the time this module runs, code is still its `<!--CODE_BLOCK_n-->`
// placeholder (see `fence::protect`) sitting right after a sibling `<p>`, not
// nested inside it. This tags the placeholder with the requested placement so
// `apply_pending_code_block_placement` can apply it once the placeholder has
// become a real `<figure class="umd-code-block">` at the end of the pipeline.
static CODE_BLOCK_PLACEMENT: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"(?s)<p>\s*(START|CENTER|END|JUSTIFY):\s*</p>\s*(<!--CODE_BLOCK_\d+-->)"#).unwrap()
});

/// Merge a placement recorded by `CODE_BLOCK_PLACEMENT` into the
/// `<figure class="umd-code-block">` that the placeholder it wraps has since
/// become. Must run after `fence::code_block::process_code_blocks`.
pub(crate) fn apply_pending_code_block_placement(html: &str) -> String {
    static PENDING: Lazy<Regex> = Lazy::new(|| {
        Regex::new(
            r#"(?s)<div data-umd-code-placement="(START|CENTER|END|JUSTIFY)">\s*(<figure\b[^>]*\bclass="[^"]*\bumd-code-block\b[^"]*"[^>]*>[\s\S]*?</figure>)\s*</div>"#,
        )
        .unwrap()
    });

    PENDING
        .replace_all(html, |caps: &Captures| {
            let placement = &caps[1];
            let figure = &caps[2];
            merge_class_into_tag(figure, placement_class_for_block(placement))
        })
        .to_string()
}

/// Apply block placement prefixes to tables, block plugins, media, and code
/// blocks.
///
/// Handles START:/CENTER:/END:/JUSTIFY: prefixes followed by newline
/// for UMD tables and block plugins (@function), and (via
/// `CODE_BLOCK_PLACEMENT`/`apply_pending_code_block_placement`) preceding a
/// fenced code block. Logical-direction names (matching
/// block_decoration.rs's paragraph alignment and scss/utilities/text.scss),
/// not physical LEFT:/RIGHT:.
///
/// # Arguments
///
/// * `html` - The HTML content to process
///
/// # Returns
///
/// HTML with block placement applied (`umd-*` reference-CSS classes)
pub fn apply_block_placement(html: &str) -> String {
    let with_code_block_placement = CODE_BLOCK_PLACEMENT
        .replace_all(html, |caps: &Captures| {
            format!(
                r#"<div data-umd-code-placement="{}">{}</div>"#,
                &caps[1], &caps[2]
            )
        })
        .to_string();

    let media_block_placement = Regex::new(
        r#"(?s)<p>\s*(START|CENTER|END|JUSTIFY):\s*\n\s*(<picture[\s\S]*?</picture>|<video[\s\S]*?</video>|<audio[\s\S]*?</audio>|<a href="[^"]+" download class="download-link[^"]*"[^>]*>[\s\S]*?</a>)\s*</p>"#,
    )
    .unwrap();

    let with_media_placement = media_block_placement
        .replace_all(&with_code_block_placement, |caps: &regex::Captures| {
            let placement = &caps[1];
            let media = &caps[2];

            let wrapper_class = match placement {
                "START" => "umd-block-start",
                "CENTER" => "umd-inline-center",
                "END" => "umd-block-end",
                "JUSTIFY" => "umd-block-justify",
                _ => "",
            };

            if wrapper_class.is_empty() {
                format!("<figure>\n{}\n</figure>", media)
            } else {
                format!("<figure class=\"{}\">\n{}\n</figure>", wrapper_class, media)
            }
        })
        .to_string();

    let table_and_plugin_placement_in_paragraph = Regex::new(
        r#"(?s)<p>\s*(START|CENTER|END|JUSTIFY):\s*\n\s*(<(?:table|template)\b[^>]*>[\s\S]*?</(?:table|template)>)\s*</p>"#,
    )
    .unwrap();

    let with_table_and_plugin_placement_in_paragraph = table_and_plugin_placement_in_paragraph
        .replace_all(&with_media_placement, |caps: &regex::Captures| {
            let placement = &caps[1];
            let block = &caps[2];
            let placement_class = placement_class_for_block(placement);

            if block.starts_with("<table") {
                return merge_class_into_tag(block, placement_class);
            }

            if block.starts_with("<template") && block.contains("umd-plugin") {
                return merge_class_into_tag(block, placement_class);
            }

            block.to_string()
        })
        .to_string();

    let table_and_plugin_placement = Regex::new(
        r#"(?s)<p>\s*(START|CENTER|END|JUSTIFY):\s*</p>\s*(<(?:table|template)\b[^>]*>[\s\S]*?</(?:table|template)>)"#,
    )
    .unwrap();

    let with_table_and_plugin_placement = table_and_plugin_placement
        .replace_all(
            &with_table_and_plugin_placement_in_paragraph,
            |caps: &regex::Captures| {
                let placement = &caps[1];
                let block = &caps[2];
                let placement_class = placement_class_for_block(placement);

                if block.starts_with("<table") {
                    return merge_class_into_tag(block, placement_class);
                }

                if block.starts_with("<template") && block.contains("umd-plugin") {
                    return merge_class_into_tag(block, placement_class);
                }

                block.to_string()
            },
        )
        .to_string();

    BLOCK_PLACEMENT
        .replace_all(
            &with_table_and_plugin_placement,
            |caps: &regex::Captures| {
                let placement = &caps[1];
                let content = &caps[2];

                let wrapper_class = placement_class_for_block(placement);

                // Wrap table or plugin in div with appropriate class
                if content.starts_with('|') {
                    // UMD table
                    format!("<div class=\"{}\">\n{}</div>", wrapper_class, content)
                } else if content.starts_with('@') {
                    // Block plugin
                    format!("<div class=\"{}\">\n{}</div>", wrapper_class, content)
                } else {
                    content.to_string()
                }
            },
        )
        .to_string()
}

/// Process table cell vertical alignment prefixes (V-START:, V-CENTER:, V-END:, BASELINE:)
///
/// Detects alignment prefixes in table cells and adds `umd-v-*` alignment
/// classes (logical-direction names, matching block_decoration.rs and
/// scss/utilities/text.scss — not physical TOP:/BOTTOM:).
/// Note: GFM tables are handled by comrak without extensions.
/// UMD tables have their own cell spanning and decoration support.
pub(crate) fn process_table_cell_alignment(html: &str) -> String {
    let mut result = html.to_string();

    // Process <td> tags
    let td_pattern = Regex::new(r"<td([^>]*)>(.*?)</td>").unwrap();
    result = td_pattern
        .replace_all(&result, |caps: &Captures| {
            let existing_attrs = &caps[1];
            let content = &caps[2];
            process_cell_content("td", existing_attrs, content)
        })
        .to_string();

    // Process <th> tags
    let th_pattern = Regex::new(r"<th([^>]*)>(.*?)</th>").unwrap();
    result = th_pattern
        .replace_all(&result, |caps: &Captures| {
            let existing_attrs = &caps[1];
            let content = &caps[2];
            process_cell_content("th", existing_attrs, content)
        })
        .to_string();

    result
}

/// Process individual cell content for alignment
fn process_cell_content(tag: &str, existing_attrs: &str, content: &str) -> String {
    // Check for a vertical alignment prefix (GFM cells only support
    // vertical alignment prefixes — horizontal alignment for GFM tables
    // comes from the `|:---:|` delimiter-row syntax instead).
    let trimmed = content.trim_start();
    let matched = VERTICAL_ALIGN.iter().find_map(|(keyword, class)| {
        trimmed
            .strip_prefix(keyword)
            .and_then(|rest| rest.strip_prefix(':'))
            .map(|rest| (*class, rest.trim_start()))
    });

    match matched {
        None => format!("<{}{}>{}</{}>", tag, existing_attrs, content, tag),
        Some((align_class, remaining_content)) => {
            if existing_attrs.contains("class=") {
                // Append to existing class attribute
                let new_attrs =
                    existing_attrs.replace("class=\"", &format!("class=\"{} ", align_class));
                format!("<{}{}>{}</{}>", tag, new_attrs, remaining_content, tag)
            } else {
                // Add new class attribute
                format!(
                    "<{} class=\"{}\"{}>{}</{}>",
                    tag, align_class, existing_attrs, remaining_content, tag
                )
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::extensions::block_decoration::apply_block_decorations;

    #[test]
    fn test_text_align() {
        let input = "CENTER: Centered text";
        let output = apply_block_decorations(input);
        assert!(output.contains("class=\"umd-center\""));
    }

    #[test]
    fn test_text_align_start() {
        let input = "START: Start-aligned text";
        let output = apply_block_decorations(input);
        assert!(output.contains("class=\"umd-start\""));
    }

    #[test]
    fn test_text_align_end() {
        let input = "END: End-aligned text";
        let output = apply_block_decorations(input);
        assert!(output.contains("class=\"umd-end\""));
    }

    #[test]
    fn test_vertical_align() {
        let input = "V-START: Top aligned";
        let output = apply_block_decorations(input);
        assert!(output.contains("class=\"umd-v-start\""));
    }

    #[test]
    fn test_vertical_align_center_and_end() {
        assert!(
            apply_block_decorations("V-CENTER: Middle aligned").contains("class=\"umd-v-center\"")
        );
        assert!(apply_block_decorations("V-END: Bottom aligned").contains("class=\"umd-v-end\""));
    }

    #[test]
    fn test_vertical_align_baseline() {
        let input = "BASELINE: Baseline aligned";
        let output = apply_block_decorations(input);
        assert!(output.contains("class=\"umd-v-baseline\""));
    }

    #[test]
    fn test_block_placement_start() {
        let input = "START:\n|Header|\n|Cell|";
        let output = apply_block_placement(input);
        assert!(output.contains(r#"<div class="umd-block-auto">"#));
        assert!(output.contains("|Header|"));
    }

    #[test]
    fn test_block_placement_center() {
        let input = "CENTER:\n|Header|\n|Cell|";
        let output = apply_block_placement(input);
        assert!(output.contains(r#"<div class="umd-block-center">"#));
    }

    #[test]
    fn test_block_placement_end() {
        let input = "END:\n|Header|\n|Cell|";
        let output = apply_block_placement(input);
        assert!(output.contains(r#"<div class="umd-block-auto umd-block-end">"#));
    }

    #[test]
    fn test_block_placement_justify() {
        let input = "JUSTIFY:\n|Header|\n|Cell|";
        let output = apply_block_placement(input);
        assert!(output.contains(r#"<div class="umd-block-justify">"#));
    }

    #[test]
    fn test_block_placement_plugin() {
        let input = "CENTER:\n@youtube{video_id}";
        let output = apply_block_placement(input);
        assert!(output.contains(r#"<div class="umd-block-center">"#));
        assert!(output.contains("@youtube"));
    }

    #[test]
    fn test_block_placement_end_media() {
        let input = r#"<p>END:
<picture>
  <img src="image.png" alt="alt" title="Title" />
</picture></p>"#;
        let output = apply_block_placement(input);
        assert!(output.contains(r#"<figure class="umd-block-end">"#));
        assert!(output.contains("<picture>"));
        assert!(!output.contains("END:"));
    }

    #[test]
    fn test_table_cell_vertical_alignment() {
        let input = r#"<table class="table"><tr><td>V-START: Cell1</td><td>V-CENTER: Cell2</td></tr></table>"#;
        let output = process_table_cell_alignment(input);
        assert!(output.contains(r#"class="umd-v-start""#));
        assert!(output.contains("Cell1"));
        assert!(output.contains(r#"class="umd-v-center""#));
        assert!(output.contains("Cell2"));
    }

    #[test]
    fn test_table_cell_multiple_alignments() {
        let input = r#"<table><tr><th>BASELINE: Header</th><td>V-END: Data</td></tr></table>"#;
        let output = process_table_cell_alignment(input);
        assert!(output.contains(r#"class="umd-v-baseline""#));
        assert!(output.contains(r#"class="umd-v-end""#));
    }

    #[test]
    fn test_cell_alignment_prefixes_cover_all_logical_directions() {
        let classes: Vec<&str> = cell_alignment_prefixes().map(|(_, c)| c).collect();
        assert!(classes.contains(&"umd-center"));
        assert!(classes.contains(&"umd-v-start"));
        assert!(classes.contains(&"umd-v-baseline"));
    }
}