Skip to content

Commit 31ac8a9

Browse files
aturonalexcrichton
authored andcommitted
rustdoc: make table of contents optional
rustdoc currently determines whether to produce a table of contents (along with numbered sections) from the input type: yes for markdown input, no for Rust input. This commit adds a flag to disable the table of contents for markdown input, which is useful for embedding the output in a larger context.
1 parent 62bddfa commit 31ac8a9

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/librustdoc/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ pub fn opts() -> Vec<getopts::OptGroup> {
131131
Markdown file or generated documentation",
132132
"FILES"),
133133
optopt("", "markdown-playground-url",
134-
"URL to send code snippets to", "URL")
134+
"URL to send code snippets to", "URL"),
135+
optflag("", "markdown-no-toc", "don't include table of contents")
135136
)
136137
}
137138

@@ -220,7 +221,8 @@ pub fn main_args(args: &[String]) -> int {
220221
return test::run(input, cfgs, libs, externs, test_args)
221222
}
222223
(false, true) => return markdown::render(input, output.unwrap_or(Path::new("doc")),
223-
&matches, &external_html),
224+
&matches, &external_html,
225+
!matches.opt_present("markdown-no-toc")),
224226
(false, false) => {}
225227
}
226228

src/librustdoc/markdown.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use externalfiles::ExternalHtml;
2020

2121
use html::escape::Escape;
2222
use html::markdown;
23-
use html::markdown::{MarkdownWithToc, find_testable_code, reset_headers};
23+
use html::markdown::{Markdown, MarkdownWithToc, find_testable_code, reset_headers};
2424
use test::Collector;
2525

2626
/// Separate any lines at the start of the file that begin with `%`.
@@ -42,7 +42,7 @@ fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) {
4242
/// Render `input` (e.g. "foo.md") into an HTML file in `output`
4343
/// (e.g. output = "bar" => "bar/foo.html").
4444
pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
45-
external_html: &ExternalHtml) -> int {
45+
external_html: &ExternalHtml, include_toc: bool) -> int {
4646
let input_p = Path::new(input);
4747
output.push(input_p.filestem().unwrap());
4848
output.set_extension("html");
@@ -80,6 +80,12 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
8080

8181
reset_headers();
8282

83+
let rendered = if include_toc {
84+
format!("{}", MarkdownWithToc(text))
85+
} else {
86+
format!("{}", Markdown(text))
87+
};
88+
8389
let err = write!(
8490
&mut out,
8591
r#"<!DOCTYPE html>
@@ -113,7 +119,7 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
113119
css = css,
114120
in_header = external_html.in_header,
115121
before_content = external_html.before_content,
116-
text = MarkdownWithToc(text),
122+
text = rendered,
117123
after_content = external_html.after_content,
118124
playground = playground,
119125
);

0 commit comments

Comments
 (0)