rushdown 0.18.0

A 100% CommonMark-compatible GitHub Flavored Markdown parser and renderer
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
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

pub mod ast;
pub mod context;
pub mod parser;
pub mod renderer;
pub mod test;
pub mod text;
pub mod util;

#[cfg(feature = "html-entities")]
mod html_entity;

mod scanner;

mod error;
use alloc::string::String;

pub use error::{Error, Result};

use crate::{
    parser::{Parser, ParserExtension},
    renderer::{html, TextWrite},
    text::BasicReader,
};

/// Trait for converting Markdown to HTML.
///
/// # Errors
/// Parsing phase will never fail, so the only possible errors are I/O errors during rendering.
pub trait MarkdownToHtml<W: TextWrite = String> {
    /// Converts the given Markdown source to HTML and writes it to the output.
    fn markdown_to_html(&self, out: &mut W, source: &str) -> Result<()>;
}

impl<W: TextWrite, F> MarkdownToHtml<W> for F
where
    F: Fn(&mut W, &str) -> Result<()>,
{
    fn markdown_to_html(&self, out: &mut W, source: &str) -> Result<()> {
        (self)(out, source)
    }
}

/// Creates a function that converts Markdown to HTML using the specified parser and renderer.
///
/// # Arguments
/// - `parser_options`: Options for the Markdown parser.
/// - `renderer_options`: Options for the HTML renderer.
/// - `parser_extension`: Extension for the Markdown parser. If no extensions are needed, use [`crate::parser::NO_EXTENSIONS`].
/// - `renderer_extension`: Extension for the HTML renderer. If no extensions are needed, use [`crate::renderer::html::NO_EXTENSIONS`].
///
/// # Examples
/// ```
/// use core::fmt::Write;
/// use rushdown::{
///     new_markdown_to_html,
///     parser::{self, ParserExtension},
///     renderer::html::{self, RendererExtension},
///     Result,
/// };
///
/// let markdown_to_html = new_markdown_to_html(
///     parser::Options::default(),
///     html::Options::default(),
///     parser::gfm_table().and(parser::gfm_task_list_item()),
///     html::NO_EXTENSIONS,
/// );
/// let mut output = String::new();
/// let input = "# Hello, World!\n\nThis is a **Markdown** document.";
/// match markdown_to_html(&mut output, input) {
///     Ok(_) => {
///         println!("HTML output:\n{}", output);
///     }
///     Err(e) => {
///         println!("Error: {:?}", e);
///     }
/// }
/// ```
pub fn new_markdown_to_html<'r, W>(
    parser_options: parser::Options,
    renderer_options: html::Options,
    parser_extension: impl ParserExtension,
    renderer_extension: impl html::RendererExtension<'r, W>,
) -> impl Fn(&mut W, &str) -> Result<()> + 'r
where
    W: TextWrite + 'r,
{
    let parser = Parser::with_extensions(parser_options, parser_extension);
    let renderer = html::Renderer::<'r, W>::with_extensions(renderer_options, renderer_extension);
    move |output: &mut W, source: &str| {
        let mut reader = BasicReader::new(source);
        let (arena, document_ref) = parser.parse(&mut reader);
        renderer.render(output, source, &arena, document_ref)
    }
}

/// Creates a function that converts Markdown to HTML using the specified parser and renderer,
/// with output written to a `String`.
pub fn new_markdown_to_html_string<'r>(
    parser_options: parser::Options,
    renderer_options: html::Options,
    parser_extension: impl ParserExtension,
    renderer_extension: impl html::RendererExtension<'r, String>,
) -> impl Fn(&mut String, &str) -> Result<()> + 'r {
    new_markdown_to_html::<String>(
        parser_options,
        renderer_options,
        parser_extension,
        renderer_extension,
    )
}

