2 use std::io::prelude::*;
3 use std::path::PathBuf;
6 use rustc_feature::UnstableFeatures;
7 use syntax::edition::Edition;
8 use syntax::source_map::DUMMY_SP;
11 use crate::config::{Options, RenderOptions};
12 use crate::externalfiles::{load_string, LoadStringError};
13 use crate::html::escape::Escape;
14 use crate::html::markdown;
15 use crate::html::markdown::{find_testable_code, ErrorCodes, IdMap, Markdown, MarkdownWithToc};
16 use crate::test::{Collector, TestOptions};
18 /// Separate any lines at the start of the file that begin with `# ` or `%`.
19 fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
20 let mut metadata = Vec::new();
23 for line in s.lines() {
24 if line.starts_with("# ") || line.starts_with("%") {
25 // trim the whitespace after the symbol
26 metadata.push(line[1..].trim_start());
27 count += line.len() + 1;
29 return (metadata, &s[count..]);
33 // if we're here, then all lines were metadata `# ` or `%` lines.
37 /// Render `input` (e.g., "foo.md") into an HTML file in `output`
38 /// (e.g., output = "bar" => "bar/foo.html").
41 options: RenderOptions,
42 diag: &errors::Handler,
45 let mut output = options.output;
46 output.push(input.file_name().unwrap());
47 output.set_extension("html");
49 let mut css = String::new();
50 for name in &options.markdown_css {
51 let s = format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{}\">\n", name);
55 let input_str = match load_string(&input, diag) {
57 Err(LoadStringError::ReadFail) => return 1,
58 Err(LoadStringError::BadUtf8) => return 2,
60 let playground_url = options.markdown_playground_url.or(options.playground_url);
61 let playground = playground_url.map(|url| markdown::Playground { crate_name: None, url });
63 let mut out = match File::create(&output) {
65 diag.struct_err(&format!("{}: {}", output.display(), e)).emit();
71 let (metadata, text) = extract_leading_metadata(&input_str);
72 if metadata.is_empty() {
73 diag.struct_err("invalid markdown file: no initial lines starting with `# ` or `%`").emit();
76 let title = metadata[0];
78 let mut ids = IdMap::new();
79 let error_codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
80 let text = if !options.markdown_no_toc {
81 MarkdownWithToc(text, &mut ids, error_codes, edition, &playground).to_string()
83 Markdown(text, &[], &mut ids, error_codes, edition, &playground).to_string()
91 <meta charset="utf-8">
92 <meta name="viewport" content="width=device-width, initial-scale=1.0">
93 <meta name="generator" content="rustdoc">
94 <title>{title}</title>
99 <body class="rustdoc">
101 <div class="warning">
102 This old browser is unsupported and will most likely display funky
108 <h1 class="title">{title}</h1>
113 title = Escape(title),
115 in_header = options.external_html.in_header,
116 before_content = options.external_html.before_content,
118 after_content = options.external_html.after_content,
123 diag.struct_err(&format!("cannot write to `{}`: {}", output.display(), e)).emit();
130 /// Runs any tests/code examples in the markdown file `input`.
131 pub fn test(mut options: Options, diag: &errors::Handler) -> i32 {
132 let input_str = match load_string(&options.input, diag) {
134 Err(LoadStringError::ReadFail) => return 1,
135 Err(LoadStringError::BadUtf8) => return 2,
138 let mut opts = TestOptions::default();
139 opts.no_crate_inject = true;
140 opts.display_warnings = options.display_warnings;
141 let mut collector = Collector::new(
142 options.input.display().to_string(),
148 options.enable_per_target_ignores,
150 collector.set_position(DUMMY_SP);
151 let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
153 find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores);
155 options.test_args.insert(0, "rustdoctest".to_string());
159 Some(testing::Options::new().display_output(options.display_warnings)),