Skip to content

Commit 134bb21

Browse files
committed
huon patch
1 parent 7affc42 commit 134bb21

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/librustdoc/html/layout.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::fmt;
1212
use std::io;
1313

1414
use externalfiles::ExternalHtml;
15+
use html::markdown
1516

1617
#[deriving(Clone)]
1718
pub struct Layout {
@@ -35,6 +36,10 @@ pub fn render<T: fmt::Show, S: fmt::Show>(
3536
dst: &mut io::Writer, layout: &Layout, page: &Page, sidebar: &S, t: &T)
3637
-> io::IoResult<()>
3738
{
39+
// Reset state on whether we've seen math, so as to avoid loading mathjax
40+
// on pages that don't actually *have* math.
41+
markdown::math_seen.replace(Some(false));
42+
3843
write!(dst,
3944
r##"<!DOCTYPE html>
4045
<html lang="en">
@@ -158,7 +163,8 @@ r##"<!DOCTYPE html>
158163
} else {
159164
format!(r#"<script src="{}playpen.js"></script>"#, page.root_path)
160165
},
161-
mathjax_js = if layout.use_mathjax {
166+
// this must be last so that `math_seen` captures all possible $$'s on this page.
167+
mathjax_js = if layout.use_mathjax && markdown::math_seen.get().map_or(false, |x| *x) {
162168
r#"<script async src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML">
163169
</script>"#.to_string()
164170
} else {

src/librustdoc/html/markdown.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ struct MyOpaque {
105105
dfltblk: extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
106106
*const hoedown_buffer, *mut libc::c_void),
107107
toc_builder: Option<TocBuilder>,
108+
math_enabled: bool,
109+
math_seen: bool,
108110
}
109111

110112
#[repr(C)]
@@ -170,6 +172,7 @@ local_data_key!(test_idx: Cell<uint>)
170172
// None == render an example, but there's no crate name
171173
local_data_key!(pub playground_krate: Option<String>)
172174
local_data_key!(pub use_mathjax: bool)
175+
local_data_key!(pub math_seen: bool)
173176

174177
pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
175178
extern fn block(ob: *mut hoedown_buffer, text: *const hoedown_buffer,
@@ -293,16 +296,48 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
293296
text.with_c_str(|p| unsafe { hoedown_buffer_puts(ob, p) });
294297
}
295298

299+
extern fn math(ob: *mut hoedown_buffer, text: *const hoedown_buffer,
300+
display_mode: libc::c_int, opaque: *mut libc::c_void) -> libc::c_int {
301+
302+
let opaque = opaque as *mut hoedown_html_renderer_state;
303+
let opaque = unsafe { &mut *((*opaque).opaque as *mut MyOpaque) };
304+
305+
opaque.math_seen = true;
306+
307+
let (open, close) = if !opaque.math_enabled {
308+
("$$", "$$")
309+
} else if displaymode == 1 {
310+
("\\[", "\\]")
311+
} else {
312+
("\\(", "\\)")
313+
};
314+
315+
open.with_c_str(|open| {
316+
close.with_c_str(|close| {
317+
unsafe {
318+
hoedown_buffer_puts(ob, open, 2);
319+
escape_html(ob, (*text).data, (*text).size);
320+
hoedown_buffer_puts(ob, close, 2);
321+
}
322+
})
323+
});
324+
325+
1
326+
}
327+
296328
unsafe {
297329
let ob = hoedown_buffer_new(DEF_OUNIT);
298330
let renderer = hoedown_html_renderer_new(0, 0);
299331
let mut opaque = MyOpaque {
300332
dfltblk: (*renderer).blockcode.unwrap(),
301-
toc_builder: if print_toc {Some(TocBuilder::new())} else {None}
333+
toc_builder: if print_toc {Some(TocBuilder::new())} else {None},
334+
math_enabled: use_mathjax.get().map_or(false, |x| *x),
335+
math_seen: false,
302336
};
303337
(*(*renderer).opaque).opaque = &mut opaque as *mut _ as *mut libc::c_void;
304338
(*renderer).blockcode = Some(block);
305339
(*renderer).header = Some(header);
340+
(*renderer).math = Some(math);
306341

307342
let document = hoedown_document_new(renderer, hoedown_extensions(), 16);
308343
hoedown_document_render(document, ob, s.as_ptr(),
@@ -322,6 +357,10 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
322357
});
323358
}
324359
hoedown_buffer_free(ob);
360+
361+
let old = math_seen.get().map_or(false, |x| *x);
362+
math_seen.replace(Some(old || opaque.math_seen));
363+
325364
ret
326365
}
327366
}

0 commit comments

Comments
 (0)