@@ -36,6 +36,7 @@ use std::str::{self, CharIndices};
3636use std:: sync:: atomic:: AtomicUsize ;
3737use std:: sync:: { Arc , Weak } ;
3838
39+ use math_core:: { LatexToMathML , MathCoreConfig , MathDisplay } ;
3940use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
4041use rustc_errors:: { Diag , DiagMessage } ;
4142use rustc_hir:: def_id:: LocalDefId ;
@@ -71,6 +72,7 @@ pub(crate) fn summary_opts() -> Options {
7172 | Options :: ENABLE_STRIKETHROUGH
7273 | Options :: ENABLE_TASKLISTS
7374 | Options :: ENABLE_SMART_PUNCTUATION
75+ | Options :: ENABLE_MATH
7476}
7577
7678#[ derive( Debug , Clone , Copy ) ]
@@ -500,6 +502,51 @@ impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for SpannedLinkReplacer<
500502 }
501503}
502504
505+ /// Convert $\LaTeX$ math markup into MathML Core
506+ struct Math < ' a , I : Iterator < Item = Event < ' a > > > {
507+ inner : I ,
508+ converter : LatexToMathML ,
509+ }
510+
511+ impl < ' a , I : Iterator < Item = Event < ' a > > > Math < ' a , I > {
512+ fn new ( iter : I ) -> Self {
513+ Self {
514+ inner : iter,
515+ converter : LatexToMathML :: new ( MathCoreConfig {
516+ pretty_print : math_core:: PrettyPrint :: Never ,
517+ xml_namespace : false ,
518+ ..MathCoreConfig :: default ( )
519+ } )
520+ . expect ( "no custom macros are specified, so error should be impossible" ) ,
521+ }
522+ }
523+ }
524+
525+ impl < ' a , I : Iterator < Item = Event < ' a > > > Iterator for Math < ' a , I > {
526+ type Item = Event < ' a > ;
527+
528+ fn next ( & mut self ) -> Option < Self :: Item > {
529+ match self . inner . next ( ) {
530+ Some ( Event :: InlineMath ( latex) ) => {
531+ let mathml = self
532+ . converter
533+ . convert_with_local_counter ( & latex, MathDisplay :: Inline )
534+ . expect ( "a production-ready version of this should handle the error somehow" ) ;
535+ Some ( Event :: InlineHtml ( mathml. into ( ) ) )
536+ }
537+ Some ( Event :: DisplayMath ( latex) ) => {
538+ let mathml = self
539+ . converter
540+ . convert_with_local_counter ( & latex, MathDisplay :: Block )
541+ . expect ( "a production-ready version of this should handle the error somehow" ) ;
542+ // as counterintuitive as it might seem, block equations are still parsed as inline
543+ Some ( Event :: InlineHtml ( mathml. into ( ) ) )
544+ }
545+ event => event,
546+ }
547+ }
548+ }
549+
503550/// Wrap HTML tables into `<div>` to prevent having the doc blocks width being too big.
504551struct TableWrapper < ' a , I : Iterator < Item = Event < ' a > > > {
505552 inner : I ,
@@ -624,7 +671,7 @@ fn check_if_allowed_tag(t: &TagEnd) -> bool {
624671 | TagEnd :: Strong
625672 | TagEnd :: Strikethrough
626673 | TagEnd :: Link
627- | TagEnd :: BlockQuote
674+ | TagEnd :: BlockQuote ( .. )
628675 )
629676}
630677
@@ -1369,6 +1416,7 @@ impl<'a> Markdown<'a> {
13691416 let p = SpannedLinkReplacer :: new ( p, links) ;
13701417 let p = footnotes:: Footnotes :: new ( p, existing_footnotes) ;
13711418 let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1419+ let p = Math :: new ( p) ;
13721420 CodeBlocks :: new ( p, codes, edition, playground)
13731421 } )
13741422 }
@@ -1451,6 +1499,7 @@ impl MarkdownWithToc<'_> {
14511499 let p = footnotes:: Footnotes :: new ( p, existing_footnotes) ;
14521500 let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
14531501 let p = CodeBlocks :: new ( p, codes, edition, playground) ;
1502+ let p = Math :: new ( p) ;
14541503 html:: push_html ( & mut s, p) ;
14551504 } ) ;
14561505
@@ -1500,6 +1549,7 @@ impl<'a> MarkdownItemInfo<'a> {
15001549 let p = p. filter ( |event| {
15011550 !matches ! ( event, Event :: Start ( Tag :: Paragraph ) | Event :: End ( TagEnd :: Paragraph ) )
15021551 } ) ;
1552+ let p = Math :: new ( p) ;
15031553 html:: write_html_fmt ( & mut f, p)
15041554 } )
15051555 }
@@ -1914,7 +1964,9 @@ pub(crate) fn markdown_links<'md, R>(
19141964 span_for_link ( & dest_url, span)
19151965 }
19161966 }
1917- LinkType :: Autolink | LinkType :: Email => unreachable ! ( ) ,
1967+ LinkType :: Autolink | LinkType :: Email | LinkType :: WikiLink { .. } => {
1968+ unreachable ! ( )
1969+ }
19181970 } ;
19191971
19201972 if let Some ( link) = preprocess_link ( MarkdownLink {
0 commit comments