markdown-ppp 2.9.2

Feature-rich Markdown Parsing and Pretty-Printing 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
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
683
684
685
//! Transformer pattern for AST modifications
//!
//! This module provides the Transformer trait for modifying AST nodes in place.
//! Unlike the visitor pattern which is read-only, transformers consume and
//! return modified AST nodes.
//!
//! # Example
//!
//! ```rust
//! use markdown_ppp::ast::*;
//! use markdown_ppp::ast_transform::{Transformer, TransformWith};
//!
//! struct UppercaseTransformer;
//!
//! impl Transformer for UppercaseTransformer {
//!     fn transform_inline(&mut self, inline: Inline) -> Inline {
//!         match inline {
//!             Inline::Text(text) => Inline::Text(text.to_uppercase()),
//!             other => self.walk_transform_inline(other),
//!         }
//!     }
//! }
//!
//! let doc = Document {
//!     blocks: vec![Block::Paragraph(vec![Inline::Text("hello".to_string())])],
//! };
//!
//! let result = doc.transform_with(&mut UppercaseTransformer);
//! ```

use crate::ast::*;

/// Transformer trait for modifying AST nodes
///
/// Provides default implementations that recursively transform child nodes.
/// Override specific methods to implement custom transformation logic.
///
/// # Example
///
/// ```rust
/// use markdown_ppp::ast::*;
/// use markdown_ppp::ast_transform::Transformer;
///
/// struct UppercaseTransformer;
///
/// impl Transformer for UppercaseTransformer {
///     fn transform_inline(&mut self, inline: Inline) -> Inline {
///         match inline {
///             Inline::Text(text) => Inline::Text(text.to_uppercase()),
///             other => self.walk_transform_inline(other),
///         }
///     }
/// }
/// ```
pub trait Transformer {
    /// Transform a document node
    fn transform_document(&mut self, doc: Document) -> Document {
        self.walk_transform_document(doc)
    }

    /// Transform a block node
    fn transform_block(&mut self, block: Block) -> Block {
        self.walk_transform_block(block)
    }

    /// Transform an inline node
    fn transform_inline(&mut self, inline: Inline) -> Inline {
        self.walk_transform_inline(inline)
    }

    /// Transform a table cell
    fn transform_table_cell(&mut self, cell: TableCell) -> TableCell {
        self.walk_transform_table_cell(cell)
    }

    /// Transform a list item
    fn transform_list_item(&mut self, item: ListItem) -> ListItem {
        self.walk_transform_list_item(item)
    }

    /// Transform a table row
    fn transform_table_row(&mut self, row: TableRow) -> TableRow {
        self.walk_transform_table_row(row)
    }

    /// Transform a heading
    fn transform_heading(&mut self, heading: Heading) -> Heading {
        self.walk_transform_heading(heading)
    }

    /// Transform a link
    fn transform_link(&mut self, link: Link) -> Link {
        self.walk_transform_link(link)
    }

    /// Transform an image
    fn transform_image(&mut self, image: Image) -> Image {
        self.walk_transform_image(image)
    }

    /// Transform a code block
    fn transform_code_block(&mut self, code_block: CodeBlock) -> CodeBlock {
        self.walk_transform_code_block(code_block)
    }

    /// Transform text content
    fn transform_text(&mut self, text: String) -> String {
        self.walk_transform_text(text)
    }

    /// Transform a footnote definition
    fn transform_footnote_definition(
        &mut self,
        footnote: FootnoteDefinition,
    ) -> FootnoteDefinition {
        self.walk_transform_footnote_definition(footnote)
    }

    /// Transform a GitHub alert
    fn transform_github_alert(&mut self, alert: GitHubAlert) -> GitHubAlert {
        self.walk_transform_github_alert(alert)
    }

    /// Default transformation for document
    fn walk_transform_document(&mut self, mut doc: Document) -> Document {
        doc.blocks = doc
            .blocks
            .into_iter()
            .map(|block| self.transform_block(block))
            .collect();
        doc
    }

