html2text 0.1.10

Render HTML as plain text.
Documentation
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
//! Implementations of the `Renderer` trait.
//!
//! This module implements helpers and concrete types for rendering from HTML
//! into different text formats.

use unicode_width::{UnicodeWidthStr,UnicodeWidthChar};
use super::Renderer;
use std::mem;
use std::vec;
use std::fmt::Debug;

/// A wrapper around a String with extra metadata.
#[derive(Debug)]
pub struct TaggedString<T:Debug> {
    /// The wrapped text.
    pub s: String,

    /// The metadata.
    pub tag: T,
}

/// An element of a line of tagged text: either a TaggedString or a
/// marker appearing in between document characters.
#[derive(Debug)]
pub enum TaggedLineElement<T:Debug+Eq+PartialEq+Clone> {
    /// A string with tag information attached.
    Str(TaggedString<T>),

    /// A zero-width marker indicating the start of a named HTML fragment.
    FragmentStart(String),
}

/// A line of tagged text (composed of a set of `TaggedString`s).
#[derive(Debug)]
pub struct TaggedLine<T:Debug+Eq+PartialEq+Clone> {
    v: Vec<TaggedLineElement<T>>,
}

impl<T:Debug+Eq+PartialEq+Clone+Default> TaggedLine<T> {
    /// Create an empty `TaggedLine`.
    pub fn new() -> TaggedLine<T> {
        TaggedLine {
            v: Vec::new(),
        }
    }

    /// Create a new TaggedLine from a string and tag.
    pub fn from_string(s: String, tag: &T) -> TaggedLine<T> {
        TaggedLine {
            v: vec![TaggedLineElement::Str(
                TaggedString{ s: s, tag: tag.clone() })],
        }
    }

    /// Join the line into a String, ignoring the tags and markers.
    pub fn into_string(self) -> String {
        let mut s = String::new();
        for tle in self.v {
            if let TaggedLineElement::Str(ts) = tle {
                s.push_str(&ts.s);
            }
        }
        s
    }

    /// Return true if the line is non-empty
    pub fn is_empty(&self) -> bool {
        self.v.len() == 0
    }

    /// Add a new tagged string fragment to the line
    pub fn push_str(&mut self, ts: TaggedString<T>) {
        use self::TaggedLineElement::Str;

        if !self.v.is_empty() {
            if let Str(ref mut ts_prev) = self.v.last_mut().unwrap() {
                if ts_prev.tag == ts.tag {
                    ts_prev.s.push_str(&ts.s);
                    return;
                }
            }
        }
        self.v.push(Str(ts));
    }

    /// Add a new general TaggedLineElement to the line
    pub fn push(&mut self, tle: TaggedLineElement<T>) {
        use self::TaggedLineElement::Str;

        if let Str(ts) = tle {
            self.push_str(ts);
        } else {
            self.v.push(tle);
        }
    }

    /// Add a new fragment to the start of the line
    pub fn insert_front(&mut self, ts: TaggedString<T>) {
        use self::TaggedLineElement::Str;

        self.v.insert(0, Str(ts));
    }

    /// Add text with a particular tag to self
    pub fn push_char(&mut self, c: char, tag: &T) {
        use self::TaggedLineElement::Str;

        if !self.v.is_empty() {
            if let Str(ref mut ts_prev) = self.v.last_mut().unwrap() {
                if ts_prev.tag == *tag {
                    ts_prev.s.push(c);
                    return;
                }
            }
        }
        let mut s = String::new();
        s.push(c);
        self.v.push(Str(TaggedString { s: s, tag: tag.clone() }));
    }

    /// Drain tl and use to extend self.
    pub fn consume(&mut self, tl: &mut TaggedLine<T>) {
        for ts in tl.v.drain(..) {
            self.push(ts);
        }
    }

    /// Drain the contained items
    pub fn drain_all(&mut self) -> vec::Drain<TaggedLineElement<T>> {
        self.v.drain(..)
    }

    /// Iterator over the chars in this line.
    #[cfg_attr(feature="clippy", allow(needless_lifetimes))]
    pub fn chars<'a>(&'a self) -> Box<dyn Iterator<Item=char>+'a> {
        use self::TaggedLineElement::Str;

        Box::new(self.v.iter().flat_map(|tle| {
            if let Str(ts) = tle { ts.s.chars() } else { "".chars() }
        }))
    }

    /// Iterator over TaggedLineElements
    pub fn iter<'a>(&'a self) -> Box<dyn Iterator<Item=&TaggedLineElement<T>>+'a> {
        Box::new(self.v.iter())
    }

    /// Return the width of the line in cells
    pub fn width(&self) -> usize {
        use self::TaggedLineElement::Str;

        let mut result = 0;
        for tle in &self.v {
            if let Str(ts) = tle {
                result += UnicodeWidthStr::width(ts.s.as_str());
            }
        }
        result
    }

    /// Pad this line to width with spaces (or if already at least this wide, do
    /// nothing).
    pub fn pad_to(&mut self, width: usize) {
        use self::TaggedLineElement::Str;

        let my_width = self.width();
        if width > my_width {
            self.v.push(Str(TaggedString{
                s: format!("{: <width$}", "", width=width-my_width),
                tag: T::default(),
            }));
        }
    }
}

