@@ -29,27 +29,23 @@ Conversion of org-mode formatted plain text to 'Pandoc' document.
2929-}
3030module Text.Pandoc.Readers.Org ( readOrg ) where
3131
32+import Text.Pandoc.Readers.Org.ParserState
33+import Text.Pandoc.Readers.Org.Parsing
34+
3235import qualified Text.Pandoc.Builder as B
3336import Text.Pandoc.Builder ( Inlines, Blocks )
3437import Text.Pandoc.Definition
3538import Text.Pandoc.Compat.Monoid ((<>))
3639import Text.Pandoc.Error
3740import Text.Pandoc.Options
38−import qualified Text.Pandoc.Parsing as P
39−import Text.Pandoc.Parsing hiding ( F, unF, askF, asksF, runF
40− , anyLine, blanklines, newline
41− , orderedListMarker
42− , parseFromString
43− )
4441import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
45−import Text.Pandoc.Readers.Org.ParserState
4642import Text.Pandoc.Shared (compactify', compactify'DL)
4743import Text.TeXMath (readTeX, writePandoc, DisplayType(..))
4844import qualified Text.TeXMath.Readers.MathML.EntityMap as MathMLEntityMap
4945
5046import Control.Arrow (first)
5147import Control.Monad (foldM, guard, mplus, mzero, when)
52−import Control.Monad.Reader ( Reader, runReader )
48+import Control.Monad.Reader ( runReader )
5349import Data.Char (isAlphaNum, isSpace, toLower, toUpper)
5450import Data.List ( foldl', intersperse, isPrefixOf, isSuffixOf )
5551import qualified Data.Map as M
@@ -63,9 +59,6 @@ readOrg :: ReaderOptions -- ^ Reader options
6359 -> Either PandocError Pandoc
6460readOrg opts s = flip runReader def $ readWithM parseOrg def{ orgStateOptions = opts } (s ++ "\n\n")
6561
66−-- | The parser used to read org files.
67−type OrgParser = ParserT [Char] OrgParserState (Reader OrgParserLocal)
68−
6962--
7063-- Functions acting on the parser state
7164--
@@ -73,14 +66,6 @@ recordAnchorId :: String -> OrgParser ()
7366recordAnchorId i = updateState $ \s ->
7467 s{ orgStateAnchorIds = i : (orgStateAnchorIds s) }
7568
76−updateLastForbiddenCharPos :: OrgParser ()
77−updateLastForbiddenCharPos = getPosition >>= \p ->
78− updateState $ \s -> s{ orgStateLastForbiddenCharPos = Just p}
79−
80−updateLastPreCharPos :: OrgParser ()
81−updateLastPreCharPos = getPosition >>= \p ->
82− updateState $ \s -> s{ orgStateLastPreCharPos = Just p}
83−
8469pushToInlineCharStack :: Char -> OrgParser ()
8570pushToInlineCharStack c = updateState $ \s ->
8671 s{ orgStateEmphasisCharStack = c:orgStateEmphasisCharStack s }
@@ -258,57 +243,6 @@ isHeaderLevelLowerEq n blk =
258243 _ -> False
259244
260245
261−--
262−-- Adaptions and specializations of parsing utilities
263−--
264−
265−-- The version Text.Pandoc.Parsing cannot be used, as we need additional parts
266−-- of the state saved and restored.
267−parseFromString :: OrgParser a -> String -> OrgParser a
268−parseFromString parser str' = do
269− oldLastPreCharPos <- orgStateLastPreCharPos <$> getState
270− updateState $ \s -> s{ orgStateLastPreCharPos = Nothing }
271− result <- P.parseFromString parser str'
272− updateState $ \s -> s{ orgStateLastPreCharPos = oldLastPreCharPos }
273− return result
274−
275−-- | Like @Text.Parsec.Char.newline@, but causes additional state changes.
276−newline :: OrgParser Char
277−newline =
278− P.newline
279− <* updateLastPreCharPos
280− <* updateLastForbiddenCharPos
281−
282−-- | Like @Text.Parsec.Char.blanklines@, but causes additional state changes.
283−blanklines :: OrgParser [Char]
284−blanklines =
285− P.blanklines
286− <* updateLastPreCharPos
287− <* updateLastForbiddenCharPos
288−
289−anyLine :: OrgParser String
290−anyLine =
291− P.anyLine
292− <* updateLastPreCharPos
293− <* updateLastForbiddenCharPos
294−
295−-- | Succeeds when we're in list context.
296−inList :: OrgParser ()
297−inList = do
298− ctx <- orgStateParserContext <$> getState
299− guard (ctx == ListItemState)
300−
301−-- | Parse in different context
302−withContext :: ParserContext -- ^ New parser context
303− -> OrgParser a -- ^ Parser to run in that context
304− -> OrgParser a
305−withContext context parser = do
306− oldContext <- orgStateParserContext <$> getState
307− updateState $ \s -> s{ orgStateParserContext = context }
308− result <- parser
309− updateState $ \s -> s{ orgStateParserContext = oldContext }
310− return result
311−
312246--
313247-- parsing blocks
314248--
@@ -398,7 +332,7 @@ keyValues = try $
398332 endOfValue :: OrgParser ()
399333 endOfValue =
400334 lookAhead $ (() <$ try (many1 spaceChar <* key))
401− <|> () <$ P.newline
335+ <|> () <$ newline
402336
403337
404338--
@@ -675,7 +609,7 @@ propertiesDrawer = try $ do
675609 key = try $ skipSpaces *> char ':' *> many1Till nonspaceChar (char ':')
676610
677611 value :: OrgParser String
678− value = try $ skipSpaces *> manyTill anyChar (try $ skipSpaces *> P.newline)
612+ value = try $ skipSpaces *> manyTill anyChar (try $ skipSpaces *> newline)
679613
680614keyValuesToAttr :: [(String, String)] -> Attr
681615keyValuesToAttr kvs =
@@ -696,7 +630,7 @@ keyValuesToAttr kvs =
696630figure :: OrgParser (F Blocks)
697631figure = try $ do
698632 figAttrs <- blockAttributes
699− src <- skipSpaces *> selfTarget <* skipSpaces <* P.newline
633+ src <- skipSpaces *> selfTarget <* skipSpaces <* newline
700634 guard . not . isNothing . blockAttrCaption $ figAttrs
701635 guard (isImageFilename src)
702636 let figName = fromMaybe mempty $ blockAttrName figAttrs
@@ -785,7 +719,7 @@ parseFormat = try $ do
785719header :: OrgParser (F Blocks)
786720header = try $ do
787721 level <- headerStart
788− title <- manyTill inline (lookAhead $ optional headerTags <* P.newline)
722+ title <- manyTill inline (lookAhead $ optional headerTags <* newline)
789723 tags <- option [] headerTags
790724 newline
791725 propAttr <- option nullAttr (keyValuesToAttr <$> propertiesDrawer)
@@ -1083,7 +1017,7 @@ definitionListItem parseMarkerGetLength = try $ do
10831017 return $ (,) <$> term' <*> fmap (:[]) contents'
10841018 where
10851019 definitionMarker =
1086− spaceChar *> string "::" <* (spaceChar <|> lookAhead P.newline)
1020+ spaceChar *> string "::" <* (spaceChar <|> lookAhead newline)
10871021
10881022
10891023-- parse raw text for one list item, excluding start marker and continuations
@@ -1559,7 +1493,7 @@ many1TillNOrLessNewlines n p end = try $
15591493 nMoreLines k cs = try $ (final k cs <|> rest k cs)
15601494 >>= uncurry nMoreLines
15611495 final _ cs = (\x -> (Nothing, cs ++ x)) <$> try finalLine
1562− rest m cs = (\x -> (minus1 <$> m, cs ++ x ++ "\n")) <$> try (manyTill p P.newline)
1496+ rest m cs = (\x -> (minus1 <$> m, cs ++ x ++ "\n")) <$> try (manyTill p newline)
15631497 finalLine = try $ manyTill p end
15641498 minus1 k = k - 1
15651499 oneOrMore cs = guard (not $ null cs) *> return cs