2014-03-06 21:31:41 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-04-06 20:39:39 -05:00
|
|
|
use std::default::Default;
|
2015-02-26 23:00:43 -06:00
|
|
|
use std::fs::File;
|
2015-03-11 17:24:14 -05:00
|
|
|
use std::io::prelude::*;
|
2015-04-06 20:39:39 -05:00
|
|
|
use std::io;
|
2015-02-26 23:00:43 -06:00
|
|
|
use std::path::{PathBuf, Path};
|
2014-03-06 21:31:41 -06:00
|
|
|
|
|
|
|
use getopts;
|
|
|
|
use testing;
|
2014-12-16 16:32:02 -06:00
|
|
|
use rustc::session::search_paths::SearchPaths;
|
2016-08-02 15:53:58 -05:00
|
|
|
use rustc::session::config::Externs;
|
2014-03-06 21:31:41 -06:00
|
|
|
|
2014-06-28 05:35:25 -05:00
|
|
|
use externalfiles::ExternalHtml;
|
|
|
|
|
2015-12-02 17:06:26 -06:00
|
|
|
use html::render::reset_ids;
|
2014-03-06 21:31:41 -06:00
|
|
|
use html::escape::Escape;
|
2014-06-06 11:12:18 -05:00
|
|
|
use html::markdown;
|
2015-12-02 17:06:26 -06:00
|
|
|
use html::markdown::{Markdown, MarkdownWithToc, find_testable_code};
|
2015-04-06 20:39:39 -05:00
|
|
|
use test::{TestOptions, Collector};
|
2014-03-06 21:31:41 -06:00
|
|
|
|
|
|
|
/// Separate any lines at the start of the file that begin with `%`.
|
|
|
|
fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) {
|
|
|
|
let mut metadata = Vec::new();
|
2015-07-30 17:29:34 -05:00
|
|
|
let mut count = 0;
|
2014-03-06 21:31:41 -06:00
|
|
|
for line in s.lines() {
|
|
|
|
if line.starts_with("%") {
|
|
|
|
// remove %<whitespace>
|
2015-07-30 17:29:34 -05:00
|
|
|
metadata.push(line[1..].trim_left());
|
|
|
|
count += line.len() + 1;
|
2014-03-06 21:31:41 -06:00
|
|
|
} else {
|
2015-07-30 17:29:34 -05:00
|
|
|
return (metadata, &s[count..]);
|
2014-03-06 21:31:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we're here, then all lines were metadata % lines.
|
|
|
|
(metadata, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Render `input` (e.g. "foo.md") into an HTML file in `output`
|
|
|
|
/// (e.g. output = "bar" => "bar/foo.html").
|
2015-02-26 23:00:43 -06:00
|
|
|
pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
|
2015-03-25 19:06:52 -05:00
|
|
|
external_html: &ExternalHtml, include_toc: bool) -> isize {
|
2014-03-06 21:31:41 -06:00
|
|
|
let input_p = Path::new(input);
|
2015-02-26 23:00:43 -06:00
|
|
|
output.push(input_p.file_stem().unwrap());
|
2014-03-06 21:31:41 -06:00
|
|
|
output.set_extension("html");
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut css = String::new();
|
2015-01-31 11:20:46 -06:00
|
|
|
for name in &matches.opt_strs("markdown-css") {
|
2014-03-06 21:31:41 -06:00
|
|
|
let s = format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{}\">\n", name);
|
2015-02-01 20:53:25 -06:00
|
|
|
css.push_str(&s)
|
2014-03-06 21:31:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let input_str = load_or_return!(input, 1, 2);
|
2014-06-06 11:12:18 -05:00
|
|
|
let playground = matches.opt_str("markdown-playground-url");
|
|
|
|
if playground.is_some() {
|
2015-04-07 13:23:14 -05:00
|
|
|
markdown::PLAYGROUND_KRATE.with(|s| { *s.borrow_mut() = Some(None); });
|
2014-06-06 11:12:18 -05:00
|
|
|
}
|
|
|
|
let playground = playground.unwrap_or("".to_string());
|
2014-03-06 21:31:41 -06:00
|
|
|
|
2015-02-26 23:00:43 -06:00
|
|
|
let mut out = match File::create(&output) {
|
2014-03-06 21:31:41 -06:00
|
|
|
Err(e) => {
|
2015-03-11 17:24:14 -05:00
|
|
|
let _ = writeln!(&mut io::stderr(),
|
2014-03-06 21:31:41 -06:00
|
|
|
"error opening `{}` for writing: {}",
|
|
|
|
output.display(), e);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
Ok(f) => f
|
|
|
|
};
|
|
|
|
|
2015-02-01 20:53:25 -06:00
|
|
|
let (metadata, text) = extract_leading_metadata(&input_str);
|
2015-03-24 18:53:34 -05:00
|
|
|
if metadata.is_empty() {
|
2015-03-11 17:24:14 -05:00
|
|
|
let _ = writeln!(&mut io::stderr(),
|
2014-03-06 21:31:41 -06:00
|
|
|
"invalid markdown file: expecting initial line with `% ...TITLE...`");
|
|
|
|
return 5;
|
|
|
|
}
|
2015-02-01 20:53:25 -06:00
|
|
|
let title = metadata[0];
|
2014-03-06 21:31:41 -06:00
|
|
|
|
2016-03-26 10:42:38 -05:00
|
|
|
reset_ids(false);
|
2014-03-06 21:31:41 -06:00
|
|
|
|
2014-07-23 17:43:03 -05:00
|
|
|
let rendered = if include_toc {
|
|
|
|
format!("{}", MarkdownWithToc(text))
|
|
|
|
} else {
|
|
|
|
format!("{}", Markdown(text))
|
|
|
|
};
|
|
|
|
|
2014-03-06 21:31:41 -06:00
|
|
|
let err = write!(
|
|
|
|
&mut out,
|
2014-03-09 06:29:25 -05:00
|
|
|
r#"<!DOCTYPE html>
|
2014-03-06 21:31:41 -06:00
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
2015-01-12 18:51:31 -06:00
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
2014-03-06 21:31:41 -06:00
|
|
|
<meta name="generator" content="rustdoc">
|
|
|
|
<title>{title}</title>
|
|
|
|
|
|
|
|
{css}
|
|
|
|
{in_header}
|
|
|
|
</head>
|
2014-08-02 20:20:27 -05:00
|
|
|
<body class="rustdoc">
|
2014-03-06 21:31:41 -06:00
|
|
|
<!--[if lte IE 8]>
|
|
|
|
<div class="warning">
|
|
|
|
This old browser is unsupported and will most likely display funky
|
|
|
|
things.
|
|
|
|
</div>
|
|
|
|
<![endif]-->
|
|
|
|
|
|
|
|
{before_content}
|
|
|
|
<h1 class="title">{title}</h1>
|
|
|
|
{text}
|
2014-06-06 11:12:18 -05:00
|
|
|
<script type="text/javascript">
|
|
|
|
window.playgroundUrl = "{playground}";
|
|
|
|
</script>
|
2014-03-06 21:31:41 -06:00
|
|
|
{after_content}
|
|
|
|
</body>
|
|
|
|
</html>"#,
|
|
|
|
title = Escape(title),
|
|
|
|
css = css,
|
2014-06-28 05:35:25 -05:00
|
|
|
in_header = external_html.in_header,
|
|
|
|
before_content = external_html.before_content,
|
2014-07-23 17:43:03 -05:00
|
|
|
text = rendered,
|
2014-06-28 05:35:25 -05:00
|
|
|
after_content = external_html.after_content,
|
2014-06-06 11:12:18 -05:00
|
|
|
playground = playground,
|
|
|
|
);
|
2014-03-06 21:31:41 -06:00
|
|
|
|
|
|
|
match err {
|
|
|
|
Err(e) => {
|
2015-03-11 17:24:14 -05:00
|
|
|
let _ = writeln!(&mut io::stderr(),
|
2014-03-06 21:31:41 -06:00
|
|
|
"error writing to `{}`: {}",
|
|
|
|
output.display(), e);
|
|
|
|
6
|
|
|
|
}
|
|
|
|
Ok(_) => 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run any tests/code examples in the markdown file `input`.
|
2016-08-02 15:53:58 -05:00
|
|
|
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
|
2015-03-25 19:06:52 -05:00
|
|
|
mut test_args: Vec<String>) -> isize {
|
2014-03-06 21:31:41 -06:00
|
|
|
let input_str = load_or_return!(input, 1, 2);
|
|
|
|
|
2015-04-06 20:39:39 -05:00
|
|
|
let mut opts = TestOptions::default();
|
|
|
|
opts.no_crate_inject = true;
|
2015-12-15 03:03:55 -06:00
|
|
|
let mut collector = Collector::new(input.to_string(), cfgs, libs, externs,
|
2015-04-06 20:39:39 -05:00
|
|
|
true, opts);
|
2015-02-01 20:53:25 -06:00
|
|
|
find_testable_code(&input_str, &mut collector);
|
2014-07-14 15:54:10 -05:00
|
|
|
test_args.insert(0, "rustdoctest".to_string());
|
2015-02-01 20:53:25 -06:00
|
|
|
testing::test_main(&test_args, collector.tests);
|
2014-03-06 21:31:41 -06:00
|
|
|
0
|
|
|
|
}
|