parser_map_tag_print/
parser-map-tag-print.rs

1use pulldown_cmark::{Event, Options, Parser, Tag};
2
3fn main() {
4    let markdown_input = concat!(
5        "# My Heading\n",
6        "\n",
7        "My paragraph.\n",
8        "\n",
9        "* a\n",
10        "* b\n",
11        "* c\n",
12        "\n",
13        "1. d\n",
14        "2. e\n",
15        "3. f\n",
16        "\n",
17        "> my block quote\n",
18        "\n",
19        "```\n",
20        "my code block\n",
21        "```\n",
22        "\n",
23        "*emphasis*\n",
24        "**strong**\n",
25        "~~strikethrough~~\n",
26        "[My Link](http://example.com)\n",
27        "![My Image](http://example.com/image.jpg)\n",
28        "\n",
29        "| a | b |\n",
30        "| - | - |\n",
31        "| c | d |\n",
32        "\n",
33        "hello[^1]\n",
34        "[^1]: my footnote\n",
35    );
36    println!(
37        "\nParsing the following markdown string:\n{}\n",
38        markdown_input
39    );
40
41    // Set up the parser. We can treat is as any other iterator.
42    // For each event, we print its details, such as the tag or string.
43    // This filter simply returns the same event without any changes;
44    // you can compare the `event-filter` example which alters the output.
45    let parser = Parser::new_ext(markdown_input, Options::all()).map(|event| {
46        match &event {
47            Event::Start(tag) => match tag {
48                Tag::HtmlBlock => println!("HtmlBlock"),
49                Tag::Heading {
50                    level,
51                    id,
52                    classes,
53                    attrs,
54                } => println!(
55                    "Heading heading_level: {} fragment identifier: {:?} classes: {:?} attrs: {:?}",
56                    level, id, classes, attrs
57                ),
58                Tag::Paragraph => println!("Paragraph"),
59                Tag::List(ordered_list_first_item_number) => println!(
60                    "List ordered_list_first_item_number: {:?}",
61                    ordered_list_first_item_number
62                ),
63                Tag::DefinitionList => println!("Definition list"),
64                Tag::DefinitionListTitle => println!("Definition title (definition list item)"),
65                Tag::DefinitionListDefinition => println!("Definition (definition list item)"),
66                Tag::Item => println!("Item (this is a list item)"),
67                Tag::Emphasis => println!("Emphasis (this is a span tag)"),
68                Tag::Superscript => println!("Superscript (this is a span tag)"),
69                Tag::Subscript => println!("Subscript (this is a span tag)"),
70                Tag::Strong => println!("Strong (this is a span tag)"),
71                Tag::Strikethrough => println!("Strikethrough (this is a span tag)"),
72                Tag::BlockQuote(kind) => println!("BlockQuote ({:?})", kind),
73                Tag::CodeBlock(code_block_kind) => {
74                    println!("CodeBlock code_block_kind: {:?}", code_block_kind)
75                }
76                Tag::Link {
77                    link_type,
78                    dest_url,
79                    title,
80                    id,
81                } => println!(
82                    "Link link_type: {:?} url: {} title: {} id: {}",
83                    link_type, dest_url, title, id
84                ),
85                Tag::Image {
86                    link_type,
87                    dest_url,
88                    title,
89                    id,
90                } => println!(
91                    "Image link_type: {:?} url: {} title: {} id: {}",
92                    link_type, dest_url, title, id
93                ),
94                Tag::Table(column_text_alignment_list) => println!(
95                    "Table column_text_alignment_list: {:?}",
96                    column_text_alignment_list
97                ),
98                Tag::TableHead => println!("TableHead (contains TableRow tags"),
99                Tag::TableRow => println!("TableRow (contains TableCell tags)"),
100                Tag::TableCell => println!("TableCell (contains inline tags)"),
101                Tag::FootnoteDefinition(label) => println!("FootnoteDefinition label: {}", label),
102                Tag::MetadataBlock(kind) => println!("MetadataBlock: {:?}", kind),
103            },
104            _ => (),
105        };
106        event
107    });
108
109    let mut html_output = String::new();
110    pulldown_cmark::html::push_html(&mut html_output, parser);
111    println!("\nHTML output:\n{}\n", &html_output);
112}