1 use std::default::Default;
3 use std::io::prelude::*;
4 use std::path::PathBuf;
5 use std::cell::RefCell;
9 use syntax::source_map::DUMMY_SP;
10 use syntax::feature_gate::UnstableFeatures;
12 use externalfiles::{LoadStringError, load_string};
14 use config::{Options, RenderOptions};
15 use html::escape::Escape;
17 use html::markdown::{ErrorCodes, IdMap, Markdown, MarkdownWithToc, find_testable_code};
18 use test::{TestOptions, Collector};
20 /// Separate any lines at the start of the file that begin with `# ` or `%`.
21 fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) {
22 let mut metadata = Vec::new();
25 for line in s.lines() {
26 if line.starts_with("# ") || line.starts_with("%") {
27 // trim the whitespace after the symbol
28 metadata.push(line[1..].trim_start());
29 count += line.len() + 1;
31 return (metadata, &s[count..]);
35 // if we're here, then all lines were metadata `# ` or `%` lines.
39 /// Render `input` (e.g., "foo.md") into an HTML file in `output`
40 /// (e.g., output = "bar" => "bar/foo.html").
41 pub fn render(input: PathBuf, options: RenderOptions, diag: &errors::Handler) -> isize {
42 let mut output = options.output;
43 output.push(input.file_stem().unwrap());
44 output.set_extension("html");
46 let mut css = String::new();
47 for name in &options.markdown_css {
48 let s = format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{}\">\n", name);
52 let input_str = match load_string(&input, diag) {
54 Err(LoadStringError::ReadFail) => return 1,
55 Err(LoadStringError::BadUtf8) => return 2,
57 let playground_url = options.markdown_playground_url
58 .or(options.playground_url);
59 if let Some(playground) = playground_url {
60 markdown::PLAYGROUND.with(|s| { *s.borrow_mut() = Some((None, playground)); });
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, RefCell::new(&mut ids), error_codes).to_string()
83 Markdown(text, &[], RefCell::new(&mut ids), error_codes).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 /// Run any tests/code examples in the markdown file `input`.
131 pub fn test(mut options: Options, diag: &errors::Handler) -> isize {
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(options.input.display().to_string(), options.cfgs,
142 options.libs, options.codegen_options, options.externs,
143 true, opts, options.maybe_sysroot, None,
145 options.linker, options.edition, options.persist_doctests);
146 collector.set_position(DUMMY_SP);
147 let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
148 let res = find_testable_code(&input_str, &mut collector, codes);
149 if let Err(err) = res {
150 diag.span_warn(DUMMY_SP, &err.to_string());
152 options.test_args.insert(0, "rustdoctest".to_string());
153 testing::test_main(&options.test_args, collector.tests,
154 testing::Options::new().display_output(options.display_warnings));