Skip to content

Commit d65092a

Browse files
committed
enable mathjax support in rustdoc
1 parent ae193f1 commit d65092a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/librustdoc/html/layout.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct Layout {
2020
pub external_html: ExternalHtml,
2121
pub krate: String,
2222
pub playground_url: String,
23+
pub use_mathjax: bool,
2324
}
2425

2526
pub struct Page<'a> {
@@ -124,6 +125,7 @@ r##"<!DOCTYPE html>
124125
<script src="{root_path}main.js"></script>
125126
{play_js}
126127
<script async src="{root_path}search-index.js"></script>
128+
{mathjax_js}
127129
</body>
128130
</html>"##,
129131
content = *t,
@@ -156,6 +158,12 @@ r##"<!DOCTYPE html>
156158
} else {
157159
format!(r#"<script src="{}playpen.js"></script>"#, page.root_path)
158160
},
161+
mathjax_js = if layout.use_mathjax {
162+
r#"<script async src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML">
163+
</script>"#.to_string()
164+
} else {
165+
"".to_string()
166+
},
159167
)
160168
}
161169

src/librustdoc/html/markdown.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3;
5656
static HOEDOWN_EXT_STRIKETHROUGH: libc::c_uint = 1 << 4;
5757
static HOEDOWN_EXT_SUPERSCRIPT: libc::c_uint = 1 << 8;
5858
static HOEDOWN_EXT_FOOTNOTES: libc::c_uint = 1 << 2;
59+
static HOEDOWN_EXT_MATH: libc::c_uint = 1 << 13;
5960

6061
static HOEDOWN_EXTENSIONS: libc::c_uint =
6162
HOEDOWN_EXT_NO_INTRA_EMPHASIS | HOEDOWN_EXT_TABLES |
@@ -147,10 +148,22 @@ fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
147148
}
148149
}
149150

151+
fn hoedown_extensions() -> libc::c_uint {
152+
let mut extensions = HOEDOWN_EXTENSIONS;
153+
154+
match use_mathjax.get().as_ref() {
155+
Some(use_math) if **use_math => { extensions |= HOEDOWN_EXT_MATH; }
156+
_ => {}
157+
}
158+
159+
extensions
160+
}
161+
150162
local_data_key!(used_header_map: RefCell<HashMap<String, uint>>)
151163
local_data_key!(test_idx: Cell<uint>)
152164
// None == render an example, but there's no crate name
153165
local_data_key!(pub playground_krate: Option<String>)
166+
local_data_key!(pub use_mathjax: bool)
154167

155168
pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
156169
extern fn block(ob: *mut hoedown_buffer, text: *const hoedown_buffer,
@@ -285,7 +298,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
285298
(*renderer).blockcode = Some(block);
286299
(*renderer).header = Some(header);
287300

288-
let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
301+
let document = hoedown_document_new(renderer, hoedown_extensions(), 16);
289302
hoedown_document_render(document, ob, s.as_ptr(),
290303
s.len() as libc::size_t);
291304
hoedown_document_free(document);
@@ -363,7 +376,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
363376
(*renderer).header = Some(header);
364377
(*(*renderer).opaque).opaque = tests as *mut _ as *mut libc::c_void;
365378

366-
let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
379+
let document = hoedown_document_new(renderer, hoedown_extensions(), 16);
367380
hoedown_document_render(document, ob, doc.as_ptr(),
368381
doc.len() as libc::size_t);
369382
hoedown_document_free(document);

src/librustdoc/html/render.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,14 @@ pub fn run(mut krate: clean::Crate, external_html: &ExternalHtml, dst: Path) ->
245245
external_html: external_html.clone(),
246246
krate: krate.name.clone(),
247247
playground_url: "".to_string(),
248+
use_mathjax: false,
248249
},
249250
include_sources: true,
250251
render_redirect_pages: false,
251252
};
252253

254+
markdown::use_mathjax.replace(None);
255+
253256
try!(mkdir(&cx.dst));
254257

255258
// Crawl the crate, building a summary of the stability levels. NOTE: this
@@ -284,6 +287,11 @@ pub fn run(mut krate: clean::Crate, external_html: &ExternalHtml, dst: Path) ->
284287
if "html_no_source" == x.as_slice() => {
285288
cx.include_sources = false;
286289
}
290+
clean::Word(ref x)
291+
if "enable_mathjax" == x.as_slice() => {
292+
cx.layout.use_mathjax = true;
293+
markdown::use_mathjax.replace(Some(true));
294+
}
287295
_ => {}
288296
}
289297
}

0 commit comments

Comments
 (0)