|
25 | 25 |
|
26 | 26 | #![allow(non_camel_case_types)]
|
27 | 27 |
|
| 28 | +use libc; |
| 29 | +use std::slice; |
| 30 | + |
28 | 31 | use std::ascii::AsciiExt;
|
29 | 32 | use std::cell::RefCell;
|
30 | 33 | use std::collections::{HashMap, VecDeque};
|
@@ -357,6 +360,194 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> {
|
357 | 360 | }
|
358 | 361 | }
|
359 | 362 |
|
| 363 | +const DEF_OUNIT: libc::size_t = 64; |
| 364 | +const HOEDOWN_EXT_NO_INTRA_EMPHASIS: libc::c_uint = 1 << 11; |
| 365 | +const HOEDOWN_EXT_TABLES: libc::c_uint = 1 << 0; |
| 366 | +const HOEDOWN_EXT_FENCED_CODE: libc::c_uint = 1 << 1; |
| 367 | +const HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3; |
| 368 | +const HOEDOWN_EXT_STRIKETHROUGH: libc::c_uint = 1 << 4; |
| 369 | +const HOEDOWN_EXT_SUPERSCRIPT: libc::c_uint = 1 << 8; |
| 370 | +const HOEDOWN_EXT_FOOTNOTES: libc::c_uint = 1 << 2; |
| 371 | + |
| 372 | +const HOEDOWN_EXTENSIONS: libc::c_uint = |
| 373 | + HOEDOWN_EXT_NO_INTRA_EMPHASIS | HOEDOWN_EXT_TABLES | |
| 374 | + HOEDOWN_EXT_FENCED_CODE | HOEDOWN_EXT_AUTOLINK | |
| 375 | + HOEDOWN_EXT_STRIKETHROUGH | HOEDOWN_EXT_SUPERSCRIPT | |
| 376 | + HOEDOWN_EXT_FOOTNOTES; |
| 377 | + |
| 378 | +enum hoedown_document {} |
| 379 | + |
| 380 | +type blockcodefn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, |
| 381 | + *const hoedown_buffer, *const hoedown_renderer_data, |
| 382 | + libc::size_t); |
| 383 | + |
| 384 | +type blockquotefn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, |
| 385 | + *const hoedown_renderer_data, libc::size_t); |
| 386 | + |
| 387 | +type headerfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, |
| 388 | + libc::c_int, *const hoedown_renderer_data, |
| 389 | + libc::size_t); |
| 390 | + |
| 391 | +type blockhtmlfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, |
| 392 | + *const hoedown_renderer_data, libc::size_t); |
| 393 | + |
| 394 | +type codespanfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, |
| 395 | + *const hoedown_renderer_data, libc::size_t) -> libc::c_int; |
| 396 | + |
| 397 | +type linkfn = extern "C" fn (*mut hoedown_buffer, *const hoedown_buffer, |
| 398 | + *const hoedown_buffer, *const hoedown_buffer, |
| 399 | + *const hoedown_renderer_data, libc::size_t) -> libc::c_int; |
| 400 | + |
| 401 | +type entityfn = extern "C" fn (*mut hoedown_buffer, *const hoedown_buffer, |
| 402 | + *const hoedown_renderer_data, libc::size_t); |
| 403 | + |
| 404 | +type normaltextfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, |
| 405 | + *const hoedown_renderer_data, libc::size_t); |
| 406 | + |
| 407 | +#[repr(C)] |
| 408 | +struct hoedown_renderer_data { |
| 409 | + opaque: *mut libc::c_void, |
| 410 | +} |
| 411 | + |
| 412 | +#[repr(C)] |
| 413 | +struct hoedown_renderer { |
| 414 | + opaque: *mut libc::c_void, |
| 415 | + |
| 416 | + blockcode: Option<blockcodefn>, |
| 417 | + blockquote: Option<blockquotefn>, |
| 418 | + header: Option<headerfn>, |
| 419 | + |
| 420 | + other_block_level_callbacks: [libc::size_t; 11], |
| 421 | + |
| 422 | + blockhtml: Option<blockhtmlfn>, |
| 423 | + |
| 424 | + /* span level callbacks - NULL or return 0 prints the span verbatim */ |
| 425 | + autolink: libc::size_t, // unused |
| 426 | + codespan: Option<codespanfn>, |
| 427 | + other_span_level_callbacks_1: [libc::size_t; 7], |
| 428 | + link: Option<linkfn>, |
| 429 | + other_span_level_callbacks_2: [libc::size_t; 6], |
| 430 | + |
| 431 | + /* low level callbacks - NULL copies input directly into the output */ |
| 432 | + entity: Option<entityfn>, |
| 433 | + normal_text: Option<normaltextfn>, |
| 434 | + |
| 435 | + /* header and footer */ |
| 436 | + other_callbacks: [libc::size_t; 2], |
| 437 | +} |
| 438 | + |
| 439 | +#[repr(C)] |
| 440 | +struct hoedown_html_renderer_state { |
| 441 | + opaque: *mut libc::c_void, |
| 442 | + toc_data: html_toc_data, |
| 443 | + flags: libc::c_uint, |
| 444 | + link_attributes: Option<extern "C" fn(*mut hoedown_buffer, |
| 445 | + *const hoedown_buffer, |
| 446 | + *const hoedown_renderer_data)>, |
| 447 | +} |
| 448 | + |
| 449 | +#[repr(C)] |
| 450 | +struct html_toc_data { |
| 451 | + header_count: libc::c_int, |
| 452 | + current_level: libc::c_int, |
| 453 | + level_offset: libc::c_int, |
| 454 | + nesting_level: libc::c_int, |
| 455 | +} |
| 456 | + |
| 457 | +#[repr(C)] |
| 458 | +struct hoedown_buffer { |
| 459 | + data: *const u8, |
| 460 | + size: libc::size_t, |
| 461 | + asize: libc::size_t, |
| 462 | + unit: libc::size_t, |
| 463 | +} |
| 464 | + |
| 465 | +extern { |
| 466 | + fn hoedown_html_renderer_new(render_flags: libc::c_uint, |
| 467 | + nesting_level: libc::c_int) |
| 468 | + -> *mut hoedown_renderer; |
| 469 | + fn hoedown_html_renderer_free(renderer: *mut hoedown_renderer); |
| 470 | + |
| 471 | + fn hoedown_document_new(rndr: *const hoedown_renderer, |
| 472 | + extensions: libc::c_uint, |
| 473 | + max_nesting: libc::size_t) -> *mut hoedown_document; |
| 474 | + fn hoedown_document_render(doc: *mut hoedown_document, |
| 475 | + ob: *mut hoedown_buffer, |
| 476 | + document: *const u8, |
| 477 | + doc_size: libc::size_t); |
| 478 | + fn hoedown_document_free(md: *mut hoedown_document); |
| 479 | + |
| 480 | + fn hoedown_buffer_new(unit: libc::size_t) -> *mut hoedown_buffer; |
| 481 | + fn hoedown_buffer_free(b: *mut hoedown_buffer); |
| 482 | +} |
| 483 | + |
| 484 | +impl hoedown_buffer { |
| 485 | + fn as_bytes(&self) -> &[u8] { |
| 486 | + unsafe { slice::from_raw_parts(self.data, self.size as usize) } |
| 487 | + } |
| 488 | +} |
| 489 | + |
| 490 | +pub fn old_find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span) { |
| 491 | + extern fn block(_ob: *mut hoedown_buffer, |
| 492 | + text: *const hoedown_buffer, |
| 493 | + lang: *const hoedown_buffer, |
| 494 | + data: *const hoedown_renderer_data, |
| 495 | + line: libc::size_t) { |
| 496 | + unsafe { |
| 497 | + if text.is_null() { return } |
| 498 | + let block_info = if lang.is_null() { |
| 499 | + LangString::all_false() |
| 500 | + } else { |
| 501 | + let lang = (*lang).as_bytes(); |
| 502 | + let s = str::from_utf8(lang).unwrap(); |
| 503 | + LangString::parse(s) |
| 504 | + }; |
| 505 | + if !block_info.rust { return } |
| 506 | + let opaque = (*data).opaque as *mut hoedown_html_renderer_state; |
| 507 | + let tests = &mut *((*opaque).opaque as *mut ::test::Collector); |
| 508 | + let line = tests.get_line() + line; |
| 509 | + let filename = tests.get_filename(); |
| 510 | + tests.add_old_test(line, filename); |
| 511 | + } |
| 512 | + } |
| 513 | + |
| 514 | + extern fn header(_ob: *mut hoedown_buffer, |
| 515 | + text: *const hoedown_buffer, |
| 516 | + level: libc::c_int, data: *const hoedown_renderer_data, |
| 517 | + _: libc::size_t) { |
| 518 | + unsafe { |
| 519 | + let opaque = (*data).opaque as *mut hoedown_html_renderer_state; |
| 520 | + let tests = &mut *((*opaque).opaque as *mut ::test::Collector); |
| 521 | + if text.is_null() { |
| 522 | + tests.register_header("", level as u32); |
| 523 | + } else { |
| 524 | + let text = (*text).as_bytes(); |
| 525 | + let text = str::from_utf8(text).unwrap(); |
| 526 | + tests.register_header(text, level as u32); |
| 527 | + } |
| 528 | + } |
| 529 | + } |
| 530 | + |
| 531 | + tests.set_position(position); |
| 532 | + |
| 533 | + unsafe { |
| 534 | + let ob = hoedown_buffer_new(DEF_OUNIT); |
| 535 | + let renderer = hoedown_html_renderer_new(0, 0); |
| 536 | + (*renderer).blockcode = Some(block); |
| 537 | + (*renderer).header = Some(header); |
| 538 | + (*((*renderer).opaque as *mut hoedown_html_renderer_state)).opaque |
| 539 | + = tests as *mut _ as *mut libc::c_void; |
| 540 | + |
| 541 | + let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16); |
| 542 | + hoedown_document_render(document, ob, doc.as_ptr(), |
| 543 | + doc.len() as libc::size_t); |
| 544 | + hoedown_document_free(document); |
| 545 | + |
| 546 | + hoedown_html_renderer_free(renderer); |
| 547 | + hoedown_buffer_free(ob); |
| 548 | + } |
| 549 | +} |
| 550 | + |
360 | 551 | pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span) {
|
361 | 552 | tests.set_position(position);
|
362 | 553 |
|
|
0 commit comments