git.iko.soy/typeable/pandoc
on master help sign in
Org reader: Read inline code blocks
71bd4fbAlbert Krewinkel
src/Text/Pandoc/Readers/Org.hs+40−3
@@ -43,6 +43,7 @@ import Text.Pandoc.Shared (compactify', compactify'DL)
43
43
44import Control.Applicative ( Applicative, pure
44import Control.Applicative ( Applicative, pure
45 , (<$>), (<$), (<*>), (<*), (*>), (<**>) )
45 , (<$>), (<$), (<*>), (<*), (*>), (<**>) )
46+import Control.Arrow (first)
46import Control.Monad (foldM, guard, liftM, liftM2, mzero, when)
47import Control.Monad (foldM, guard, liftM, liftM2, mzero, when)
47import Control.Monad.Reader (Reader, runReader, ask, asks)
48import Control.Monad.Reader (Reader, runReader, ask, asks)
48import Data.Char (isAlphaNum, toLower)
49import Data.Char (isAlphaNum, toLower)
@@ -721,7 +722,6 @@ bulletList = fmap B.bulletList . fmap compactify' . sequence
721 <$> many1 (listItem bulletListStart)
722 <$> many1 (listItem bulletListStart)
722
723
723orderedList :: OrgParser (F Blocks)
724orderedList :: OrgParser (F Blocks)
724−-- orderedList = B.orderedList . compactify' <$> many1 (listItem orderedListStart)
725orderedList = fmap B.orderedList . fmap compactify' . sequence
725orderedList = fmap B.orderedList . fmap compactify' . sequence
726 <$> many1 (listItem orderedListStart)
726 <$> many1 (listItem orderedListStart)
727
727
@@ -746,11 +746,11 @@ definitionListItem :: OrgParser Int
746definitionListItem parseMarkerGetLength = try $ do
746definitionListItem parseMarkerGetLength = try $ do
747 markerLength <- parseMarkerGetLength
747 markerLength <- parseMarkerGetLength
748 term <- manyTill (noneOf "\n\r") (try $ string "::")
748 term <- manyTill (noneOf "\n\r") (try $ string "::")
749− first <- anyLineNewline
749+ line1 <- anyLineNewline
750 blank <- option "" ("\n" <$ blankline)
750 blank <- option "" ("\n" <$ blankline)
751 cont <- concat <$> many (listContinuation markerLength)
751 cont <- concat <$> many (listContinuation markerLength)
752 term' <- parseFromString inline term
752 term' <- parseFromString inline term
753− contents' <- parseFromString parseBlocks $ first ++ blank ++ cont
753+ contents' <- parseFromString parseBlocks $ line1 ++ blank ++ cont
754 return $ (,) <$> term' <*> fmap (:[]) contents'
754 return $ (,) <$> term' <*> fmap (:[]) contents'
755
755
756
756
@@ -789,6 +789,7 @@ inline =
789 , footnote
789 , footnote
790 , linkOrImage
790 , linkOrImage
791 , anchor
791 , anchor
792+ , inlineCodeBlock
792 , str
793 , str
793 , endline
794 , endline
794 , emph
795 , emph
@@ -989,6 +990,42 @@ solidify = map replaceSpecialChar
989 | c `elem` "_.-:" = c
990 | c `elem` "_.-:" = c
990 | otherwise = '-'
991 | otherwise = '-'
991
992
993+-- | Parses an inline code block and marks it as an babel block.
994+inlineCodeBlock :: OrgParser (F Inlines)
995+inlineCodeBlock = try $ do
996+ string "src_"
997+ lang <- many1 orgArgWordChar
998+ opts <- option [] $ enclosedByPair '[' ']' blockOption
999+ inlineCode <- enclosedByPair '{' '}' (noneOf "\n\r")
1000+ let attrClasses = [translateLang lang, rundocBlockClass]
1001+ let attrKeyVal = map toRundocAttrib (("language", lang) : opts)
1002+ returnF $ B.codeWith ("", attrClasses, attrKeyVal) inlineCode
1003+ where enclosedByPair s e p = char s *> many1Till p (char e)
1004+
1005+-- | The class-name used to mark rundoc blocks.
1006+rundocBlockClass :: String
1007+rundocBlockClass = "rundoc-block"
1008+
1009+blockOption :: OrgParser (String, String)
1010+blockOption = try $ (,) <$> orgArgKey <*> orgArgValue
1011+
1012+orgArgKey :: OrgParser String
1013+orgArgKey = try $
1014+ skipSpaces *> char ':'
1015+ *> many1 orgArgWordChar
1016+ <* many1 spaceChar
1017+
1018+orgArgValue :: OrgParser String
1019+orgArgValue = try $
1020+ skipSpaces *> many1 orgArgWordChar
1021+ <* skipSpaces
1022+
1023+orgArgWordChar :: OrgParser Char
1024+orgArgWordChar = alphaNum <|> oneOf "-_"
1025+
1026+toRundocAttrib :: (String, String) -> (String, String)
1027+toRundocAttrib = first ("rundoc-" ++)
1028+
992emph :: OrgParser (F Inlines)
1029emph :: OrgParser (F Inlines)
993emph = fmap B.emph <$> emphasisBetween '/'
1030emph = fmap B.emph <$> emphasisBetween '/'
994
1031
tests/Tests/Readers/Org.hs+18
@@ -207,6 +207,24 @@ tests =
207 "<<anchor>> Link here later." =?>
207 "<<anchor>> Link here later." =?>
208 (para $ spanWith ("anchor", [], []) mempty <>
208 (para $ spanWith ("anchor", [], []) mempty <>
209 "Link" <> space <> "here" <> space <> "later.")
209 "Link" <> space <> "here" <> space <> "later.")
210+
211+ , "Inline code block" =:
212+ "src_emacs-lisp{(message \"Hello\")}" =?>
213+ (para $ codeWith ( ""
214+ , [ "commonlisp", "rundoc-block" ]
215+ , [ ("rundoc-language", "emacs-lisp") ])
216+ "(message \"Hello\")")
217+
218+ , "Inline code block with arguments" =:
219+ "src_sh[:export both :results output]{echo 'Hello, World'}" =?>
220+ (para $ codeWith ( ""
221+ , [ "bash", "rundoc-block" ]
222+ , [ ("rundoc-language", "sh")
223+ , ("rundoc-export", "both")
224+ , ("rundoc-results", "output")
225+ ]
226+ )
227+ "echo 'Hello, World'")
210 ]
228 ]
211
229
212 , testGroup "Meta Information" $
230 , testGroup "Meta Information" $