/// A type to build up wrapped text, allowing extra metadata for
/// spans.
#[derive(Debug)]
struct WrappedBlock<T:Clone+Eq+Debug+Default> {
    width: usize,
    text: Vec<TaggedLine<T>>,
    textlen: usize,
    line: TaggedLine<T>,
    linelen: usize,
    spacetag: Option<T>,         // Tag for the whitespace before the current word
    word: TaggedLine<T>,         // The current word (with no whitespace).
    wordlen: usize,
}

impl<T:Clone+Eq+Debug+Default> WrappedBlock<T> {
    pub fn new(width: usize) -> WrappedBlock<T> {
        assert!(width > 0);
        WrappedBlock {
            width: width,
            text: Vec::new(),
            textlen: 0,
            line: TaggedLine::new(),
            linelen: 0,
            spacetag: None,
            word: TaggedLine::new(),
            wordlen: 0,
        }
    }

    fn flush_word(&mut self) {
        use self::TaggedLineElement::Str;

        /* Finish the word. */
        html_trace_quiet!("flush_word: word={:?}, linelen={}", self.word, self.linelen);
        if !self.word.is_empty() {
            let space_in_line = self.width - self.linelen;
            let space_needed = self.wordlen +
                        if self.linelen > 0 { 1 } else { 0 }; // space
            if space_needed <= space_in_line {
                if self.linelen > 0 {
                    self.line.push(Str(TaggedString{s: " ".into(), tag: self.spacetag.take().unwrap()}));
                    self.linelen += 1;
                }
                self.line.consume(&mut self.word);
                self.linelen += self.wordlen;
            } else {
                /* Start a new line */
                self.flush_line();
                if self.wordlen <= self.width {
                    let mut new_word = TaggedLine::new();
                    mem::swap(&mut new_word, &mut self.word);
                    mem::swap(&mut self.line, &mut new_word);
                    self.linelen = self.wordlen;
                } else {
                    /* We need to split the word. */
                    let mut wordbits = self.word.drain_all();
                    /* Note: there's always at least one piece */
                    let mut opt_elt = wordbits.next();
                    let mut lineleft = self.width;
                    while let Some(elt) = opt_elt.take() {
                        if let Str(piece) = elt {
                            let w = UnicodeWidthStr::width(piece.s.as_str());
                            if w <= lineleft {
                                self.line.push(Str(piece));
                                lineleft -= w;
                                self.linelen += w;
                                opt_elt = wordbits.next();
                            } else {
                                /* Split into two */
                                let mut split_idx = 0;
                                for (idx,c) in piece.s.char_indices() {
                                    let c_w = UnicodeWidthChar::width(c).unwrap();
                                    if c_w <= lineleft {
                                        lineleft -= c_w;
                                    } else {
                                        split_idx = idx;
                                        break;
                                    }
                                }
                                self.line.push(Str(TaggedString{
                                    s: piece.s[..split_idx].into(),
                                    tag: piece.tag.clone(),
                                }));
                                {
                                    let mut tmp_line = TaggedLine::new();
                                    mem::swap(&mut tmp_line, &mut self.line);
                                    self.text.push(tmp_line);
                                }
                                lineleft = self.width;
                                self.linelen = 0;
                                opt_elt = Some(Str(TaggedString{
                                    s: piece.s[split_idx..].into(),
                                    tag: piece.tag,
                                }));
                            }
                        } else {
                            self.line.push(elt);
                        }
                    }
                }
            }
        }
        self.wordlen = 0;
    }

    fn flush_line(&mut self) {
        if !self.line.is_empty() {
            let mut tmp_line = TaggedLine::new();
            mem::swap(&mut tmp_line, &mut self.line);
            self.text.push(tmp_line);
            self.linelen = 0;
        }
    }

    fn flush(&mut self) {
        self.flush_word();
        self.flush_line();
    }

    /// Consume self and return a vector of lines.
    /*
    pub fn into_untagged_lines(mut self) -> Vec<String> {
        self.flush();

        let mut result = Vec::new();
        for line in self.text.into_iter() {
            let mut line_s = String::new();
            for TaggedString{ s, .. } in line.into_iter() {
                line_s.push_str(&s);
            }
            result.push(line_s);
        }
        result
    }
    */

    /// Consume self and return vector of lines including annotations.
    pub fn into_lines(mut self) -> Vec<TaggedLine<T>> {
        self.flush();

        self.text
    }

    pub fn add_text(&mut self, text: &str, tag: &T) {
        html_trace!("WrappedBlock::add_text({}), {:?}", text, tag);
        for c in text.chars() {
            if c.is_whitespace() {
                /* Whitespace is mostly ignored, except to terminate words. */
                self.flush_word();
                self.spacetag = Some(tag.clone());
            } else if let Some(charwidth) = UnicodeWidthChar::width(c) {
                /* Not whitespace; add to the current word. */
                self.word.push_char(c, tag);
                self.wordlen += charwidth;
            }
            html_trace_quiet!("  Added char {:?}, wordlen={}", c, self.wordlen);
        }
    }

