Skip to content

Commit 3388cd8

Browse files
committed
huon patch with fixups
1 parent 7affc42 commit 3388cd8

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/librustdoc/html/layout.rs

+7-1
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

+52-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ struct hoedown_renderer {
8080
*mut libc::c_void)>,
8181
header: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
8282
libc::c_int, *mut libc::c_void)>,
83-
other: [libc::size_t, ..29],
83+
math: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
84+
libc::c_int, *mut libc::c_void) -> libc::c_int>,
85+
other: [libc::size_t, ..28],
8486
}
8587

8688
#[repr(C)]
@@ -105,6 +107,8 @@ struct MyOpaque {
105107
dfltblk: extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
106108
*const hoedown_buffer, *mut libc::c_void),
107109
toc_builder: Option<TocBuilder>,
110+
math_enabled: bool,
111+
math_seen: bool,
108112
}
109113

110114
#[repr(C)]
@@ -137,8 +141,13 @@ extern {
137141

138142
fn hoedown_buffer_new(unit: libc::size_t) -> *mut hoedown_buffer;
139143
fn hoedown_buffer_puts(b: *mut hoedown_buffer, c: *const libc::c_char);
140-
fn hoedown_buffer_free(b: *mut hoedown_buffer);
144+
fn hoedown_buffer_put(b: *mut hoedown_buffer, data: *const libc::c_void, len: libc::size_t);
141145

146+
fn hoedown_buffer_free(b: *mut hoedown_buffer);
147+
fn hoedown_escape_html(ob: *mut hoedown_buffer,
148+
src: *const libc::uint8_t,
149+
size: libc::size_t,
150+
secure: libc::c_int);
142151
}
143152

144153
/// Returns Some(code) if `s` is a line that should be stripped from
@@ -170,6 +179,7 @@ local_data_key!(test_idx: Cell<uint>)
170179
// None == render an example, but there's no crate name
171180
local_data_key!(pub playground_krate: Option<String>)
172181
local_data_key!(pub use_mathjax: bool)
182+
local_data_key!(pub math_seen: bool)
173183

174184
pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
175185
extern fn block(ob: *mut hoedown_buffer, text: *const hoedown_buffer,
@@ -192,6 +202,9 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
192202
size: text.len() as libc::size_t,
193203
asize: text.len() as libc::size_t,
194204
unit: 0,
205+
data_free: None,
206+
data_realloc: None,
207+
buffer_free: None,
195208
};
196209
let rendered = if lang.is_null() {
197210
false
@@ -293,16 +306,48 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
293306
text.with_c_str(|p| unsafe { hoedown_buffer_puts(ob, p) });
294307
}
295308

309+
extern fn math(ob: *mut hoedown_buffer, text: *const hoedown_buffer,
310+
display_mode: libc::c_int, opaque: *mut libc::c_void) -> libc::c_int {
311+
312+
let opaque = opaque as *mut hoedown_html_renderer_state;
313+
let opaque = unsafe { &mut *((*opaque).opaque as *mut MyOpaque) };
314+
315+
opaque.math_seen = true;
316+
317+
let (open, close) = if !opaque.math_enabled {
318+
("$$", "$$")
319+
} else if display_mode == 1 {
320+
("\\[", "\\]")
321+
} else {
322+
("\\(", "\\)")
323+
};
324+
325+
open.with_c_str(|open| {
326+
close.with_c_str(|close| {
327+
unsafe {
328+
hoedown_buffer_put(ob, open as *const libc::c_void, 2);
329+
hoedown_escape_html(ob, (*text).data, (*text).size, 0);
330+
hoedown_buffer_put(ob, close as *const libc::c_void, 2);
331+
}
332+
})
333+
});
334+
335+
1
336+
}
337+
296338
unsafe {
297339
let ob = hoedown_buffer_new(DEF_OUNIT);
298340
let renderer = hoedown_html_renderer_new(0, 0);
299341
let mut opaque = MyOpaque {
300342
dfltblk: (*renderer).blockcode.unwrap(),
301-
toc_builder: if print_toc {Some(TocBuilder::new())} else {None}
343+
toc_builder: if print_toc {Some(TocBuilder::new())} else {None},
344+
math_enabled: use_mathjax.get().map_or(false, |x| *x),
345+
math_seen: false,
302346
};
303347
(*(*renderer).opaque).opaque = &mut opaque as *mut _ as *mut libc::c_void;
304348
(*renderer).blockcode = Some(block);
305349
(*renderer).header = Some(header);
350+
(*renderer).math = Some(math);
306351

307352
let document = hoedown_document_new(renderer, hoedown_extensions(), 16);
308353
hoedown_document_render(document, ob, s.as_ptr(),
@@ -322,6 +367,10 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
322367
});
323368
}
324369
hoedown_buffer_free(ob);
370+
371+
let old = math_seen.get().map_or(false, |x| *x);
372+
math_seen.replace(Some(old || opaque.math_seen));
373+
325374
ret
326375
}
327376
}

0 commit comments

Comments
 (0)