21
21
//! playground: &None,
22
22
//! heading_offset: HeadingOffset::H2,
23
23
//! };
24
- //! let html = md.into_string();
24
+ //! let mut html = String::new();
25
+ //! md.write_into(&mut html).unwrap();
25
26
//! // ... something using html
26
27
//! ```
27
28
28
29
use std:: borrow:: Cow ;
29
30
use std:: collections:: VecDeque ;
30
- use std:: fmt:: Write ;
31
+ use std:: fmt:: { self , Write } ;
31
32
use std:: iter:: Peekable ;
32
33
use std:: ops:: { ControlFlow , Range } ;
33
34
use std:: path:: PathBuf ;
@@ -1328,16 +1329,13 @@ impl LangString {
1328
1329
}
1329
1330
1330
1331
impl < ' a > Markdown < ' a > {
1331
- pub fn into_string ( self ) -> String {
1332
+ pub fn write_into ( self , f : impl fmt :: Write ) -> fmt :: Result {
1332
1333
// This is actually common enough to special-case
1333
1334
if self . content . is_empty ( ) {
1334
- return String :: new ( ) ;
1335
+ return Ok ( ( ) ) ;
1335
1336
}
1336
1337
1337
- let mut s = String :: new ( ) ;
1338
- html:: push_html ( & mut s, self . into_iter ( ) ) ;
1339
-
1340
- s
1338
+ html:: write_html_fmt ( f, self . into_iter ( ) )
1341
1339
}
1342
1340
1343
1341
fn into_iter ( self ) -> CodeBlocks < ' a , ' a , impl Iterator < Item = Event < ' a > > > {
@@ -1453,19 +1451,20 @@ impl MarkdownWithToc<'_> {
1453
1451
1454
1452
( toc. into_toc ( ) , s)
1455
1453
}
1456
- pub ( crate ) fn into_string ( self ) -> String {
1454
+
1455
+ pub ( crate ) fn write_into ( self , mut f : impl fmt:: Write ) -> fmt:: Result {
1457
1456
let ( toc, s) = self . into_parts ( ) ;
1458
- format ! ( "<nav id=\" rustdoc\" >{toc}</nav>{s}" , toc = toc. print( ) )
1457
+ write ! ( f , "<nav id=\" rustdoc\" >{toc}</nav>{s}" , toc = toc. print( ) )
1459
1458
}
1460
1459
}
1461
1460
1462
1461
impl MarkdownItemInfo < ' _ > {
1463
- pub ( crate ) fn into_string ( self ) -> String {
1462
+ pub ( crate ) fn write_into ( self , mut f : impl fmt :: Write ) -> fmt :: Result {
1464
1463
let MarkdownItemInfo ( md, ids) = self ;
1465
1464
1466
1465
// This is actually common enough to special-case
1467
1466
if md. is_empty ( ) {
1468
- return String :: new ( ) ;
1467
+ return Ok ( ( ) ) ;
1469
1468
}
1470
1469
let p = Parser :: new_ext ( md, main_body_opts ( ) ) . into_offset_iter ( ) ;
1471
1470
@@ -1475,19 +1474,15 @@ impl MarkdownItemInfo<'_> {
1475
1474
_ => event,
1476
1475
} ) ;
1477
1476
1478
- let mut s = String :: new ( ) ;
1479
-
1480
1477
ids. handle_footnotes ( |ids, existing_footnotes| {
1481
1478
let p = HeadingLinks :: new ( p, None , ids, HeadingOffset :: H1 ) ;
1482
1479
let p = footnotes:: Footnotes :: new ( p, existing_footnotes) ;
1483
1480
let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1484
1481
let p = p. filter ( |event| {
1485
1482
!matches ! ( event, Event :: Start ( Tag :: Paragraph ) | Event :: End ( TagEnd :: Paragraph ) )
1486
1483
} ) ;
1487
- html:: push_html ( & mut s, p) ;
1488
- } ) ;
1489
-
1490
- s
1484
+ html:: write_html_fmt ( & mut f, p)
1485
+ } )
1491
1486
}
1492
1487
}
1493
1488
0 commit comments