git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1781 from bergey/rst
704cfc1John MacFarlane
src/Text/Pandoc/Readers/RST.hs+17−3
@@ -47,7 +47,7 @@ import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
47import qualified Text.Pandoc.Builder as B
47import qualified Text.Pandoc.Builder as B
48import Data.Monoid (mconcat, mempty)
48import Data.Monoid (mconcat, mempty)
49import Data.Sequence (viewr, ViewR(..))
49import Data.Sequence (viewr, ViewR(..))
50−import Data.Char (toLower, isHexDigit)
50+import Data.Char (toLower, isHexDigit, isSpace)
51
51
52-- | Parse reStructuredText string and return Pandoc document.
52-- | Parse reStructuredText string and return Pandoc document.
53readRST :: ReaderOptions -- ^ Reader options
53readRST :: ReaderOptions -- ^ Reader options
@@ -335,6 +335,13 @@ indentedBlock = try $ do
335 optional blanklines
335 optional blanklines
336 return $ unlines lns
336 return $ unlines lns
337
337
338+quotedBlock :: Parser [Char] st [Char]
339+quotedBlock = try $ do
340+ quote <- lookAhead $ oneOf "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
341+ lns <- many1 $ lookAhead (char quote) >> anyLine
342+ optional blanklines
343+ return $ unlines lns
344+
338codeBlockStart :: Parser [Char] st Char
345codeBlockStart :: Parser [Char] st Char
339codeBlockStart = string "::" >> blankline >> blankline
346codeBlockStart = string "::" >> blankline >> blankline
340
347
@@ -342,7 +349,8 @@ codeBlock :: Parser [Char] st Blocks
342codeBlock = try $ codeBlockStart >> codeBlockBody
349codeBlock = try $ codeBlockStart >> codeBlockBody
343
350
344codeBlockBody :: Parser [Char] st Blocks
351codeBlockBody :: Parser [Char] st Blocks
345−codeBlockBody = try $ B.codeBlock . stripTrailingNewlines <$> indentedBlock
352+codeBlockBody = try $ B.codeBlock . stripTrailingNewlines <$>
353+ (indentedBlock <|> quotedBlock)
346
354
347lhsCodeBlock :: RSTParser Blocks
355lhsCodeBlock :: RSTParser Blocks
348lhsCodeBlock = try $ do
356lhsCodeBlock = try $ do
@@ -513,7 +521,6 @@ directive = try $ do
513-- TODO: line-block, parsed-literal, table, csv-table, list-table
521-- TODO: line-block, parsed-literal, table, csv-table, list-table
514-- date
522-- date
515-- include
523-- include
516−-- class
517-- title
524-- title
518directive' :: RSTParser Blocks
525directive' :: RSTParser Blocks
519directive' = do
526directive' = do
@@ -594,6 +601,13 @@ directive' = do
594 Just t -> B.link (escapeURI $ trim t) ""
601 Just t -> B.link (escapeURI $ trim t) ""
595 $ B.image src "" alt
602 $ B.image src "" alt
596 Nothing -> B.image src "" alt
603 Nothing -> B.image src "" alt
604+ "class" -> do
605+ let attrs = ("", (splitBy isSpace $ trim top), map (\(k,v) -> (k, trimr v)) fields)
606+ -- directive content or the first immediately following element
607+ children <- case body of
608+ "" -> block
609+ _ -> parseFromString parseBlocks body'
610+ return $ B.divWith attrs children
597 _ -> return mempty
611 _ -> return mempty
598
612
599-- TODO:
613-- TODO:
tests/Tests/Readers/RST.hs+22−1
@@ -67,5 +67,26 @@ tests = [ "line block with blank line" =:
67 link "http://foo.bar.baz" "" "http://foo.bar.baz" <> ". " <>
67 link "http://foo.bar.baz" "" "http://foo.bar.baz" <> ". " <>
68 link "http://foo.bar/baz_(bam)" "" "http://foo.bar/baz_(bam)"
68 link "http://foo.bar/baz_(bam)" "" "http://foo.bar/baz_(bam)"
69 <> " (" <> link "http://foo.bar" "" "http://foo.bar" <> ")")
69 <> " (" <> link "http://foo.bar" "" "http://foo.bar" <> ")")
70+ , "indented literal block" =: unlines
71+ [ "::"
72+ , ""
73+ , " block quotes"
74+ , ""
75+ , " can go on for many lines"
76+ , "but must stop here"]
77+ =?> (doc $
78+ codeBlock "block quotes\n\ncan go on for many lines" <>
79+ para "but must stop here")
80+ , "line block with 3 lines" =: "| a\n| b\n| c"
81+ =?> para ("a" <> linebreak <> "b" <> linebreak <> "c")
82+ , "quoted literal block using >" =: "::\n\n> quoted\n> block\n\nOrdinary paragraph"
83+ =?> codeBlock "> quoted\n> block" <> para "Ordinary paragraph"
84+ , "quoted literal block using | (not a line block)" =: "::\n\n| quoted\n| block\n\nOrdinary paragraph"
85+ =?> codeBlock "| quoted\n| block" <> para "Ordinary paragraph"
86+ , "class directive with single paragraph" =: ".. class:: special\n\nThis is a \"special\" paragraph."
87+ =?> divWith ("", ["special"], []) (para "This is a \"special\" paragraph.")
88+ , "class directive with two paragraphs" =: ".. class:: exceptional remarkable\n\n First paragraph.\n\n Second paragraph."
89+ =?> divWith ("", ["exceptional", "remarkable"], []) (para "First paragraph." <> para "Second paragraph.")
90+ , "class directive around literal block" =: ".. class:: classy\n\n::\n\n a\n b"
91+ =?> divWith ("", ["classy"], []) (codeBlock "a\nb")
70 ]
92 ]
71−