    /// Default transformation for block nodes
    fn walk_transform_block(&mut self, block: Block) -> Block {
        match block {
            Block::Paragraph(inlines) => Block::Paragraph(
                inlines
                    .into_iter()
                    .map(|inline| self.transform_inline(inline))
                    .collect(),
            ),
            Block::Heading(heading) => Block::Heading(self.transform_heading(heading)),
            Block::BlockQuote(blocks) => Block::BlockQuote(
                blocks
                    .into_iter()
                    .map(|block| self.transform_block(block))
                    .collect(),
            ),
            Block::List(mut list) => {
                list.items = list
                    .items
                    .into_iter()
                    .map(|item| self.transform_list_item(item))
                    .collect();
                Block::List(list)
            }
            Block::Table(mut table) => {
                table.rows = table
                    .rows
                    .into_iter()
                    .map(|row| self.transform_table_row(row))
                    .collect();
                Block::Table(table)
            }
            Block::FootnoteDefinition(footnote) => {
                Block::FootnoteDefinition(self.transform_footnote_definition(footnote))
            }
            Block::GitHubAlert(alert) => Block::GitHubAlert(self.transform_github_alert(alert)),
            Block::Definition(mut def) => {
                def.label = def
                    .label
                    .into_iter()
                    .map(|inline| self.transform_inline(inline))
                    .collect();
                Block::Definition(def)
            }
            Block::CodeBlock(code_block) => Block::CodeBlock(self.transform_code_block(code_block)),
            // Terminal nodes - no transformation needed
            other => other,
        }
    }

    /// Default transformation for inline nodes
    fn walk_transform_inline(&mut self, inline: Inline) -> Inline {
        match inline {
            Inline::Emphasis(inlines) => Inline::Emphasis(
                inlines
                    .into_iter()
                    .map(|inline| self.transform_inline(inline))
                    .collect(),
            ),
            Inline::Strong(inlines) => Inline::Strong(
                inlines
                    .into_iter()
                    .map(|inline| self.transform_inline(inline))
                    .collect(),
            ),
            Inline::Strikethrough(inlines) => Inline::Strikethrough(
                inlines
                    .into_iter()
                    .map(|inline| self.transform_inline(inline))
                    .collect(),
            ),
            Inline::Link(link) => Inline::Link(self.transform_link(link)),
            Inline::LinkReference(mut link_ref) => {
                link_ref.label = link_ref
                    .label
                    .into_iter()
                    .map(|inline| self.transform_inline(inline))
                    .collect();
                link_ref.text = link_ref
                    .text
                    .into_iter()
                    .map(|inline| self.transform_inline(inline))
                    .collect();
                Inline::LinkReference(link_ref)
            }
            Inline::Image(image) => Inline::Image(self.transform_image(image)),
            Inline::Text(text) => Inline::Text(self.transform_text(text)),
            // Terminal nodes - no transformation needed
            other => other,
        }
    }

    /// Default transformation for table cells
    fn walk_transform_table_cell(&mut self, cell: TableCell) -> TableCell {
        cell.into_iter()
            .map(|inline| self.transform_inline(inline))
            .collect()
    }

    /// Default transformation for list items
    fn walk_transform_list_item(&mut self, mut item: ListItem) -> ListItem {
        item.blocks = item
            .blocks
            .into_iter()
            .map(|block| self.transform_block(block))
            .collect();
        item
    }

    /// Default transformation for table rows
    fn walk_transform_table_row(&mut self, row: TableRow) -> TableRow {
        row.into_iter()
            .map(|cell| self.transform_table_cell(cell))
            .collect()
    }

    /// Default transformation for headings
    fn walk_transform_heading(&mut self, mut heading: Heading) -> Heading {
        heading.content = heading
            .content
            .into_iter()
            .map(|inline| self.transform_inline(inline))
            .collect();
        heading
    }

    /// Default transformation for links
    fn walk_transform_link(&mut self, mut link: Link) -> Link {
        link.children = link
            .children
            .into_iter()
            .map(|inline| self.transform_inline(inline))
            .collect();
        link
    }

    /// Default transformation for images
    fn walk_transform_image(&mut self, image: Image) -> Image {
        // Images are terminal nodes
        image
    }

    /// Default transformation for code blocks
    fn walk_transform_code_block(&mut self, code_block: CodeBlock) -> CodeBlock {
        // Code blocks are terminal nodes
        code_block
    }

