Skip to content

Commit 2088184

Browse files
committed
Allow LaTeX in standalone markdown files via --markdown-enable-math.
Requires the KaTeX files to be arranged correctly.
1 parent ca92372 commit 2088184

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/librustdoc/html/render.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,6 @@ pub fn run(mut krate: clean::Crate, external_html: &ExternalHtml, dst: Path) ->
253253
render_redirect_pages: false,
254254
};
255255

256-
markdown::enable_math.replace(None);
257-
258256
try!(mkdir(&cx.dst));
259257

260258
// Crawl the crate, building a summary of the stability levels. NOTE: this

src/librustdoc/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ pub fn opts() -> Vec<getopts::OptGroup> {
174174
"FILES"),
175175
optopt("", "markdown-playground-url",
176176
"URL to send code snippets to", "URL"),
177-
optflag("", "markdown-no-toc", "don't include table of contents")
177+
optflag("", "markdown-no-toc", "don't include table of contents"),
178+
optflag("", "markdown-enable-math",
179+
"render LaTeX expressions surrounded by `$$` via the KaTeX javascript library."),
178180
)
179181
}
180182

@@ -265,7 +267,8 @@ pub fn main_args(args: &[String]) -> int {
265267
}
266268
(false, true) => return markdown::render(input, output.unwrap_or(Path::new("doc")),
267269
&matches, &external_html,
268-
!matches.opt_present("markdown-no-toc")),
270+
!matches.opt_present("markdown-no-toc"),
271+
matches.opt_present("markdown-enable-math")),
269272
(false, false) => {}
270273
}
271274

src/librustdoc/markdown.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) {
4141
/// Render `input` (e.g. "foo.md") into an HTML file in `output`
4242
/// (e.g. output = "bar" => "bar/foo.html").
4343
pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
44-
external_html: &ExternalHtml, include_toc: bool) -> int {
44+
external_html: &ExternalHtml,
45+
include_toc: bool,
46+
enable_math: bool) -> int {
4547
let input_p = Path::new(input);
4648
output.push(input_p.filestem().unwrap());
4749
output.set_extension("html");
@@ -79,12 +81,18 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
7981

8082
reset_headers();
8183

84+
if enable_math {
85+
markdown::enable_math.replace(Some(true));
86+
}
87+
8288
let rendered = if include_toc {
8389
format!("{}", MarkdownWithToc(text))
8490
} else {
8591
format!("{}", Markdown(text))
8692
};
8793

94+
let saw_math = enable_math && markdown::math_seen.get().map_or(false, |x| *x);
95+
8896
let err = write!(
8997
&mut out,
9098
r#"<!DOCTYPE html>
@@ -95,6 +103,7 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
95103
<title>{title}</title>
96104
97105
{css}
106+
{math_css}
98107
{in_header}
99108
</head>
100109
<body class="rustdoc">
@@ -112,6 +121,7 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
112121
window.playgroundUrl = "{playground}";
113122
</script>
114123
{after_content}
124+
{math_js}
115125
</body>
116126
</html>"#,
117127
title = Escape(title),
@@ -121,6 +131,26 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
121131
text = rendered,
122132
after_content = external_html.after_content,
123133
playground = playground,
134+
135+
math_css = if saw_math {
136+
"<link rel=\"stylesheet\" type=\"text/css\" href=\"katex/katex.min.css\">
137+
<style>.math-display { text-align: center; }</style>"
138+
} else {
139+
""
140+
},
141+
math_js = if saw_math {
142+
r#"<script src="katex/katex.min.js"></script>
143+
<script type="text/javascript">
144+
var elements = document.getElementsByClassName("latex-math");
145+
for (var i = 0; i < elements.length; i++) {
146+
var e = elements[i];
147+
var style = e.tagName == "P" ? "display" : "text";
148+
katex.render("\\" + style + "style " + e.textContent, e);
149+
}
150+
</script>"#
151+
} else {
152+
""
153+
}
124154
);
125155

126156
match err {

0 commit comments

Comments
 (0)