Skip to main content

rustdoc/
markdown.rs

1//! Standalone markdown rendering.
2//!
3//! For the (much more common) case of rendering markdown in doc-comments, see
4//! [crate::html::markdown].
5//!
6//! This is used when [rendering a markdown file to an html file][docs], without processing
7//! rust source code.
8//!
9//! [docs]: https://doc.rust-lang.org/stable/rustdoc/#using-standalone-markdown-files
10
11use std::fmt::{self, Write as _};
12use std::fs::{File, create_dir_all};
13use std::io::prelude::*;
14use std::path::PathBuf;
15use std::sync::Arc;
16
17use rustc_span::SourceFile;
18use rustc_span::edition::Edition;
19
20use crate::config::RenderOptions;
21use crate::html::escape::Escape;
22use crate::html::markdown;
23use crate::html::markdown::{ErrorCodes, HeadingOffset, IdMap, Markdown, MarkdownWithToc};
24
25/// Separate any lines at the start of the file that begin with `# ` or `%`.
26fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
27    let mut metadata = Vec::new();
28    let mut count = 0;
29
30    for line in s.lines() {
31        if line.starts_with("# ") || line.starts_with('%') {
32            // trim the whitespace after the symbol
33            metadata.push(line[1..].trim_start());
34            count += line.len() + 1;
35        } else {
36            return (metadata, &s[count..]);
37        }
38    }
39
40    // if we're here, then all lines were metadata `# ` or `%` lines.
41    (metadata, "")
42}
43
44/// Render `input` (e.g., "foo.md") into an HTML file in `output`
45/// (e.g., output = "bar" => "bar/foo.html").
46///
47/// Requires session globals to be available, for symbol interning.
48pub(crate) fn render_and_write(
49    input: Arc<SourceFile>,
50    options: RenderOptions,
51    edition: Edition,
52) -> Result<(), String> {
53    if let Err(e) = create_dir_all(&options.output) {
54        return Err(format!("{output}: {e}", output = options.output.display()));
55    }
56
57    let input_path = input.name.clone().into_local_path().unwrap_or(PathBuf::new());
58    let mut output = options.output;
59    output.push(input_path.file_name().unwrap());
60    output.set_extension("html");
61
62    let mut css = String::new();
63    for name in &options.markdown_css {
64        write!(css, r#"<link rel="stylesheet" href="{name}">"#)
65            .expect("Writing to a String can't fail");
66    }
67
68    let input_str = input.src.as_ref().map(|src| &src[..]).unwrap_or("");
69    let playground_url = options.markdown_playground_url.or(options.playground_url);
70    let playground = playground_url.map(|url| markdown::Playground { crate_name: None, url });
71
72    let mut out =
73        File::create(&output).map_err(|e| format!("{output}: {e}", output = output.display()))?;
74
75    let (metadata, text) = extract_leading_metadata(&input_str);
76    if metadata.is_empty() {
77        return Err("invalid markdown file: no initial lines starting with `# ` or `%`".to_owned());
78    }
79    let title = metadata[0];
80
81    let error_codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
82    let text = fmt::from_fn(|f| {
83        if !options.markdown_no_toc {
84            MarkdownWithToc {
85                content: text,
86                links: &[],
87                ids: &mut IdMap::new(),
88                error_codes,
89                edition,
90                playground: &playground,
91            }
92            .write_into(f)
93        } else {
94            Markdown {
95                content: text,
96                links: &[],
97                ids: &mut IdMap::new(),
98                error_codes,
99                edition,
100                playground: &playground,
101                heading_offset: HeadingOffset::H1,
102            }
103            .write_into(f)
104        }
105    });
106
107    let res = write!(
108        &mut out,
109        r#"<!DOCTYPE html>
110<html lang="en">
111<head>
112    <meta charset="utf-8">
113    <meta name="viewport" content="width=device-width, initial-scale=1.0">
114    <meta name="generator" content="rustdoc">
115    <title>{title}</title>
116
117    {css}
118    {in_header}
119</head>
120<body class="rustdoc">
121    <!--[if lte IE 8]>
122    <div class="warning">
123        This old browser is unsupported and will most likely display funky
124        things.
125    </div>
126    <![endif]-->
127
128    {before_content}
129    <h1 class="title">{title}</h1>
130    {text}
131    {after_content}
132</body>
133</html>"#,
134        title = Escape(title),
135        in_header = options.external_html.in_header,
136        before_content = options.external_html.before_content,
137        after_content = options.external_html.after_content,
138    );
139
140    res.map_err(|e| format!("cannot write to `{output}`: {e}", output = output.display()))
141}