/// Converts Markdown(CommonMark) to HTML using default parser and renderer options.
///
/// # Examples
/// ```
/// use rushdown::markdown_to_html_string;
/// let mut output = String::new();
/// let input = "# Hello, World!\n\nThis is a **Markdown** document.";
/// match markdown_to_html_string(&mut output, input) {
///     Ok(_) => {
///         println!("HTML output:\n{}", output);
///     }
///     Err(e) => {
///     println!("Error: {:?}", e);
///     }
///  };
///  ```
pub fn markdown_to_html_string(output: &mut String, source: &str) -> Result<()> {
    let parser = Parser::with_options(parser::Options::default());
    let renderer = html::Renderer::with_options(html::Options::default());
    let mut reader = BasicReader::new(source);
    let (arena, document_ref) = parser.parse(&mut reader);
    renderer.render(output, source, &arena, document_ref)
}

// macros {{{

/// Helper macro to match kind data.
///
/// # Examples
/// ```
/// use rushdown::ast::{Arena, NodeRef, KindData, Paragraph};
/// use rushdown::matches_kind;
///
/// let mut arena = Arena::new();
/// let para_ref: NodeRef = arena.new_node(Paragraph::new());
/// assert!(matches_kind!(arena, para_ref, Paragraph));
/// assert!(matches_kind!(arena[para_ref], Paragraph));
/// ```
#[macro_export]
macro_rules! matches_kind {
    ($arena:expr, $node_ref:expr, $variant:ident) => {
        matches!(
            $arena[$node_ref].kind_data(),
            $crate::ast::KindData::$variant(_)
        )
    };
    ($node:expr, $variant:ident) => {
        matches!($node.kind_data(), $crate::ast::KindData::$variant(_))
    };
}

/// Helper macro to match extension kind.
///
/// # Examples
/// ```
/// use core::fmt::{self, Write};
/// use rushdown::ast::{Arena, NodeRef, NodeType, NodeKind, KindData, PrettyPrint, pp_indent};
/// use rushdown::matches_extension_kind;
///
/// #[derive(Debug)]
/// struct Admonition {
///     kind: String,
/// }
///
/// impl NodeKind for Admonition {
///     fn typ(&self) -> NodeType { NodeType::ContainerBlock }
///
///     fn kind_name(&self) -> &'static str { "Admonition" }
/// }
///
/// impl PrettyPrint for Admonition {
///     fn pretty_print(&self, w: &mut dyn Write, _source: &str, level: usize) -> fmt::Result {
///         writeln!(w, "{}kind: {}", pp_indent(level), self.kind)
///     }
/// }
///
/// impl From<Admonition> for KindData {
///     fn from(e: Admonition) -> Self { KindData::Extension(Box::new(e)) }
/// }
///
/// let mut arena = Arena::new();
/// let ext_ref: NodeRef = arena.new_node(Admonition{kind: "note".to_string()});
/// assert!(matches_extension_kind!(arena, ext_ref, Admonition));
/// assert!(matches_extension_kind!(arena[ext_ref], Admonition));
/// ```
///
#[macro_export]
macro_rules! matches_extension_kind {
    ($arena:expr, $ref:expr, $ext_type:ty) => {
        (if let $crate::ast::KindData::Extension(ref d) = $arena[$ref].kind_data() {
            (d.as_ref() as &dyn ::core::any::Any)
                .downcast_ref::<$ext_type>()
                .is_some()
        } else {
            false
        })
    };
    ($node:expr, $ext_type:ty) => {
        (if let $crate::ast::KindData::Extension(ref d) = $node.kind_data() {
            (d.as_ref() as &dyn ::core::any::Any)
                .downcast_ref::<$ext_type>()
                .is_some()
        } else {
            false
        })
    };
}

