git.iko.soy/typeable/pandoc
on master help sign in
Org reader: Support inline math (like $E=mc^2$)

Closes #1223.

commitc47bd8404fda0a782719848ef190b56eb0fdb9dc authorAlbert Krewinkel <tarleb@moltkeplatz.de> dateMon 7 Apr 2014, 11:00 (13 years ago) parent480b33b
2 files changed+20−6
src/Text/Pandoc/Readers/Org.hs+16−6 tests/Tests/Readers/Org.hs+4
src/Text/Pandoc/Readers/Org.hs+16−6
@@ -459,6 +459,7 @@ inline = choice inlineParsers <?> "inline"
459459 , strikeout
460460 , underline
461461 , code
462+ , math
462463 , verbatim
463464 , subscript
464465 , superscript
@@ -530,10 +531,13 @@ underline = B.strong <$> inlinesEnclosedBy '_'
530531code :: OrgParser Inlines
531532code = B.code <$> rawEnclosedBy '='
532533
533−verbatim :: OrgParser Inlines
534+math :: OrgParser Inlines
535+math = B.math <$> rawEnclosedBy '$'
536+
537+verbatim :: OrgParser Inlines
534538verbatim = B.rawInline "" <$> rawEnclosedBy '~'
535539
536−subscript :: OrgParser Inlines
540+subscript :: OrgParser Inlines
537541subscript = B.subscript <$> (try $ char '_' *> maybeGroupedByBraces)
538542
539543superscript :: OrgParser Inlines
@@ -580,18 +584,24 @@ rawEnclosedBy c = enclosedRaw (atStart $ char c) (atEnd $ char c)
580584-- succeeds only if we're not right after a str (ie. in middle of word)
581585atStart :: OrgParser a -> OrgParser a
582586atStart p = do
583− pos <- getPosition
584− st <- getState
585− guard $ orgLastStrPos st /= Just pos
587+ guard =<< not <$> isRightAfterString
586588 p
587589
588590-- | succeeds only if we're at the end of a word
589591atEnd :: OrgParser a -> OrgParser a
590592atEnd p = try $ do
591− p <* lookingAtEndOfWord
593+ p <* lookingAtEndOfWord
592594 where lookingAtEndOfWord =
593595 eof <|> const (return ()) =<< lookAhead . oneOf =<< postWordChars
594596
597+isRightAfterString :: OrgParser Bool
598+isRightAfterString = do
599+ pos <- getPosition
600+ st <- getState
601+ -- the position `Nothing` isn't after a String, either, hence the double
602+ -- negation
603+ return $ not $ orgLastStrPos st /= Just pos
604+
595605postWordChars :: OrgParser [Char]
596606postWordChars = do
597607 st <- getState
tests/Tests/Readers/Org.hs+4
@@ -54,6 +54,10 @@ tests =
5454 "=Robot.rock()=" =?>
5555 para (code "Robot.rock()")
5656
57+ , "Math" =:
58+ "$E=mc^2$" =?>
59+ para (math "E=mc^2")
60+
5761 , "Verbatim" =:
5862 "~word for word~" =?>
5963 para (rawInline "" "word for word")