From cf306f34e5e23a1416b598766ca73d63d7367283 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 21 Mar 2017 15:41:58 +0100 Subject: Plain writer: use _(..) or ^(..) for super/subscript... ...unless unicode super/subscripted characters are available. --- src/Text/Pandoc/Writers/Markdown.hs | 55 +++++++++++++++++++++---------------- test/writer.opml | 2 +- test/writer.plain | 4 +-- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index c1a02e609..88dd53808 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -915,8 +915,11 @@ inlineToMarkdown opts (Superscript lst) = then "^" <> contents <> "^" else if isEnabled Ext_raw_html opts then "" <> contents <> "" - else text $ map toSuperscript - $ render Nothing contents + else + let rendered = render Nothing contents + in case mapM toSuperscript rendered of + Just r -> text r + Nothing -> text $ "^(" ++ rendered ++ ")" inlineToMarkdown opts (Subscript lst) = local (\env -> env {envEscapeSpaces = True}) $ do contents <- inlineListToMarkdown opts lst @@ -924,8 +927,11 @@ inlineToMarkdown opts (Subscript lst) = then "~" <> contents <> "~" else if isEnabled Ext_raw_html opts then "" <> contents <> "" - else text $ map toSubscript - $ render Nothing contents + else + let rendered = render Nothing contents + in case mapM toSubscript rendered of + Just r -> text r + Nothing -> text $ "_(" ++ rendered ++ ")" inlineToMarkdown opts (SmallCaps lst) = do plain <- asks envPlain if not plain && @@ -1120,28 +1126,29 @@ makeMathPlainer = walk go go (Emph xs) = Span nullAttr xs go x = x -toSuperscript :: Char -> Char -toSuperscript '1' = '\x00B9' -toSuperscript '2' = '\x00B2' -toSuperscript '3' = '\x00B3' -toSuperscript '+' = '\x207A' -toSuperscript '-' = '\x207B' -toSuperscript '=' = '\x207C' -toSuperscript '(' = '\x207D' -toSuperscript ')' = '\x207E' +toSuperscript :: Char -> Maybe Char +toSuperscript '1' = Just '\x00B9' +toSuperscript '2' = Just '\x00B2' +toSuperscript '3' = Just '\x00B3' +toSuperscript '+' = Just '\x207A' +toSuperscript '-' = Just '\x207B' +toSuperscript '=' = Just '\x207C' +toSuperscript '(' = Just '\x207D' +toSuperscript ')' = Just '\x207E' toSuperscript c | c >= '0' && c <= '9' = - chr (0x2070 + (ord c - 48)) - | otherwise = c + Just $ chr (0x2070 + (ord c - 48)) + | isSpace c = Just c + | otherwise = Nothing -toSubscript :: Char -> Char -toSubscript '+' = '\x208A' -toSubscript '-' = '\x208B' -toSubscript '=' = '\x208C' -toSubscript '(' = '\x208D' -toSubscript ')' = '\x208E' +toSubscript :: Char -> Maybe Char +toSubscript '+' = Just '\x208A' +toSubscript '-' = Just '\x208B' +toSubscript '=' = Just '\x208C' +toSubscript '(' = Just '\x208D' +toSubscript ')' = Just '\x208E' toSubscript c | c >= '0' && c <= '9' = - chr (0x2080 + (ord c - 48)) - | otherwise = c - + Just $ chr (0x2080 + (ord c - 48)) + | isSpace c = Just c + | otherwise = Nothing diff --git a/test/writer.opml b/test/writer.opml index 0b621823e..51b0cb2d5 100644 --- a/test/writer.opml +++ b/test/writer.opml @@ -46,7 +46,7 @@ - + diff --git a/test/writer.plain b/test/writer.plain index f34af9100..175efb608 100644 --- a/test/writer.plain +++ b/test/writer.plain @@ -448,9 +448,9 @@ This is code: >, $, \, \$, . ~~This is _strikeout_.~~ -Superscripts: abcd a_hello_ ahello there. +Superscripts: a^(bc)d a^(_hello_) a^(hello there). -Subscripts: H₂O, H₂₃O, Hmany of themO. +Subscripts: H₂O, H₂₃O, H_(many of them)O. These should not be superscripts or subscripts, because of the unescaped spaces: a^b c^d, a~b c~d. -- cgit v1.2.3