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
//! Tests for generic expandable transformations with user data

use crate::ast::generic::*;
use crate::ast_transform::{GenericExpandWith, GenericTransformer};

/// Test user data type for tracking node IDs
#[derive(Debug, Clone, PartialEq, Default)]
struct NodeId(u32);

/// Test user data type for tracking source locations
#[derive(Debug, Clone, PartialEq, Default)]
struct SourceLocation {
    line: u32,
    column: u32,
}

/// Transformer that assigns unique IDs to nodes and expands long paragraphs
struct IdAssignerExpander {
    next_id: u32,
}

impl IdAssignerExpander {
    fn new() -> Self {
        Self { next_id: 1 }
    }

    fn next_id(&mut self) -> NodeId {
        let id = NodeId(self.next_id);
        self.next_id += 1;
        id
    }
}

impl GenericTransformer<NodeId> for IdAssignerExpander {
    fn walk_expand_block(&mut self, block: Block<NodeId>) -> Vec<Block<NodeId>> {
        match block {
            Block::Paragraph { content, .. } if content.len() > 3 => {
                // Split long paragraphs into two with new IDs
                let mid = content.len() / 2;
                let (first_half, second_half) = content.split_at(mid);

                let mut result = Vec::new();

                // Process first half: assign block ID, then process content
                let first_block_id = self.next_id();
                let transformed_first_half: Vec<_> = first_half
                    .iter()
                    .flat_map(|inline| self.walk_expand_inline(inline.clone()))
                    .collect();
                result.push(Block::Paragraph {
                    content: transformed_first_half,
                    user_data: first_block_id,
                });

                // Process second half: assign block ID, then process content
                let second_block_id = self.next_id();
                let transformed_second_half: Vec<_> = second_half
                    .iter()
                    .flat_map(|inline| self.walk_expand_inline(inline.clone()))
                    .collect();
                result.push(Block::Paragraph {
                    content: transformed_second_half,
                    user_data: second_block_id,
                });

                result
            }
            Block::Paragraph { content, .. } => {
                // Short paragraphs get new ID but no split
                let block_id = self.next_id();
                let transformed_content: Vec<_> = content
                    .into_iter()
                    .flat_map(|inline| self.walk_expand_inline(inline))
                    .collect();
                vec![Block::Paragraph {
                    content: transformed_content,
                    user_data: block_id,
                }]
            }
            other => {
                // Apply default transformation logic
                vec![self.transform_block(other)]
            }
        }
    }

    fn walk_expand_inline(&mut self, inline: Inline<NodeId>) -> Vec<Inline<NodeId>> {
        let transformed_inline = match inline {
            Inline::Text { content, .. } => Inline::Text {
                content,
                user_data: self.next_id(),
            },
            Inline::Emphasis { content, .. } => Inline::Emphasis {
                content: content
                    .into_iter()
                    .flat_map(|inline| self.walk_expand_inline(inline))
                    .collect(),
                user_data: self.next_id(),
            },
            other => other,
        };
        vec![transformed_inline]
    }
}

