1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
//! Convert HTML to text formats.
//!
//! This crate renders HTML into a text format, wrapped to a specified width.
//! This can either be plain text or with extra annotations to (for example)
//! show in a terminal which supports colours.
//!
//! # Examples
//!
//! ```rust
//! # use html2text::from_read;
//! let html = b"
//!        <ul>
//!          <li>Item one</li>
//!          <li>Item two</li>
//!          <li>Item three</li>
//!        </ul>";
//! assert_eq!(from_read(&html[..], 20),
//!            "\
//! * Item one
//! * Item two
//! * Item three
//! ");
//! ```
//! A couple of simple demonstration programs are included as examples:
//!
//! ### html2text
//!
//! The simplest example uses `from_read` to convert HTML on stdin into plain
//! text:
//!
//! ```sh
//! $ cargo run --example html2text < foo.html
//! [...]
//! ```
//!
//! ### html2term
//!
//! A very simple example of using the rich interface (`from_read_rich`) for a
//! slightly interactive console HTML viewer is provided as `html2term`.
//!
//! ```sh
//! $ cargo run --example html2term foo.html
//! [...]
//! ```
//!
//! Note that this example takes the HTML file as a parameter so that it can
//! read keys from stdin.
//!

#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
#![deny(missing_docs)]

#[macro_use]
extern crate html5ever_atoms;
extern crate html5ever;
extern crate unicode_width;
extern crate backtrace;

#[macro_use]
mod macros;

pub mod render;

use render::Renderer;
use render::text_renderer::{TextRenderer,PlainDecorator,RichDecorator,
                            RichAnnotation,TaggedLine};

use std::io;
use std::io::Write;
use std::cmp::max;
use std::iter::{once,repeat};
use html5ever::{parse_document};
use html5ever::driver::ParseOpts;
use html5ever::tree_builder::TreeBuilderOpts;
use html5ever::rcdom::{RcDom,Handle,Text,Element,Document,Comment};
use html5ever::tendril::TendrilSink;

/// A dummy writer which does nothing
struct Discard {}
impl Write for Discard {
    fn write(&mut self, bytes: &[u8]) -> std::result::Result<usize, io::Error> { Ok(bytes.len()) }
    fn flush(&mut self) -> std::result::Result<(), io::Error> { Ok(()) }
}

fn get_text(handle: Handle) -> String {
    let node = handle.borrow();
    let mut result = String::new();
    if let Text(ref tstr) = node.node {
        result.push_str(tstr);
    } else {
        for child in &node.children {
            result.push_str(&get_text(child.clone()));
        }
    }
    result
}

fn render_block<T:Write, R:Renderer>(builder: &mut R, handle: Handle,
                         err_out: &mut T) {
    builder.start_block();
    render_children(builder, handle, err_out);
    builder.end_block();
}

fn render_pre<T:Write, R:Renderer>(builder: &mut R, handle: Handle, _: &mut T) {
    builder.add_preformatted_block(&get_text(handle));
}

fn render_children<T:Write, R:Renderer>(builder: &mut R, handle: Handle,
                            err_out: &mut T) {
    for child in &handle.borrow().children {
        dom_to_string(builder, child.clone(), err_out);
    }
}

