@@ -80,7 +80,9 @@ struct hoedown_renderer {
80
80
* mut libc:: c_void ) > ,
81
81
header : Option < extern "C" fn ( * mut hoedown_buffer , * const hoedown_buffer ,
82
82
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 ] ,
84
86
}
85
87
86
88
#[ repr( C ) ]
@@ -105,6 +107,8 @@ struct MyOpaque {
105
107
dfltblk : extern "C" fn ( * mut hoedown_buffer , * const hoedown_buffer ,
106
108
* const hoedown_buffer , * mut libc:: c_void ) ,
107
109
toc_builder : Option < TocBuilder > ,
110
+ math_enabled : bool ,
111
+ math_seen : bool ,
108
112
}
109
113
110
114
#[ repr( C ) ]
@@ -137,8 +141,13 @@ extern {
137
141
138
142
fn hoedown_buffer_new ( unit : libc:: size_t ) -> * mut hoedown_buffer ;
139
143
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 ) ;
141
145
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 ) ;
142
151
}
143
152
144
153
/// Returns Some(code) if `s` is a line that should be stripped from
@@ -170,6 +179,7 @@ local_data_key!(test_idx: Cell<uint>)
170
179
// None == render an example, but there's no crate name
171
180
local_data_key ! ( pub playground_krate: Option <String >)
172
181
local_data_key ! ( pub use_mathjax: bool )
182
+ local_data_key ! ( pub math_seen: bool )
173
183
174
184
pub fn render ( w : & mut fmt:: Formatter , s : & str , print_toc : bool ) -> fmt:: Result {
175
185
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 {
192
202
size : text. len ( ) as libc:: size_t ,
193
203
asize : text. len ( ) as libc:: size_t ,
194
204
unit : 0 ,
205
+ data_free : None ,
206
+ data_realloc : None ,
207
+ buffer_free : None ,
195
208
} ;
196
209
let rendered = if lang. is_null ( ) {
197
210
false
@@ -293,16 +306,48 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
293
306
text. with_c_str ( |p| unsafe { hoedown_buffer_puts ( ob, p) } ) ;
294
307
}
295
308
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
+
296
338
unsafe {
297
339
let ob = hoedown_buffer_new ( DEF_OUNIT ) ;
298
340
let renderer = hoedown_html_renderer_new ( 0 , 0 ) ;
299
341
let mut opaque = MyOpaque {
300
342
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 ,
302
346
} ;
303
347
( * ( * renderer) . opaque ) . opaque = & mut opaque as * mut _ as * mut libc:: c_void ;
304
348
( * renderer) . blockcode = Some ( block) ;
305
349
( * renderer) . header = Some ( header) ;
350
+ ( * renderer) . math = Some ( math) ;
306
351
307
352
let document = hoedown_document_new ( renderer, hoedown_extensions ( ) , 16 ) ;
308
353
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 {
322
367
} ) ;
323
368
}
324
369
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
+
325
374
ret
326
375
}
327
376
}
0 commit comments