git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: use unicode super/subscript characters...

when possible if the superscript or subscript extension or raw_html aren't available.

commit826430003266a3f592b68abc178b43bf9dd209fa authorJohn MacFarlane <jgm@berkeley.edu> dateSat 18 Jul 2020, 15:16 (6 years ago) parent0e8e0dc
1 file changed+44−18
src/Text/Pandoc/Writers/Markdown.hs+44−18
src/Text/Pandoc/Writers/Markdown.hs+44−18
@@ -1086,28 +1086,38 @@ inlineToMarkdown _ (Superscript []) = return empty
10861086inlineToMarkdown opts (Superscript lst) =
10871087 local (\env -> env {envEscapeSpaces = True}) $ do
10881088 contents <- inlineListToMarkdown opts lst
1089− return $ if isEnabled Ext_superscript opts
1090− then "^" <> contents <> "^"
1091− else if isEnabled Ext_raw_html opts
1092− then "<sup>" <> contents <> "</sup>"
1093− else
1094− let rendered = render Nothing contents
1095− in case mapM toSuperscript (T.unpack rendered) of
1096− Just r -> literal $ T.pack r
1097− Nothing -> literal $ "^(" <> rendered <> ")"
1089+ if isEnabled Ext_superscript opts
1090+ then return $ "^" <> contents <> "^"
1091+ else if isEnabled Ext_raw_html opts
1092+ then return $ "<sup>" <> contents <> "</sup>"
1093+ else
1094+ case traverse toSuperscriptInline lst of
1095+ Just xs' | not (writerPreferAscii opts)
1096+ -> inlineListToMarkdown opts xs'
1097+ _ -> do
1098+ let rendered = render Nothing contents
1099+ return $
1100+ case mapM toSuperscript (T.unpack rendered) of
1101+ Just r -> literal $ T.pack r
1102+ Nothing -> literal $ "^(" <> rendered <> ")"
10981103inlineToMarkdown _ (Subscript []) = return empty
10991104inlineToMarkdown opts (Subscript lst) =
11001105 local (\env -> env {envEscapeSpaces = True}) $ do
11011106 contents <- inlineListToMarkdown opts lst
1102− return $ if isEnabled Ext_subscript opts
1103− then "~" <> contents <> "~"
1104− else if isEnabled Ext_raw_html opts
1105− then "<sub>" <> contents <> "</sub>"
1106− else
1107− let rendered = render Nothing contents
1108− in case mapM toSubscript (T.unpack rendered) of
1109− Just r -> literal $ T.pack r
1110− Nothing -> literal $ "_(" <> rendered <> ")"
1107+ if isEnabled Ext_subscript opts
1108+ then return $ "~" <> contents <> "~"
1109+ else if isEnabled Ext_raw_html opts
1110+ then return $ "<sub>" <> contents <> "</sub>"
1111+ else
1112+ case traverse toSubscriptInline lst of
1113+ Just xs' | not (writerPreferAscii opts)
1114+ -> inlineListToMarkdown opts xs'
1115+ _ -> do
1116+ let rendered = render Nothing contents
1117+ return $
1118+ case mapM toSuperscript (T.unpack rendered) of
1119+ Just r -> literal $ T.pack r
1120+ Nothing -> literal $ "_(" <> rendered <> ")"
11111121inlineToMarkdown opts (SmallCaps lst) = do
11121122 plain <- asks envPlain
11131123 if not plain &&
@@ -1332,3 +1342,19 @@ lineBreakToSpace :: Inline -> Inline
13321342lineBreakToSpace LineBreak = Space
13331343lineBreakToSpace SoftBreak = Space
13341344lineBreakToSpace x = x
1345+
1346+toSubscriptInline :: Inline -> Maybe Inline
1347+toSubscriptInline Space = Just Space
1348+toSubscriptInline (Span attr ils) = Span attr <$> traverse toSubscriptInline ils
1349+toSubscriptInline (Str s) = Str . T.pack <$> traverse toSubscript (T.unpack s)
1350+toSubscriptInline LineBreak = Just LineBreak
1351+toSubscriptInline SoftBreak = Just SoftBreak
1352+toSubscriptInline _ = Nothing
1353+
1354+toSuperscriptInline :: Inline -> Maybe Inline
1355+toSuperscriptInline Space = Just Space
1356+toSuperscriptInline (Span attr ils) = Span attr <$> traverse toSuperscriptInline ils
1357+toSuperscriptInline (Str s) = Str . T.pack <$> traverse toSuperscript (T.unpack s)
1358+toSuperscriptInline LineBreak = Just LineBreak
1359+toSuperscriptInline SoftBreak = Just SoftBreak
1360+toSuperscriptInline _ = Nothing