@@ -33,8 +33,11 @@ use crate::html::toc::TocBuilder;
33
33
use crate :: html:: highlight;
34
34
use crate :: test;
35
35
36
- use pulldown_cmark:: { html, Event , Tag , Parser } ;
37
- use pulldown_cmark:: { Options , OPTION_ENABLE_FOOTNOTES , OPTION_ENABLE_TABLES } ;
36
+ use pulldown_cmark:: { html, CowStr , Event , Options , Parser , Tag } ;
37
+
38
+ fn opts ( ) -> Options {
39
+ Options :: ENABLE_TABLES | Options :: ENABLE_FOOTNOTES
40
+ }
38
41
39
42
/// A unit struct which has the `fmt::Display` trait implemented. When
40
43
/// formatted, this struct will emit the HTML corresponding to the rendered
@@ -297,12 +300,11 @@ impl<'a, 'b, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, 'b, I>
297
300
298
301
fn next ( & mut self ) -> Option < Self :: Item > {
299
302
let event = self . inner . next ( ) ;
300
- if let Some ( Event :: Start ( Tag :: Link ( dest, text) ) ) = event {
301
- if let Some ( & ( _, ref replace) ) = self . links . into_iter ( ) . find ( |link| & * link. 0 == & * dest)
302
- {
303
- Some ( Event :: Start ( Tag :: Link ( replace. to_owned ( ) . into ( ) , text) ) )
303
+ if let Some ( Event :: Start ( Tag :: Link ( kind, dest, text) ) ) = event {
304
+ if let Some ( & ( _, ref replace) ) = self . links . iter ( ) . find ( |link| link. 0 == * dest) {
305
+ Some ( Event :: Start ( Tag :: Link ( kind, replace. to_owned ( ) . into ( ) , text) ) )
304
306
} else {
305
- Some ( Event :: Start ( Tag :: Link ( dest, text) ) )
307
+ Some ( Event :: Start ( Tag :: Link ( kind , dest, text) ) )
306
308
}
307
309
} else {
308
310
event
@@ -393,7 +395,7 @@ fn check_if_allowed_tag(t: &Tag<'_>) -> bool {
393
395
| Tag :: Emphasis
394
396
| Tag :: Strong
395
397
| Tag :: Code
396
- | Tag :: Link ( _ , _ )
398
+ | Tag :: Link ( .. )
397
399
| Tag :: BlockQuote => true ,
398
400
_ => false ,
399
401
}
@@ -520,63 +522,39 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> {
520
522
}
521
523
}
522
524
523
- pub struct TestableCodeError ( ( ) ) ;
524
-
525
- impl fmt:: Display for TestableCodeError {
526
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
527
- write ! ( f, "invalid start of a new code block" )
528
- }
529
- }
530
-
531
- pub fn find_testable_code < T : test:: Tester > (
532
- doc : & str ,
533
- tests : & mut T ,
534
- error_codes : ErrorCodes ,
535
- ) -> Result < ( ) , TestableCodeError > {
525
+ pub fn find_testable_code < T : test:: Tester > ( doc : & str , tests : & mut T , error_codes : ErrorCodes ) {
536
526
let mut parser = Parser :: new ( doc) ;
537
527
let mut prev_offset = 0 ;
538
528
let mut nb_lines = 0 ;
539
529
let mut register_header = None ;
540
- ' main : while let Some ( event) = parser. next ( ) {
530
+ while let Some ( event) = parser. next ( ) {
541
531
match event {
542
532
Event :: Start ( Tag :: CodeBlock ( s) ) => {
533
+ let offset = parser. get_offset ( ) ;
534
+
543
535
let block_info = if s. is_empty ( ) {
544
536
LangString :: all_false ( )
545
537
} else {
546
538
LangString :: parse ( & * s, error_codes)
547
539
} ;
548
540
if !block_info. rust {
549
- continue
541
+ continue ;
550
542
}
551
543
let mut test_s = String :: new ( ) ;
552
- let mut offset = None ;
553
- loop {
554
- let event = parser. next ( ) ;
555
- if let Some ( event) = event {
556
- match event {
557
- Event :: End ( Tag :: CodeBlock ( _) ) => break ,
558
- Event :: Text ( ref s) => {
559
- test_s. push_str ( s) ;
560
- if offset. is_none ( ) {
561
- offset = Some ( parser. get_offset ( ) ) ;
562
- }
563
- }
564
- _ => { }
565
- }
566
- } else {
567
- break ' main;
568
- }
569
- }
570
- if let Some ( offset) = offset {
571
- let lines = test_s. lines ( ) . map ( |l| map_line ( l) . for_code ( ) ) ;
572
- let text = lines. collect :: < Vec < Cow < ' _ , str > > > ( ) . join ( "\n " ) ;
573
- nb_lines += doc[ prev_offset..offset] . lines ( ) . count ( ) ;
574
- let line = tests. get_line ( ) + ( nb_lines - 1 ) ;
575
- tests. add_test ( text, block_info, line) ;
576
- prev_offset = offset;
577
- } else {
578
- return Err ( TestableCodeError ( ( ) ) ) ;
544
+
545
+ while let Some ( Event :: Text ( s) ) = parser. next ( ) {
546
+ test_s. push_str ( & s) ;
579
547
}
548
+
549
+ let text = test_s
550
+ . lines ( )
551
+ . map ( |l| map_line ( l) . for_code ( ) )
552
+ . collect :: < Vec < Cow < ' _ , str > > > ( )
553
+ . join ( "\n " ) ;
554
+ nb_lines += doc[ prev_offset..offset] . lines ( ) . count ( ) ;
555
+ let line = tests. get_line ( ) + nb_lines;
556
+ tests. add_test ( text, block_info, line) ;
557
+ prev_offset = offset;
580
558
}
581
559
Event :: Start ( Tag :: Header ( level) ) => {
582
560
register_header = Some ( level as u32 ) ;
@@ -593,7 +571,6 @@ pub fn find_testable_code<T: test::Tester>(
593
571
_ => { }
594
572
}
595
573
}
596
- Ok ( ( ) )
597
574
}
598
575
599
576
#[ derive( Eq , PartialEq , Clone , Debug ) ]
@@ -687,10 +664,6 @@ impl<'a> fmt::Display for Markdown<'a> {
687
664
688
665
// This is actually common enough to special-case
689
666
if md. is_empty ( ) { return Ok ( ( ) ) }
690
- let mut opts = Options :: empty ( ) ;
691
- opts. insert ( OPTION_ENABLE_TABLES ) ;
692
- opts. insert ( OPTION_ENABLE_FOOTNOTES ) ;
693
-
694
667
let replacer = |_: & str , s : & str | {
695
668
if let Some ( & ( _, ref replace) ) = links. into_iter ( ) . find ( |link| & * link. 0 == s) {
696
669
Some ( ( replace. clone ( ) , s. to_owned ( ) ) )
@@ -699,7 +672,7 @@ impl<'a> fmt::Display for Markdown<'a> {
699
672
}
700
673
} ;
701
674
702
- let p = Parser :: new_with_broken_link_callback ( md, opts, Some ( & replacer) ) ;
675
+ let p = Parser :: new_with_broken_link_callback ( md, opts ( ) , Some ( & replacer) ) ;
703
676
704
677
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
705
678
@@ -718,11 +691,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
718
691
let MarkdownWithToc ( md, ref ids, codes) = * self ;
719
692
let mut ids = ids. borrow_mut ( ) ;
720
693
721
- let mut opts = Options :: empty ( ) ;
722
- opts. insert ( OPTION_ENABLE_TABLES ) ;
723
- opts. insert ( OPTION_ENABLE_FOOTNOTES ) ;
724
-
725
- let p = Parser :: new_ext ( md, opts) ;
694
+ let p = Parser :: new_ext ( md, opts ( ) ) ;
726
695
727
696
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
728
697
@@ -748,11 +717,7 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
748
717
749
718
// This is actually common enough to special-case
750
719
if md. is_empty ( ) { return Ok ( ( ) ) }
751
- let mut opts = Options :: empty ( ) ;
752
- opts. insert ( OPTION_ENABLE_TABLES ) ;
753
- opts. insert ( OPTION_ENABLE_FOOTNOTES ) ;
754
-
755
- let p = Parser :: new_ext ( md, opts) ;
720
+ let p = Parser :: new_ext ( md, opts ( ) ) ;
756
721
757
722
// Treat inline HTML as plain text.
758
723
let p = p. map ( |event| match event {
@@ -868,10 +833,6 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
868
833
return vec ! [ ] ;
869
834
}
870
835
871
- let mut opts = Options :: empty ( ) ;
872
- opts. insert ( OPTION_ENABLE_TABLES ) ;
873
- opts. insert ( OPTION_ENABLE_FOOTNOTES ) ;
874
-
875
836
let mut links = vec ! [ ] ;
876
837
let shortcut_links = RefCell :: new ( vec ! [ ] ) ;
877
838
@@ -894,20 +855,19 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
894
855
shortcut_links. borrow_mut ( ) . push ( ( s. to_owned ( ) , locate ( s) ) ) ;
895
856
None
896
857
} ;
897
- let p = Parser :: new_with_broken_link_callback ( md, opts,
898
- Some ( & push) ) ;
858
+ let p = Parser :: new_with_broken_link_callback ( md, opts ( ) , Some ( & push) ) ;
899
859
900
860
// There's no need to thread an IdMap through to here because
901
861
// the IDs generated aren't going to be emitted anywhere.
902
862
let mut ids = IdMap :: new ( ) ;
903
863
let iter = Footnotes :: new ( HeadingLinks :: new ( p, None , & mut ids) ) ;
904
864
905
865
for ev in iter {
906
- if let Event :: Start ( Tag :: Link ( dest, _) ) = ev {
866
+ if let Event :: Start ( Tag :: Link ( _ , dest, _) ) = ev {
907
867
debug ! ( "found link: {}" , dest) ;
908
868
links. push ( match dest {
909
- Cow :: Borrowed ( s) => ( s. to_owned ( ) , locate ( s) ) ,
910
- Cow :: Owned ( s ) => ( s, None ) ,
869
+ CowStr :: Borrowed ( s) => ( s. to_owned ( ) , locate ( s) ) ,
870
+ s @ CowStr :: Boxed ( .. ) | s @ CowStr :: Inlined ( .. ) => ( s. into_string ( ) , None ) ,
911
871
} ) ;
912
872
}
913
873
}
@@ -939,10 +899,7 @@ crate fn rust_code_blocks(md: &str) -> Vec<RustCodeBlock> {
939
899
return code_blocks;
940
900
}
941
901
942
- let mut opts = Options :: empty ( ) ;
943
- opts. insert ( OPTION_ENABLE_TABLES ) ;
944
- opts. insert ( OPTION_ENABLE_FOOTNOTES ) ;
945
- let mut p = Parser :: new_ext ( md, opts) ;
902
+ let mut p = Parser :: new_ext ( md, opts ( ) ) ;
946
903
947
904
let mut code_block_start = 0 ;
948
905
let mut code_start = 0 ;
@@ -1013,7 +970,7 @@ crate fn rust_code_blocks(md: &str) -> Vec<RustCodeBlock> {
1013
970
end : code_end,
1014
971
} ,
1015
972
syntax : if !syntax. is_empty ( ) {
1016
- Some ( syntax. into_owned ( ) )
973
+ Some ( syntax. into_string ( ) )
1017
974
} else {
1018
975
None
1019
976
} ,
0 commit comments