Skip to content

Commit

Permalink
huon patch with fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
Gankra committed Sep 5, 2014
1 parent 7affc42 commit 3388cd8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::fmt;
use std::io;

use externalfiles::ExternalHtml;
use html::markdown;

#[deriving(Clone)]
pub struct Layout {
Expand All @@ -35,6 +36,10 @@ pub fn render<T: fmt::Show, S: fmt::Show>(
dst: &mut io::Writer, layout: &Layout, page: &Page, sidebar: &S, t: &T)
-> io::IoResult<()>
{
// Reset state on whether we've seen math, so as to avoid loading mathjax
// on pages that don't actually *have* math.
markdown::math_seen.replace(Some(false));

write!(dst,
r##"<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -158,7 +163,8 @@ r##"<!DOCTYPE html>
} else {
format!(r#"<script src="{}playpen.js"></script>"#, page.root_path)
},
mathjax_js = if layout.use_mathjax {
// this must be last so that `math_seen` captures all possible $$'s on this page.
mathjax_js = if layout.use_mathjax && markdown::math_seen.get().map_or(false, |x| *x) {
r#"<script async src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML">
</script>"#.to_string()
} else {
Expand Down
55 changes: 52 additions & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ struct hoedown_renderer {
*mut libc::c_void)>,
header: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
libc::c_int, *mut libc::c_void)>,
other: [libc::size_t, ..29],
math: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
libc::c_int, *mut libc::c_void) -> libc::c_int>,
other: [libc::size_t, ..28],
}

#[repr(C)]
Expand All @@ -105,6 +107,8 @@ struct MyOpaque {
dfltblk: extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
*const hoedown_buffer, *mut libc::c_void),
toc_builder: Option<TocBuilder>,
math_enabled: bool,
math_seen: bool,
}

#[repr(C)]
Expand Down Expand Up @@ -137,8 +141,13 @@ extern {

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

fn hoedown_buffer_free(b: *mut hoedown_buffer);
fn hoedown_escape_html(ob: *mut hoedown_buffer,
src: *const libc::uint8_t,
size: libc::size_t,
secure: libc::c_int);
}

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

pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
extern fn block(ob: *mut hoedown_buffer, text: *const hoedown_buffer,
Expand All @@ -192,6 +202,9 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
size: text.len() as libc::size_t,
asize: text.len() as libc::size_t,
unit: 0,
data_free: None,
data_realloc: None,
buffer_free: None,
};
let rendered = if lang.is_null() {
false
Expand Down Expand Up @@ -293,16 +306,48 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
text.with_c_str(|p| unsafe { hoedown_buffer_puts(ob, p) });
}

extern fn math(ob: *mut hoedown_buffer, text: *const hoedown_buffer,
display_mode: libc::c_int, opaque: *mut libc::c_void) -> libc::c_int {

let opaque = opaque as *mut hoedown_html_renderer_state;
let opaque = unsafe { &mut *((*opaque).opaque as *mut MyOpaque) };

opaque.math_seen = true;

let (open, close) = if !opaque.math_enabled {
("$$", "$$")
} else if display_mode == 1 {
("\\[", "\\]")
} else {
("\\(", "\\)")
};

open.with_c_str(|open| {
close.with_c_str(|close| {
unsafe {
hoedown_buffer_put(ob, open as *const libc::c_void, 2);
hoedown_escape_html(ob, (*text).data, (*text).size, 0);
hoedown_buffer_put(ob, close as *const libc::c_void, 2);
}
})
});

1
}

unsafe {
let ob = hoedown_buffer_new(DEF_OUNIT);
let renderer = hoedown_html_renderer_new(0, 0);
let mut opaque = MyOpaque {
dfltblk: (*renderer).blockcode.unwrap(),
toc_builder: if print_toc {Some(TocBuilder::new())} else {None}
toc_builder: if print_toc {Some(TocBuilder::new())} else {None},
math_enabled: use_mathjax.get().map_or(false, |x| *x),
math_seen: false,
};
(*(*renderer).opaque).opaque = &mut opaque as *mut _ as *mut libc::c_void;
(*renderer).blockcode = Some(block);
(*renderer).header = Some(header);
(*renderer).math = Some(math);

let document = hoedown_document_new(renderer, hoedown_extensions(), 16);
hoedown_document_render(document, ob, s.as_ptr(),
Expand All @@ -322,6 +367,10 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
});
}
hoedown_buffer_free(ob);

let old = math_seen.get().map_or(false, |x| *x);
math_seen.replace(Some(old || opaque.math_seen));

ret
}
}
Expand Down

0 comments on commit 3388cd8

Please sign in to comment.