git.iko.soy/typeable/pandoc
on master help sign in
expose warnings from RST reader; refactor

This commit moves some code which was only used for the Markdown Reader into a generic form which can be used for any Reader. Otherwise, it takes naming and interface cues from the preexisting Markdown code.

commit15816853a3611a96994842a34a975e91c253c7ab authorDaniel Bergey <bergey@alum.mit.edu> dateFri 5 Dec 2014, 22:21 (12 years ago) parent87e536b
4 files changed+32−20
src/Text/Pandoc.hs+12−13 src/Text/Pandoc/Parsing.hs+10 src/Text/Pandoc/Readers/Markdown.hs+1−5 src/Text/Pandoc/Readers/RST.hs+9−2
src/Text/Pandoc.hs+12−13
@@ -201,19 +201,18 @@ parseFormatSpec = parse formatSpec ""
201201 '-' -> Set.delete ext
202202 _ -> Set.insert ext
203203
204−-- auxiliary function for readers:
205−markdown :: ReaderOptions -> String -> IO Pandoc
206−markdown o s = do
207− let (doc, warnings) = readMarkdownWithWarnings o s
208− mapM_ warn warnings
209− return doc
210−
211204data Reader = StringReader (ReaderOptions -> String -> IO Pandoc)
212205 | ByteStringReader (ReaderOptions -> BL.ByteString -> IO (Pandoc, MediaBag))
213206
214207mkStringReader :: (ReaderOptions -> String -> Pandoc) -> Reader
215208mkStringReader r = StringReader (\o s -> return $ r o s)
216209
210+mkStringReaderWithWarnings :: (ReaderOptions -> String -> (Pandoc, [String])) -> Reader
211+mkStringReaderWithWarnings r = StringReader $ \o s -> do
212+ let (doc, warnings) = r o s
213+ mapM_ warn warnings
214+ return doc
215+
217216mkBSReader :: (ReaderOptions -> BL.ByteString -> (Pandoc, MediaBag)) -> Reader
218217mkBSReader r = ByteStringReader (\o s -> return $ r o s)
219218
@@ -221,12 +220,12 @@ mkBSReader r = ByteStringReader (\o s -> return $ r o s)
221220readers :: [(String, Reader)]
222221readers = [ ("native" , StringReader $ \_ s -> return $ readNative s)
223222 ,("json" , mkStringReader readJSON )
224− ,("markdown" , StringReader markdown)
225− ,("markdown_strict" , StringReader markdown)
226− ,("markdown_phpextra" , StringReader markdown)
227− ,("markdown_github" , StringReader markdown)
228− ,("markdown_mmd", StringReader markdown)
229− ,("rst" , mkStringReader readRST )
223+ ,("markdown" , mkStringReaderWithWarnings readMarkdownWithWarnings)
224+ ,("markdown_strict" , mkStringReaderWithWarnings readMarkdownWithWarnings)
225+ ,("markdown_phpextra" , mkStringReaderWithWarnings readMarkdownWithWarnings)
226+ ,("markdown_github" , mkStringReaderWithWarnings readMarkdownWithWarnings)
227+ ,("markdown_mmd", mkStringReaderWithWarnings readMarkdownWithWarnings)
228+ ,("rst" , mkStringReaderWithWarnings readRSTWithWarnings )
230229 ,("mediawiki" , mkStringReader readMediaWiki)
231230 ,("docbook" , mkStringReader readDocBook)
232231 ,("opml" , mkStringReader readOPML)
src/Text/Pandoc/Parsing.hs+10
@@ -65,6 +65,7 @@ module Text.Pandoc.Parsing ( anyLine,
6565 widthsFromIndices,
6666 gridTableWith,
6767 readWith,
68+ readWithWarnings,
6869 readWithM,
6970 testStringWith,
7071 guardEnabled,
@@ -881,6 +882,15 @@ readWith :: Parser [Char] st a
881882 -> a
882883readWith p t inp = runIdentity $ readWithM p t inp
883884
885+readWithWarnings :: Parser [Char] ParserState a
886+ -> ParserState
887+ -> String
888+ -> (a, [String])
889+readWithWarnings p = readWith $ do
890+ doc <- p
891+ warnings <- stateWarnings <$> getState
892+ return (doc, warnings)
893+
884894-- | Parse a string with @parser@ (for testing).
885895testStringWith :: (Show a, Stream [Char] Identity Char)
886896 => ParserT [Char] ParserState Identity a
src/Text/Pandoc/Readers/Markdown.hs+1−5
@@ -79,11 +79,7 @@ readMarkdownWithWarnings :: ReaderOptions -- ^ Reader options
7979 -> String -- ^ String to parse (assuming @'\n'@ line endings)
8080 -> (Pandoc, [String])
8181readMarkdownWithWarnings opts s =
82− (readWith parseMarkdownWithWarnings) def{ stateOptions = opts } (s ++ "\n\n")
83− where parseMarkdownWithWarnings = do
84− doc <- parseMarkdown
85− warnings <- stateWarnings <$> getState
86− return (doc, warnings)
82+ (readWithWarnings parseMarkdown) def{ stateOptions = opts } (s ++ "\n\n")
8783
8884trimInlinesF :: F Inlines -> F Inlines
8985trimInlinesF = liftM trimInlines
src/Text/Pandoc/Readers/RST.hs+9−2
@@ -29,7 +29,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2929Conversion from reStructuredText to 'Pandoc' document.
3030-}
3131module Text.Pandoc.Readers.RST (
32− readRST
32+ readRST,
33+ readRSTWithWarnings
3334 ) where
3435import Text.Pandoc.Definition
3536import Text.Pandoc.Builder (setMeta, fromList)
@@ -55,6 +56,9 @@ readRST :: ReaderOptions -- ^ Reader options
5556 -> Pandoc
5657readRST opts s = (readWith parseRST) def{ stateOptions = opts } (s ++ "\n\n")
5758
59+readRSTWithWarnings :: ReaderOptions -> String -> (Pandoc, [String])
60+readRSTWithWarnings opts s = (readWithWarnings parseRST) def{ stateOptions = opts } (s ++ "\n\n")
61+
5862type RSTParser = Parser [Char] ParserState
5963
6064--
@@ -1016,7 +1020,10 @@ renderRole contents fmt role attr = case role of
10161020 fmtStr = fmt `mplus` newFmt
10171021 (newRole, newAttr) = inherit attr
10181022 in renderRole contents fmtStr newRole newAttr
1019− Nothing -> return $ B.str contents -- Undefined role
1023+ Nothing -> do
1024+ pos <- getPosition
1025+ addWarning (Just pos) $ "ignoring unknown role :" ++ custom ++ ": in "
1026+ return $ B.str contents -- Undefined role
10201027 where
10211028 titleRef ref = return $ B.str ref -- FIXME: Not a sensible behaviour
10221029 rfcLink rfcNo = B.link rfcUrl ("RFC " ++ rfcNo) $ B.str ("RFC " ++ rfcNo)