    pub fn add_preformatted_text(&mut self, text: &str, tag: &T) {
        html_trace!("WrappedBlock::add_preformatted_text({}), {:?}", text, tag);
        // Make sure that any previous word has been sent to the line, as we
        // bypass the word buffer.
        self.flush_word();

        for c in text.chars() {
            if let Some(charwidth) = UnicodeWidthChar::width(c) {
                if self.linelen + charwidth > self.width {
                    self.flush_line();
                }
                self.line.push_char(c, tag);
                self.linelen += charwidth;
            } else {
                match c {
                    '\n' => {
                        self.flush_line();
                    }
                    '\t' => {
                        let tab_stop = 8;
                        let mut at_least_one_space = false;
                        while self.linelen % tab_stop != 0 || !at_least_one_space {
                            if self.linelen >= self.width {
                                self.flush_line();
                            } else {
                                self.line.push_char(' ', tag);
                                self.linelen += 1;
                                at_least_one_space = true;
                            }
                        }
                    }
                    _ => {
                        eprintln!("Got character: {:?}", c);
                    }
                }
            }
            html_trace_quiet!("  Added char {:?}", c);
        }
    }

    pub fn add_element(&mut self, elt: TaggedLineElement<T>) {
        self.word.push(elt);
    }

    pub fn text_len(&self) -> usize {
        self.textlen + self.linelen + self.wordlen
    }
}

/// Allow decorating/styling text.
pub trait TextDecorator {
    /// An annotation which can be added to text, and which will
    /// be attached to spans of text.
    type Annotation: Eq+PartialEq+Debug+Clone+Default;

    /// Return an annotation and rendering prefix for a link.
    fn decorate_link_start(&mut self, url: &str) -> (String, Self::Annotation);

    /// Return a suffix for after a link.
    fn decorate_link_end(&mut self) -> String;

    /// Return an annotation and rendering prefix for em
    fn decorate_em_start(&mut self) -> (String, Self::Annotation);

    /// Return a suffix for after an em.
    fn decorate_em_end(&mut self) -> String;

    /// Return an annotation and rendering prefix for strongm
    fn decorate_strong_start(&mut self) -> (String, Self::Annotation);

    /// Return a suffix for after an strong.
    fn decorate_strong_end(&mut self) -> String;

    /// Return an annotation and rendering prefix for code
    fn decorate_code_start(&mut self) -> (String, Self::Annotation);

    /// Return a suffix for after an code.
    fn decorate_code_end(&mut self) -> String;

    /// Return an annotation for the initial part of a preformatted line
    fn decorate_preformat_first(&mut self) -> Self::Annotation;

    /// Return an annotation for a continuation line when a preformatted
    /// line doesn't fit.
    fn decorate_preformat_cont(&mut self) -> Self::Annotation;

    /// Return an annotation and rendering prefix for a link.
    fn decorate_image(&mut self, title: &str) -> (String, Self::Annotation);

    /// Return a new decorator of the same type which can be used
    /// for sub blocks.
    fn make_subblock_decorator(&self) -> Self;

    /// Finish with a document, and return extra lines (eg footnotes)
    /// to add to the rendered text.
    fn finalise(self) -> Vec<TaggedLine<Self::Annotation>>;
}

/// A space on a horizontal row.
#[derive(Copy,Clone,Debug)]
pub enum BorderSegHoriz {
    /// Pure horizontal line
    Straight,
    /// Joined with a line above
    JoinAbove,
    /// Joins with a line below
    JoinBelow,
    /// Joins both ways
    JoinCross,
}

/// A dividing line between table rows which tracks intersections
/// with vertical lines.
#[derive(Clone,Debug)]
pub struct BorderHoriz {
    /// The segments for the line.
    pub segments: Vec<BorderSegHoriz>,
}

impl BorderHoriz {
    /// Create a new blank border line.
    pub fn new(width: usize) -> BorderHoriz {
        BorderHoriz {
            segments: vec![BorderSegHoriz::Straight; width],
        }
    }

    /// Make a join to a line above at the xth cell
    pub fn join_above(&mut self, x: usize) {
        use self::BorderSegHoriz::*;
        let prev = self.segments[x];
        self.segments[x] = match prev {
            Straight | JoinAbove => JoinAbove,
            JoinBelow | JoinCross => JoinCross,
        }
    }

    /// Make a join to a line below at the xth cell
    pub fn join_below(&mut self, x: usize) {
        use self::BorderSegHoriz::*;
        let prev = self.segments[x];
        self.segments[x] = match prev {
            Straight | JoinBelow => JoinBelow,
            JoinAbove | JoinCross => JoinCross,
        }
    }

    /// Merge a (possibly partial) border line below into this one.
    pub fn merge_from_below(&mut self, other: &BorderHoriz, pos: usize) {
        use self::BorderSegHoriz::*;
        for (idx, seg) in other.segments.iter().enumerate()
        {
            match *seg {
                Straight => (),
                JoinAbove | JoinBelow | JoinCross => { self.join_below(idx+pos); },
            }
        }
    }