    /// Default transformation for text
    fn walk_transform_text(&mut self, text: String) -> String {
        // Text is a terminal node
        text
    }

    // ——————————————————————————————————————————————————————————————————————————
    // Expandable transformation methods (1-to-many)
    // ——————————————————————————————————————————————————————————————————————————

    /// Transform a document with possibility to expand into multiple documents
    ///
    /// Default implementation delegates to the regular transform_document method
    fn expand_document(&mut self, doc: Document) -> Vec<Document> {
        vec![self.transform_document(doc)]
    }

    /// Transform a block with possibility to expand into multiple blocks
    ///
    /// This enables scenarios like:
    /// - Split one paragraph into paragraph + list
    /// - Expand one block into multiple blocks
    /// - Transform one block based on content patterns
    ///
    /// Default implementation delegates to the regular transform_block method
    fn expand_block(&mut self, block: Block) -> Vec<Block> {
        vec![self.transform_block(block)]
    }

    /// Transform an inline with possibility to expand into multiple inlines
    ///
    /// This enables scenarios like:
    /// - Split text into multiple text nodes
    /// - Transform one inline element into several elements
    /// - Expand abbreviations or macros
    ///
    /// Default implementation delegates to the regular transform_inline method
    fn expand_inline(&mut self, inline: Inline) -> Vec<Inline> {
        vec![self.transform_inline(inline)]
    }

    /// Transform a table cell with possibility to expand into multiple cells
    fn expand_table_cell(&mut self, cell: TableCell) -> Vec<TableCell> {
        vec![self.transform_table_cell(cell)]
    }

    /// Transform a list item with possibility to expand into multiple items
    fn expand_list_item(&mut self, item: ListItem) -> Vec<ListItem> {
        vec![self.transform_list_item(item)]
    }

    /// Transform a table row with possibility to expand into multiple rows
    fn expand_table_row(&mut self, row: TableRow) -> Vec<TableRow> {
        vec![self.transform_table_row(row)]
    }

    /// Transform a heading with possibility to expand into multiple headings
    fn expand_heading(&mut self, heading: Heading) -> Vec<Heading> {
        vec![self.transform_heading(heading)]
    }

    /// Transform a link with possibility to expand into multiple links
    fn expand_link(&mut self, link: Link) -> Vec<Link> {
        vec![self.transform_link(link)]
    }

    /// Transform an image with possibility to expand into multiple images
    fn expand_image(&mut self, image: Image) -> Vec<Image> {
        vec![self.transform_image(image)]
    }

    /// Transform a code block with possibility to expand into multiple code blocks
    fn expand_code_block(&mut self, code_block: CodeBlock) -> Vec<CodeBlock> {
        vec![self.transform_code_block(code_block)]
    }

    /// Transform text with possibility to expand into multiple text strings
    fn expand_text(&mut self, text: String) -> Vec<String> {
        vec![self.transform_text(text)]
    }

    /// Transform a footnote definition with possibility to expand into multiple definitions
    fn expand_footnote_definition(
        &mut self,
        footnote: FootnoteDefinition,
    ) -> Vec<FootnoteDefinition> {
        vec![self.transform_footnote_definition(footnote)]
    }

    /// Transform a GitHub alert with possibility to expand into multiple alerts
    fn expand_github_alert(&mut self, alert: GitHubAlert) -> Vec<GitHubAlert> {
        vec![self.transform_github_alert(alert)]
    }

    // ——————————————————————————————————————————————————————————————————————————
    // Walk methods for expandable transformations
    // ——————————————————————————————————————————————————————————————————————————

    /// Walk document with expandable transformations
    ///
    /// Use this when you want to apply expandable transformations with the default
    /// behavior of using expand_* methods for child nodes.
    fn walk_expand_document(&mut self, mut doc: Document) -> Vec<Document> {
        doc.blocks = doc
            .blocks
            .into_iter()
            .flat_map(|block| self.walk_expand_block(block))
            .collect();
        vec![doc]
    }