#[test]
fn test_id_assigner_expander() {
    let doc = Document {
        blocks: vec![
            // Long paragraph (will be split)
            Block::Paragraph {
                content: vec![
                    Inline::Text {
                        content: "One".to_string(),
                        user_data: NodeId(0),
                    },
                    Inline::Text {
                        content: "Two".to_string(),
                        user_data: NodeId(0),
                    },
                    Inline::Text {
                        content: "Three".to_string(),
                        user_data: NodeId(0),
                    },
                    Inline::Text {
                        content: "Four".to_string(),
                        user_data: NodeId(0),
                    },
                ],
                user_data: NodeId(0),
            },
            // Short paragraph (won't be split)
            Block::Paragraph {
                content: vec![Inline::Text {
                    content: "Short".to_string(),
                    user_data: NodeId(0),
                }],
                user_data: NodeId(0),
            },
        ],
        user_data: NodeId(0),
    };

    let mut transformer = IdAssignerExpander::new();
    let result = transformer.walk_expand_document(doc);

    assert_eq!(result.len(), 1);
    assert_eq!(result[0].blocks.len(), 3); // Long paragraph split into 2 + short paragraph = 3

    // Check first split paragraph
    if let Block::Paragraph { content, user_data } = &result[0].blocks[0] {
        assert_eq!(content.len(), 2); // First half
        assert_eq!(user_data, &NodeId(1));
        // Check that text nodes got new IDs
        if let Inline::Text { user_data, .. } = &content[0] {
            assert_eq!(user_data, &NodeId(2));
        }
        if let Inline::Text { user_data, .. } = &content[1] {
            assert_eq!(user_data, &NodeId(3));
        }
    } else {
        panic!("Expected first block to be paragraph");
    }

    // Check second split paragraph
    if let Block::Paragraph { content, user_data } = &result[0].blocks[1] {
        assert_eq!(content.len(), 2); // Second half
        assert_eq!(user_data, &NodeId(4));
        // Check that text nodes got new IDs
        if let Inline::Text { user_data, .. } = &content[0] {
            assert_eq!(user_data, &NodeId(5));
        }
        if let Inline::Text { user_data, .. } = &content[1] {
            assert_eq!(user_data, &NodeId(6));
        }
    } else {
        panic!("Expected second block to be paragraph");
    }

    // Check short paragraph (not split)
    if let Block::Paragraph { content, user_data } = &result[0].blocks[2] {
        assert_eq!(content.len(), 1);
        assert_eq!(user_data, &NodeId(7));
        if let Inline::Text { user_data, .. } = &content[0] {
            assert_eq!(user_data, &NodeId(8));
        }
    } else {
        panic!("Expected third block to be paragraph");
    }
}

/// Transformer that works with source location data
struct SourceLocationExpander;

impl GenericTransformer<SourceLocation> for SourceLocationExpander {
    fn walk_expand_inline(
        &mut self,
        inline: Inline<SourceLocation>,
    ) -> Vec<Inline<SourceLocation>> {
        match inline {
            Inline::Text { content, user_data } if content.contains("SPLIT") => {
                // Split text at "SPLIT" with adjusted source locations
                let parts: Vec<&str> = content.split("SPLIT").collect();
                let mut result = Vec::new();
                let mut column_offset = 0;

                for (i, part) in parts.iter().enumerate() {
                    if !part.is_empty() {
                        result.push(Inline::Text {
                            content: part.to_string(),
                            user_data: SourceLocation {
                                line: user_data.line,
                                column: user_data.column + column_offset,
                            },
                        });
                    }
                    column_offset += part.len() as u32 + 5; // +5 for "SPLIT" length

                    // Add separator between parts (except after last)
                    if i < parts.len() - 1 && !parts[i + 1].is_empty() {
                        result.push(Inline::Emphasis {
                            content: vec![Inline::Text {
                                content: " | ".to_string(),
                                user_data: SourceLocation {
                                    line: user_data.line,
                                    column: user_data.column + column_offset - 5,
                                },
                            }],
                            user_data: SourceLocation {
                                line: user_data.line,
                                column: user_data.column + column_offset - 5,
                            },
                        });
                    }
                }

                result
            }
            other => {
                // For other types, use default transformation
                vec![self.transform_inline(other)]
            }
        }
    }
}

#[test]
fn test_source_location_expander() {
    let doc = Document {
        blocks: vec![Block::Paragraph {
            content: vec![Inline::Text {
                content: "HelloSPLITWorld".to_string(),
                user_data: SourceLocation {
                    line: 1,
                    column: 10,
                },
            }],
            user_data: SourceLocation { line: 1, column: 5 },
        }],
        user_data: SourceLocation { line: 1, column: 0 },
    };

    let mut transformer = SourceLocationExpander;
    let result = transformer.walk_expand_document(doc);

    assert_eq!(result.len(), 1);
    assert_eq!(result[0].blocks.len(), 1);

    if let Block::Paragraph { content, .. } = &result[0].blocks[0] {
        assert_eq!(content.len(), 3); // "Hello", " | ", "World"

        // Check first text node
        if let Inline::Text { content, user_data } = &content[0] {
            assert_eq!(content, "Hello");
            assert_eq!(user_data.line, 1);
            assert_eq!(user_data.column, 10); // Original column
        } else {
            panic!("Expected first inline to be text");
        }

        // Check separator
        if let Inline::Emphasis {
            content: emphasis_content,
            user_data,
        } = &content[1]
        {
            assert_eq!(user_data.line, 1);
            assert_eq!(user_data.column, 15); // 10 + 5 (Hello) + 5 (SPLIT) - 5 = 15
            if let Inline::Text { content, .. } = &emphasis_content[0] {
                assert_eq!(content, " | ");
            }
        } else {
            panic!("Expected second inline to be emphasis");
        }

        // Check second text node
        if let Inline::Text { content, user_data } = &content[2] {
            assert_eq!(content, "World");
            assert_eq!(user_data.line, 1);
            assert_eq!(user_data.column, 20); // 10 + 5 + 5 = 20
        } else {
            panic!("Expected third inline to be text");
        }
    } else {
        panic!("Expected paragraph");
    }
}