    /// Merge a (possibly partial) border line above into this one.
    pub fn merge_from_above(&mut self, other: &BorderHoriz, pos: usize) {
        use self::BorderSegHoriz::*;
        for (idx, seg) in other.segments.iter().enumerate()
        {
            match *seg {
                Straight => (),
                JoinAbove | JoinBelow | JoinCross => { self.join_above(idx+pos); },
            }
        }
    }

    /// Return a string of spaces and vertical lines which would match
    /// just above this line.
    pub fn to_vertical_lines_above(&self) -> String {
        use self::BorderSegHoriz::*;
        self.segments
            .iter()
            .map(|seg| match *seg {
                          Straight | JoinBelow => ' ',
                          JoinAbove | JoinCross => '│',
                      })
            .collect()
    }

    /// Turn into a string with drawing characters
    pub fn into_string(self) -> String {
        self.segments
            .into_iter()
            .map(|seg| match seg {
                BorderSegHoriz::Straight => '─',
                BorderSegHoriz::JoinAbove => 'â”´',
                BorderSegHoriz::JoinBelow => '┬',
                BorderSegHoriz::JoinCross => '┼',
            })
            .collect::<String>()
    }

    /// Return a string without destroying self
    pub fn to_string(&self) -> String {
        self.clone().into_string()
    }
}

/// A line, which can either be text or a line.
#[derive(Debug)]
pub enum RenderLine<T:PartialEq+Eq+Clone+Debug+Default> {
    /// Some rendered text
    Text(TaggedLine<T>),
    /// A table border line
    Line(BorderHoriz),
}

impl<T:PartialEq+Eq+Clone+Debug+Default> RenderLine<T> {
    /// Turn the rendered line into a String
    pub fn into_string(self) -> String {
        match self {
            RenderLine::Text(tagged) => tagged.into_string(),
            RenderLine::Line(border) => border.into_string(),
        }
    }

    /// Convert into a `TaggedLine<T>`, if necessary squashing the
    /// BorderHoriz into one.
    pub fn into_tagged_line(self) -> TaggedLine<T> {
        use self::TaggedLineElement::Str;

        match self {
            RenderLine::Text(tagged) => tagged,
            RenderLine::Line(border) => {
                let mut tagged = TaggedLine::new();
                tagged.push(Str(TaggedString {
                    s: border.into_string(),
                    tag: T::default()
                }));
                tagged
            }
        }
    }
}

/// A renderer which just outputs plain text with
/// annotations depending on a decorator.
pub struct TextRenderer<D:TextDecorator> {
    width: usize,
    lines: Vec<RenderLine<Vec<D::Annotation>>>,
    /// True at the end of a block, meaning we should add
    /// a blank line if any other text is added.
    at_block_end: bool,
    wrapping: Option<WrappedBlock<Vec<D::Annotation>>>,
    decorator: Option<D>,
    ann_stack: Vec<D::Annotation>,
    /// The depth of <pre> block stacking.
    pre_depth: usize,
}

impl<D:TextDecorator> TextRenderer<D> {
    /// Construct a new empty TextRenderer.
    pub fn new(width: usize, decorator: D) -> TextRenderer<D> {
        html_trace!("new({})", width);
        TextRenderer {
            width: width,
            lines: Vec::new(),
            at_block_end: false,
            wrapping: None,
            decorator: Some(decorator),
            ann_stack: Vec::new(),
            pre_depth: 0,
        }
    }

    fn ensure_wrapping_exists(&mut self) {
        if self.wrapping.is_none() {
            self.wrapping = Some(WrappedBlock::new(self.width));
        }
    }

    /// Get the current line wrapping context (and create if
    /// needed).
    fn current_text(&mut self) -> &mut WrappedBlock<Vec<D::Annotation>> {
        self.ensure_wrapping_exists();
        self.wrapping.as_mut().unwrap()
    }

    /// Add a prerendered (multiline) string with the current annotations.
    pub fn add_subblock(&mut self, s: &str) {
        use self::TaggedLineElement::Str;

        html_trace!("add_subblock({}, {})", self.width, s);
        let tag = self.ann_stack.clone();
        self.lines.extend(s.lines().map(|l| {
            let mut line = TaggedLine::new();
            line.push(Str(TaggedString{s: l.into(), tag: tag.clone()}));
            RenderLine::Text(line)
        }));
    }

    /// Flushes the current wrapped block into the lines.
    fn flush_wrapping(&mut self) {
        if let Some(w) = self.wrapping.take() {
            self.lines.extend(w.into_lines().into_iter().map(RenderLine::Text))
        }
    }

    /// Flush the wrapping text and border.  Only one should have
    /// anything to do.
    fn flush_all(&mut self) {
        self.flush_wrapping();
    }

    /// Consumes this renderer and return a multiline `String` with the result.
    pub fn into_string(self) -> String {
        let mut result = String::new();
        #[cfg(feature="html_trace")]
        let width: usize = self.width;
        for line in self.into_lines() {
            result.push_str(&line.into_string());
            result.push('\n');
        }
        html_trace!("into_string({}, {:?})", width, result);
        result
    }