    /// Walk block with expandable transformations
    ///
    /// Override this method to implement custom expandable block transformations.
    /// By default, delegates to transform_block (1-to-1 transformation) but processes
    /// child nodes with expandable transformations.
    fn walk_expand_block(&mut self, block: Block) -> Vec<Block> {
        match block {
            Block::Paragraph(inlines) => {
                let expanded_inlines: Vec<Inline> = inlines
                    .into_iter()
                    .flat_map(|inline| self.walk_expand_inline(inline))
                    .collect();
                vec![Block::Paragraph(expanded_inlines)]
            }
            Block::Heading(mut heading) => {
                heading.content = heading
                    .content
                    .into_iter()
                    .flat_map(|inline| self.walk_expand_inline(inline))
                    .collect();
                vec![Block::Heading(heading)]
            }
            Block::BlockQuote(blocks) => {
                let expanded_blocks: Vec<Block> = blocks
                    .into_iter()
                    .flat_map(|block| self.walk_expand_block(block))
                    .collect();
                vec![Block::BlockQuote(expanded_blocks)]
            }
            Block::List(mut list) => {
                list.items = list
                    .items
                    .into_iter()
                    .flat_map(|item| self.walk_expand_list_item(item))
                    .collect();
                vec![Block::List(list)]
            }
            Block::Table(mut table) => {
                table.rows = table
                    .rows
                    .into_iter()
                    .flat_map(|row| self.walk_expand_table_row(row))
                    .collect();
                vec![Block::Table(table)]
            }
            Block::FootnoteDefinition(mut footnote) => {
                footnote.blocks = footnote
                    .blocks
                    .into_iter()
                    .flat_map(|block| self.walk_expand_block(block))
                    .collect();
                vec![Block::FootnoteDefinition(footnote)]
            }
            Block::GitHubAlert(mut alert) => {
                alert.blocks = alert
                    .blocks
                    .into_iter()
                    .flat_map(|block| self.walk_expand_block(block))
                    .collect();
                vec![Block::GitHubAlert(alert)]
            }
            Block::Definition(mut def) => {
                def.label = def
                    .label
                    .into_iter()
                    .flat_map(|inline| self.walk_expand_inline(inline))
                    .collect();
                vec![Block::Definition(def)]
            }
            // Terminal nodes - no transformation needed
            other => vec![self.transform_block(other)],
        }
    }

    /// Walk inline with expandable transformations
    ///
    /// Override this method to implement custom expandable inline transformations.
    /// By default, delegates to transform_inline (1-to-1 transformation).
    fn walk_expand_inline(&mut self, inline: Inline) -> Vec<Inline> {
        vec![self.transform_inline(inline)]
    }

    /// Walk table cell with expandable transformations
    fn walk_expand_table_cell(&mut self, cell: TableCell) -> Vec<TableCell> {
        let expanded_cell = cell
            .into_iter()
            .flat_map(|inline| self.expand_inline(inline))
            .collect();
        vec![expanded_cell]
    }

    /// Walk list item with expandable transformations
    fn walk_expand_list_item(&mut self, mut item: ListItem) -> Vec<ListItem> {
        item.blocks = item
            .blocks
            .into_iter()
            .flat_map(|block| self.expand_block(block))
            .collect();
        vec![item]
    }

    /// Walk table row with expandable transformations
    fn walk_expand_table_row(&mut self, row: TableRow) -> Vec<TableRow> {
        let expanded_row = row
            .into_iter()
            .flat_map(|cell| self.expand_table_cell(cell))
            .collect();
        vec![expanded_row]
    }

    /// Walk heading with expandable transformations
    fn walk_expand_heading(&mut self, mut heading: Heading) -> Vec<Heading> {
        heading.content = heading
            .content
            .into_iter()
            .flat_map(|inline| self.expand_inline(inline))
            .collect();
        vec![heading]
    }

    /// Walk link with expandable transformations
    fn walk_expand_link(&mut self, mut link: Link) -> Vec<Link> {
        link.children = link
            .children
            .into_iter()
            .flat_map(|inline| self.expand_inline(inline))
            .collect();
        vec![link]
    }

    /// Walk image with expandable transformations
    fn walk_expand_image(&mut self, image: Image) -> Vec<Image> {
        // Images are terminal nodes
        vec![image]
    }

    /// Walk code block with expandable transformations
    fn walk_expand_code_block(&mut self, code_block: CodeBlock) -> Vec<CodeBlock> {
        // Code blocks are terminal nodes
        vec![code_block]
    }

