umd 0.1.0

Universal Markdown - A post-Markdown superset with Bootstrap 5 integration 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
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
//! Plugin system for Universal Markdown
//!
//! Provides plugin syntax support:
//! - Inline plugins: &function(args){content};
//! - Block plugins (multiline): @function(args){{ content }}
//! - Block plugins (single line): @function(args){content}
//!
//! Note: This only parses plugin syntax and outputs <template> with <data> elements.
//! Actual plugin execution is handled by backend (Nuxt/Laravel) or frontend.
//! Content within plugins may contain nested plugins or other Wiki syntax.

use once_cell::sync::Lazy;
use regex::Regex;

/// Escape HTML special characters
///
/// # Arguments
///
/// * `input` - Text to escape
///
/// # Returns
///
/// HTML-escaped string
fn escape_html_text(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

/// Parse comma-separated args into a vector
///
/// # Arguments
///
/// * `args` - Comma-separated argument string
///
/// # Returns
///
/// Vector of trimmed argument strings
fn parse_args(args: &str) -> Vec<String> {
    if args.trim().is_empty() {
        return vec![];
    }
    args.split(',').map(|s| s.trim().to_string()).collect()
}

/// Render args as <data> elements
///
/// # Arguments
///
/// * `args` - Comma-separated argument string
///
/// # Returns
///
/// HTML string with <data value="index">arg</data> elements
fn render_args_as_data(args: &str) -> String {
    parse_args(args)
        .iter()
        .enumerate()
        .map(|(i, arg)| format!("<data value=\"{}\">{}</data>", i, escape_html_text(arg)))
        .collect::<Vec<_>>()
        .join("")
}

// Standard plugins that output direct HTML instead of <template>
// @detail plugin for <details> element
static CLEAR_PLUGIN: Lazy<Regex> = Lazy::new(|| Regex::new(r"@clear\(\)").unwrap());

static DETAIL_PLUGIN: Lazy<Regex> = Lazy::new(|| {
    // Match @detail(summary) or @detail(summary, open){{ content }}
    Regex::new(r"@detail\(([^,)]+)(?:,\s*open)?\)\{\{([\s\S]*?)\}\}").unwrap()
});

static DETAIL_PLUGIN_OPEN: Lazy<Regex> = Lazy::new(|| {
    // Separate pattern to detect 'open' attribute
    Regex::new(r"@detail\([^,)]+,\s*open\)").unwrap()
});

// Block plugin patterns
static BLOCK_PLUGIN_MULTILINE: Lazy<Regex> = Lazy::new(|| {
    // Match @function(args){{ content }} using non-greedy match
    Regex::new(r"@(\w+)\(([^)]*)\)\{\{([\s\S]*?)\}\}").unwrap()
});

static BLOCK_PLUGIN_SINGLELINE: Lazy<Regex> = Lazy::new(|| {
    // Match @function(args){content} (single braces)
    Regex::new(r"@(\w+)\(([^)]*)\)\{([^}]*)\}").unwrap()
});

// Block plugin with args only (no content): @function(args)
static BLOCK_PLUGIN_ARGSONLY: Lazy<Regex> = Lazy::new(|| {
    // Match @function(args) - args only, no content
    // This should be processed AFTER patterns with { and {{
    Regex::new(r"@(\w+)\(([^)]*)\)").unwrap()
});

// Block plugin without args: @function()
static BLOCK_PLUGIN_NOARGS: Lazy<Regex> = Lazy::new(|| {
    // Match @function() - parens required to distinguish from @mentions
    Regex::new(r"@(\w+)\(\)").unwrap()
});

// Inline plugin pattern
static INLINE_PLUGIN: Lazy<Regex> = Lazy::new(|| {
    // Match &function(args){content};
    // Content may contain nested braces for nested plugins
    Regex::new(r"&(\w+)\(([^)]*)\)\{((?:[^{}]|\{[^}]*\})*)\};").unwrap()
});

// Inline plugin with args only: &function(args);
static INLINE_PLUGIN_ARGSONLY: Lazy<Regex> = Lazy::new(|| {
    // Match &function(args); (no content)
    Regex::new(r"&(\w+)\(([^)]*)\);").unwrap()
});

// Inline plugin without args: &function;
static INLINE_PLUGIN_NOARGS: Lazy<Regex> = Lazy::new(|| {
    // Match &function; (no args, no content)
    // Function name must start with a letter to avoid conflicts with HTML entities
    Regex::new(r"&([a-zA-Z]\w*);").unwrap()
});

// Common HTML entities that should NOT be treated as plugins
static HTML_ENTITIES: Lazy<std::collections::HashSet<&'static str>> = Lazy::new(|| {
    [
        "lt", "gt", "amp", "nbsp", "quot", "apos", "ndash", "mdash", "hellip", "copy", "reg",
        "trade", "times", "divide", "plusmn", "le", "ge", "ne", "asymp", "equiv", "forall",
        "exist", "empty", "nabla", "isin", "notin", "ni", "prod", "sum", "minus", "lowast",
        "radic", "prop", "infin", "ang", "and", "or", "cap", "cup", "int", "there4", "sim", "cong",
        "sub", "sup", "nsub", "sube", "supe", "oplus", "otimes", "perp", "sdot", "lceil", "rceil",
        "lfloor", "rfloor", "lang", "rang", "loz", "spades", "clubs", "hearts", "diams", "alpha",
        "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda",
        "mu", "nu", "xi", "omicron", "pi", "rho", "sigma", "tau", "upsilon", "phi", "chi", "psi",
        "omega", "Iuml", "iuml", "Uuml", "uuml", "Auml", "auml", "Ouml", "ouml", "Euml", "euml",
        "Aring", "aring", "AElig", "aelig", "Ccedil", "ccedil", "Eth", "eth", "Ntilde", "ntilde",
        "Oslash", "oslash", "Thorn", "thorn", "szlig", "yuml", "Agrave", "agrave", "Aacute",
        "aacute", "Acirc", "acirc", "Atilde", "atilde", "Egrave", "egrave", "Eacute", "eacute",
        "Ecirc", "ecirc", "Igrave", "igrave", "Iacute", "iacute", "Icirc", "icirc", "Ograve",
        "ograve", "Oacute", "oacute", "Ocirc", "ocirc", "Otilde", "otilde", "Ugrave", "ugrave",
        "Uacute", "uacute", "Ucirc", "ucirc", "Yacute", "yacute", "cent", "pound", "curren", "yen",
        "brvbar", "sect", "uml", "ordf", "laquo", "not", "shy", "macr", "deg", "sup2", "sup3",
        "acute", "micro", "para", "middot", "cedil", "sup1", "ordm", "raquo", "frac14", "frac12",
        "frac34", "iquest", "ensp", "emsp", "thinsp", "zwnj", "zwj", "lrm", "rlm",
    ]
    .iter()
    .copied()
    .collect()
});