    /// Returns a `Vec` of `TaggedLine`s with therendered text.
    pub fn into_lines(mut self) -> Vec<RenderLine<Vec<D::Annotation>>> {
        self.flush_wrapping();
        // And add the links
        let mut trailer = self.decorator.take().unwrap().finalise();
        if !trailer.is_empty() {
            self.start_block();
            for line in trailer.drain(0..) {
                /* Hard wrap */
                let mut output = String::new();
                let mut pos = 0;
                for c in line.chars() {
                    // FIXME: should we percent-escape?  This is probably
                    // an invalid URL to start with.
                    let c = match c {
                        '\n' => ' ',
                        x => x,
                    };
                    let c_width = UnicodeWidthChar::width(c).unwrap_or(0);
                    if pos + c_width > self.width {
                        let mut tmp_s = String::new();
                        mem::swap(&mut output, &mut tmp_s);
                        self.lines.push(RenderLine::Text(TaggedLine::from_string(tmp_s, &vec![])));
                        output.push(c);
                        pos = c_width;
                    } else {
                        output.push(c);
                        pos += c_width;
                    }
                }
                self.lines.push(RenderLine::Text(TaggedLine::from_string(output, &vec![])));
            }
        }
        self.lines
    }
}

impl<D:TextDecorator> Renderer for TextRenderer<D> {
    fn add_empty_line(&mut self) {
        html_trace!("add_empty_line()");
        self.flush_all();
        self.lines.push(RenderLine::Text(TaggedLine::new()));
        html_trace_quiet!("add_empty_line: at_block_end <- false");
        self.at_block_end = false;
        html_trace_quiet!("add_empty_line: new lines: {:?}", self.lines);
    }

    fn new_sub_renderer(&self, width: usize) -> Self {
        assert!(width > 0);
        TextRenderer::new(width, self.decorator.as_ref().unwrap().make_subblock_decorator())
    }

    fn start_block(&mut self) {
        html_trace!("start_block({})", self.width);
        self.flush_all();
        if !self.lines.is_empty() {
            self.add_empty_line();
        }
        html_trace_quiet!("start_block; at_block_end <- false");
        self.at_block_end = false;
    }

    fn new_line(&mut self) {
        self.flush_all();
    }

    fn new_line_hard(&mut self) {
        match self.wrapping {
            None => self.add_empty_line(),
            Some(WrappedBlock { linelen: 0, wordlen: 0, .. }) => self.add_empty_line(),
            Some(_) => self.flush_all(),
        }
    }

    fn add_horizontal_border(&mut self) {
        self.flush_wrapping();
        self.lines.push(RenderLine::Line(BorderHoriz::new(self.width)));
    }

    fn start_pre(&mut self) {
        self.pre_depth += 1;
    }

    fn end_pre(&mut self) {
        if self.pre_depth > 0 {
            self.pre_depth -= 1;
        } else {
            panic!("Attempt to end a preformatted block which wasn't opened.");
        }
    }

    fn add_preformatted_block(&mut self, text: &str) {
        use self::TaggedLineElement::Str;

        html_trace!("add_block({}, {})", self.width, text);

        // Get the tags ready for normal and continuation lines.
        let mut tag_first = self.ann_stack.clone();
        let mut tag_cont = self.ann_stack.clone();
        tag_first.push(self.decorator.as_mut().unwrap().decorate_preformat_first());
        tag_cont.push(self.decorator.as_mut().unwrap().decorate_preformat_cont());
        // Drop mutability
        let tag_first = tag_first;
        let tag_cont = tag_cont;

        let width = self.width;
        self.start_block();

        /* We do actually want to wrap, but just a hard wrapping
         * at the end, and add a "continuation line" tag so that the
         * UI can show them differently.
         */
        for formatted_line in text.lines() {
            let mut acc = String::new();
            let mut cur_width = 0;
            let mut first = true;
            for c in formatted_line.chars() {
                if let Some(char_width) = UnicodeWidthChar::width(c) {
                    if cur_width + char_width > width {
                        let mut line = TaggedLine::new();
                        /* Push what we have */
                        line.push(Str(TaggedString {
                            s: acc,
                            tag: if first { tag_first.clone() } else { tag_cont.clone() },
                        }));
                        self.lines.push(RenderLine::Text(line));
                        acc = String::new();
                        cur_width = 0;
                        first = false;
                    }
                    acc.push(c);
                    cur_width += char_width;
                } else {
                    match c {
                        '\t' => {
                            let tab_stop = 8;
                            let wanted_pos = cur_width + tab_stop - (cur_width % tab_stop);
                            let spaces = if wanted_pos > width {
                                    width - cur_width
                                } else {
                                    wanted_pos - cur_width
                                };
                            acc.extend((0..spaces).map(|_| ' '));
                            cur_width += spaces;
                        },
                        _ => (),
                    }
                }
            }
            if acc.len() > 0 {
                let mut line = TaggedLine::new();
                /* Push what we have */
                line.push(Str(TaggedString {
                    s: acc,
                    tag: if first { tag_first.clone() } else { tag_cont.clone() },
                }));
                self.lines.push(RenderLine::Text(line));
            }
        }

        html_trace_quiet!("add_block: at_block_end <- true");
        self.at_block_end = true;
    }

    fn end_block(&mut self) {
        self.at_block_end = true;
    }

