2022-04-12 11:04:37 -04:00
|
|
|
use std::fmt::Write as _;
|
2020-05-04 23:36:22 +02:00
|
|
|
use std::fs::{create_dir_all, read_to_string, File};
|
2015-03-11 15:24:14 -07:00
|
|
|
use std::io::prelude::*;
|
2020-05-04 23:36:22 +02:00
|
|
|
use std::path::Path;
|
2014-03-07 14:31:41 +11:00
|
|
|
|
2020-01-01 19:40:49 +01:00
|
|
|
use rustc_span::edition::Edition;
|
2020-01-01 19:25:28 +01:00
|
|
|
use rustc_span::source_map::DUMMY_SP;
|
2017-11-24 23:12:09 +01:00
|
|
|
|
2019-02-23 16:40:07 +09:00
|
|
|
use crate::config::{Options, RenderOptions};
|
2021-12-03 19:49:31 -08:00
|
|
|
use crate::doctest::{Collector, GlobalTestOptions};
|
2019-02-23 16:40:07 +09:00
|
|
|
use crate::html::escape::Escape;
|
|
|
|
|
use crate::html::markdown;
|
2021-10-04 21:54:00 -04:00
|
|
|
use crate::html::markdown::{
|
|
|
|
|
find_testable_code, ErrorCodes, HeadingOffset, IdMap, Markdown, MarkdownWithToc,
|
|
|
|
|
};
|
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 `%`.
|
2019-06-25 19:43:18 +02:00
|
|
|
fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
|
2014-03-07 14:31:41 +11:00
|
|
|
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() {
|
2020-02-26 13:03:46 +01:00
|
|
|
if line.starts_with("# ") || line.starts_with('%') {
|
2017-03-25 15:33:44 -04:00
|
|
|
// trim the whitespace after the symbol
|
2018-12-05 06:42:56 -08:00
|
|
|
metadata.push(line[1..].trim_start());
|
2015-07-30 15:29:34 -07:00
|
|
|
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, "")
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Render `input` (e.g., "foo.md") into an HTML file in `output`
|
|
|
|
|
/// (e.g., output = "bar" => "bar/foo.html").
|
Clean up rustdoc startup.
rustc's startup has several layers, including:
- `interface::run_compiler` passes a closure, `f`, to
`run_in_thread_pool_with_globals`, which creates a thread pool, sets
up session globals, and passes `f` to `create_compiler_and_run`.
- `create_compiler_and_run` creates a `Session`, a `Compiler`, sets the
source map, and calls `f`.
rustdoc is a bit different.
- `main_args` calls `main_options` via
`run_in_thread_pool_with_globals`, which (again) creates a thread pool
(hardcoded to a single thread!) and sets up session globals.
- `main_options` has four different paths.
- The second one calls `interface::run_compiler`, which redoes the
`run_in_thread_pool_with_globals`! This is bad.
- The fourth one calls `interface::create_compiler_and_run`, which is
reasonable.
- The first and third ones don't do anything of note involving the
above functions, except for some symbol interning which requires
session globals.
In other words, rustdoc calls into `rustc_interface` at three different
levels. It's a bit confused, and feels like code where functionality has
been added by different people at different times without fully
understanding how the globally accessible stuff is set up.
This commit tidies things up. It removes the
`run_in_thread_pool_with_globals` call in `main_args`, and adjust the
four paths in `main_options` as follows.
- `markdown::test` calls `test::test_main`, which provides its own
parallelism and so doesn't need a thread pool. It had one small use of
symbol interning, which required session globals, but the commit
removes this.
- `doctest::run` already calls `interface::run_compiler`, so it doesn't
need further adjustment.
- `markdown::render` is simple but needs session globals for interning
(which can't easily be removed), so it's now wrapped in
`create_session_globals_then`.
- The fourth path now uses `interface::run_compiler`, which is
equivalent to the old `run_in_thread_pool_with_globals` +
`create_compiler_and_run` pairing.
2022-10-07 13:57:32 +11:00
|
|
|
///
|
|
|
|
|
/// Requires session globals to be available, for symbol interning.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn render<P: AsRef<Path>>(
|
2020-05-04 23:36:22 +02:00
|
|
|
input: P,
|
2019-04-17 20:17:12 -05:00
|
|
|
options: RenderOptions,
|
|
|
|
|
edition: Edition,
|
2020-05-04 23:36:22 +02:00
|
|
|
) -> Result<(), String> {
|
2020-03-26 22:57:33 +01:00
|
|
|
if let Err(e) = create_dir_all(&options.output) {
|
2020-05-04 23:36:22 +02:00
|
|
|
return Err(format!("{}: {}", options.output.display(), e));
|
2020-03-26 22:57:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-04 23:36:22 +02:00
|
|
|
let input = input.as_ref();
|
2018-10-30 13:35:10 -05:00
|
|
|
let mut output = options.output;
|
2019-09-02 00:14:58 +07:00
|
|
|
output.push(input.file_name().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();
|
2018-10-30 13:35:10 -05:00
|
|
|
for name in &options.markdown_css {
|
2022-12-12 15:17:49 -07:00
|
|
|
write!(css, r#"<link rel="stylesheet" href="{name}">"#)
|
2022-04-12 11:04:37 -04:00
|
|
|
.expect("Writing to a String can't fail");
|
2014-03-07 14:31:41 +11:00
|
|
|
}
|
|
|
|
|
|
2020-05-04 23:36:22 +02:00
|
|
|
let input_str = read_to_string(input).map_err(|err| format!("{}: {}", input.display(), err))?;
|
2018-10-30 13:35:10 -05:00
|
|
|
let playground_url = options.markdown_playground_url.or(options.playground_url);
|
2019-08-10 18:07:07 -04:00
|
|
|
let playground = playground_url.map(|url| markdown::Playground { crate_name: None, url });
|
2014-03-07 14:31:41 +11:00
|
|
|
|
2020-05-04 23:36:22 +02:00
|
|
|
let mut out = File::create(&output).map_err(|e| format!("{}: {}", output.display(), e))?;
|
2014-03-07 14:31:41 +11:00
|
|
|
|
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() {
|
2020-05-04 23:36:22 +02:00
|
|
|
return Err("invalid markdown file: no initial lines starting with `# ` or `%`".to_owned());
|
2014-03-07 14:31:41 +11:00
|
|
|
}
|
2015-02-01 21:53:25 -05:00
|
|
|
let title = metadata[0];
|
2014-03-07 14:31:41 +11:00
|
|
|
|
2018-07-22 07:25:00 -06:00
|
|
|
let mut ids = IdMap::new();
|
2020-10-10 14:27:52 -04:00
|
|
|
let error_codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
|
2018-10-30 13:35:10 -05:00
|
|
|
let text = if !options.markdown_no_toc {
|
2020-07-15 10:55:40 +00:00
|
|
|
MarkdownWithToc(text, &mut ids, error_codes, edition, &playground).into_string()
|
2014-07-23 15:43:03 -07:00
|
|
|
} else {
|
2021-10-04 21:08:58 -04:00
|
|
|
Markdown {
|
|
|
|
|
content: text,
|
|
|
|
|
links: &[],
|
|
|
|
|
ids: &mut ids,
|
|
|
|
|
error_codes,
|
|
|
|
|
edition,
|
|
|
|
|
playground: &playground,
|
2021-10-04 21:54:00 -04:00
|
|
|
heading_offset: HeadingOffset::H1,
|
2021-10-04 21:08:58 -04:00
|
|
|
}
|
|
|
|
|
.into_string()
|
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,
|
2018-10-30 13:35:10 -05:00
|
|
|
in_header = options.external_html.in_header,
|
|
|
|
|
before_content = options.external_html.before_content,
|
Remove hoedown from rustdoc
Is it really time? Have our months, no, *years* of suffering come to an end? Are we finally able to cast off the pall of Hoedown? The weight which has dragged us down for so long?
-----
So, timeline for those who need to catch up:
* Way back in December 2016, [we decided we wanted to switch out the markdown renderer](https://github.com/rust-lang/rust/issues/38400). However, this was put on hold because the build system at the time made it difficult to pull in dependencies from crates.io.
* A few months later, in March 2017, [the first PR was done, to switch out the renderers entirely](https://github.com/rust-lang/rust/pull/40338). The PR itself was fraught with CI and build system issues, but eventually landed.
* However, not all was well in the Rustdoc world. During the PR and shortly after, we noticed [some differences in the way the two parsers handled some things](https://github.com/rust-lang/rust/issues/40912), and some of these differences were major enough to break the docs for some crates.
* A couple weeks afterward, [Hoedown was put back in](https://github.com/rust-lang/rust/pull/41290), at this point just to catch tests that Pulldown was "spuriously" running. This would at least provide some warning about spurious tests, rather than just breaking spontaneously.
* However, the problems had created enough noise by this point that just a few days after that, [Hoedown was switched back to the default](https://github.com/rust-lang/rust/pull/41431) while we came up with a solution for properly warning about the differences.
* That solution came a few weeks later, [as a series of warnings when the HTML emitted by the two parsers was semantically different](https://github.com/rust-lang/rust/pull/41991). But that came at a cost, as now rustdoc needed proc-macro support (the new crate needed some custom derives farther down its dependency tree), and the build system was not equipped to handle it at the time. It was worked on for three months as the issue stumped more and more people.
* In that time, [bootstrap was completely reworked](https://github.com/rust-lang/rust/pull/43059) to change how it ordered compilation, and [the method by which it built rustdoc would change](https://github.com/rust-lang/rust/pull/43482), as well. This allowed it to only be built after stage1, when proc-macros would be available, allowing the "rendering differences" PR to finally land.
* The warnings were not perfect, and revealed a few [spurious](https://github.com/rust-lang/rust/pull/44368) [differences](https://github.com/rust-lang/rust/pull/45421) between how we handled the renderers.
* Once these were handled, [we flipped the switch to turn on the "rendering difference" warnings all the time](https://github.com/rust-lang/rust/pull/45324), in October 2017. This began the "warning cycle" for this change, and landed in stable in 1.23, on 2018-01-04.
* Once those warnings hit stable, and after a couple weeks of seeing whether we would get any more reports than what we got from sitting on nightly/beta, [we switched the renderers](https://github.com/rust-lang/rust/pull/47398), making Pulldown the default but still offering the option to use Hoedown.
And that brings us to the present. We haven't received more new issues from this in the meantime, and the "switch by default" is now on beta. Our reasoning is that, at this point, anyone who would have been affected by this has run into it already.
2018-02-16 15:09:19 +01:00
|
|
|
text = text,
|
2018-10-30 13:35:10 -05:00
|
|
|
after_content = options.external_html.after_content,
|
2017-11-24 23:12:09 +01:00
|
|
|
);
|
2014-03-07 14:31:41 +11:00
|
|
|
|
|
|
|
|
match err {
|
2020-05-04 23:36:22 +02:00
|
|
|
Err(e) => Err(format!("cannot write to `{}`: {}", output.display(), e)),
|
|
|
|
|
Ok(_) => Ok(()),
|
2014-03-07 14:31:41 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Runs any tests/code examples in the markdown file `input`.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn test(options: Options) -> Result<(), String> {
|
2020-05-09 13:37:26 +02:00
|
|
|
let input_str = read_to_string(&options.input)
|
|
|
|
|
.map_err(|err| format!("{}: {}", options.input.display(), err))?;
|
2021-12-03 19:49:31 -08:00
|
|
|
let mut opts = GlobalTestOptions::default();
|
2015-04-06 18:39:39 -07:00
|
|
|
opts.no_crate_inject = true;
|
2019-08-29 23:15:31 +02:00
|
|
|
let mut collector = Collector::new(
|
Clean up rustdoc startup.
rustc's startup has several layers, including:
- `interface::run_compiler` passes a closure, `f`, to
`run_in_thread_pool_with_globals`, which creates a thread pool, sets
up session globals, and passes `f` to `create_compiler_and_run`.
- `create_compiler_and_run` creates a `Session`, a `Compiler`, sets the
source map, and calls `f`.
rustdoc is a bit different.
- `main_args` calls `main_options` via
`run_in_thread_pool_with_globals`, which (again) creates a thread pool
(hardcoded to a single thread!) and sets up session globals.
- `main_options` has four different paths.
- The second one calls `interface::run_compiler`, which redoes the
`run_in_thread_pool_with_globals`! This is bad.
- The fourth one calls `interface::create_compiler_and_run`, which is
reasonable.
- The first and third ones don't do anything of note involving the
above functions, except for some symbol interning which requires
session globals.
In other words, rustdoc calls into `rustc_interface` at three different
levels. It's a bit confused, and feels like code where functionality has
been added by different people at different times without fully
understanding how the globally accessible stuff is set up.
This commit tidies things up. It removes the
`run_in_thread_pool_with_globals` call in `main_args`, and adjust the
four paths in `main_options` as follows.
- `markdown::test` calls `test::test_main`, which provides its own
parallelism and so doesn't need a thread pool. It had one small use of
symbol interning, which required session globals, but the commit
removes this.
- `doctest::run` already calls `interface::run_compiler`, so it doesn't
need further adjustment.
- `markdown::render` is simple but needs session globals for interning
(which can't easily be removed), so it's now wrapped in
`create_session_globals_then`.
- The fourth path now uses `interface::run_compiler`, which is
equivalent to the old `run_in_thread_pool_with_globals` +
`create_compiler_and_run` pairing.
2022-10-07 13:57:32 +11:00
|
|
|
options.input.display().to_string(),
|
2019-08-29 23:15:31 +02:00
|
|
|
options.clone(),
|
2019-08-27 15:44:11 -07:00
|
|
|
true,
|
|
|
|
|
opts,
|
|
|
|
|
None,
|
|
|
|
|
Some(options.input),
|
|
|
|
|
options.enable_per_target_ignores,
|
|
|
|
|
);
|
2018-07-21 17:15:25 -06:00
|
|
|
collector.set_position(DUMMY_SP);
|
2022-10-17 10:51:40 +11:00
|
|
|
let codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
|
2019-04-20 13:03:59 -04:00
|
|
|
|
2020-04-21 23:49:06 +02:00
|
|
|
find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);
|
2019-04-20 13:03:59 -04:00
|
|
|
|
2021-11-26 13:52:28 -05:00
|
|
|
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
|
2020-05-09 13:37:26 +02:00
|
|
|
Ok(())
|
2014-03-07 14:31:41 +11:00
|
|
|
}
|