@@ -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
171180local_data_key ! ( pub playground_krate: Option <String >)
172181local_data_key ! ( pub use_mathjax: bool )
182+ local_data_key ! ( pub math_seen: bool )
173183
174184pub 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