1
1
use crate :: errors:: * ;
2
2
use log:: { debug, trace, warn} ;
3
- use memchr:: { self , Memchr } ;
4
- use pulldown_cmark:: { self , Event , HeadingLevel , Tag } ;
3
+ use memchr:: Memchr ;
4
+ use pulldown_cmark:: { DefaultBrokenLinkCallback , Event , HeadingLevel , Tag , TagEnd } ;
5
5
use serde:: { Deserialize , Serialize } ;
6
6
use std:: fmt:: { self , Display , Formatter } ;
7
7
use std:: iter:: FromIterator ;
@@ -163,7 +163,7 @@ impl From<Link> for SummaryItem {
163
163
/// > match the following regex: "[^<>\n[]]+".
164
164
struct SummaryParser < ' a > {
165
165
src : & ' a str ,
166
- stream : pulldown_cmark:: OffsetIter < ' a , ' a > ,
166
+ stream : pulldown_cmark:: OffsetIter < ' a , DefaultBrokenLinkCallback > ,
167
167
offset : usize ,
168
168
169
169
/// We can't actually put an event back into the `OffsetIter` stream, so instead we store it
@@ -210,7 +210,7 @@ macro_rules! collect_events {
210
210
}
211
211
212
212
impl < ' a > SummaryParser < ' a > {
213
- fn new ( text : & str ) -> SummaryParser < ' _ > {
213
+ fn new ( text : & ' a str ) -> SummaryParser < ' a > {
214
214
let pulldown_parser = pulldown_cmark:: Parser :: new ( text) . into_offset_iter ( ) ;
215
215
216
216
SummaryParser {
@@ -265,7 +265,12 @@ impl<'a> SummaryParser<'a> {
265
265
loop {
266
266
match self . next_event ( ) {
267
267
Some ( ev @ Event :: Start ( Tag :: List ( ..) ) )
268
- | Some ( ev @ Event :: Start ( Tag :: Heading ( HeadingLevel :: H1 , ..) ) ) => {
268
+ | Some (
269
+ ev @ Event :: Start ( Tag :: Heading {
270
+ level : HeadingLevel :: H1 ,
271
+ ..
272
+ } ) ,
273
+ ) => {
269
274
if is_prefix {
270
275
// we've finished prefix chapters and are at the start
271
276
// of the numbered section.
@@ -275,8 +280,8 @@ impl<'a> SummaryParser<'a> {
275
280
bail ! ( self . parse_error( "Suffix chapters cannot be followed by a list" ) ) ;
276
281
}
277
282
}
278
- Some ( Event :: Start ( Tag :: Link ( _type , href , _title ) ) ) => {
279
- let link = self . parse_link ( href . to_string ( ) ) ;
283
+ Some ( Event :: Start ( Tag :: Link { dest_url , .. } ) ) => {
284
+ let link = self . parse_link ( dest_url . to_string ( ) ) ;
280
285
items. push ( SummaryItem :: Link ( link) ) ;
281
286
}
282
287
Some ( Event :: Rule ) => items. push ( SummaryItem :: Separator ) ,
@@ -304,10 +309,13 @@ impl<'a> SummaryParser<'a> {
304
309
break ;
305
310
}
306
311
307
- Some ( Event :: Start ( Tag :: Heading ( HeadingLevel :: H1 , ..) ) ) => {
312
+ Some ( Event :: Start ( Tag :: Heading {
313
+ level : HeadingLevel :: H1 ,
314
+ ..
315
+ } ) ) => {
308
316
debug ! ( "Found a h1 in the SUMMARY" ) ;
309
317
310
- let tags = collect_events ! ( self . stream, end Tag :: Heading ( HeadingLevel :: H1 , .. ) ) ;
318
+ let tags = collect_events ! ( self . stream, end TagEnd :: Heading ( HeadingLevel :: H1 ) ) ;
311
319
Some ( stringify_events ( tags) )
312
320
}
313
321
@@ -336,7 +344,7 @@ impl<'a> SummaryParser<'a> {
336
344
/// Finishes parsing a link once the `Event::Start(Tag::Link(..))` has been opened.
337
345
fn parse_link ( & mut self , href : String ) -> Link {
338
346
let href = href. replace ( "%20" , " " ) ;
339
- let link_content = collect_events ! ( self . stream, end Tag :: Link ( .. ) ) ;
347
+ let link_content = collect_events ! ( self . stream, end TagEnd :: Link ) ;
340
348
let name = stringify_events ( link_content) ;
341
349
342
350
let path = if href. is_empty ( ) {
@@ -377,7 +385,12 @@ impl<'a> SummaryParser<'a> {
377
385
}
378
386
// The expectation is that pulldown cmark will terminate a paragraph before a new
379
387
// heading, so we can always count on this to return without skipping headings.
380
- Some ( ev @ Event :: Start ( Tag :: Heading ( HeadingLevel :: H1 , ..) ) ) => {
388
+ Some (
389
+ ev @ Event :: Start ( Tag :: Heading {
390
+ level : HeadingLevel :: H1 ,
391
+ ..
392
+ } ) ,
393
+ ) => {
381
394
// we're starting a new part
382
395
self . back ( ev) ;
383
396
break ;
@@ -398,7 +411,7 @@ impl<'a> SummaryParser<'a> {
398
411
399
412
// Skip over the contents of this tag
400
413
while let Some ( event) = self . next_event ( ) {
401
- if event == Event :: End ( other_tag. clone ( ) ) {
414
+ if event == Event :: End ( other_tag. clone ( ) . into ( ) ) {
402
415
break ;
403
416
}
404
417
}
@@ -469,7 +482,7 @@ impl<'a> SummaryParser<'a> {
469
482
470
483
last_item. nested_items = sub_items;
471
484
}
472
- Some ( Event :: End ( Tag :: List ( ..) ) ) => break ,
485
+ Some ( Event :: End ( TagEnd :: List ( ..) ) ) => break ,
473
486
Some ( _) => { }
474
487
None => break ,
475
488
}
@@ -486,8 +499,8 @@ impl<'a> SummaryParser<'a> {
486
499
loop {
487
500
match self . next_event ( ) {
488
501
Some ( Event :: Start ( Tag :: Paragraph ) ) => continue ,
489
- Some ( Event :: Start ( Tag :: Link ( _type , href , _title ) ) ) => {
490
- let mut link = self . parse_link ( href . to_string ( ) ) ;
502
+ Some ( Event :: Start ( Tag :: Link { dest_url , .. } ) ) => {
503
+ let mut link = self . parse_link ( dest_url . to_string ( ) ) ;
491
504
492
505
let mut number = parent. clone ( ) ;
493
506
number. 0 . push ( num_existing_items as u32 + 1 ) ;
@@ -529,14 +542,18 @@ impl<'a> SummaryParser<'a> {
529
542
fn parse_title ( & mut self ) -> Option < String > {
530
543
loop {
531
544
match self . next_event ( ) {
532
- Some ( Event :: Start ( Tag :: Heading ( HeadingLevel :: H1 , ..) ) ) => {
545
+ Some ( Event :: Start ( Tag :: Heading {
546
+ level : HeadingLevel :: H1 ,
547
+ ..
548
+ } ) ) => {
533
549
debug ! ( "Found a h1 in the SUMMARY" ) ;
534
550
535
- let tags = collect_events ! ( self . stream, end Tag :: Heading ( HeadingLevel :: H1 , .. ) ) ;
551
+ let tags = collect_events ! ( self . stream, end TagEnd :: Heading ( HeadingLevel :: H1 ) ) ;
536
552
return Some ( stringify_events ( tags) ) ;
537
553
}
538
554
// Skip a HTML element such as a comment line.
539
- Some ( Event :: Html ( _) ) => { }
555
+ Some ( Event :: Html ( _) | Event :: InlineHtml ( _) )
556
+ | Some ( Event :: Start ( Tag :: HtmlBlock ) | Event :: End ( TagEnd :: HtmlBlock ) ) => { }
540
557
// Otherwise, no title.
541
558
Some ( ev) => {
542
559
self . back ( ev) ;
@@ -744,7 +761,7 @@ mod tests {
744
761
let _ = parser. stream . next ( ) ; // Discard opening paragraph
745
762
746
763
let href = match parser. stream . next ( ) {
747
- Some ( ( Event :: Start ( Tag :: Link ( _type , href , _title ) ) , _range) ) => href . to_string ( ) ,
764
+ Some ( ( Event :: Start ( Tag :: Link { dest_url , .. } ) , _range) ) => dest_url . to_string ( ) ,
748
765
other => panic ! ( "Unreachable, {:?}" , other) ,
749
766
} ;
750
767
0 commit comments