Debian Codegraph @

crates/mdbook-html/src/html/mod.rs

raw
1 //! HTML rendering support.
2 //!
3 //! This module's primary entry point is [`render_markdown`] which will take
4 //! markdown text and render it to HTML. In summary, the general procedure of
5 //! that function is:
6 //!
7 //! 1. Use [`pulldown_cmark`] to parse the markdown and generate events.
8 //! 2. [`tree`] converts those events to a tree data structure.
9 //! 1. Parse HTML inside the markdown using [`tokenizer`].
10 //! 2. Apply various transformations to the tree data structure, such as adding header links.
11 //! 3. Serialize the tree to HTML in [`serialize()`].
12
13 use ego_tree::Tree;
14 use mdbook_core::book::{Book, Chapter};
15 use mdbook_core::config::{HtmlConfig, RustEdition};
16 use mdbook_markdown::{MarkdownOptions, new_cmark_parser};
17 use std::path::{Path, PathBuf};
18
19 mod admonitions;
20 mod hide_lines;
21 mod print;
22 mod serialize;
23 #[cfg(test)]
24 mod tests;
25 mod tokenizer;
26 mod tree;
27
28 pub(crate) use hide_lines::{hide_lines, wrap_rust_main};
29 pub(crate) use print::render_print_page;
30 pub(crate) use serialize::serialize;
31 pub(crate) use tree::{Element, Node};
32
33 /// Options for converting a single chapter's markdown to HTML.
34 pub(crate) struct HtmlRenderOptions<'a> {
35 /// Options for parsing markdown.
36 pub markdown_options: MarkdownOptions,
37 /// The chapter's location, relative to the `SUMMARY.md` file.
38 pub path: &'a Path,
39 /// The default Rust edition, used to set the proper class on the code blocks.
40 pub edition: Option<RustEdition>,
41 /// The [`HtmlConfig`], whose options affect how the HTML is generated.
42 pub config: &'a HtmlConfig,
43 }
44
45 impl<'a> HtmlRenderOptions<'a> {
46 /// Creates a new [`HtmlRenderOptions`].
47 pub(crate) fn new(
48 path: &'a Path,
49 config: &'a HtmlConfig,
50 edition: Option<RustEdition>,
51 ) -> HtmlRenderOptions<'a> {
52 let mut markdown_options = MarkdownOptions::default();
53 markdown_options.smart_punctuation = config.smart_punctuation;
54 markdown_options.definition_lists = config.definition_lists;
55 markdown_options.admonitions = config.admonitions;
56 HtmlRenderOptions {
57 markdown_options,
58 path,
59 edition,
60 config,
61 }
62 }
63 }
64
65 /// Renders markdown to HTML.
66 pub(crate) fn render_markdown(text: &str, options: &HtmlRenderOptions<'_>) -> String {
67 let tree = build_tree(text, options);
68 let mut output = String::new();
69 serialize::serialize(&tree, &mut output);
70 output
71 }
72
73 /// Renders markdown to a [`Tree`].
74 fn build_tree(text: &str, options: &HtmlRenderOptions<'_>) -> Tree<Node> {
75 let events = new_cmark_parser(text, &options.markdown_options);
76 tree::MarkdownTreeBuilder::build(options, events)
77 }
78
79 /// The parsed chapter, and some information about the chapter.
80 pub(crate) struct ChapterTree<'book> {
81 pub(crate) chapter: &'book Chapter,
82 /// The path to the chapter relative to the root with the `.html` extension.
83 pub(crate) html_path: PathBuf,
84 /// The chapter tree.
85 pub(crate) tree: Tree<Node>,
86 }
87
88 /// Creates all of the [`ChapterTree`]s for the book.
89 pub(crate) fn build_trees<'book>(
90 book: &'book Book,
91 html_config: &HtmlConfig,
92 edition: Option<RustEdition>,
93 ) -> Vec<ChapterTree<'book>> {
94 book.chapters()
95 .map(|ch| {
96 let path = ch.path.as_ref().unwrap();
97 let html_path = ch.path.as_ref().unwrap().with_extension("html");
98 let options = HtmlRenderOptions::new(path, html_config, edition);
99 let tree = build_tree(&ch.content, &options);
100
101 ChapterTree {
102 chapter: ch,
103 html_path,
104 tree,
105 }
106 })
107 .collect()
108 }