1
Fork 0

Use markdown::render instead of using pulldown_cmark directly

This commit is contained in:
Guillaume Gomez 2018-09-29 16:40:37 +02:00
parent f7c8258037
commit a40b758cd8
5 changed files with 45 additions and 49 deletions

View file

@ -714,7 +714,7 @@ impl<'a> Builder<'a> {
"build" => self.cargo_out(compiler, mode, target), "build" => self.cargo_out(compiler, mode, target),
// This is the intended out directory for crate documentation. // This is the intended out directory for crate documentation.
"doc" => self.crate_doc_out(target), "doc" | "rustdoc" => self.crate_doc_out(target),
_ => self.stage_out(compiler, mode), _ => self.stage_out(compiler, mode),
}; };
@ -743,7 +743,7 @@ impl<'a> Builder<'a> {
_ => compile::librustc_stamp(self, cmp, target), _ => compile::librustc_stamp(self, cmp, target),
}; };
if cmd == "doc" { if cmd == "doc" || cmd == "rustdoc" {
if mode == Mode::Rustc || mode == Mode::ToolRustc || mode == Mode::Codegen { if mode == Mode::Rustc || mode == Mode::ToolRustc || mode == Mode::Codegen {
// This is the intended out directory for compiler documentation. // This is the intended out directory for compiler documentation.
my_out = self.compiler_doc_out(target); my_out = self.compiler_doc_out(target);
@ -883,7 +883,7 @@ impl<'a> Builder<'a> {
.env("RUSTDOC", self.out.join("bootstrap/debug/rustdoc")) .env("RUSTDOC", self.out.join("bootstrap/debug/rustdoc"))
.env( .env(
"RUSTDOC_REAL", "RUSTDOC_REAL",
if cmd == "doc" || (cmd == "test" && want_rustdoc) { if cmd == "doc" || cmd == "rustdoc" || (cmd == "test" && want_rustdoc) {
self.rustdoc(compiler.host) self.rustdoc(compiler.host)
} else { } else {
PathBuf::from("/path/to/nowhere/rustdoc/not/required") PathBuf::from("/path/to/nowhere/rustdoc/not/required")

View file

@ -405,6 +405,7 @@ impl Step for Standalone {
cmd.arg("--html-after-content").arg(&footer) cmd.arg("--html-after-content").arg(&footer)
.arg("--html-before-content").arg(&version_info) .arg("--html-before-content").arg(&version_info)
.arg("--html-in-header").arg(&favicon) .arg("--html-in-header").arg(&favicon)
.arg("--markdown-no-toc")
.arg("--index-page").arg(&builder.src.join("src/doc/index.md")) .arg("--index-page").arg(&builder.src.join("src/doc/index.md"))
.arg("--markdown-playground-url") .arg("--markdown-playground-url")
.arg("https://play.rust-lang.org/") .arg("https://play.rust-lang.org/")
@ -412,8 +413,7 @@ impl Step for Standalone {
.arg(&path); .arg(&path);
if filename == "not_found.md" { if filename == "not_found.md" {
cmd.arg("--markdown-no-toc") cmd.arg("--markdown-css")
.arg("--markdown-css")
.arg("https://doc.rust-lang.org/rust.css"); .arg("https://doc.rust-lang.org/rust.css");
} else { } else {
cmd.arg("--markdown-css").arg("rust.css"); cmd.arg("--markdown-css").arg("rust.css");
@ -481,6 +481,7 @@ impl Step for Std {
// will also directly handle merging. // will also directly handle merging.
let my_out = builder.crate_doc_out(target); let my_out = builder.crate_doc_out(target);
t!(symlink_dir_force(&builder.config, &my_out, &out_dir)); t!(symlink_dir_force(&builder.config, &my_out, &out_dir));
t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
let run_cargo_rustdoc_for = |package: &str| { let run_cargo_rustdoc_for = |package: &str| {
let mut cargo = builder.cargo(compiler, Mode::Std, target, "rustdoc"); let mut cargo = builder.cargo(compiler, Mode::Std, target, "rustdoc");
@ -495,7 +496,9 @@ impl Step for Std {
// FIXME: Cargo should probably do this itself. // FIXME: Cargo should probably do this itself.
t!(fs::create_dir_all(out_dir.join(package))); t!(fs::create_dir_all(out_dir.join(package)));
cargo.arg("--") cargo.arg("--")
.arg("index-page").arg(&builder.src.join("src/doc/index.md")); .arg("--markdown-css").arg("rust.css")
.arg("--markdown-no-toc")
.arg("--index-page").arg(&builder.src.join("src/doc/index.md"));
builder.run(&mut cargo); builder.run(&mut cargo);
builder.cp_r(&my_out, &out); builder.cp_r(&my_out, &out);

View file

@ -54,6 +54,9 @@ use std::rc::Rc;
use externalfiles::ExternalHtml; use externalfiles::ExternalHtml;
use errors;
use getopts;
use serialize::json::{ToJson, Json, as_json}; use serialize::json::{ToJson, Json, as_json};
use syntax::ast; use syntax::ast;
use syntax::ext::base::MacroKind; use syntax::ext::base::MacroKind;
@ -80,8 +83,6 @@ use html::{highlight, layout};
use minifier; use minifier;
use pulldown_cmark;
/// A pair of name and its optional document. /// A pair of name and its optional document.
pub type NameDoc = (String, Option<String>); pub type NameDoc = (String, Option<String>);
@ -508,6 +509,8 @@ pub fn run(mut krate: clean::Crate,
id_map: IdMap, id_map: IdMap,
enable_index_page: bool, enable_index_page: bool,
index_page: Option<PathBuf>, index_page: Option<PathBuf>,
matches: &getopts::Matches,
diag: &errors::Handler,
) -> Result<(), Error> { ) -> Result<(), Error> {
let src_root = match krate.src { let src_root = match krate.src {
FileName::Real(ref p) => match p.parent() { FileName::Real(ref p) => match p.parent() {
@ -675,7 +678,7 @@ pub fn run(mut krate: clean::Crate,
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone()); CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
CURRENT_LOCATION_KEY.with(|s| s.borrow_mut().clear()); CURRENT_LOCATION_KEY.with(|s| s.borrow_mut().clear());
write_shared(&cx, &krate, &*cache, index, enable_minification)?; write_shared(&cx, &krate, &*cache, index, enable_minification, matches, diag)?;
// And finally render the whole crate's documentation // And finally render the whole crate's documentation
cx.krate(krate) cx.krate(krate)
@ -751,11 +754,15 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
Json::Object(crate_data)) Json::Object(crate_data))
} }
fn write_shared(cx: &Context, fn write_shared(
cx: &Context,
krate: &clean::Crate, krate: &clean::Crate,
cache: &Cache, cache: &Cache,
search_index: String, search_index: String,
enable_minification: bool) -> Result<(), Error> { enable_minification: bool,
matches: &getopts::Matches,
diag: &errors::Handler,
) -> Result<(), Error> {
// Write out the shared files. Note that these are shared among all rustdoc // Write out the shared files. Note that these are shared among all rustdoc
// docs placed in the output directory, so this needs to be a synchronized // docs placed in the output directory, so this needs to be a synchronized
// operation with respect to all other rustdocs running around. // operation with respect to all other rustdocs running around.
@ -983,30 +990,15 @@ themePicker.onblur = handleThemeButtonsBlur;
} }
try_err!(writeln!(&mut w, "initSearch(searchIndex);"), &dst); try_err!(writeln!(&mut w, "initSearch(searchIndex);"), &dst);
<<<<<<< HEAD
if cx.disable_index_page == false {
let dst = cx.dst.join("index.html");
=======
if cx.enable_index_page == true { if cx.enable_index_page == true {
>>>>>>> a2642cf... f
if let Some(ref index_page) = cx.index_page { if let Some(ref index_page) = cx.index_page {
let mut content = Vec::with_capacity(100000); ::markdown::render(index_page,
cx.dst.clone(),
let mut f = try_err!(File::open(&index_page), &index_page); &matches, &(*cx.shared).layout.external_html,
try_err!(f.read_to_end(&mut content), &index_page); !matches.opt_present("markdown-no-toc"),
let content = match String::from_utf8(content) { diag);
Ok(c) => c,
Err(_) => return Err(Error::new(
io::Error::new(
io::ErrorKind::Other, "invalid markdown"),
&index_page)),
};
let parser = pulldown_cmark::Parser::new(&content);
let mut html_buf = String::new();
pulldown_cmark::html::push_html(&mut html_buf, parser);
let mut f = try_err!(File::create(&dst), &dst);
try_err!(f.write_all(html_buf.as_bytes()), &dst);
} else { } else {
let dst = cx.dst.join("index.html");
let mut w = BufWriter::new(try_err!(File::create(&dst), &dst)); let mut w = BufWriter::new(try_err!(File::create(&dst), &dst));
let page = layout::Page { let page = layout::Page {
title: "Index of crates", title: "Index of crates",

View file

@ -462,7 +462,7 @@ fn main_args(args: &[String]) -> isize {
diag.struct_err("too many file operands").emit(); diag.struct_err("too many file operands").emit();
return 1; return 1;
} }
let input = &matches.free[0]; let input = matches.free[0].clone();
let mut libs = SearchPaths::new(); let mut libs = SearchPaths::new();
for s in &matches.opt_strs("L") { for s in &matches.opt_strs("L") {
@ -490,7 +490,7 @@ fn main_args(args: &[String]) -> isize {
.collect(); .collect();
let should_test = matches.opt_present("test"); let should_test = matches.opt_present("test");
let markdown_input = Path::new(input).extension() let markdown_input = Path::new(&input).extension()
.map_or(false, |e| e == "md" || e == "markdown"); .map_or(false, |e| e == "md" || e == "markdown");
let output = matches.opt_str("o").map(|s| PathBuf::from(&s)); let output = matches.opt_str("o").map(|s| PathBuf::from(&s));
@ -568,14 +568,14 @@ fn main_args(args: &[String]) -> isize {
match (should_test, markdown_input) { match (should_test, markdown_input) {
(true, true) => { (true, true) => {
return markdown::test(input, cfgs, libs, externs, test_args, maybe_sysroot, return markdown::test(&input, cfgs, libs, externs, test_args, maybe_sysroot,
display_warnings, linker, edition, cg, &diag) display_warnings, linker, edition, cg, &diag)
} }
(true, false) => { (true, false) => {
return test::run(Path::new(input), cfgs, libs, externs, test_args, crate_name, return test::run(Path::new(&input), cfgs, libs, externs, test_args, crate_name,
maybe_sysroot, display_warnings, linker, edition, cg) maybe_sysroot, display_warnings, linker, edition, cg)
} }
(false, true) => return markdown::render(Path::new(input), (false, true) => return markdown::render(Path::new(&input),
output.unwrap_or(PathBuf::from("doc")), output.unwrap_or(PathBuf::from("doc")),
&matches, &external_html, &matches, &external_html,
!matches.opt_present("markdown-no-toc"), &diag), !matches.opt_present("markdown-no-toc"), &diag),
@ -584,8 +584,8 @@ fn main_args(args: &[String]) -> isize {
let output_format = matches.opt_str("w"); let output_format = matches.opt_str("w");
let res = acquire_input(PathBuf::from(input), externs, edition, cg, &matches, error_format, let res = acquire_input(PathBuf::from(input), externs, edition, cg, matches, error_format,
move |out| { move |out, matches| {
let Output { krate, passes, renderinfo } = out; let Output { krate, passes, renderinfo } = out;
let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing); let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
info!("going to format"); info!("going to format");
@ -624,11 +624,11 @@ fn acquire_input<R, F>(input: PathBuf,
externs: Externs, externs: Externs,
edition: Edition, edition: Edition,
cg: CodegenOptions, cg: CodegenOptions,
matches: &getopts::Matches, matches: getopts::Matches,
error_format: ErrorOutputType, error_format: ErrorOutputType,
f: F) f: F)
-> Result<R, String> -> Result<R, String>
where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R { where R: 'static + Send, F: 'static + Send + FnOnce(Output, &getopts::Matches) -> R {
match matches.opt_str("r").as_ref().map(|s| &**s) { match matches.opt_str("r").as_ref().map(|s| &**s) {
Some("rust") => Ok(rust_input(input, externs, edition, cg, matches, error_format, f)), Some("rust") => Ok(rust_input(input, externs, edition, cg, matches, error_format, f)),
Some(s) => Err(format!("unknown input format: {}", s)), Some(s) => Err(format!("unknown input format: {}", s)),
@ -682,11 +682,11 @@ fn rust_input<R, F>(cratefile: PathBuf,
externs: Externs, externs: Externs,
edition: Edition, edition: Edition,
cg: CodegenOptions, cg: CodegenOptions,
matches: &getopts::Matches, matches: getopts::Matches,
error_format: ErrorOutputType, error_format: ErrorOutputType,
f: F) -> R f: F) -> R
where R: 'static + Send, where R: 'static + Send,
F: 'static + Send + FnOnce(Output) -> R F: 'static + Send + FnOnce(Output, &getopts::Matches) -> R
{ {
let default_passes = if matches.opt_present("no-defaults") { let default_passes = if matches.opt_present("no-defaults") {
passes::DefaultPassOption::None passes::DefaultPassOption::None
@ -731,7 +731,7 @@ where R: 'static + Send,
*x == "ui-testing" *x == "ui-testing"
}); });
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format); let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(&matches, error_format);
let (tx, rx) = channel(); let (tx, rx) = channel();
@ -783,7 +783,8 @@ where R: 'static + Send,
krate = pass(krate); krate = pass(krate);
} }
tx.send(f(Output { krate: krate, renderinfo: renderinfo, passes: passes })).unwrap(); tx.send(f(Output { krate: krate, renderinfo: renderinfo, passes: passes },
&matches)).unwrap();
})); }));
match result { match result {

View file

@ -77,7 +77,7 @@ pub fn render(input: &Path, mut output: PathBuf, matches: &getopts::Matches,
diag.struct_err(&format!("{}: {}", output.display(), e)).emit(); diag.struct_err(&format!("{}: {}", output.display(), e)).emit();
return 4; return 4;
} }
Ok(f) => f Ok(f) => f,
}; };
let (metadata, text) = extract_leading_metadata(&input_str); let (metadata, text) = extract_leading_metadata(&input_str);