@@ -56,6 +56,7 @@ static HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3;
56
56
static HOEDOWN_EXT_STRIKETHROUGH : libc:: c_uint = 1 << 4 ;
57
57
static HOEDOWN_EXT_SUPERSCRIPT : libc:: c_uint = 1 << 8 ;
58
58
static HOEDOWN_EXT_FOOTNOTES : libc:: c_uint = 1 << 2 ;
59
+ static HOEDOWN_EXT_MATH : libc:: c_uint = 1 << 13 ;
59
60
60
61
static HOEDOWN_EXTENSIONS : libc:: c_uint =
61
62
HOEDOWN_EXT_NO_INTRA_EMPHASIS | HOEDOWN_EXT_TABLES |
@@ -64,6 +65,9 @@ static HOEDOWN_EXTENSIONS: libc::c_uint =
64
65
HOEDOWN_EXT_FOOTNOTES ;
65
66
66
67
type hoedown_document = libc:: c_void ; // this is opaque to us
68
+ type hoedown_realloc_callback = extern "C" fn ( * mut libc:: c_void , libc:: size_t )
69
+ -> * mut libc:: size_t ;
70
+ type hoedown_free_callback = extern "C" fn ( * mut libc:: c_void ) ;
67
71
68
72
#[ repr( C ) ]
69
73
struct hoedown_renderer {
@@ -76,7 +80,10 @@ struct hoedown_renderer {
76
80
* mut libc:: c_void ) > ,
77
81
header : Option < extern "C" fn ( * mut hoedown_buffer , * const hoedown_buffer ,
78
82
libc:: c_int , * mut libc:: c_void ) > ,
79
- other : [ libc:: size_t , ..28 ] ,
83
+ other1 : [ libc:: size_t , ..24 ] ,
84
+ math : Option < extern "C" fn ( * mut hoedown_buffer , * const hoedown_buffer ,
85
+ libc:: c_int , * mut libc:: c_void ) -> libc:: c_int > ,
86
+ other2 : [ libc:: size_t , ..4 ] ,
80
87
}
81
88
82
89
#[ repr( C ) ]
@@ -101,6 +108,8 @@ struct MyOpaque {
101
108
dfltblk : extern "C" fn ( * mut hoedown_buffer , * const hoedown_buffer ,
102
109
* const hoedown_buffer , * mut libc:: c_void ) ,
103
110
toc_builder : Option < TocBuilder > ,
111
+ math_enabled : bool ,
112
+ math_seen : bool ,
104
113
}
105
114
106
115
#[ repr( C ) ]
@@ -109,6 +118,9 @@ struct hoedown_buffer {
109
118
size : libc:: size_t ,
110
119
asize : libc:: size_t ,
111
120
unit : libc:: size_t ,
121
+ data_realloc : Option < hoedown_realloc_callback > ,
122
+ data_free : Option < hoedown_free_callback > ,
123
+ buffer_free : Option < hoedown_free_callback > ,
112
124
}
113
125
114
126
// hoedown FFI
@@ -130,8 +142,13 @@ extern {
130
142
131
143
fn hoedown_buffer_new ( unit : libc:: size_t ) -> * mut hoedown_buffer ;
132
144
fn hoedown_buffer_puts ( b : * mut hoedown_buffer , c : * const libc:: c_char ) ;
133
- fn hoedown_buffer_free ( b : * mut hoedown_buffer ) ;
145
+ fn hoedown_buffer_put ( b : * mut hoedown_buffer , data : * const libc :: c_void , len : libc :: size_t ) ;
134
146
147
+ fn hoedown_buffer_free ( b : * mut hoedown_buffer ) ;
148
+ fn hoedown_escape_html ( ob : * mut hoedown_buffer ,
149
+ src : * const libc:: uint8_t ,
150
+ size : libc:: size_t ,
151
+ secure : libc:: c_int ) ;
135
152
}
136
153
137
154
/// Returns Some(code) if `s` is a line that should be stripped from
@@ -147,10 +164,23 @@ fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
147
164
}
148
165
}
149
166
167
+ fn hoedown_extensions ( ) -> libc:: c_uint {
168
+ let mut extensions = HOEDOWN_EXTENSIONS ;
169
+
170
+ match use_mathjax. get ( ) . as_ref ( ) {
171
+ Some ( use_math) if * * use_math => { extensions |= HOEDOWN_EXT_MATH ; }
172
+ _ => { }
173
+ }
174
+
175
+ extensions
176
+ }
177
+
150
178
local_data_key ! ( used_header_map: RefCell <HashMap <String , uint>>)
151
179
local_data_key ! ( test_idx: Cell <uint>)
152
180
// None == render an example, but there's no crate name
153
181
local_data_key ! ( pub playground_krate: Option <String >)
182
+ local_data_key ! ( pub use_mathjax: bool )
183
+ local_data_key ! ( pub math_seen: bool )
154
184
155
185
pub fn render ( w : & mut fmt:: Formatter , s : & str , print_toc : bool ) -> fmt:: Result {
156
186
extern fn block ( ob : * mut hoedown_buffer , orig_text : * const hoedown_buffer ,
@@ -268,18 +298,49 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
268
298
text. with_c_str ( |p| unsafe { hoedown_buffer_puts ( ob, p) } ) ;
269
299
}
270
300
301
+ extern fn math ( ob : * mut hoedown_buffer , text : * const hoedown_buffer ,
302
+ display_mode : libc:: c_int , opaque : * mut libc:: c_void ) -> libc:: c_int {
303
+ let opaque = opaque as * mut hoedown_html_renderer_state ;
304
+ let opaque = unsafe { & mut * ( ( * opaque) . opaque as * mut MyOpaque ) } ;
305
+
306
+ opaque. math_seen = true ;
307
+
308
+ let ( open, close) = if !opaque. math_enabled {
309
+ ( "$$" , "$$" )
310
+ } else if display_mode == 1 {
311
+ ( "\\ [" , "\\ ]" )
312
+ } else {
313
+ ( "\\ (" , "\\ )" )
314
+ } ;
315
+
316
+ open. with_c_str ( |open| {
317
+ close. with_c_str ( |close| {
318
+ unsafe {
319
+ hoedown_buffer_put ( ob, open as * const libc:: c_void , 2 ) ;
320
+ hoedown_escape_html ( ob, ( * text) . data , ( * text) . size , 0 ) ;
321
+ hoedown_buffer_put ( ob, close as * const libc:: c_void , 2 ) ;
322
+ }
323
+ } )
324
+ } ) ;
325
+
326
+ 1
327
+ }
328
+
271
329
unsafe {
272
330
let ob = hoedown_buffer_new ( DEF_OUNIT ) ;
273
331
let renderer = hoedown_html_renderer_new ( 0 , 0 ) ;
274
332
let mut opaque = MyOpaque {
275
333
dfltblk : ( * renderer) . blockcode . unwrap ( ) ,
276
- toc_builder : if print_toc { Some ( TocBuilder :: new ( ) ) } else { None }
334
+ toc_builder : if print_toc { Some ( TocBuilder :: new ( ) ) } else { None } ,
335
+ math_enabled : use_mathjax. get ( ) . map_or ( false , |x| * x) ,
336
+ math_seen : false ,
277
337
} ;
278
338
( * ( * renderer) . opaque ) . opaque = & mut opaque as * mut _ as * mut libc:: c_void ;
279
339
( * renderer) . blockcode = Some ( block) ;
280
340
( * renderer) . header = Some ( header) ;
341
+ ( * renderer) . math = Some ( math) ;
281
342
282
- let document = hoedown_document_new ( renderer, HOEDOWN_EXTENSIONS , 16 ) ;
343
+ let document = hoedown_document_new ( renderer, hoedown_extensions ( ) , 16 ) ;
283
344
hoedown_document_render ( document, ob, s. as_ptr ( ) ,
284
345
s. len ( ) as libc:: size_t ) ;
285
346
hoedown_document_free ( document) ;
@@ -297,6 +358,10 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
297
358
} ) ;
298
359
}
299
360
hoedown_buffer_free ( ob) ;
361
+
362
+ let old = math_seen. get ( ) . map_or ( false , |x| * x) ;
363
+ math_seen. replace ( Some ( old || opaque. math_seen ) ) ;
364
+
300
365
ret
301
366
}
302
367
}
@@ -357,7 +422,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
357
422
( * renderer) . header = Some ( header) ;
358
423
( * ( * renderer) . opaque ) . opaque = tests as * mut _ as * mut libc:: c_void ;
359
424
360
- let document = hoedown_document_new ( renderer, HOEDOWN_EXTENSIONS , 16 ) ;
425
+ let document = hoedown_document_new ( renderer, hoedown_extensions ( ) , 16 ) ;
361
426
hoedown_document_render ( document, ob, doc. as_ptr ( ) ,
362
427
doc. len ( ) as libc:: size_t ) ;
363
428
hoedown_document_free ( document) ;
0 commit comments