Plain writer: use _(..) or ^(..) for super/subscript...

...unless unicode super/subscripted characters are available.
This commit is contained in:
John MacFarlane 2017-03-21 15:41:58 +01:00
parent 430e2db9ba
commit cf306f34e5
3 changed files with 34 additions and 27 deletions

View file

@ -915,8 +915,11 @@ inlineToMarkdown opts (Superscript lst) =
then "^" <> contents <> "^"
else if isEnabled Ext_raw_html opts
then "<sup>" <> contents <> "</sup>"
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 "<sub>" <> contents <> "</sub>"
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

View file

@ -46,7 +46,7 @@
</outline>
<outline text="HTML Blocks" _note="Simple block on one line:&#10;&#10;foo&#10;&#10;And nested without indentation:&#10;&#10;foo&#10;&#10;bar&#10;&#10;Interpreted markdown in a table:&#10;&#10;This is *emphasized*&#10;And this is **strong**&#10;Heres a simple block:&#10;&#10;foo&#10;&#10;This should be a code block, though:&#10;&#10; &lt;div&gt;&#10; foo&#10; &lt;/div&gt;&#10;&#10;As should this:&#10;&#10; &lt;div&gt;foo&lt;/div&gt;&#10;&#10;Now, nested:&#10;&#10;foo&#10;&#10;This should just be an HTML comment:&#10;&#10;Multiline:&#10;&#10;Code block:&#10;&#10; &lt;!-- Comment --&gt;&#10;&#10;Just plain comment, with trailing spaces on the line:&#10;&#10;Code:&#10;&#10; &lt;hr /&gt;&#10;&#10;Hrs:&#10;&#10;------------------------------------------------------------------------">
</outline>
<outline text="Inline Markup" _note="This is *emphasized*, and so *is this*.&#10;&#10;This is **strong**, and so **is this**.&#10;&#10;An *[emphasized link](/url)*.&#10;&#10;***This is strong and em.***&#10;&#10;So is ***this*** word.&#10;&#10;***This is strong and em.***&#10;&#10;So is ***this*** word.&#10;&#10;This is code: `&gt;`, `$`, `\`, `\$`, `&lt;html&gt;`.&#10;&#10;This is *strikeout*.&#10;&#10;Superscripts: abcd a*hello* ahello there.&#10;&#10;Subscripts: H₂O, H₂₃O, Hmany of themO.&#10;&#10;These should not be superscripts or subscripts, because of the unescaped&#10;spaces: a^b c^d, a~b c~d.&#10;&#10;------------------------------------------------------------------------">
<outline text="Inline Markup" _note="This is *emphasized*, and so *is this*.&#10;&#10;This is **strong**, and so **is this**.&#10;&#10;An *[emphasized link](/url)*.&#10;&#10;***This is strong and em.***&#10;&#10;So is ***this*** word.&#10;&#10;***This is strong and em.***&#10;&#10;So is ***this*** word.&#10;&#10;This is code: `&gt;`, `$`, `\`, `\$`, `&lt;html&gt;`.&#10;&#10;This is *strikeout*.&#10;&#10;Superscripts: a^(bc)d a^(*hello*) a^(hello there).&#10;&#10;Subscripts: H₂O, H₂₃O, H_(many of them)O.&#10;&#10;These should not be superscripts or subscripts, because of the unescaped&#10;spaces: a^b c^d, a~b c~d.&#10;&#10;------------------------------------------------------------------------">
</outline>
<outline text="Smart quotes, ellipses, dashes" _note="“Hello,” said the spider. “Shelob is my name.”&#10;&#10;A, B, and C are letters.&#10;&#10;Oak, elm, and beech are names of trees. So is pine.&#10;&#10;He said, “I want to go.”’ Were you alive in the 70s?&#10;&#10;Here is some quoted `code` and a “[quoted&#10;link](http://example.com/?foo=1&amp;bar=2)”.&#10;&#10;Some dashes: one—two — three—four — five.&#10;&#10;Dashes between numbers: 57, 25566, 19871999.&#10;&#10;Ellipses…and…and….&#10;&#10;------------------------------------------------------------------------">
</outline>

View file

@ -448,9 +448,9 @@ This is code: >, $, \, \$, <html>.
~~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.