fn dom_to_string<T:Write, R:Renderer>(builder: &mut R, handle: Handle,
                          err_out: &mut T) {
    let node = handle.borrow();
    match node.node {
        Document | Comment(_) => {},
        Element(ref name, _, ref attrs) => {
            match *name {
                qualname!(html, "html") |
                qualname!(html, "span") |
                qualname!(html, "body") => {
                    /* process children, but don't add anything */
                },
                qualname!(html, "link") |
                qualname!(html, "meta") |
                qualname!(html, "hr") |
                qualname!(html, "script") |
                qualname!(html, "style") |
                qualname!(html, "head") => {
                    /* Ignore the head and its children */
                    return;
                },
                qualname!(html, "a") => {
                    let mut target = None;
                    for attr in attrs {
                        if &attr.name.local == "href" {
                            target = Some(&*attr.value);
                            break;
                        }
                    }
                    if let Some(href) = target {
                        builder.start_link(href);
                        render_children(builder, handle.clone(), err_out);
                        builder.end_link();
                    } else {
                        render_children(builder, handle.clone(), err_out);
                    }
                    return;
                },
                qualname!(html, "em") => {
                    builder.start_emphasis();
                    render_children(builder, handle.clone(), err_out);
                    builder.end_emphasis();
                    return;
                },
                qualname!(html, "img") => {
                    let mut title = None;
                    for attr in attrs {
                        if &attr.name.local == "alt" {
                            title = Some(&*attr.value);
                            break;
                        }
                    }
                    if let Some(title) = title {
                        builder.add_image(title);
                    }
                    return;
                },
                qualname!(html, "h1") |
                qualname!(html, "h2") |
                qualname!(html, "h3") |
                qualname!(html, "h4") |
                qualname!(html, "p") => {
                    render_block(builder, handle.clone(), err_out);
                    return;
                },
                qualname!(html, "div") => {
                    builder.new_line();
                    render_children(builder, handle.clone(), err_out);
                    builder.new_line();
                    return;
                },
                qualname!(html, "pre") => {
                    return render_pre(builder, handle.clone(), err_out);
                },
                qualname!(html, "br") => {
                    builder.add_empty_line();
                    return;
                }
                qualname!(html, "table") => return render_table(builder, handle.clone(), err_out),
                qualname!(html, "blockquote") => return render_blockquote(builder, handle.clone(), err_out),
                qualname!(html, "ul") => return render_ul(builder, handle.clone(), err_out),
                qualname!(html, "ol") => return render_ol(builder, handle.clone(), err_out),
                _ => {
                    write!(err_out, "Unhandled element: {:?}\n", name.local).unwrap();
                },
            }
          },
        Text(ref tstr) => {
            builder.add_inline_text(tstr);
            return;
        }
        _ => { write!(err_out, "Unhandled: {:?}\n", node).unwrap(); },
    }
    render_children(builder, handle.clone(), err_out);
}

#[derive(Debug)]
struct TableCell {
    colspan: usize,
    content: Handle,
}

#[derive(Debug)]
struct TableRow {
    cells: Vec<TableCell>,
}

#[derive(Debug)]
struct Table {
    rows: Vec<TableRow>,
}

impl Table {
    pub fn new() -> Table {
        Table{ rows: Vec::new() }
    }
    pub fn push(&mut self, row: TableRow) {
        self.rows.push(row);
    }
    pub fn rows(&self) -> std::slice::Iter<TableRow> {
        self.rows.iter()
    }
}

impl TableRow {
    pub fn new() -> TableRow {
        TableRow{ cells: Vec::new() }
    }
    pub fn push(&mut self, cell: TableCell) {
        self.cells.push(cell);
    }
    pub fn cells(&self) -> std::slice::Iter<TableCell> {
        self.cells.iter()
    }
    /// Return an iterator over (column, &cell)s, which
    /// takes into account colspan.
    pub fn cell_columns(&self) -> Vec<(usize, &TableCell)> {
        let mut result = Vec::new();
        let mut colno = 0;
        for cell in &self.cells {
            result.push((colno, cell));
            colno += cell.colspan;
        }
        result
    }
    /// Count the number of cells in the row.
    /// Takes into account colspan.
    pub fn num_cells(&self) -> usize {
        self.cells.iter().map(|cell| cell.colspan).sum()
    }
}

impl TableCell {
    pub fn new(s: Handle) -> TableCell {
        if let Element(_, _, ref attrs) = s.borrow().node {
            let mut colspan = 1;
            for attr in attrs {
                if &attr.name.local == "colspan" {
                    let v:&str = &*attr.value;
                    colspan = v.parse().unwrap_or(1);
                } else {
                    //println!("Attr: {:?}", attr);
                }
            }
            TableCell{ content: s.clone(), colspan: colspan }
        } else {
            panic!("TableCell::new received a non-Element");
        }
    }
    pub fn render<T:Write, R:Renderer>(&self, builder: &mut R, err_out: &mut T)
    {
        dom_to_string(builder, self.content.clone(), err_out)
    }
}

fn handle_td(handle: Handle) -> TableCell {
    TableCell::new(handle)
}

fn handle_tr<T:Write>(handle: Handle, _: &mut T) -> TableRow {
    let node = handle.borrow();

    let mut row = TableRow::new();

    for child in &node.children {
        match child.borrow().node {
            Element(ref name, _, _) => {
                match *name {
                    qualname!(html, "th") |
                    qualname!(html, "td") => {
                        row.push(handle_td(child.clone()));
                    },
                    _ => println!("  [[tr child: {:?}]]", name),
                }
            },
            Comment(_) => {},
            _ => { html_trace!("Unhandled in table: {:?}\n", node); },
        }
    }

    row
}

