From 9cd0bdb41a2c14e0f28e5ab179b0da73a0b8ba78 Mon Sep 17 00:00:00 2001
From: Matthew Pickering <matthewtpickering@gmail.com>
Date: Mon, 2 Feb 2015 10:38:30 +0000
Subject: [PATCH] Factor out "returnState" into Parsing module

---
 src/Text/Pandoc/Parsing.hs          | 5 +++++
 src/Text/Pandoc/Readers/Markdown.hs | 9 +--------
 src/Text/Pandoc/Readers/Org.hs      | 8 +-------
 3 files changed, 7 insertions(+), 15 deletions(-)

diff --git a/src/Text/Pandoc/Parsing.hs b/src/Text/Pandoc/Parsing.hs
index 3e9d559dc..aebdcae4c 100644
--- a/src/Text/Pandoc/Parsing.hs
+++ b/src/Text/Pandoc/Parsing.hs
@@ -66,6 +66,7 @@ module Text.Pandoc.Parsing ( anyLine,
                              gridTableWith,
                              readWith,
                              returnWarnings,
+                             returnState,
                              readWithM,
                              testStringWith,
                              guardEnabled,
@@ -873,6 +874,10 @@ returnWarnings p = do
          warnings <- stateWarnings <$> getState
          return (doc, warnings)
 
+-- | Return the final internal state with the result of a parser
+returnState :: (Stream s m c) => ParsecT s st m a -> ParsecT s st m (a, st)
+returnState p = (,) <$> p <*> getState
+
 -- | Parse a string with @parser@ (for testing).
 testStringWith :: (Show a, Stream [Char] Identity Char)
                => ParserT [Char] ParserState Identity a
diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs
index 043d7e94c..92cf9a22e 100644
--- a/src/Text/Pandoc/Readers/Markdown.hs
+++ b/src/Text/Pandoc/Readers/Markdown.hs
@@ -81,17 +81,10 @@ readMarkdownWithWarnings :: ReaderOptions -- ^ Reader options
                          -> (Pandoc, [String])
 readMarkdownWithWarnings opts s = runMarkdown opts s (returnWarnings parseMarkdown)
 
-
-retState :: MarkdownParser a -> MarkdownParser (a, ParserState)
-retState p = do
-  r <- p
-  s <- getState
-  return (r, s)
-
 runMarkdown :: ReaderOptions -> String -> MarkdownParser a -> a
 runMarkdown opts inp p = fst res
   where
-    imd = readWithM (retState p) def{ stateOptions = opts } (inp ++ "\n\n")
+    imd = readWithM (returnState p) def{ stateOptions = opts } (inp ++ "\n\n")
     res = runReader imd s
     s :: ParserState
     s   = snd $ runReader imd s
diff --git a/src/Text/Pandoc/Readers/Org.hs b/src/Text/Pandoc/Readers/Org.hs
index 5cb66bfa7..fd58956d0 100644
--- a/src/Text/Pandoc/Readers/Org.hs
+++ b/src/Text/Pandoc/Readers/Org.hs
@@ -67,17 +67,11 @@ readOrg opts s = runOrg opts s parseOrg
 runOrg :: ReaderOptions -> String -> OrgParser a -> a
 runOrg opts inp p = fst res
   where
-    imd = readWithM (retState p) def{ orgStateOptions = opts } (inp ++ "\n\n")
+    imd = readWithM (returnState p) def{ orgStateOptions = opts } (inp ++ "\n\n")
     res = runReader imd s
     s :: OrgParserState
     s   = snd $ runReader imd s
 
-retState :: OrgParser a -> OrgParser (a, OrgParserState)
-retState p = do
-  r <- p
-  s <- getState
-  return (r, s)
-
 type OrgParser a = ParserT [Char] OrgParserState (Reader OrgParserState) a
 
 parseOrg :: OrgParser Pandoc