]> git.proxmox.com Git - rustc.git/blob - src/vendor/pulldown-cmark/README.md
New upstream version 1.17.0+dfsg1
[rustc.git] / src / vendor / pulldown-cmark / README.md
1 # pulldown-cmark
2
3 This library is a pull parser for [CommonMark](http://commonmark.org/), written
4 in [Rust](http://www.rust-lang.org/). It comes with a simple command-line tool,
5 useful for rendering to HTML, and is also designed to be easy to use from as
6 a library.
7
8 It is designed to be:
9
10 * Fast; a bare minimum of allocation and copying
11 * Safe; written in pure Rust with no unsafe blocks
12 * Versatile; in particular source-maps are supported
13 * Correct; the goal is 100% compliance with the [CommonMark spec](http://spec.commonmark.org/)
14
15 ## Why a pull parser?
16
17 There are many parsers for Markdown and its variants, but to my knowledge none
18 use pull parsing. Pull parsing has become popular for XML, especially for
19 memory-conscious applications, because it uses dramatically less memory than
20 construcing a document tree, but is much easier to use than push parsers. Push
21 parsers are notoriously difficult to use, and also often error-prone because of
22 the need for user to delicately juggle state in a series of callbacks.
23
24 In a clean design, the parsing and rendering stages are neatly separated, but
25 this is often sacrificed in the name of performance and expedience. Many Markdown
26 implementations mix parsing and rendering together, and even designs that try
27 to separate them (such as the popular [hoedown](https://github.com/hoedown/hoedown)),
28 make the assumption that the rendering process can be fully represented as a
29 serialized string.
30
31 Pull parsing is in some sense the most versatile architecture. It's possible to
32 drive a push interface, also with minimal memory, and quite straightforward to
33 construct an AST. Another advantage is that source-map information (the mapping
34 between parsed blocks and offsets within the source text) is readily available;
35 you basically just call `get_offset()` as you consume events.
36
37 While manipulating AST's is the most flexible way to transform documents,
38 operating on iterators is surprisingly easy, and quite efficient. Here, for
39 example, is the code to transform soft line breaks into hard breaks:
40
41 ```rust
42 let parser = parser.map(|event| match event {
43 Event::SoftBreak => Event::HardBreak,
44 _ => event
45 });
46 ```
47
48 Or expanding an abbreviation in text:
49
50 ```rust
51 let parser = parser.map(|event| match event {
52 Event::Str(text) => Event::Str(text.replace("abbr", "abbreviation")),
53 _ => event
54 });
55 ```
56
57 Another simple example is code to determine the max nesting level:
58
59 ```rust
60 let mut max_nesting = 0;
61 let mut level = 0;
62 for event in parser {
63 match event {
64 Event::Start(_) => {
65 level += 1;
66 max_nesting = std::cmp::max(max_nesting, level);
67 }
68 Event::End(_) => level -= 1,
69 _ => ()
70 }
71 }
72 ```
73
74 ## Using Rust idiomatically
75
76 A lot of the internal scanning code is written at a pretty low level (it
77 pretty much scans byte patterns for the bits of syntax), but the external
78 interface is designed to be idiomatic Rust.
79
80 Pull parsers are at heart an iterator of events (start and end tags, text,
81 and other bits and pieces). The parser data structure implements the
82 Rust Iterator trait directly, and Event is an enum. Thus, you can use the
83 full power and expressivity of Rust's iterator infrastructure, including
84 for loops and `map` (as in the examples above), collecting the events into
85 a vector (for recording, playback, and manipulation), and more.
86
87 Further, the Str event (representing text) is a copy-on-write string (note:
88 this isn't quite true yet). The vast majority of text fragments are just
89 slices of the source document. For these, copy-on-write gives a convenient
90 representation that requires no allocation or copying, but allocated
91 strings are available when they're needed. Thus, when rendering text to
92 HTML, most text is copied just once, from the source document to the
93 HTML buffer.
94
95 ## Authors
96
97 The main author is Raph Levien.
98
99 ## Contributions
100
101 We gladly accept contributions via GitHub pull requests, as long as the author
102 has signed the Google Contributor License. Please see CONTRIBUTIONS.md for
103 more details.
104
105 ### Disclaimer
106
107 This is not an official Google product (experimental or otherwise), it
108 is just code that happens to be owned by Google.