rustdoc_to_markdown.rs

  1//! Provides conversion from rustdoc's HTML output to Markdown.
  2
  3#![deny(missing_docs)]
  4
  5mod html_element;
  6mod markdown_writer;
  7
  8use std::io::Read;
  9
 10use anyhow::{Context, Result};
 11use html5ever::driver::ParseOpts;
 12use html5ever::parse_document;
 13use html5ever::tendril::TendrilSink;
 14use html5ever::tree_builder::TreeBuilderOpts;
 15use markup5ever_rcdom::RcDom;
 16
 17use crate::markdown_writer::MarkdownWriter;
 18
 19/// Converts the provided rustdoc HTML to Markdown.
 20pub fn convert_rustdoc_to_markdown(mut html: impl Read) -> Result<String> {
 21    let parse_options = ParseOpts {
 22        tree_builder: TreeBuilderOpts {
 23            drop_doctype: true,
 24            ..Default::default()
 25        },
 26        ..Default::default()
 27    };
 28    let dom = parse_document(RcDom::default(), parse_options)
 29        .from_utf8()
 30        .read_from(&mut html)
 31        .context("failed to parse rustdoc HTML")?;
 32
 33    let markdown_writer = MarkdownWriter::new();
 34    let markdown = markdown_writer
 35        .run(&dom.document)
 36        .context("failed to convert rustdoc to HTML")?;
 37
 38    Ok(markdown)
 39}
 40
 41#[cfg(test)]
 42mod tests {
 43    use indoc::indoc;
 44    use pretty_assertions::assert_eq;
 45
 46    use super::*;
 47
 48    #[test]
 49    fn test_main_heading_buttons_get_removed() {
 50        let html = indoc! {r##"
 51            <div class="main-heading">
 52                <h1>Crate <a class="mod" href="#">serde</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1>
 53                <span class="out-of-band">
 54                    <a class="src" href="../src/serde/lib.rs.html#1-340">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button>
 55                </span>
 56            </div>
 57        "##};
 58        let expected = indoc! {"
 59            # Crate serde
 60        "}
 61        .trim();
 62
 63        assert_eq!(
 64            convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
 65            expected
 66        )
 67    }
 68
 69    #[test]
 70    fn test_single_paragraph() {
 71        let html = indoc! {r#"
 72            <p>In particular, the last point is what sets <code>axum</code> apart from other frameworks.
 73            <code>axum</code> doesn’t have its own middleware system but instead uses
 74            <a href="https://docs.rs/tower-service/0.3.2/x86_64-unknown-linux-gnu/tower_service/trait.Service.html" title="trait tower_service::Service"><code>tower::Service</code></a>. This means <code>axum</code> gets timeouts, tracing, compression,
 75            authorization, and more, for free. It also enables you to share middleware with
 76            applications written using <a href="http://crates.io/crates/hyper"><code>hyper</code></a> or <a href="http://crates.io/crates/tonic"><code>tonic</code></a>.</p>
 77        "#};
 78        let expected = indoc! {"
 79            In particular, the last point is what sets `axum` apart from other frameworks. `axum` doesn’t have its own middleware system but instead uses `tower::Service`. This means `axum` gets timeouts, tracing, compression, authorization, and more, for free. It also enables you to share middleware with applications written using `hyper` or `tonic`.
 80        "}
 81        .trim();
 82
 83        assert_eq!(
 84            convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
 85            expected
 86        )
 87    }
 88
 89    #[test]
 90    fn test_multiple_paragraphs() {
 91        let html = indoc! {r##"
 92            <h2 id="serde"><a class="doc-anchor" href="#serde">§</a>Serde</h2>
 93            <p>Serde is a framework for <em><strong>ser</strong></em>ializing and <em><strong>de</strong></em>serializing Rust data
 94            structures efficiently and generically.</p>
 95            <p>The Serde ecosystem consists of data structures that know how to serialize
 96            and deserialize themselves along with data formats that know how to
 97            serialize and deserialize other things. Serde provides the layer by which
 98            these two groups interact with each other, allowing any supported data
 99            structure to be serialized and deserialized using any supported data format.</p>
100            <p>See the Serde website <a href="https://serde.rs/">https://serde.rs/</a> for additional documentation and
101            usage examples.</p>
102            <h3 id="design"><a class="doc-anchor" href="#design">§</a>Design</h3>
103            <p>Where many other languages rely on runtime reflection for serializing data,
104            Serde is instead built on Rust’s powerful trait system. A data structure
105            that knows how to serialize and deserialize itself is one that implements
106            Serde’s <code>Serialize</code> and <code>Deserialize</code> traits (or uses Serde’s derive
107            attribute to automatically generate implementations at compile time). This
108            avoids any overhead of reflection or runtime type information. In fact in
109            many situations the interaction between data structure and data format can
110            be completely optimized away by the Rust compiler, leaving Serde
111            serialization to perform the same speed as a handwritten serializer for the
112            specific selection of data structure and data format.</p>
113        "##};
114        let expected = indoc! {"
115            ## Serde
116
117            Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.
118
119            The Serde ecosystem consists of data structures that know how to serialize and deserialize themselves along with data formats that know how to serialize and deserialize other things. Serde provides the layer by which these two groups interact with each other, allowing any supported data structure to be serialized and deserialized using any supported data format.
120
121            See the Serde website https://serde.rs/ for additional documentation and usage examples.
122
123            ### Design
124
125            Where many other languages rely on runtime reflection for serializing data, Serde is instead built on Rust’s powerful trait system. A data structure that knows how to serialize and deserialize itself is one that implements Serde’s `Serialize` and `Deserialize` traits (or uses Serde’s derive attribute to automatically generate implementations at compile time). This avoids any overhead of reflection or runtime type information. In fact in many situations the interaction between data structure and data format can be completely optimized away by the Rust compiler, leaving Serde serialization to perform the same speed as a handwritten serializer for the specific selection of data structure and data format.
126        "}
127        .trim();
128
129        assert_eq!(
130            convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
131            expected
132        )
133    }
134
135    #[test]
136    fn test_rust_code_block() {
137        let html = indoc! {r#"
138            <pre class="rust rust-example-rendered"><code><span class="kw">use </span>axum::extract::{Path, Query, Json};
139            <span class="kw">use </span>std::collections::HashMap;
140
141            <span class="comment">// `Path` gives you the path parameters and deserializes them.
142            </span><span class="kw">async fn </span>path(Path(user_id): Path&lt;u32&gt;) {}
143
144            <span class="comment">// `Query` gives you the query parameters and deserializes them.
145            </span><span class="kw">async fn </span>query(Query(params): Query&lt;HashMap&lt;String, String&gt;&gt;) {}
146
147            <span class="comment">// Buffer the request body and deserialize it as JSON into a
148            // `serde_json::Value`. `Json` supports any type that implements
149            // `serde::Deserialize`.
150            </span><span class="kw">async fn </span>json(Json(payload): Json&lt;serde_json::Value&gt;) {}</code></pre>
151        "#};
152        let expected = indoc! {"
153            ```rs
154            use axum::extract::{Path, Query, Json};
155            use std::collections::HashMap;
156
157            // `Path` gives you the path parameters and deserializes them.
158            async fn path(Path(user_id): Path<u32>) {}
159
160            // `Query` gives you the query parameters and deserializes them.
161            async fn query(Query(params): Query<HashMap<String, String>>) {}
162
163            // Buffer the request body and deserialize it as JSON into a
164            // `serde_json::Value`. `Json` supports any type that implements
165            // `serde::Deserialize`.
166            async fn json(Json(payload): Json<serde_json::Value>) {}
167            ```
168        "}
169        .trim();
170
171        assert_eq!(
172            convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
173            expected
174        )
175    }
176
177    #[test]
178    fn test_toml_code_block() {
179        let html = indoc! {r##"
180            <h2 id="required-dependencies"><a class="doc-anchor" href="#required-dependencies">§</a>Required dependencies</h2>
181            <p>To use axum there are a few dependencies you have to pull in as well:</p>
182            <div class="example-wrap"><pre class="language-toml"><code>[dependencies]
183            axum = &quot;&lt;latest-version&gt;&quot;
184            tokio = { version = &quot;&lt;latest-version&gt;&quot;, features = [&quot;full&quot;] }
185            tower = &quot;&lt;latest-version&gt;&quot;
186            </code></pre></div>
187        "##};
188        let expected = indoc! {r#"
189            ## Required dependencies
190
191            To use axum there are a few dependencies you have to pull in as well:
192
193            ```toml
194            [dependencies]
195            axum = "<latest-version>"
196            tokio = { version = "<latest-version>", features = ["full"] }
197            tower = "<latest-version>"
198
199            ```
200        "#}
201        .trim();
202
203        assert_eq!(
204            convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
205            expected
206        )
207    }
208
209    #[test]
210    fn test_item_table() {
211        let html = indoc! {r##"
212            <h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2>
213            <ul class="item-table">
214            <li><div class="item-name"><a class="struct" href="struct.Error.html" title="struct axum::Error">Error</a></div><div class="desc docblock-short">Errors that can happen when using axum.</div></li>
215            <li><div class="item-name"><a class="struct" href="struct.Extension.html" title="struct axum::Extension">Extension</a></div><div class="desc docblock-short">Extractor and response for extensions.</div></li>
216            <li><div class="item-name"><a class="struct" href="struct.Form.html" title="struct axum::Form">Form</a><span class="stab portability" title="Available on crate feature `form` only"><code>form</code></span></div><div class="desc docblock-short">URL encoded extractor and response.</div></li>
217            <li><div class="item-name"><a class="struct" href="struct.Json.html" title="struct axum::Json">Json</a><span class="stab portability" title="Available on crate feature `json` only"><code>json</code></span></div><div class="desc docblock-short">JSON Extractor / Response.</div></li>
218            <li><div class="item-name"><a class="struct" href="struct.Router.html" title="struct axum::Router">Router</a></div><div class="desc docblock-short">The router type for composing handlers and services.</div></li></ul>
219            <h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2>
220            <ul class="item-table">
221            <li><div class="item-name"><a class="fn" href="fn.serve.html" title="fn axum::serve">serve</a><span class="stab portability" title="Available on crate feature `tokio` and (crate features `http1` or `http2`) only"><code>tokio</code> and (<code>http1</code> or <code>http2</code>)</span></div><div class="desc docblock-short">Serve the service with the supplied listener.</div></li>
222            </ul>
223        "##};
224        let expected = indoc! {r#"
225            ## Structs
226
227            - `Error`: Errors that can happen when using axum.
228            - `Extension`: Extractor and response for extensions.
229            - `Form` [`form`]: URL encoded extractor and response.
230            - `Json` [`json`]: JSON Extractor / Response.
231            - `Router`: The router type for composing handlers and services.
232
233            ## Functions
234
235            - `serve` [`tokio` and (`http1` or `http2`)]: Serve the service with the supplied listener.
236        "#}
237        .trim();
238
239        assert_eq!(
240            convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
241            expected
242        )
243    }
244
245    #[test]
246    fn test_table() {
247        let html = indoc! {r##"
248            <h2 id="feature-flags"><a class="doc-anchor" href="#feature-flags">§</a>Feature flags</h2>
249            <p>axum uses a set of <a href="https://doc.rust-lang.org/cargo/reference/features.html#the-features-section">feature flags</a> to reduce the amount of compiled and
250            optional dependencies.</p>
251            <p>The following optional features are available:</p>
252            <div><table><thead><tr><th>Name</th><th>Description</th><th>Default?</th></tr></thead><tbody>
253            <tr><td><code>http1</code></td><td>Enables hyper’s <code>http1</code> feature</td><td>Yes</td></tr>
254            <tr><td><code>http2</code></td><td>Enables hyper’s <code>http2</code> feature</td><td>No</td></tr>
255            <tr><td><code>json</code></td><td>Enables the <a href="struct.Json.html" title="struct axum::Json"><code>Json</code></a> type and some similar convenience functionality</td><td>Yes</td></tr>
256            <tr><td><code>macros</code></td><td>Enables optional utility macros</td><td>No</td></tr>
257            <tr><td><code>matched-path</code></td><td>Enables capturing of every request’s router path and the <a href="extract/struct.MatchedPath.html" title="struct axum::extract::MatchedPath"><code>MatchedPath</code></a> extractor</td><td>Yes</td></tr>
258            <tr><td><code>multipart</code></td><td>Enables parsing <code>multipart/form-data</code> requests with <a href="extract/struct.Multipart.html" title="struct axum::extract::Multipart"><code>Multipart</code></a></td><td>No</td></tr>
259            <tr><td><code>original-uri</code></td><td>Enables capturing of every request’s original URI and the <a href="extract/struct.OriginalUri.html" title="struct axum::extract::OriginalUri"><code>OriginalUri</code></a> extractor</td><td>Yes</td></tr>
260            <tr><td><code>tokio</code></td><td>Enables <code>tokio</code> as a dependency and <code>axum::serve</code>, <code>SSE</code> and <code>extract::connect_info</code> types.</td><td>Yes</td></tr>
261            <tr><td><code>tower-log</code></td><td>Enables <code>tower</code>’s <code>log</code> feature</td><td>Yes</td></tr>
262            <tr><td><code>tracing</code></td><td>Log rejections from built-in extractors</td><td>Yes</td></tr>
263            <tr><td><code>ws</code></td><td>Enables WebSockets support via <a href="extract/ws/index.html" title="mod axum::extract::ws"><code>extract::ws</code></a></td><td>No</td></tr>
264            <tr><td><code>form</code></td><td>Enables the <code>Form</code> extractor</td><td>Yes</td></tr>
265            <tr><td><code>query</code></td><td>Enables the <code>Query</code> extractor</td><td>Yes</td></tr>
266            </tbody></table>
267        "##};
268        let expected = indoc! {r#"
269            ## Feature flags
270
271            axum uses a set of feature flags to reduce the amount of compiled and optional dependencies.
272
273            The following optional features are available:
274
275            | Name | Description | Default? |
276            | --- | --- | --- |
277            | `http1` | Enables hyper’s `http1` feature | Yes |
278            | `http2` | Enables hyper’s `http2` feature | No |
279            | `json` | Enables the `Json` type and some similar convenience functionality | Yes |
280            | `macros` | Enables optional utility macros | No |
281            | `matched-path` | Enables capturing of every request’s router path and the `MatchedPath` extractor | Yes |
282            | `multipart` | Enables parsing `multipart/form-data` requests with `Multipart` | No |
283            | `original-uri` | Enables capturing of every request’s original URI and the `OriginalUri` extractor | Yes |
284            | `tokio` | Enables `tokio` as a dependency and `axum::serve`, `SSE` and `extract::connect_info` types. | Yes |
285            | `tower-log` | Enables `tower`’s `log` feature | Yes |
286            | `tracing` | Log rejections from built-in extractors | Yes |
287            | `ws` | Enables WebSockets support via `extract::ws` | No |
288            | `form` | Enables the `Form` extractor | Yes |
289            | `query` | Enables the `Query` extractor | Yes |
290        "#}
291        .trim();
292
293        assert_eq!(
294            convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
295            expected
296        )
297    }
298}