2014-03-07 14:31:41 +11: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 18:39:39 -07:00
|
|
|
use std::default::Default;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::fs::File;
|
2015-03-11 15:24:14 -07:00
|
|
|
use std::io::prelude::*;
|
2015-04-06 18:39:39 -07:00
|
|
|
use std::io;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::path::{PathBuf, Path};
|
2014-03-07 14:31:41 +11:00
|
|
|
|
|
|
|
use getopts;
|
|
|
|
use testing;
|
2014-12-16 14:32:02 -08:00
|
|
|
use rustc::session::search_paths::SearchPaths;
|
2016-08-02 16:53:58 -04:00
|
|
|
use rustc::session::config::Externs;
|
2017-02-06 22:11:03 +01:00
|
|
|
use syntax::codemap::DUMMY_SP;
|
2014-03-07 14:31:41 +11:00
|
|
|
|
2016-10-08 22:55:40 -04:00
|
|
|
use externalfiles::{ExternalHtml, LoadStringError, load_string};
|
2014-06-28 03:35:25 -07:00
|
|
|
|
2015-12-03 00:06:26 +01:00
|
|
|
use html::render::reset_ids;
|
2014-03-07 14:31:41 +11:00
|
|
|
use html::escape::Escape;
|
2014-06-06 09:12:18 -07:00
|
|
|
use html::markdown;
|
2017-04-14 01:23:14 +02:00
|
|
|
use html::markdown::{Markdown, MarkdownWithToc, find_testable_code, old_find_testable_code};
|
2015-04-06 18:39:39 -07:00
|
|
|
use test::{TestOptions, Collector};
|
2014-03-07 14:31:41 +11:00
|
|
|
|
2017-03-25 15:33:44 -04:00
|
|
|
/// Separate any lines at the start of the file that begin with `# ` or `%`.
|
2014-03-07 14:31:41 +11:00
|
|
|
fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) {
|
|
|
|
let mut metadata = Vec::new();
|
2015-07-30 15:29:34 -07:00
|
|
|
let mut count = 0;
|
2017-03-25 15:33:44 -04:00
|
|
|
|
2014-03-07 14:31:41 +11:00
|
|
|
for line in s.lines() {
|
2017-03-25 15:33:44 -04:00
|
|
|
if line.starts_with("# ") || line.starts_with("%") {
|
|
|
|
// trim the whitespace after the symbol
|
2015-07-30 15:29:34 -07:00
|
|
|
metadata.push(line[1..].trim_left());
|
|
|
|
count += line.len() + 1;
|
2014-03-07 14:31:41 +11:00
|
|
|
} else {
|
2015-07-30 15:29:34 -07:00
|
|
|
return (metadata, &s[count..]);
|
2014-03-07 14:31:41 +11:00
|
|
|
}
|
|
|
|
}
|
2017-03-25 15:33:44 -04:00
|
|
|
|
|
|
|
// if we're here, then all lines were metadata `# ` or `%` lines.
|
2014-03-07 14:31:41 +11:00
|
|
|
(metadata, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Render `input` (e.g. "foo.md") into an HTML file in `output`
|
|
|
|
/// (e.g. output = "bar" => "bar/foo.html").
|
2015-02-26 21:00:43 -08:00
|
|
|
pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
|
2015-03-25 17:06:52 -07:00
|
|
|
external_html: &ExternalHtml, include_toc: bool) -> isize {
|
2014-03-07 14:31:41 +11:00
|
|
|
let input_p = Path::new(input);
|
2015-02-26 21:00:43 -08:00
|
|
|
output.push(input_p.file_stem().unwrap());
|
2014-03-07 14:31:41 +11:00
|
|
|
output.set_extension("html");
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
let mut css = String::new();
|
2015-01-31 12:20:46 -05:00
|
|
|
for name in &matches.opt_strs("markdown-css") {
|
2014-03-07 14:31:41 +11:00
|
|
|
let s = format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{}\">\n", name);
|
2015-02-01 21:53:25 -05:00
|
|
|
css.push_str(&s)
|
2014-03-07 14:31:41 +11:00
|
|
|
}
|
|
|
|
|
2016-10-08 22:55:40 -04:00
|
|
|
let input_str = match load_string(input) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(LoadStringError::ReadFail) => return 1,
|
|
|
|
Err(LoadStringError::BadUtf8) => return 2,
|
|
|
|
};
|
2016-11-30 10:25:08 +08:00
|
|
|
if let Some(playground) = matches.opt_str("markdown-playground-url").or(
|
|
|
|
matches.opt_str("playground-url")) {
|
2016-10-11 09:56:30 +01:00
|
|
|
markdown::PLAYGROUND.with(|s| { *s.borrow_mut() = Some((None, playground)); });
|
2014-06-06 09:12:18 -07:00
|
|
|
}
|
2014-03-07 14:31:41 +11:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
let mut out = match File::create(&output) {
|
2014-03-07 14:31:41 +11:00
|
|
|
Err(e) => {
|
2015-03-11 15:24:14 -07:00
|
|
|
let _ = writeln!(&mut io::stderr(),
|
2016-12-05 03:21:08 +01:00
|
|
|
"rustdoc: {}: {}",
|
2014-03-07 14:31:41 +11:00
|
|
|
output.display(), e);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
Ok(f) => f
|
|
|
|
};
|
|
|
|
|
2015-02-01 21:53:25 -05:00
|
|
|
let (metadata, text) = extract_leading_metadata(&input_str);
|
2015-03-24 16:53:34 -07:00
|
|
|
if metadata.is_empty() {
|
2016-12-05 03:21:08 +01:00
|
|
|
let _ = writeln!(
|
|
|
|
&mut io::stderr(),
|
2017-03-25 15:33:44 -04:00
|
|
|
"rustdoc: invalid markdown file: no initial lines starting with `# ` or `%`"
|
2016-12-05 03:21:08 +01:00
|
|
|
);
|
2014-03-07 14:31:41 +11:00
|
|
|
return 5;
|
|
|
|
}
|
2015-02-01 21:53:25 -05:00
|
|
|
let title = metadata[0];
|
2014-03-07 14:31:41 +11:00
|
|
|
|
2016-03-26 16:42:38 +01:00
|
|
|
reset_ids(false);
|
2014-03-07 14:31:41 +11:00
|
|
|
|
2014-07-23 15:43:03 -07:00
|
|
|
let rendered = if include_toc {
|
|
|
|
format!("{}", MarkdownWithToc(text))
|
|
|
|
} else {
|
2017-04-06 13:09:20 +01:00
|
|
|
format!("{}", Markdown(text))
|
2014-07-23 15:43:03 -07:00
|
|
|
};
|
|
|
|
|
2014-03-07 14:31:41 +11:00
|
|
|
let err = write!(
|
|
|
|
&mut out,
|
2014-03-09 12:29:25 +01:00
|
|
|
r#"<!DOCTYPE html>
|
2014-03-07 14:31:41 +11:00
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
2015-01-12 16:51:31 -08:00
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
2014-03-07 14:31:41 +11:00
|
|
|
<meta name="generator" content="rustdoc">
|
|
|
|
<title>{title}</title>
|
|
|
|
|
|
|
|
{css}
|
|
|
|
{in_header}
|
|
|
|
</head>
|
2014-08-02 18:20:27 -07:00
|
|
|
<body class="rustdoc">
|
2014-03-07 14:31:41 +11: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}
|
|
|
|
{after_content}
|
|
|
|
</body>
|
|
|
|
</html>"#,
|
|
|
|
title = Escape(title),
|
|
|
|
css = css,
|
2014-06-28 03:35:25 -07:00
|
|
|
in_header = external_html.in_header,
|
|
|
|
before_content = external_html.before_content,
|
2014-07-23 15:43:03 -07:00
|
|
|
text = rendered,
|
2014-06-28 03:35:25 -07:00
|
|
|
after_content = external_html.after_content,
|
2014-06-06 09:12:18 -07:00
|
|
|
);
|
2014-03-07 14:31:41 +11:00
|
|
|
|
|
|
|
match err {
|
|
|
|
Err(e) => {
|
2015-03-11 15:24:14 -07:00
|
|
|
let _ = writeln!(&mut io::stderr(),
|
2016-12-05 03:21:08 +01:00
|
|
|
"rustdoc: cannot write to `{}`: {}",
|
2014-03-07 14:31:41 +11:00
|
|
|
output.display(), e);
|
|
|
|
6
|
|
|
|
}
|
|
|
|
Ok(_) => 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run any tests/code examples in the markdown file `input`.
|
2016-08-02 16:53:58 -04:00
|
|
|
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
|
2016-12-24 16:04:48 +00:00
|
|
|
mut test_args: Vec<String>, maybe_sysroot: Option<PathBuf>) -> isize {
|
2016-10-08 22:55:40 -04:00
|
|
|
let input_str = match load_string(input) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(LoadStringError::ReadFail) => return 1,
|
|
|
|
Err(LoadStringError::BadUtf8) => return 2,
|
|
|
|
};
|
2014-03-07 14:31:41 +11:00
|
|
|
|
2015-04-06 18:39:39 -07:00
|
|
|
let mut opts = TestOptions::default();
|
|
|
|
opts.no_crate_inject = true;
|
2015-12-15 18:03:55 +09:00
|
|
|
let mut collector = Collector::new(input.to_string(), cfgs, libs, externs,
|
2017-02-13 18:11:20 +01:00
|
|
|
true, opts, maybe_sysroot, None,
|
|
|
|
Some(input.to_owned()));
|
2017-04-14 01:23:14 +02:00
|
|
|
old_find_testable_code(&input_str, &mut collector, DUMMY_SP);
|
2017-02-06 22:11:03 +01:00
|
|
|
find_testable_code(&input_str, &mut collector, DUMMY_SP);
|
2014-07-14 13:54:10 -07:00
|
|
|
test_args.insert(0, "rustdoctest".to_string());
|
2015-02-01 21:53:25 -05:00
|
|
|
testing::test_main(&test_args, collector.tests);
|
2014-03-07 14:31:41 +11:00
|
|
|
0
|
|
|
|
}
|