/// Helper macro to downcast extension data.
///
/// # Examples
/// ```
/// use core::fmt::{self, Write};
/// use rushdown::ast::{Arena, NodeRef, NodeType, NodeKind, KindData, PrettyPrint, pp_indent};
/// use rushdown::as_extension_data;
///
/// #[derive(Debug)]
/// struct Admonition {
///     kind: String,
/// }
///
/// impl NodeKind for Admonition {
///     fn typ(&self) -> NodeType { NodeType::ContainerBlock }
///
///     fn kind_name(&self) -> &'static str { "Admonition" }
/// }
///
/// impl PrettyPrint for Admonition {
///     fn pretty_print(&self, w: &mut dyn Write, _source: &str, level: usize) -> fmt::Result {
///         writeln!(w, "{}kind: {}", pp_indent(level), self.kind)
///     }
/// }
///
/// impl From<Admonition> for KindData {
///     fn from(e: Admonition) -> Self { KindData::Extension(Box::new(e)) }
/// }
///
/// let mut arena = Arena::new();
/// let ext_ref: NodeRef = arena.new_node(Admonition{kind: "note".to_string()});
/// let ext_data = as_extension_data!(arena, ext_ref, Admonition);
/// assert_eq!(ext_data.kind, "note");
/// let ext_data = as_extension_data!(arena[ext_ref], Admonition);
/// assert_eq!(ext_data.kind, "note");
/// ```
///
#[macro_export]
macro_rules! as_extension_data {
    ($arena:expr, $ref:expr, $ext_type:ty) => {
        (if let $crate::ast::KindData::Extension(ref d) = $arena[$ref].kind_data() {
            (d.as_ref() as &dyn ::core::any::Any)
                .downcast_ref::<$ext_type>()
                .expect("Failed to downcast extension data")
        } else {
            panic!("Node is not an extension node")
        })
    };
    ($node:expr, $ext_type:ty) => {
        (if let $crate::ast::KindData::Extension(ref d) = $node.kind_data() {
            (d.as_ref() as &dyn ::core::any::Any)
                .downcast_ref::<$ext_type>()
                .expect("Failed to downcast extension data")
        } else {
            panic!("Node is not an extension node")
        })
    };
}

/// Helper macro to downcast mutable extension data.
///
/// See [`as_extension_data!`] for examples.
#[macro_export]
macro_rules! as_extension_data_mut {
    ($arena:expr, $ref:expr, $ext_type:ty) => {
        (if let $crate::ast::KindData::Extension(ref mut d) = $arena[$ref].kind_data_mut() {
            (d.as_mut() as &mut dyn ::core::any::Any)
                .downcast_mut::<$ext_type>()
                .expect("Failed to downcast extension data")
        } else {
            panic!("Node is not an extension node")
        })
    };
    ($node:expr, $ext_type:ty) => {
        (if let $crate::ast::KindData::Extension(ref mut d) = $node.kind_data_mut() {
            (d.as_mut() as &mut dyn ::core::any::Any)
                .downcast_mut::<$ext_type>()
                .expect("Failed to downcast extension data")
        } else {
            panic!("Node is not an extension node")
        })
    };
}

/// Helper macro to work with kind data.
///
/// # Examples
/// ```
/// use rushdown::ast::{Arena, NodeRef, KindData, List};
/// use rushdown::as_kind_data;
///
/// let mut arena = Arena::new();
/// let para_ref: NodeRef = arena.new_node(List::new(b'-'));
/// let data = as_kind_data!(arena, para_ref, List);
/// assert_eq!(data.marker(), b'-');
/// let data = as_kind_data!(arena[para_ref], List);
/// assert_eq!(data.marker(), b'-');
/// ```
#[macro_export]
macro_rules! as_kind_data {
    ($arena:expr, $node_ref:expr, $variant:ident) => {
        (if let $crate::ast::KindData::$variant(ref d) = $arena[$node_ref].kind_data() {
            d
        } else {
            panic!(
                "Expected kind data variant {} but found {:?}",
                stringify!($variant),
                $arena[$node_ref].kind_data()
            )
        })
    };
    ($node:expr, $variant:ident) => {
        (if let $crate::ast::KindData::$variant(ref d) = $node.kind_data() {
            d
        } else {
            panic!(
                "Expected kind data variant {} but found {:?}",
                stringify!($variant),
                $node.kind_data()
            )
        })
    };
}

