@@ -33,8 +33,11 @@ use crate::html::toc::TocBuilder;
3333use crate :: html:: highlight;
3434use crate :: test;
3535
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+ }
3841
3942/// A unit struct which has the `fmt::Display` trait implemented. When
4043/// 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>
297300
298301 fn next ( & mut self ) -> Option < Self :: Item > {
299302 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) ) )
304306 } else {
305- Some ( Event :: Start ( Tag :: Link ( dest, text) ) )
307+ Some ( Event :: Start ( Tag :: Link ( kind , dest, text) ) )
306308 }
307309 } else {
308310 event
@@ -393,7 +395,7 @@ fn check_if_allowed_tag(t: &Tag<'_>) -> bool {
393395 | Tag :: Emphasis
394396 | Tag :: Strong
395397 | Tag :: Code
396- | Tag :: Link ( _ , _ )
398+ | Tag :: Link ( .. )
397399 | Tag :: BlockQuote => true ,
398400 _ => false ,
399401 }
@@ -520,63 +522,39 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> {
520522 }
521523}
522524
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 ) {
536526 let mut parser = Parser :: new ( doc) ;
537527 let mut prev_offset = 0 ;
538528 let mut nb_lines = 0 ;
539529 let mut register_header = None ;
540- ' main : while let Some ( event) = parser. next ( ) {
530+ while let Some ( event) = parser. next ( ) {
541531 match event {
542532 Event :: Start ( Tag :: CodeBlock ( s) ) => {
533+ let offset = parser. get_offset ( ) ;
534+
543535 let block_info = if s. is_empty ( ) {
544536 LangString :: all_false ( )
545537 } else {
546538 LangString :: parse ( & * s, error_codes)
547539 } ;
548540 if !block_info. rust {
549- continue
541+ continue ;
550542 }
551543 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) ;
579547 }
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;
580558 }
581559 Event :: Start ( Tag :: Header ( level) ) => {
582560 register_header = Some ( level as u32 ) ;
@@ -593,7 +571,6 @@ pub fn find_testable_code<T: test::Tester>(
593571 _ => { }
594572 }
595573 }
596- Ok ( ( ) )
597574}
598575
599576#[ derive( Eq , PartialEq , Clone , Debug ) ]
@@ -687,10 +664,6 @@ impl<'a> fmt::Display for Markdown<'a> {
687664
688665 // This is actually common enough to special-case
689666 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-
694667 let replacer = |_: & str , s : & str | {
695668 if let Some ( & ( _, ref replace) ) = links. into_iter ( ) . find ( |link| & * link. 0 == s) {
696669 Some ( ( replace. clone ( ) , s. to_owned ( ) ) )
@@ -699,7 +672,7 @@ impl<'a> fmt::Display for Markdown<'a> {
699672 }
700673 } ;
701674
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) ) ;
703676
704677 let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
705678
@@ -718,11 +691,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
718691 let MarkdownWithToc ( md, ref ids, codes) = * self ;
719692 let mut ids = ids. borrow_mut ( ) ;
720693
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 ( ) ) ;
726695
727696 let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
728697
@@ -748,11 +717,7 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
748717
749718 // This is actually common enough to special-case
750719 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 ( ) ) ;
756721
757722 // Treat inline HTML as plain text.
758723 let p = p. map ( |event| match event {
@@ -868,10 +833,6 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
868833 return vec ! [ ] ;
869834 }
870835
871- let mut opts = Options :: empty ( ) ;
872- opts. insert ( OPTION_ENABLE_TABLES ) ;
873- opts. insert ( OPTION_ENABLE_FOOTNOTES ) ;
874-
875836 let mut links = vec ! [ ] ;
876837 let shortcut_links = RefCell :: new ( vec ! [ ] ) ;
877838
@@ -894,20 +855,19 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
894855 shortcut_links. borrow_mut ( ) . push ( ( s. to_owned ( ) , locate ( s) ) ) ;
895856 None
896857 } ;
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) ) ;
899859
900860 // There's no need to thread an IdMap through to here because
901861 // the IDs generated aren't going to be emitted anywhere.
902862 let mut ids = IdMap :: new ( ) ;
903863 let iter = Footnotes :: new ( HeadingLinks :: new ( p, None , & mut ids) ) ;
904864
905865 for ev in iter {
906- if let Event :: Start ( Tag :: Link ( dest, _) ) = ev {
866+ if let Event :: Start ( Tag :: Link ( _ , dest, _) ) = ev {
907867 debug ! ( "found link: {}" , dest) ;
908868 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 ) ,
911871 } ) ;
912872 }
913873 }
@@ -939,10 +899,7 @@ crate fn rust_code_blocks(md: &str) -> Vec<RustCodeBlock> {
939899 return code_blocks;
940900 }
941901
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 ( ) ) ;
946903
947904 let mut code_block_start = 0 ;
948905 let mut code_start = 0 ;
@@ -1013,7 +970,7 @@ crate fn rust_code_blocks(md: &str) -> Vec<RustCodeBlock> {
1013970 end : code_end,
1014971 } ,
1015972 syntax : if !syntax. is_empty ( ) {
1016- Some ( syntax. into_owned ( ) )
973+ Some ( syntax. into_string ( ) )
1017974 } else {
1018975 None
1019976 } ,
0 commit comments