@@ -441,6 +441,42 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
441441 }
442442}
443443
444+ /// Wrap HTML tables into `<div>` to prevent having the doc blocks width being too big.
445+ struct TableWrapper < ' a , I : Iterator < Item = Event < ' a > > > {
446+ inner : I ,
447+ stored_events : VecDeque < Event < ' a > > ,
448+ }
449+
450+ impl < ' a , I : Iterator < Item = Event < ' a > > > TableWrapper < ' a , I > {
451+ fn new ( iter : I ) -> Self {
452+ Self { inner : iter, stored_events : VecDeque :: new ( ) }
453+ }
454+ }
455+
456+ impl < ' a , I : Iterator < Item = Event < ' a > > > Iterator for TableWrapper < ' a , I > {
457+ type Item = Event < ' a > ;
458+
459+ fn next ( & mut self ) -> Option < Self :: Item > {
460+ if let Some ( first) = self . stored_events . pop_front ( ) {
461+ return Some ( first) ;
462+ }
463+
464+ let event = self . inner . next ( ) ?;
465+
466+ Some ( match event {
467+ Event :: Start ( Tag :: Table ( t) ) => {
468+ self . stored_events . push_back ( Event :: Start ( Tag :: Table ( t) ) ) ;
469+ Event :: Html ( CowStr :: Borrowed ( "<div>" ) )
470+ }
471+ Event :: End ( Tag :: Table ( t) ) => {
472+ self . stored_events . push_back ( Event :: Html ( CowStr :: Borrowed ( "</div>" ) ) ) ;
473+ Event :: End ( Tag :: Table ( t) )
474+ }
475+ e => e,
476+ } )
477+ }
478+ }
479+
444480type SpannedEvent < ' a > = ( Event < ' a > , Range < usize > ) ;
445481
446482/// Make headings links with anchor IDs and build up TOC.
@@ -983,6 +1019,7 @@ impl Markdown<'_> {
9831019 let p = HeadingLinks :: new ( p, None , & mut ids) ;
9841020 let p = Footnotes :: new ( p) ;
9851021 let p = LinkReplacer :: new ( p. map ( |( ev, _) | ev) , links) ;
1022+ let p = TableWrapper :: new ( p) ;
9861023 let p = CodeBlocks :: new ( p, codes, edition, playground) ;
9871024 html:: push_html ( & mut s, p) ;
9881025
@@ -1003,7 +1040,8 @@ impl MarkdownWithToc<'_> {
10031040 {
10041041 let p = HeadingLinks :: new ( p, Some ( & mut toc) , & mut ids) ;
10051042 let p = Footnotes :: new ( p) ;
1006- let p = CodeBlocks :: new ( p. map ( |( ev, _) | ev) , codes, edition, playground) ;
1043+ let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1044+ let p = CodeBlocks :: new ( p, codes, edition, playground) ;
10071045 html:: push_html ( & mut s, p) ;
10081046 }
10091047
@@ -1031,7 +1069,8 @@ impl MarkdownHtml<'_> {
10311069
10321070 let p = HeadingLinks :: new ( p, None , & mut ids) ;
10331071 let p = Footnotes :: new ( p) ;
1034- let p = CodeBlocks :: new ( p. map ( |( ev, _) | ev) , codes, edition, playground) ;
1072+ let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1073+ let p = CodeBlocks :: new ( p, codes, edition, playground) ;
10351074 html:: push_html ( & mut s, p) ;
10361075
10371076 s
0 commit comments