    fn add_inline_text(&mut self, text: &str) {
        html_trace!("add_inline_text({}, {})", self.width, text);
        if self.pre_depth == 0 && self.at_block_end && text.chars().all(char::is_whitespace) {
            // Ignore whitespace between blocks.
            return;
        }
        if self.at_block_end {
            self.start_block();
        }
        // ensure wrapping is set
        let _ = self.current_text();
        if self.pre_depth == 0 {
            self.wrapping.as_mut().unwrap().add_text(text, &self.ann_stack);
        } else {
            self.wrapping.as_mut().unwrap().add_preformatted_text(text, &self.ann_stack);
        }
    }

    fn width(&self) -> usize {
        self.width
    }

    fn add_block_line(&mut self, line: &str)
    {
        self.add_subblock(line);
    }

    fn append_subrender<'a, I>(&mut self, other: Self,
                               prefixes: I)
                           where I:Iterator<Item=&'a str>
    {
        use self::TaggedLineElement::Str;

        self.flush_wrapping();
        let tag = self.ann_stack.clone();
        self.lines.extend(other.into_lines()
                               .into_iter()
                               .zip(prefixes)
                               .map(|(line, prefix)| {
                                   match line {
                                       RenderLine::Text(mut tline) => {
                                           tline.insert_front(TaggedString{
                                               s: prefix.to_string(),
                                               tag: tag.clone()
                                           });
                                           RenderLine::Text(tline)
                                       },
                                       RenderLine::Line(l) => {
                                           let mut tline = TaggedLine::new();
                                           tline.push(Str(TaggedString {
                                               s: prefix.to_string(),
                                               tag: tag.clone()
                                           }));
                                           tline.push(Str(TaggedString {
                                               s: l.into_string(),
                                               tag: tag.clone()
                                           }));
                                           RenderLine::Text(tline)
                                       }
                                   }
                                }));
    }

    fn append_columns_with_borders<I>(&mut self, cols: I, collapse: bool)
                           where I:IntoIterator<Item=Self> {
        use self::TaggedLineElement::Str;

        self.flush_wrapping();

        let mut next_border = BorderHoriz::new(self.width);

        let mut line_sets = cols.into_iter()
                            .map(|sub_r| {
                                let width = sub_r.width;
                                (width, sub_r.into_lines()
                                             .into_iter()
                                             .map(|mut line| {
                                                 match line {
                                                     RenderLine::Text(ref mut tline) => {
                                                         tline.pad_to(width);
                                                     },
                                                     RenderLine::Line(_) => {},
                                                 }
                                                 line})
                                             .collect())
                                 })
                            .collect::<Vec<(usize, Vec<RenderLine<_>>)>>();

        // Join the vertical lines to all the borders
        {
            let mut pos = 0;
            if let &mut RenderLine::Line(ref mut prev_border) = self.lines.last_mut().unwrap() {
                for &(w, _) in &line_sets[..line_sets.len()-1] {
                    prev_border.join_below(pos+w);
                    next_border.join_above(pos+w);
                    pos += w + 1;
                }
            } else {
                panic!("Expected a border line");
            }
        }

        // If we're collapsing bottom borders, then the bottom border of a
        // nested table is being merged into the bottom border of the
        // containing cell.  If that cell happens not to be the tallest
        // cell in the row, then we need to extend any vertical lines
        // to the bottom.  We'll remember what to do when we update the
        // containing border.
        let mut column_padding = vec![None; line_sets.len()];

        // If we're collapsing borders, do so.
        if collapse {
            /* Collapse any top border */
            let mut pos = 0;
            for &mut (w, ref mut sublines) in &mut line_sets {
                let starts_border = if sublines.len() > 0 {
                    if let RenderLine::Line(_) = sublines[0] {
                        true
                    } else {
                        false
                    }
                } else {
                    false
                };
                if starts_border {
                    if let &mut RenderLine::Line(ref mut prev_border) = self.lines.last_mut().expect("No previous line") {
                        if let RenderLine::Line(line) = sublines.remove(0) {
                            prev_border.merge_from_below(&line, pos);
                        }
                    } else {
                        unreachable!();
                    }
                }
                pos += w + 1;
            }

            /* Collapse any bottom border */
            let mut pos = 0;
            for (col_no, &mut (w, ref mut sublines)) in line_sets.iter_mut().enumerate() {
                let ends_border = if sublines.len() > 0 {
                    if let Some(&RenderLine::Line(_)) = sublines.last() {
                        true
                    } else {
                        false
                    }
                } else {
                    false
                };
                if ends_border {
                    if let RenderLine::Line(line) = sublines.pop().unwrap() {
                        next_border.merge_from_above(&line, pos);
                        column_padding[col_no] = Some(line.to_vertical_lines_above())
                    }
                }
                pos += w + 1;
            }
        }

        let cell_height = line_sets.iter()
                                   .map(|&(_, ref v)| v.len())
                                   .max().unwrap_or(0);
        let spaces: String = (0..self.width).map(|_| ' ').collect();
        let last_cellno = line_sets.len()-1;
        for i in 0..cell_height {
            let mut line = TaggedLine::new();
            for (cellno, &mut (width, ref mut ls)) in line_sets.iter_mut().enumerate() {
                if let Some(piece) = ls.get_mut(i) {
                    match piece {
                        &mut RenderLine::Text(ref mut tline) => {
                            line.consume(tline);
                        },
                        &mut RenderLine::Line(ref bord) => {
                            line.push(Str(TaggedString {
                                s: bord.to_string(),
                                tag: self.ann_stack.clone(),
                            }));
                        },
                    };
                } else {
                    line.push(Str(TaggedString {
                        s: column_padding[cellno].as_ref().map(|s| s.clone())
                                                 .unwrap_or_else(||spaces[0..width].to_string()),

                        tag: self.ann_stack.clone(),
                    }));
                }
                if cellno != last_cellno {
                    line.push_char('│', &self.ann_stack);
                }
            }
            self.lines.push(RenderLine::Text(line));
        }
        self.lines.push(RenderLine::Line(next_border));
    }

    fn empty(&self) -> bool {
        self.lines.is_empty() && self.wrapping.is_none()
    }

    fn text_len(&self) -> usize {
        let mut result = 0;
        for line in &self.lines {
            result += match *line {
                RenderLine::Text(ref tline) => tline.width(),
                RenderLine::Line(_) => 0, // FIXME: should borders count?
            };
        }
        if let Some(ref w) = self.wrapping {
            result += w.text_len();
        }
        result
    }

    fn start_link(&mut self, target: &str)
    {
        if let Some((s, annotation)) = self.decorator.as_mut().map(|d| d.decorate_link_start(target)) {
            self.ann_stack.push(annotation);
            self.add_inline_text(&s);
        }
    }
    fn end_link(&mut self)
    {
        if let Some(s) = self.decorator.as_mut().map(|d| d.decorate_link_end()) {
            self.add_inline_text(&s);
            self.ann_stack.pop();
        }
    }
    fn start_emphasis(&mut self)
    {
        if let Some((s, annotation)) = self.decorator.as_mut().map(|d| d.decorate_em_start()) {
            self.ann_stack.push(annotation);
            self.add_inline_text(&s);
        }
    }
    fn end_emphasis(&mut self)
    {
        if let Some(s) = self.decorator.as_mut().map(|d| d.decorate_em_end()) {
            self.add_inline_text(&s);
            self.ann_stack.pop();
        }
    }
    fn start_strong(&mut self)
    {
        if let Some((s, annotation)) = self.decorator.as_mut().map(|d| d.decorate_strong_start()) {
            self.ann_stack.push(annotation);
            self.add_inline_text(&s);
        }
    }
    fn end_strong(&mut self)
    {
        if let Some(s) = self.decorator.as_mut().map(|d| d.decorate_strong_end()) {
            self.add_inline_text(&s);
            self.ann_stack.pop();
        }
    }
    fn start_code(&mut self)
    {
        if let Some((s, annotation)) = self.decorator.as_mut().map(|d| d.decorate_code_start()) {
            self.ann_stack.push(annotation);
            self.add_inline_text(&s);
        }
    }
    fn end_code(&mut self)
    {
        if let Some(s) = self.decorator.as_mut().map(|d| d.decorate_code_end()) {
            self.add_inline_text(&s);
            self.ann_stack.pop();
        }
    }
    fn add_image(&mut self, title: &str)
    {
        if let Some((s, tag)) = self.decorator.as_mut().map(|d| d.decorate_image(title)) {
            self.ann_stack.push(tag);
            self.add_inline_text(&s);
            self.ann_stack.pop();
        }
    }
    fn record_frag_start(&mut self, fragname: &str)
    {
        use self::TaggedLineElement::FragmentStart;

        self.ensure_wrapping_exists();
        self.wrapping.as_mut().unwrap().add_element(
            FragmentStart(fragname.to_string()));
    }
}