/// Helper macro to work with mutable kind data.
///
/// See [`as_kind_data!`] for examples.
#[macro_export]
macro_rules! as_kind_data_mut {
    ($arena:expr, $node_ref:expr, $variant:ident) => {
        (if let $crate::ast::KindData::$variant(ref mut d) = $arena[$node_ref].kind_data_mut() {
            d
        } else {
            panic!(
                "Expected kind data variant {} but found {:?}",
                stringify!($variant),
                $arena[$node_ref].kind_data()
            )
        })
    };
    ($node:expr, $variant:ident) => {
        (if let $crate::ast::KindData::$variant(ref mut d) = $node.kind_data_mut() {
            d
        } else {
            panic!(
                "Expected kind data variant {} but found {:?}",
                stringify!($variant),
                $node.kind_data()
            )
        })
    };
}

/// Helper macro to work with type data.
///
/// # Examples
/// ```
/// use rushdown::ast::{Arena, NodeRef, TypeData, Block, Paragraph};
/// use rushdown::as_type_data;
///
/// let mut arena = Arena::new();
/// let para_ref: NodeRef = arena.new_node(Paragraph::new());
/// let data = as_type_data!(arena, para_ref, Block);
/// assert!(data.source().is_empty());
/// let data = as_type_data!(arena[para_ref], Block);
/// assert!(data.source().is_empty());
/// ```
///
#[macro_export]
macro_rules! as_type_data {
    ($arena:expr, $node_ref:expr, $variant:ident) => {
        (if let $crate::ast::TypeData::$variant(ref d) = $arena[$node_ref].type_data() {
            d
        } else {
            panic!(
                "Expected type data variant {} but found {:?}",
                stringify!($variant),
                $arena[$node_ref].type_data()
            )
        })
    };
    ($node:expr, $variant:ident) => {
        (if let $crate::ast::TypeData::$variant(ref d) = $node.type_data() {
            d
        } else {
            panic!(
                "Expected type data variant {} but found {:?}",
                stringify!($variant),
                $node.type_data()
            )
        })
    };
}

/// Helper macro to work with mutable type data.
///
/// See [`as_type_data!`] for examples.
#[macro_export]
macro_rules! as_type_data_mut {
    ($arena:expr, $node_ref:expr, $variant:ident) => {
        (if let $crate::ast::TypeData::$variant(ref mut d) = $arena[$node_ref].type_data_mut() {
            d
        } else {
            panic!(
                "Expected type data variant {} but found {:?}",
                stringify!($variant),
                $arena[$node_ref].type_data()
            )
        })
    };
    ($node:expr, $variant:ident) => {
        (if let $crate::ast::TypeData::$variant(ref mut d) = $node.type_data_mut() {
            d
        } else {
            panic!(
                "Expected type data variant {} but found {:?}",
                stringify!($variant),
                $node.type_data()
            )
        })
    };
}