/// Apply plugin syntax transformation
///
/// Converts plugin syntax to <template> elements with <data> children.
/// The parser only detects and preserves plugin metadata; actual execution happens
/// on the backend (Nuxt/Laravel) or frontend.
///
/// Supports multiple plugin patterns:
/// - Inline: `&function(args){content};`
/// - Block multiline: `@function(args){{ content }}`
/// - Block singleline: `@function(args){content}`
/// - Args only: `@function(args)` or `&function(args);`
/// - No args: `@function()` or `&function;`
///
/// Content within plugins is escaped and can contain nested plugins
/// or other Wiki syntax that will be processed by the plugin at runtime.
///
/// # Plugin-specific behavior
///
/// **Table plugin (`@table`)**: Only the first table element found within `{{}}`
/// will be processed with the specified Bootstrap classes. Multiple tables or
/// nested `@table` plugins are not recommended and may cause unexpected behavior.
///
/// # Arguments
///
/// * `html` - The HTML content to process
///
/// # Returns
///
/// HTML with plugin syntax converted to <template> containers
///
/// # Examples
///
/// ```
/// use umd::extensions::plugins::apply_plugin_syntax;
///
/// // Block plugin
/// let input = "@toc(2){{ }}";
/// let output = apply_plugin_syntax(input);
/// assert!(output.contains("class=\"umd-plugin umd-plugin-toc\""));
/// assert!(output.contains("<data value=\"0\">2</data>"));
///
/// // Inline plugin
/// let input = "&highlight(yellow){important text};";
/// let output = apply_plugin_syntax(input);
/// assert!(output.contains("class=\"umd-plugin umd-plugin-highlight\""));
/// assert!(output.contains("<data value=\"0\">yellow</data>"));
/// assert!(output.contains("important text"));
/// ```
pub fn apply_plugin_syntax(html: &str) -> String {
    let mut result = html.to_string();

    // Process standard plugins first - @clear() outputs a clearfix block
    result = CLEAR_PLUGIN
        .replace_all(&result, "\n<div class=\"clearfix\"></div>\n")
        .to_string();

    // Process standard plugins first - @detail(summary[, open]){{ content }}
    // This outputs direct HTML <details> instead of <template>
    result = DETAIL_PLUGIN
        .replace_all(&result, |caps: &regex::Captures| {
            let summary = caps.get(1).map_or("", |m| m.as_str().trim());
            let content = caps.get(2).map_or("", |m| m.as_str().trim());

            // Check if 'open' attribute is present in the full match
            let full_match = caps.get(0).map_or("", |m| m.as_str());
            let is_open = DETAIL_PLUGIN_OPEN.is_match(full_match);

            let open_attr = if is_open { " open" } else { "" };

            format!(
                "\n<details{}>\n  <summary>{}</summary>\n  {}\n</details>\n",
                open_attr, summary, content
            )
        })
        .to_string();

    // Process block plugins (multiline) first - @function(args){{ content }}
    result = BLOCK_PLUGIN_MULTILINE
        .replace_all(&result, |caps: &regex::Captures| {
            let function = caps.get(1).map_or("", |m| m.as_str());
            let args = caps.get(2).map_or("", |m| m.as_str());
            let content = caps.get(3).map_or("", |m| m.as_str());

            let args_html = render_args_as_data(args);
            let escaped_content = escape_html_text(content);

            if escaped_content.is_empty() {
                format!(
                    "\n<template class=\"umd-plugin umd-plugin-{}\">{}</template>\n",
                    function, args_html
                )
            } else {
                format!(
                    "\n<template class=\"umd-plugin umd-plugin-{}\">{}{}</template>\n",
                    function, args_html, escaped_content
                )
            }
        })
        .to_string();

    // Process block plugins (singleline) - @function(args){content}
    result = BLOCK_PLUGIN_SINGLELINE
        .replace_all(&result, |caps: &regex::Captures| {
            let function = caps.get(1).map_or("", |m| m.as_str());
            let args = caps.get(2).map_or("", |m| m.as_str());
            let content = caps.get(3).map_or("", |m| m.as_str());

            let args_html = render_args_as_data(args);
            let escaped_content = escape_html_text(content);

            if escaped_content.is_empty() {
                format!(
                    "\n<template class=\"umd-plugin umd-plugin-{}\">{}</template>\n",
                    function, args_html
                )
            } else {
                format!(
                    "\n<template class=\"umd-plugin umd-plugin-{}\">{}{}</template>\n",
                    function, args_html, escaped_content
                )
            }
        })
        .to_string();

    // Process block plugins (args only, no content) - @function(args)
    result = BLOCK_PLUGIN_ARGSONLY
        .replace_all(&result, |caps: &regex::Captures| {
            let function = caps.get(1).map_or("", |m| m.as_str());
            let args = caps.get(2).map_or("", |m| m.as_str());

            let args_html = render_args_as_data(args);
            format!(
                "\n<template class=\"umd-plugin umd-plugin-{}\">{}</template>\n",
                function, args_html
            )
        })
        .to_string();

    // Process block plugins (no args) - @function()
    result = BLOCK_PLUGIN_NOARGS
        .replace_all(&result, |caps: &regex::Captures| {
            let function = caps.get(1).map_or("", |m| m.as_str());
            format!(
                "\n<template class=\"umd-plugin umd-plugin-{}\"></template>\n",
                function
            )
        })
        .to_string();

    // Process inline plugins - &function(args){content};
    result = INLINE_PLUGIN
        .replace_all(&result, |caps: &regex::Captures| {
            let function = caps.get(1).map_or("", |m| m.as_str());
            let args = caps.get(2).map_or("", |m| m.as_str());
            let content = caps.get(3).map_or("", |m| m.as_str());

            let args_html = render_args_as_data(args);
            let escaped_content = escape_html_text(content);

            if escaped_content.is_empty() {
                format!(
                    "<template class=\"umd-plugin umd-plugin-{}\">{}</template>",
                    function, args_html
                )
            } else {
                format!(
                    "<template class=\"umd-plugin umd-plugin-{}\">{}{}</template>",
                    function, args_html, escaped_content
                )
            }
        })
        .to_string();

    // Process inline plugins (args only) - &function(args);
    result = INLINE_PLUGIN_ARGSONLY
        .replace_all(&result, |caps: &regex::Captures| {
            let function = caps.get(1).map_or("", |m| m.as_str());
            let args = caps.get(2).map_or("", |m| m.as_str());

            let args_html = render_args_as_data(args);
            format!(
                "<template class=\"umd-plugin umd-plugin-{}\">{}</template>",
                function, args_html
            )
        })
        .to_string();

    // Process inline plugins (no args) - &function;
    result = INLINE_PLUGIN_NOARGS
        .replace_all(&result, |caps: &regex::Captures| {
            let function = caps.get(1).map_or("", |m| m.as_str());

            // Skip HTML entities
            if HTML_ENTITIES.contains(function) {
                return caps[0].to_string(); // Return original match unchanged
            }

            format!(
                "<template class=\"umd-plugin umd-plugin-{}\"></template>",
                function
            )
        })
        .to_string();

    result
}

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

    #[test]
    fn test_simple_plugin() {
        let input = "@toc(2){{ }}";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("class=\"umd-plugin umd-plugin-toc\""));
        assert!(output.contains("<data value=\"0\">2</data>"));
    }

    #[test]
    fn test_plugin_with_complex_args() {
        let input = "@calendar(2024,1,true){{ }}";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("umd-plugin-calendar"));
        assert!(output.contains("<data value=\"0\">2024</data>"));
        assert!(output.contains("<data value=\"1\">1</data>"));
        assert!(output.contains("<data value=\"2\">true</data>"));
    }

    #[test]
    fn test_plugin_no_args() {
        let input = "@timestamp(){{ }}";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("umd-plugin-timestamp"));
        assert!(!output.contains("<data"));
    }

    #[test]
    fn test_plugin_with_content() {
        let input = "@code(rust){{ fn main() {} }}";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("umd-plugin-code"));
        assert!(output.contains("<data value=\"0\">rust</data>"));
        assert!(output.contains("fn main()"));
    }

    #[test]
    fn test_multiple_plugins() {
        let input = "@toc(2){{ }} and @timestamp(){{ }}";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("umd-plugin-toc"));
        assert!(output.contains("umd-plugin-timestamp"));
    }

    #[test]
    fn test_no_plugin() {
        let input = "This is normal text with @mention but not a plugin";
        let output = apply_plugin_syntax(input);
        // @mention without parens should not match
        assert_eq!(output, input);
    }

    #[test]
    fn test_inline_plugin() {
        let input = "&highlight(yellow){important text};";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("class=\"umd-plugin umd-plugin-highlight\""));
        assert!(output.contains("<data value=\"0\">yellow</data>"));
        assert!(output.contains("important text"));
        assert!(output.contains("<template"));
    }

    #[test]
    fn test_block_plugin_singleline() {
        let input = "@include(file.txt){default content}";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("class=\"umd-plugin umd-plugin-include\""));
        assert!(output.contains("<data value=\"0\">file.txt</data>"));
        assert!(output.contains("default content"));
    }

    #[test]
    fn test_nested_plugins() {
        let input = "&outer(arg1){text &inner(arg2){nested}; more};";
        let output = apply_plugin_syntax(input);
        println!("Nested output: {}", output);
        assert!(output.contains("class=\"umd-plugin umd-plugin-outer\""));
        // Content should preserve the nested plugin syntax (escaped)
        // & is escaped to &amp; in the content
        assert!(output.contains("&amp;"));
    }

    #[test]
    fn test_plugin_with_wiki_syntax() {
        let input = "@box(){{ **bold** and text }}";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("class=\"umd-plugin umd-plugin-box\""));
        // Content should preserve wiki syntax (escaped for backend processing)
        assert!(output.contains("**bold**"));
    }

    #[test]
    fn test_mixed_plugin_types() {
        let input = "@block(){{ content }} and &inline(arg){text}; mixed";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("umd-plugin-block"));
        assert!(output.contains("umd-plugin-inline"));
    }

    // New tests for additional patterns
    #[test]
    fn test_block_plugin_no_args() {
        let input = "@toc()";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("class=\"umd-plugin umd-plugin-toc\""));
        assert!(!output.contains("<data"));
    }

    #[test]
    fn test_detail_plugin_basic() {
        let input = "@detail(Click to expand){{ Hidden content }}";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("<details>"));
        assert!(output.contains("<summary>Click to expand</summary>"));
        assert!(output.contains("Hidden content"));
        assert!(output.contains("</details>"));
        assert!(!output.contains("open")); // Should not have open attribute
    }

    #[test]
    fn test_detail_plugin_with_open() {
        let input = "@detail(Already visible, open){{ This is shown by default }}";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("<details open>"));
        assert!(output.contains("<summary>Already visible</summary>"));
        assert!(output.contains("This is shown by default"));
    }

    #[test]
    fn test_block_plugin_args_only() {
        let input = "@feed(https://example.com/feed.atom, 10)";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("class=\"umd-plugin umd-plugin-feed\""));
        assert!(output.contains("<data value=\"0\">https://example.com/feed.atom</data>"));
        assert!(output.contains("<data value=\"1\">10</data>"));
    }

    #[test]
    fn test_inline_plugin_args_only() {
        let input = "&icon(mdi-pencil);";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("class=\"umd-plugin umd-plugin-icon\""));
        assert!(output.contains("<data value=\"0\">mdi-pencil</data>"));
    }

    #[test]
    fn test_inline_plugin_no_args() {
        let input = "&br;";
        let output = apply_plugin_syntax(input);
        assert!(output.contains("class=\"umd-plugin umd-plugin-br\""));
        assert!(!output.contains("<data"));
    }

    #[test]
    fn test_html_escaping_in_args() {
        let input = "@test(<script>alert('xss')</script>){{ }}";
        let output = apply_plugin_syntax(input);
        println!("Escaped args output: {}", output);
        // Args are escaped in <data> elements
        assert!(output.contains("&lt;"));
        assert!(output.contains("&gt;"));
    }

    #[test]
    fn test_html_escaping_in_content() {
        let input = "&test(arg){<b>content</b>};";
        let output = apply_plugin_syntax(input);
        println!("Escaped content output: {}", output);
        // Content is escaped
        assert!(output.contains("&lt;"));
        assert!(output.contains("&gt;"));
    }
}