Skip to content

Loading…

Exposes parsers in the API (issue #418) #567

Open
wants to merge 3 commits into from

1 participant

@noinia

I managed to get the changes required to fix issue 418 (#418) in git. Hopefully this can cat committed and fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 30, 2012
  1. exposed the readers in the API

    Frank Staals committed
  2. Merge branch 'master' of git://github.com/jgm/pandoc

    Frank Staals committed
Commits on Jul 14, 2012
  1. Merge branch 'master' of git://github.com/jgm/pandoc

    Frank Staals committed
View
20 src/Text/Pandoc/Parsing.hs
@@ -59,6 +59,7 @@ module Text.Pandoc.Parsing ( (>>~),
tableWith,
gridTableWith,
readWith,
+ dumpParseError,
testStringWith,
ParserState (..),
defaultParserState,
@@ -608,15 +609,22 @@ gridTableFooter = blanklines
---
--- | Parse a string with a given parser and state.
+
+-- | Takes a parser result and returns the string, if the parsing failed
+-- it just returns an error
+dumpParseError :: Either ParseError a -- ^ the result of of running a parser
+ -> a
+dumpParseError r = case r of
+ Left err' -> error $ "\nError:\n" ++ show err'
+ Right result -> result
+
+-- | Run the parser on a given input and state
readWith :: GenParser t ParserState a -- ^ parser
-> ParserState -- ^ initial state
-> [t] -- ^ input
- -> a
-readWith parser state input =
- case runParser parser state "source" input of
- Left err' -> error $ "\nError:\n" ++ show err'
- Right result -> result
+ -> Either ParseError a
+readWith parser state = runParser parser state "source"
+
-- | Parse a string with @parser@ (for testing).
testStringWith :: (Show a) => GenParser Char ParserState a
View
19 src/Text/Pandoc/Readers/HTML.hs
@@ -28,6 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Conversion of HTML to 'Pandoc' document.
-}
module Text.Pandoc.Readers.HTML ( readHtml
+ , readHtml'
, htmlTag
, htmlInBalanced
, isInlineTag
@@ -59,12 +60,18 @@ isSpace _ = False
readHtml :: ParserState -- ^ Parser state
-> String -- ^ String to parse (assumes @'\n'@ line endings)
-> Pandoc
-readHtml st inp = Pandoc meta blocks
- where blocks = readWith parseBody st rest
- tags = canonicalizeTags $
- parseTagsOptions parseOptions{ optTagPosition = True } inp
- hasHeader = any (~== TagOpen "head" []) tags
- (meta, rest) = if hasHeader
+readHtml state = dumpParseError . readHtml' state
+
+readHtml' :: ParserState -- ^ Parser state
+ -> String -- ^ String to parse (assumes @'\n'@ line endings)
+ -> Either ParseError Pandoc
+readHtml' st inp = either Left mkPandoc . readWith parseBody st $ rest
+ where
+ mkPandoc = Right . Pandoc meta
+ tags = canonicalizeTags $
+ parseTagsOptions parseOptions{ optTagPosition = True } inp
+ hasHeader = any (~== TagOpen "head" []) tags
+ (meta, rest) = if hasHeader
then parseHeader tags
else (Meta [] [] [], tags)
View
9 src/Text/Pandoc/Readers/LaTeX.hs
@@ -28,6 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Conversion of LaTeX to 'Pandoc' document.
-}
module Text.Pandoc.Readers.LaTeX ( readLaTeX,
+ readLaTeX',
rawLaTeXInline,
rawLaTeXBlock,
handleIncludes
@@ -52,7 +53,12 @@ import qualified Data.Map as M
readLaTeX :: ParserState -- ^ Parser state, including options for parser
-> String -- ^ String to parse (assumes @'\n'@ line endings)
-> Pandoc
-readLaTeX = readWith parseLaTeX
+readLaTeX st = dumpParseError . readLaTeX' st
+
+readLaTeX' :: ParserState -- ^ Parser state, including options for parser
+ -> String -- ^ String to parse (assumes @'\n'@ line endings)
+ -> Either ParseError Pandoc
+readLaTeX' = readWith parseLaTeX
parseLaTeX :: LP Pandoc
parseLaTeX = do
@@ -985,4 +991,3 @@ simpTable = try $ do
else header'
lookAhead $ controlSeq "end" -- make sure we're at end
return $ table mempty (zip aligns (repeat 0)) header'' rows
-
View
9 src/Text/Pandoc/Readers/Markdown.hs
@@ -28,7 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Conversion of markdown-formatted plain text to 'Pandoc' document.
-}
-module Text.Pandoc.Readers.Markdown ( readMarkdown ) where
+module Text.Pandoc.Readers.Markdown ( readMarkdown , readMarkdown' ) where
import Data.List ( transpose, sortBy, findIndex, intercalate )
import qualified Data.Map as M
@@ -52,7 +52,12 @@ import Text.HTML.TagSoup.Match (tagOpen)
readMarkdown :: ParserState -- ^ Parser state, including options for parser
-> String -- ^ String to parse (assuming @'\n'@ line endings)
-> Pandoc
-readMarkdown state s = (readWith parseMarkdown) state (s ++ "\n\n")
+readMarkdown state = dumpParseError . readMarkdown' state
+
+readMarkdown' :: ParserState -- ^ Parser state, including options for parser
+ -> String -- ^ String to parse (assuming @'\n'@ line endings)
+ -> Either ParseError Pandoc
+readMarkdown' state s = (readWith parseMarkdown) state (s ++ "\n\n")
--
-- Constants and data structure definitions
View
11 src/Text/Pandoc/Readers/RST.hs
@@ -27,8 +27,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Conversion from reStructuredText to 'Pandoc' document.
-}
-module Text.Pandoc.Readers.RST (
- readRST
+module Text.Pandoc.Readers.RST ( readRST
+ , readRST'
) where
import Text.Pandoc.Definition
import Text.Pandoc.Shared
@@ -44,7 +44,12 @@ import Data.Maybe ( catMaybes )
readRST :: ParserState -- ^ Parser state, including options for parser
-> String -- ^ String to parse (assuming @'\n'@ line endings)
-> Pandoc
-readRST state s = (readWith parseRST) state (s ++ "\n\n")
+readRST st = dumpParseError . readRST' st
+
+readRST' :: ParserState -- ^ Parser state, including options for parser
+ -> String -- ^ String to parse (assuming @'\n'@ line endings)
+ -> Either ParseError Pandoc
+readRST' state s = (readWith parseRST) state (s ++ "\n\n")
--
-- Constants and data structure definitions
View
9 src/Text/Pandoc/Readers/Textile.hs
@@ -53,7 +53,7 @@ TODO : refactor common patterns across readers :
-}
-module Text.Pandoc.Readers.Textile ( readTextile) where
+module Text.Pandoc.Readers.Textile ( readTextile, readTextile' ) where
import Text.Pandoc.Definition
import Text.Pandoc.Shared
@@ -70,7 +70,12 @@ import Control.Applicative ((<$>), (*>), (<*))
readTextile :: ParserState -- ^ Parser state, including options for parser
-> String -- ^ String to parse (assuming @'\n'@ line endings)
-> Pandoc
-readTextile state s =
+readTextile st = dumpParseError . readTextile' st
+
+readTextile' :: ParserState -- ^ Parser state, including options for parser
+ -> String -- ^ String to parse (assuming @'\n'@ line endings)
+ -> Either ParseError Pandoc
+readTextile' state s =
(readWith parseTextile) state{ stateOldDashes = True } (s ++ "\n\n")
Something went wrong with that request. Please try again.