/// Helper macro to construct an AST.
///
/// # Examples
/// ```rust
/// use rushdown::md_ast;
/// use rushdown::ast::*;
/// use rushdown::renderer::html;
///
/// let mut arena = Arena::new();
/// let doc = md_ast!(&mut arena, Document::new() => {
///     Blockquote::new() => {
///         Paragraph::new(); { |node: &mut Node| {
///             node.attributes_mut().insert("class", "paragraph".into());
///         } } => {
///             Text::new("Hello, World!")
///         },
///         Paragraph::new() => {
///             Text::new("This is a test.")
///         }
///     }
/// });
/// let renderer = html::Renderer::with_options(html::Options::default());
/// let mut output = String::new();
/// renderer.render(&mut output, "", &arena, doc).expect("Failed to render HTML");
/// assert_eq!(output, "<blockquote>\n<p class=\"paragraph\">Hello, World!</p>\n<p>This is a test.</p>\n</blockquote>\n");
/// ```
#[macro_export]
macro_rules! md_ast {
    ($arena:expr, $root:expr => { $($children:tt)* }) => {{
        let __root = $arena.new_node($root);
        md_ast!(@children $arena, __root, { $($children)* });
        __root
    }};

    // make a node, optional post hook written as: ;{ <expr> }
    (@mk $arena:expr, $spec:expr) => {{
        $arena.new_node($spec)
    }};
    (@mk $arena:expr, $spec:expr, @{ $post:expr }) => {{
        let __n = $arena.new_node($spec);
        ($post)(&mut $arena[__n]);
        __n
    }};

    (@children $arena:expr, $parent:ident, { }) => {};

    // child with grandchildren
    (@children $arena:expr, $parent:ident, {
        $child:expr $( ;{ $post:expr } )? => { $($grand:tt)* } $(, $($rest:tt)*)?
    }) => {{
        let __child = md_ast!(@mk $arena, $child $(, @{ $post })?);
        $parent.append_child($arena, __child);

        md_ast!(@children $arena, __child, { $($grand)* });
        md_ast!(@children $arena, $parent, { $($($rest)*)? });
    }};

    // leaf child
    (@children $arena:expr, $parent:ident, {
        $child:expr $( ;{ $post:expr } )? $(, $($rest:tt)*)?
    }) => {{
        let __child = md_ast!(@mk $arena, $child $(, ;{ $post })?);
        $parent.append_child($arena, __child);

        md_ast!(@children $arena, $parent, { $($($rest)*)? });
    }};
}

/// Helper macro to traverse the AST by calling methods on nodes.
///
/// # Examples
/// ```rust
/// use rushdown::md_ast;
/// use rushdown::node_path;
/// use rushdown::as_kind_data;
/// use rushdown::ast::*;
/// use rushdown::renderer::html;
///
/// let mut arena = Arena::new();
/// let doc = md_ast!(&mut arena, Document::new() => {
///     Blockquote::new() => {
///         Paragraph::new(); { |node: &mut Node| {
///             node.attributes_mut().insert("class", "paragraph".into());
///         } } => {
///             Text::new("Hello, World!")
///         },
///         Paragraph::new() => {
///             Text::new("This is a test.")
///         }
///     }
/// });
/// let text_opt = node_path!(arena, doc, first_child, first_child, first_child);
/// assert_eq!(as_kind_data!(arena, text_opt.unwrap(), Text).str(""), "Hello, World!");
/// ```
#[macro_export]
macro_rules! node_path {
    ($arena:expr, $start:expr $(, $method:ident )* ) => {{
        let mut node_opt = Some($start);
        $(
            node_opt = node_opt.and_then(|n| $arena[n].$method());
        )*
        node_opt
    }};
}

// }}} macros

// debug stuff {{{

#[cfg(not(feature = "std"))]
pub mod debug {
    #[cfg(feature = "no-std-unix-debug")]
    extern crate libc;

    use core::fmt::{self, Write};

    #[allow(dead_code)]
    pub struct Stdout;

    impl Write for Stdout {
        #[allow(unreachable_code, unused)]
        fn write_str(&mut self, s: &str) -> fmt::Result {
            #[cfg(feature = "no-std-unix-debug")]
            unsafe {
                libc::write(1, s.as_ptr() as *const _, s.len());
            }
            Ok(())
        }
    }

    #[macro_export]
    macro_rules! print {
        ($($arg:tt)*) => {{
            use core::fmt::Write;
            let mut out = $crate::debug::Stdout;
            core::write!(&mut out, $($arg)*).ok();
        }};
    }

    #[macro_export]
    macro_rules! println {
        ($($arg:tt)*) => {{
            $crate::print!("{}\n", format_args!($($arg)*));
        }};
    }
}
// }}}