]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
9346a6ac | 11 | use std::default::Default; |
c34b1796 | 12 | use std::fs::File; |
c34b1796 | 13 | use std::io::prelude::*; |
9346a6ac | 14 | use std::io; |
c34b1796 | 15 | use std::path::{PathBuf, Path}; |
1a4d82fc JJ |
16 | |
17 | use core; | |
18 | use getopts; | |
19 | use testing; | |
20 | use rustc::session::search_paths::SearchPaths; | |
21 | ||
22 | use externalfiles::ExternalHtml; | |
23 | ||
24 | use html::escape::Escape; | |
25 | use html::markdown; | |
26 | use html::markdown::{Markdown, MarkdownWithToc, find_testable_code, reset_headers}; | |
9346a6ac | 27 | use test::{TestOptions, Collector}; |
1a4d82fc JJ |
28 | |
29 | /// Separate any lines at the start of the file that begin with `%`. | |
30 | fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) { | |
31 | let mut metadata = Vec::new(); | |
c1a9b12d | 32 | let mut count = 0; |
1a4d82fc JJ |
33 | for line in s.lines() { |
34 | if line.starts_with("%") { | |
35 | // remove %<whitespace> | |
c1a9b12d SL |
36 | metadata.push(line[1..].trim_left()); |
37 | count += line.len() + 1; | |
1a4d82fc | 38 | } else { |
c1a9b12d | 39 | return (metadata, &s[count..]); |
1a4d82fc JJ |
40 | } |
41 | } | |
42 | // if we're here, then all lines were metadata % lines. | |
43 | (metadata, "") | |
44 | } | |
45 | ||
46 | /// Render `input` (e.g. "foo.md") into an HTML file in `output` | |
47 | /// (e.g. output = "bar" => "bar/foo.html"). | |
c34b1796 AL |
48 | pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches, |
49 | external_html: &ExternalHtml, include_toc: bool) -> isize { | |
1a4d82fc | 50 | let input_p = Path::new(input); |
c34b1796 | 51 | output.push(input_p.file_stem().unwrap()); |
1a4d82fc JJ |
52 | output.set_extension("html"); |
53 | ||
54 | let mut css = String::new(); | |
85aaf69f | 55 | for name in &matches.opt_strs("markdown-css") { |
1a4d82fc | 56 | let s = format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{}\">\n", name); |
85aaf69f | 57 | css.push_str(&s) |
1a4d82fc JJ |
58 | } |
59 | ||
60 | let input_str = load_or_return!(input, 1, 2); | |
61 | let playground = matches.opt_str("markdown-playground-url"); | |
62 | if playground.is_some() { | |
9346a6ac | 63 | markdown::PLAYGROUND_KRATE.with(|s| { *s.borrow_mut() = Some(None); }); |
1a4d82fc JJ |
64 | } |
65 | let playground = playground.unwrap_or("".to_string()); | |
66 | ||
c34b1796 | 67 | let mut out = match File::create(&output) { |
1a4d82fc | 68 | Err(e) => { |
c34b1796 | 69 | let _ = writeln!(&mut io::stderr(), |
1a4d82fc JJ |
70 | "error opening `{}` for writing: {}", |
71 | output.display(), e); | |
72 | return 4; | |
73 | } | |
74 | Ok(f) => f | |
75 | }; | |
76 | ||
85aaf69f | 77 | let (metadata, text) = extract_leading_metadata(&input_str); |
9346a6ac | 78 | if metadata.is_empty() { |
c34b1796 | 79 | let _ = writeln!(&mut io::stderr(), |
1a4d82fc JJ |
80 | "invalid markdown file: expecting initial line with `% ...TITLE...`"); |
81 | return 5; | |
82 | } | |
85aaf69f | 83 | let title = metadata[0]; |
1a4d82fc JJ |
84 | |
85 | reset_headers(); | |
86 | ||
87 | let rendered = if include_toc { | |
88 | format!("{}", MarkdownWithToc(text)) | |
89 | } else { | |
90 | format!("{}", Markdown(text)) | |
91 | }; | |
92 | ||
93 | let err = write!( | |
94 | &mut out, | |
95 | r#"<!DOCTYPE html> | |
96 | <html lang="en"> | |
97 | <head> | |
98 | <meta charset="utf-8"> | |
85aaf69f | 99 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
1a4d82fc JJ |
100 | <meta name="generator" content="rustdoc"> |
101 | <title>{title}</title> | |
102 | ||
103 | {css} | |
104 | {in_header} | |
105 | </head> | |
106 | <body class="rustdoc"> | |
107 | <!--[if lte IE 8]> | |
108 | <div class="warning"> | |
109 | This old browser is unsupported and will most likely display funky | |
110 | things. | |
111 | </div> | |
112 | <![endif]--> | |
113 | ||
114 | {before_content} | |
115 | <h1 class="title">{title}</h1> | |
116 | {text} | |
117 | <script type="text/javascript"> | |
118 | window.playgroundUrl = "{playground}"; | |
119 | </script> | |
120 | {after_content} | |
121 | </body> | |
122 | </html>"#, | |
123 | title = Escape(title), | |
124 | css = css, | |
125 | in_header = external_html.in_header, | |
126 | before_content = external_html.before_content, | |
127 | text = rendered, | |
128 | after_content = external_html.after_content, | |
129 | playground = playground, | |
130 | ); | |
131 | ||
132 | match err { | |
133 | Err(e) => { | |
c34b1796 | 134 | let _ = writeln!(&mut io::stderr(), |
1a4d82fc JJ |
135 | "error writing to `{}`: {}", |
136 | output.display(), e); | |
137 | 6 | |
138 | } | |
139 | Ok(_) => 0 | |
140 | } | |
141 | } | |
142 | ||
143 | /// Run any tests/code examples in the markdown file `input`. | |
144 | pub fn test(input: &str, libs: SearchPaths, externs: core::Externs, | |
c34b1796 | 145 | mut test_args: Vec<String>) -> isize { |
1a4d82fc JJ |
146 | let input_str = load_or_return!(input, 1, 2); |
147 | ||
9346a6ac AL |
148 | let mut opts = TestOptions::default(); |
149 | opts.no_crate_inject = true; | |
150 | let mut collector = Collector::new(input.to_string(), libs, externs, | |
151 | true, opts); | |
85aaf69f | 152 | find_testable_code(&input_str, &mut collector); |
1a4d82fc | 153 | test_args.insert(0, "rustdoctest".to_string()); |
85aaf69f | 154 | testing::test_main(&test_args, collector.tests); |
1a4d82fc JJ |
155 | 0 |
156 | } |