fn handle_tbody<T:Write, R:Renderer>(builder: &mut R, handle: Handle, err_out: &mut T) {
    let node = handle.borrow();

    let mut table = Table::new();

    for child in &node.children {
        match child.borrow().node {
            Element(ref name, _, _) => {
                match *name {
                    qualname!(html, "tr") => {
                        table.push(handle_tr(child.clone(), err_out));
                    },
                    _ => println!("  [[tbody child: {:?}]]", name),
                }
            },
            Comment(_) => {},
            _ => { html_trace!("Unhandled in table: {:?}\n", node); },
        }
    }

    /* Now lay out the table.  Use the simple option of giving each column
     * same width.  TODO: be cleverer, and handle multi-width cells, etc. */
    let num_columns = table.rows().map(|r| r.num_cells()).max().unwrap();

    /* Heuristic: scale the column widths according to how much content there is. */
    let test_col_width = 1000;  // Render width for measurement; shouldn't make much difference.
    let min_width = 5;
    let mut col_sizes = vec![0usize; num_columns];

    for row in table.rows() {
        let mut colno = 0;
        for cell in row.cells() {
            let mut cellbuilder = builder.new_sub_renderer(test_col_width);
            cell.render(&mut cellbuilder, &mut Discard{});
            let cellsize = cellbuilder.text_len();
            // If the cell has a colspan>1, then spread its size between the
            // columns.
            let col_size = cellsize / cell.colspan;
            for i in 0..cell.colspan {
                col_sizes[colno + i] += col_size;
            }
            colno += cell.colspan;
        }
    }
    let tot_size: usize = col_sizes.iter().sum();
    let width = builder.width();
    let mut col_widths:Vec<usize> = col_sizes.iter()
                                         .map(|sz| {
                                             if *sz == 0 {
                                                 0
                                             } else {
                                                 max(sz * width / tot_size, min_width)
                                             }
                                          }).collect();
    /* The minimums may have put the total width too high */
    while col_widths.iter().cloned().sum::<usize>() > width {
        let (i, _) = col_widths.iter().cloned().enumerate().max_by_key(|k| k.1).unwrap();
        col_widths[i] -= 1;
    }
    if !col_widths.is_empty() {
        // Slight fudge; we're not drawing extreme edges, so one of the columns
        // can gets a free character cell from not having a border.
        // make it the last.
        let last = col_widths.len() - 1;
        col_widths[last] += 1;
    }

    builder.start_block();

    let mut rowline = String::new();
    for width in col_widths.iter().cloned().filter(|w:&usize| *w > 0) {
        rowline.push_str(&(0..(width-1)).map(|_| '-').collect::<String>());
        rowline.push('+');
    }
    if !rowline.is_empty() {
        rowline.pop().unwrap();  // Remove the last '+'.
    }
    builder.add_block_line(&rowline);

    for row in table.rows() {
        let rendered_cells: Vec<R::Sub> = row.cell_columns()
                                             .into_iter()
                                             .flat_map(|(colno, cell)| {
                                                  let col_width:usize = col_widths[colno..colno+cell.colspan]
                                                                     .iter().sum();
                                                  if col_width > 0 {
                                                      let mut cellbuilder = builder.new_sub_renderer(col_width-1);
                                                      cell.render(&mut cellbuilder, err_out);
                                                      Some(cellbuilder)
                                                  } else {
                                                      None
                                                  }
                                              }).collect();
        if rendered_cells.iter().any(|r| !r.empty()) {
            builder.append_columns(rendered_cells, '|');
            builder.add_block_line(&rowline);
        }
    }
}

fn render_table<T:Write, R:Renderer>(builder: &mut R, handle: Handle, err_out: &mut T) {
    let node = handle.borrow();

    for child in &node.children {
        match child.borrow().node {
            Element(ref name, _, _) => {
                match *name {
                    qualname!(html, "tbody") => return handle_tbody(builder, child.clone(), err_out),
                    _ => { writeln!(err_out, "  [[table child: {:?}]]", name).unwrap();},
                }
            },
            Comment(_) => {},
            _ => { html_trace!("Unhandled in table: {:?}\n", node); },
        }
    }
}

fn render_blockquote<T:Write, R:Renderer>(builder: &mut R, handle: Handle, err_out: &mut T) {

    let mut sub_builder = builder.new_sub_renderer(builder.width()-2);
    render_children(&mut sub_builder, handle, err_out);

    builder.start_block();
    builder.append_subrender(sub_builder, repeat("> "));
    builder.end_block();
}