/// Test using GenericExpandWith trait
#[test]
fn test_generic_expand_with_trait() {
    let block = Block::Paragraph {
        content: vec![
            Inline::Text {
                content: "One".to_string(),
                user_data: NodeId(0),
            },
            Inline::Text {
                content: "Two".to_string(),
                user_data: NodeId(0),
            },
            Inline::Text {
                content: "Three".to_string(),
                user_data: NodeId(0),
            },
            Inline::Text {
                content: "Four".to_string(),
                user_data: NodeId(0),
            },
        ],
        user_data: NodeId(0),
    };

    let mut transformer = IdAssignerExpander::new();
    let result = block.expand_with(&mut transformer);

    assert_eq!(result.len(), 2); // Long paragraph should be split

    if let Block::Paragraph { content, user_data } = &result[0] {
        assert_eq!(content.len(), 2);
        assert_eq!(user_data, &NodeId(1));
    } else {
        panic!("Expected first result to be paragraph");
    }

    if let Block::Paragraph { content, user_data } = &result[1] {
        assert_eq!(content.len(), 2);
        assert_eq!(user_data, &NodeId(4));
    } else {
        panic!("Expected second result to be paragraph");
    }
}

/// Test transformer that doesn't expand but modifies user data
struct UserDataModifier;

impl GenericTransformer<NodeId> for UserDataModifier {
    fn walk_expand_block(&mut self, block: Block<NodeId>) -> Vec<Block<NodeId>> {
        let transformed_block = match block {
            Block::Paragraph { content, user_data } => Block::Paragraph {
                content: content
                    .into_iter()
                    .flat_map(|inline| self.walk_expand_inline(inline))
                    .collect(),
                user_data: NodeId(user_data.0 + 100), // Add 100 to ID
            },
            other => self.walk_transform_block(other),
        };
        vec![transformed_block]
    }

    fn walk_expand_inline(&mut self, inline: Inline<NodeId>) -> Vec<Inline<NodeId>> {
        let transformed_inline = match inline {
            Inline::Text { content, user_data } => Inline::Text {
                content,
                user_data: NodeId(user_data.0 + 100), // Add 100 to ID
            },
            other => self.walk_transform_inline(other),
        };
        vec![transformed_inline]
    }
}

#[test]
fn test_user_data_modification() {
    let doc = Document {
        blocks: vec![Block::Paragraph {
            content: vec![Inline::Text {
                content: "Test".to_string(),
                user_data: NodeId(5),
            }],
            user_data: NodeId(10),
        }],
        user_data: NodeId(1),
    };

    let mut transformer = UserDataModifier;
    let result = transformer.walk_expand_document(doc);

    assert_eq!(result.len(), 1);
    assert_eq!(result[0].blocks.len(), 1);

    // Document user data should be unchanged (we don't transform it)
    assert_eq!(result[0].user_data, NodeId(1));

    if let Block::Paragraph { content, user_data } = &result[0].blocks[0] {
        assert_eq!(user_data, &NodeId(110)); // 10 + 100

        if let Inline::Text { user_data, .. } = &content[0] {
            assert_eq!(user_data, &NodeId(105)); // 5 + 100
        } else {
            panic!("Expected text inline");
        }
    } else {
        panic!("Expected paragraph");
    }
}