/// A decorator for use with `TextRenderer` which outputs plain UTF-8 text
/// with no annotations.  Markup is rendered as text characters or footnotes.
#[derive(Clone)]
pub struct PlainDecorator {
    links: Vec<String>,
}

impl PlainDecorator {
    /// Create a new `PlainDecorator`.
    #[cfg_attr(feature="clippy", allow(new_without_default_derive))]
    pub fn new() -> PlainDecorator {
        PlainDecorator {
            links: Vec::new(),
        }
    }
}

impl TextDecorator for PlainDecorator {
    type Annotation = ();

    fn decorate_link_start(&mut self, url: &str) -> (String, Self::Annotation)
    {
        self.links.push(url.to_string());
        ("[".to_string(), ())
    }

    fn decorate_link_end(&mut self) -> String
    {
        format!("][{}]", self.links.len())
    }

    fn decorate_em_start(&mut self) -> (String, Self::Annotation)
    {
        ("*".to_string(), ())
    }

    fn decorate_em_end(&mut self) -> String
    {
        "*".to_string()
    }

    fn decorate_strong_start(&mut self) -> (String, Self::Annotation)
    {
        ("**".to_string(), ())
    }

    fn decorate_strong_end(&mut self) -> String
    {
        "**".to_string()
    }

    fn decorate_code_start(&mut self) -> (String, Self::Annotation)
    {
        ("`".to_string(), ())
    }

    fn decorate_code_end(&mut self) -> String
    {
        "`".to_string()
    }