fn render_ul<T:Write, R:Renderer>(builder: &mut R, handle: Handle, err_out: &mut T) {
    let node = handle.borrow();

    builder.start_block();

    for child in &node.children {
        match child.borrow().node {
            Element(ref name, _, _) => {
                match *name {
                    qualname!(html, "li") => {
                        let mut sub_builder = builder.new_sub_renderer(builder.width()-2);
                        render_block(&mut sub_builder, child.clone(), err_out);
                        builder.append_subrender(sub_builder, once("* ").chain(repeat("  ")));
                    },
                    _ => println!("  [[ul child: {:?}]]", name),
                }
            },
            Comment(_) => {},
            _ => { html_trace!("Unhandled in table: {:?}\n", node); },
        }
    }
}

/// Count children of a particular element type
fn count_li_children(handle: Handle) -> usize {
    handle.borrow()
          .children
          .iter()
          .filter(|child| {
                     if let Element(qualname!(html, "li"), _, _) = child.borrow().node {
                         true
                     } else {
                         false
                     }
                   })
          .count()
}

fn render_ol<T:Write, R:Renderer>(builder: &mut R, handle: Handle, err_out: &mut T) {
    let num_items = count_li_children(handle.clone());
    let node = handle.borrow();

    builder.start_block();

    let prefix_width = format!("{}", num_items).len() + 2;

    let mut i = 1;
    let prefixn = format!("{: <width$}", "", width=prefix_width);
    for child in &node.children {
        match child.borrow().node {
            Element(ref name, _, _) => {
                match *name {
                    qualname!(html, "li") => {
                        let mut sub_builder = builder.new_sub_renderer(builder.width()-prefix_width);
                        render_block(&mut sub_builder, child.clone(), err_out);
                        let prefix1 = format!("{}.", i);
                        let prefix1 = format!("{: <width$}", prefix1, width=prefix_width);
                        builder.append_subrender(sub_builder, once(prefix1.as_str()).chain(repeat(prefixn.as_str())));
                        i += 1;
                    },
                    _ => println!("  [[ol child: {:?}]]", name),
                }
            },
            Comment(_) => {},
            _ => { html_trace!("Unhandled in table: {:?}\n", node); },
        }
    }
}

/// Reads HTML from `input`, and returns a `String` with text wrapped to
/// `width` columns.
pub fn from_read<R>(mut input: R, width: usize) -> String where R: io::Read {
    let opts = ParseOpts {
        tree_builder: TreeBuilderOpts {
            drop_doctype: true,
            ..Default::default()
        },
        ..Default::default()
    };
    let dom = parse_document(RcDom::default(), opts)
                   .from_utf8()
                   .read_from(&mut input)
                   .unwrap();

    let decorator = PlainDecorator::new();
    let mut builder = TextRenderer::new(width, decorator);
    dom_to_string(&mut builder, dom.document, &mut Discard{} /* &mut io::stderr()*/);
    builder.into_string()
}

/// Reads HTML from `input`, and returns text wrapped to `width` columns.
/// The text is returned as a `Vec<TaggedLine<_>>`; the annotations are vectors
/// of `RichAnnotation`.  The "outer" annotation comes first in the `Vec`.
pub fn from_read_rich<R>(mut input: R, width: usize) -> Vec<TaggedLine<Vec<RichAnnotation>>>
        where R: io::Read
{
    let opts = ParseOpts {
        tree_builder: TreeBuilderOpts {
            drop_doctype: true,
            ..Default::default()
        },
        ..Default::default()
    };
    let dom = parse_document(RcDom::default(), opts)
                   .from_utf8()
                   .read_from(&mut input)
                   .unwrap();

    let decorator = RichDecorator::new();
    let mut builder = TextRenderer::new(width, decorator);
    dom_to_string(&mut builder, dom.document, &mut Discard{} /* &mut io::stderr()*/);
    builder.into_lines()
}

#[cfg(test)]
mod tests {
    use super::{from_read};

    /// Like assert_eq!(), but prints out the results normally as well
    macro_rules! assert_eq_str {
        ($a:expr, $b:expr) => {
            if $a != $b {
                println!("<<<\n{}===\n{}>>>", $a, $b);
                assert_eq!($a, $b);
            }
        }
    }
    fn test_html(input: &[u8], expected: &str, width: usize) {
        assert_eq_str!(from_read(input, width), expected);
    }