    /// Walk text with expandable transformations
    fn walk_expand_text(&mut self, text: String) -> Vec<String> {
        // Text is a terminal node
        vec![text]
    }

    /// Walk footnote definition with expandable transformations
    fn walk_expand_footnote_definition(
        &mut self,
        mut footnote: FootnoteDefinition,
    ) -> Vec<FootnoteDefinition> {
        footnote.blocks = footnote
            .blocks
            .into_iter()
            .flat_map(|block| self.expand_block(block))
            .collect();
        vec![footnote]
    }

    /// Walk GitHub alert with expandable transformations
    fn walk_expand_github_alert(&mut self, mut alert: GitHubAlert) -> Vec<GitHubAlert> {
        alert.blocks = alert
            .blocks
            .into_iter()
            .flat_map(|block| self.expand_block(block))
            .collect();
        vec![alert]
    }

    /// Default transformation for footnote definitions
    fn walk_transform_footnote_definition(
        &mut self,
        mut footnote: FootnoteDefinition,
    ) -> FootnoteDefinition {
        footnote.blocks = footnote
            .blocks
            .into_iter()
            .map(|block| self.transform_block(block))
            .collect();
        footnote
    }

    /// Default transformation for GitHub alerts
    fn walk_transform_github_alert(&mut self, mut alert: GitHubAlert) -> GitHubAlert {
        alert.blocks = alert
            .blocks
            .into_iter()
            .map(|block| self.transform_block(block))
            .collect();
        alert
    }
}

/// Extension trait for transforming documents
pub trait TransformWith {
    /// Apply a transformer to this AST node
    fn transform_with<T: Transformer>(self, transformer: &mut T) -> Self;
}

impl TransformWith for Document {
    fn transform_with<T: Transformer>(self, transformer: &mut T) -> Self {
        transformer.transform_document(self)
    }
}

impl TransformWith for Block {
    fn transform_with<T: Transformer>(self, transformer: &mut T) -> Self {
        transformer.transform_block(self)
    }
}

impl TransformWith for Inline {
    fn transform_with<T: Transformer>(self, transformer: &mut T) -> Self {
        transformer.transform_inline(self)
    }
}

/// Extension trait for expandable transformations
pub trait ExpandWith {
    /// Apply an expandable transformer to this AST node, returning multiple nodes
    fn expand_with<T: Transformer>(self, transformer: &mut T) -> Vec<Self>
    where
        Self: Sized;
}

impl ExpandWith for Document {
    fn expand_with<T: Transformer>(self, transformer: &mut T) -> Vec<Self> {
        transformer.walk_expand_document(self)
    }
}

impl ExpandWith for Block {
    fn expand_with<T: Transformer>(self, transformer: &mut T) -> Vec<Self> {
        transformer.walk_expand_block(self)
    }
}

impl ExpandWith for Inline {
    fn expand_with<T: Transformer>(self, transformer: &mut T) -> Vec<Self> {
        transformer.walk_expand_inline(self)
    }
}

/// Composite transformer that applies multiple transformers in sequence
pub struct CompositeTransformer {
    transformers: Vec<Box<dyn Transformer>>,
}

impl CompositeTransformer {
    /// Create a new composite transformer
    pub fn new() -> Self {
        Self {
            transformers: Vec::new(),
        }
    }

    /// Add a transformer to the sequence
    pub fn add_transformer<T: Transformer + 'static>(mut self, transformer: T) -> Self {
        self.transformers.push(Box::new(transformer));
        self
    }
}

impl Default for CompositeTransformer {
    fn default() -> Self {
        Self::new()
    }
}

impl Transformer for CompositeTransformer {
    fn transform_document(&mut self, mut doc: Document) -> Document {
        for transformer in &mut self.transformers {
            doc = transformer.transform_document(doc);
        }
        doc
    }

    fn transform_block(&mut self, mut block: Block) -> Block {
        for transformer in &mut self.transformers {
            block = transformer.transform_block(block);
        }
        block
    }

    fn transform_inline(&mut self, mut inline: Inline) -> Inline {
        for transformer in &mut self.transformers {
            inline = transformer.transform_inline(inline);
        }
        inline
    }
}