    fn decorate_preformat_first(&mut self) -> Self::Annotation { () }
    fn decorate_preformat_cont(&mut self) -> Self::Annotation { () }

    fn decorate_image(&mut self, title: &str) -> (String, Self::Annotation)
    {
        (format!("[{}]", title), ())
    }

    fn finalise(self) -> Vec<TaggedLine<()>> {
        self.links.into_iter().enumerate().map(|(idx,s)|
            TaggedLine::from_string(format!("[{}] {}", idx+1, s), &())).collect()
    }

    fn make_subblock_decorator(&self) -> Self {
        PlainDecorator::new()
    }
}

/// A decorator for use with `TextRenderer` which outputs plain UTF-8 text
/// with no annotations or markup, emitting only the literal text.
#[derive(Clone)]
pub struct TrivialDecorator {}

impl TrivialDecorator {
    /// Create a new `TrivialDecorator`.
    #[cfg_attr(feature="clippy", allow(new_without_default_derive))]
    pub fn new() -> TrivialDecorator {
        TrivialDecorator {}
    }
}

impl TextDecorator for TrivialDecorator {
    type Annotation = ();

    fn decorate_link_start(&mut self, _url: &str) -> (String, Self::Annotation)
    {
        ("".to_string(), ())
    }

    fn decorate_link_end(&mut self) -> String
    {
        "".to_string()
    }

    fn decorate_em_start(&mut self) -> (String, Self::Annotation)
    {
        ("".to_string(), ())
    }

    fn decorate_em_end(&mut self) -> String
    {
        "".to_string()
    }

    fn decorate_strong_start(&mut self) -> (String, Self::Annotation)
    {
        ("".to_string(), ())
    }

    fn decorate_strong_end(&mut self) -> String
    {
        "".to_string()
    }

    fn decorate_code_start(&mut self) -> (String, Self::Annotation)
    {
        ("".to_string(), ())
    }

    fn decorate_code_end(&mut self) -> String
    {
        "".to_string()
    }

    fn decorate_preformat_first(&mut self) -> Self::Annotation { () }
    fn decorate_preformat_cont(&mut self) -> Self::Annotation { () }

    fn decorate_image(&mut self, title: &str) -> (String, Self::Annotation)
    {
        // FIXME: this should surely be the alt text, not the title text
        (title.to_string(), ())
    }

    fn finalise(self) -> Vec<TaggedLine<()>> {
        Vec::new()
    }

    fn make_subblock_decorator(&self) -> Self {
        TrivialDecorator::new()
    }
}

/// A decorator to generate rich text (styled) rather than
/// pure text output.
#[derive(Clone)]
pub struct RichDecorator {
}

/// Annotation type for "rich" text.  Text is associated with a set of
/// these.
#[derive(PartialEq,Eq,Clone,Debug)]
pub enum RichAnnotation {
    /// Normal text.
    Default,
    /// A link with the target.
    Link(String),
    /// An image (attached to the title text)
    Image,
    /// Emphasised text, which might be rendered in bold or another colour.
    Emphasis,
    /// Strong text, which might be rendered in bold or another colour.
    Strong,
    /// Code
    Code,
    /// Preformatted; true if a continuation line for an overly-long line.
    Preformat(bool),
}

impl Default for RichAnnotation {
    fn default() -> Self {
        RichAnnotation::Default
    }
}

impl RichDecorator {
    /// Create a new `RichDecorator`.
    #[cfg_attr(feature="clippy", allow(new_without_default_derive))]
    pub fn new() -> RichDecorator {
        RichDecorator {
        }
    }
}

impl TextDecorator for RichDecorator {
    type Annotation = RichAnnotation;

    fn decorate_link_start(&mut self, url: &str) -> (String, Self::Annotation)
    {
        ("".to_string(), RichAnnotation::Link(url.to_string()))
    }

    fn decorate_link_end(&mut self) -> String
    {
        "".to_string()
    }

    fn decorate_em_start(&mut self) -> (String, Self::Annotation)
    {
        ("".to_string(), RichAnnotation::Emphasis)
    }

    fn decorate_em_end(&mut self) -> String
    {
        "".to_string()
    }

    fn decorate_strong_start(&mut self) -> (String, Self::Annotation)
    {
        ("*".to_string(), RichAnnotation::Strong)
    }

    fn decorate_strong_end(&mut self) -> String
    {
        "*".to_string()
    }

    fn decorate_code_start(&mut self) -> (String, Self::Annotation)
    {
        ("`".to_string(), RichAnnotation::Code)
    }

    fn decorate_code_end(&mut self) -> String
    {
        "`".to_string()
    }

    fn decorate_preformat_first(&mut self) -> Self::Annotation
    {
        RichAnnotation::Preformat(false)
    }

    fn decorate_preformat_cont(&mut self) -> Self::Annotation
    {
        RichAnnotation::Preformat(true)
    }

    fn decorate_image(&mut self, title: &str) -> (String, Self::Annotation)
    {
        (title.to_string(), RichAnnotation::Image)
    }

    fn finalise(self) -> Vec<TaggedLine<RichAnnotation>> {
        Vec::new()
    }

    fn make_subblock_decorator(&self) -> Self {
        RichDecorator::new()
    }
}