    #[test]
    fn test_table() {
        assert_eq!(from_read(&br##"
       <table>
         <tr>
           <td>1</td>
           <td>2</td>
           <td>3</td>
         </tr>
       </table>
"##[..], 12), r#"---+---+----
1  |2  |3   
---+---+----
"#);
     }

     #[test]
     fn test_colspan() {
        assert_eq!(from_read(&br##"
       <table>
         <tr>
           <td>1</td>
           <td>2</td>
           <td>3</td>
         </tr>
         <tr>
           <td colspan="2">12</td>
           <td>3</td>
         </tr>
         <tr>
           <td>1</td>
           <td colspan="2">23</td>
         </tr>
       </table>
"##[..], 12), r#"---+---+----
1  |2  |3   
---+---+----
12     |3   
---+---+----
1  |23      
---+---+----
"#);
     }

     #[test]
     fn test_para() {
        assert_eq_str!(from_read(&b"<p>Hello</p>"[..], 10),
                   "Hello\n");
     }

     #[test]
     fn test_para2() {
        assert_eq_str!(from_read(&b"<p>Hello, world!</p>"[..], 20),
                   "Hello, world!\n");
     }

     #[test]
     fn test_blockquote() {
        assert_eq_str!(from_read(&br#"<p>Hello</p>
        <blockquote>One, two, three</blockquote>
        <p>foo</p>
"#[..], 12), r#"Hello

> One, two,
> three

foo
"#);
     }

     #[test]
     fn test_ul() {
         test_html(br#"
            <ul>
              <li>Item one</li>
              <li>Item two</li>
              <li>Item three</li>
            </ul>
         "#, r#"* Item one
* Item two
* Item
  three
"#, 10);
     }

     #[test]
     fn test_strip_nl() {
         test_html(br#"
            <p>
               One
               Two
               Three
            </p>
         "#, "One Two Three\n", 40);
     }
     #[test]
     fn test_strip_nl2() {
         test_html(br#"
            <p>
               One
               <span>
                   Two
               </span>
               Three
            </p>
         "#, "One Two Three\n", 40);
     }
     #[test]
     fn test_strip_nl_tbl() {
         test_html(br#"
           <table>
             <tr>
                <td>
                   One
                   <span>
                       Two
                   </span>
                   Three
                </td>
              </tr>
            </table>
         "#, r"--------------------
One Two Three       
--------------------
", 20);
     }
     #[test]
     fn test_strip_nl_tbl_p() {
         test_html(br#"
           <table>
             <tr>
                <td><p>
                   One
                   <span>
                       Two
                   </span>
                   Three
                </p></td>
              </tr>
            </table>
         "#, r"--------------------
One Two Three       
--------------------
", 20);
     }
     #[test]
     fn test_pre() {
         test_html(br#"
           <pre>foo
    bar
  wib   asdf;
</pre>
<p>Hello</p>
         "#, r"foo
    bar
  wib   asdf;

Hello
", 20);
    }
     #[test]
     fn test_link() {
         test_html(br#"
           <p>Hello, <a href="http://www.example.com/">world</a></p>"#, r"Hello, [world][1]

[1] http://www.example.com/
", 80);
    }
     #[test]
     fn test_link2() {
         test_html(br#"
           <p>Hello, <a href="http://www.example.com/">world</a>!</p>"#, r"Hello, [world][1]!

[1] http://www.example.com/
", 80);
     }

     #[test]
     fn test_link3() {
         test_html(br#"
           <p>Hello, <a href="http://www.example.com/">w</a>orld</p>"#, r"Hello, [w][1]orld

[1] http://www.example.com/
", 80);
     }

     #[test]
     fn test_link_wrap() {
         test_html(br#"
           <a href="http://www.example.com/">Hello</a>"#, r"[Hello][1]

[1] http:/
/www.examp
le.com/
", 10);
     }

     #[test]
     fn test_wrap() {
         test_html(br"<p>Hello, world.  Superlongwordreally</p>",
                   r#"Hello,
world.
Superlon
gwordrea
lly
"#, 8);
     }

     #[test]
     fn test_wrap2() {
         test_html(br"<p>Hello, world.  This is a long sentence with a
few words, which we want to be wrapped correctly.</p>",
r#"Hello, world. This
is a long sentence
with a few words,
which we want to be
wrapped correctly.
"#, 20);
     }

     #[test]
     fn test_wrap3() {
         test_html(br#"<p><a href="dest">http://example.org/blah/</a> one two three"#,
r#"[http://example.org/blah/
][1] one two three

[1] dest
"#, 25);
     }

     #[test]
     fn test_div() {
         test_html(br"<p>Hello</p><div>Div</div>",
r#"Hello

Div
"#, 20);
         test_html(br"<p>Hello</p><div>Div</div><div>Div2</div>",
r#"Hello

Div
Div2
"#, 20);
     }

     #[test]
     fn test_img_alt() {
         test_html(br"<p>Hello <img src='foo.jpg' alt='world'></p>",
                   "Hello [world]\n", 80);
     }
}