@@ -29,27 +29,23 @@ Conversion of org-mode formatted plain text to 'Pandoc' document.
30module Text.Pandoc.Readers.Org ( readOrg ) where
30module Text.Pandoc.Readers.Org ( readOrg ) where
32+import Text.Pandoc.Readers.Org.ParserState
33+import Text.Pandoc.Readers.Org.Parsing
32import qualified Text.Pandoc.Builder as B
35import qualified Text.Pandoc.Builder as B
33import Text.Pandoc.Builder ( Inlines, Blocks )
36import Text.Pandoc.Builder ( Inlines, Blocks )
34import Text.Pandoc.Definition
37import Text.Pandoc.Definition
35import Text.Pandoc.Compat.Monoid ((<>))
38import Text.Pandoc.Compat.Monoid ((<>))
36import Text.Pandoc.Error
39import Text.Pandoc.Error
37import Text.Pandoc.Options
40import 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
44import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
41import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
45−import Text.Pandoc.Readers.Org.ParserState
46import Text.Pandoc.Shared (compactify', compactify'DL)
42import Text.Pandoc.Shared (compactify', compactify'DL)
47import Text.TeXMath (readTeX, writePandoc, DisplayType(..))
43import Text.TeXMath (readTeX, writePandoc, DisplayType(..))
48import qualified Text.TeXMath.Readers.MathML.EntityMap as MathMLEntityMap
44import qualified Text.TeXMath.Readers.MathML.EntityMap as MathMLEntityMap
50import Control.Arrow (first)
46import Control.Arrow (first)
51import Control.Monad (foldM, guard, mplus, mzero, when)
47import Control.Monad (foldM, guard, mplus, mzero, when)
52−import Control.Monad.Reader ( Reader, runReader )
48+import Control.Monad.Reader ( runReader )
53import Data.Char (isAlphaNum, isSpace, toLower, toUpper)
49import Data.Char (isAlphaNum, isSpace, toLower, toUpper)
54import Data.List ( foldl', intersperse, isPrefixOf, isSuffixOf )
50import Data.List ( foldl', intersperse, isPrefixOf, isSuffixOf )
55import qualified Data.Map as M
51import qualified Data.Map as M
@@ -63,9 +59,6 @@ readOrg :: ReaderOptions -- ^ Reader options
63 -> Either PandocError Pandoc
59 -> Either PandocError Pandoc
64readOrg opts s = flip runReader def $ readWithM parseOrg def{ orgStateOptions = opts } (s ++ "\n\n")
60readOrg opts s = flip runReader def $ readWithM parseOrg def{ orgStateOptions = opts } (s ++ "\n\n")
66−-- | The parser used to read org files.
67−type OrgParser = ParserT [Char] OrgParserState (Reader OrgParserLocal)
70-- Functions acting on the parser state
63-- Functions acting on the parser state
@@ -73,14 +66,6 @@ recordAnchorId :: String -> OrgParser ()
73recordAnchorId i = updateState $ \s ->
66recordAnchorId i = updateState $ \s ->
74 s{ orgStateAnchorIds = i : (orgStateAnchorIds s) }
67 s{ orgStateAnchorIds = i : (orgStateAnchorIds s) }
76−updateLastForbiddenCharPos :: OrgParser ()
77−updateLastForbiddenCharPos = getPosition >>= \p ->
78− updateState $ \s -> s{ orgStateLastForbiddenCharPos = Just p}
80−updateLastPreCharPos :: OrgParser ()
81−updateLastPreCharPos = getPosition >>= \p ->
82− updateState $ \s -> s{ orgStateLastPreCharPos = Just p}
84pushToInlineCharStack :: Char -> OrgParser ()
69pushToInlineCharStack :: Char -> OrgParser ()
85pushToInlineCharStack c = updateState $ \s ->
70pushToInlineCharStack c = updateState $ \s ->
86 s{ orgStateEmphasisCharStack = c:orgStateEmphasisCharStack s }
71 s{ orgStateEmphasisCharStack = c:orgStateEmphasisCharStack s }
@@ -258,57 +243,6 @@ isHeaderLevelLowerEq n blk =
258 _ -> False
243 _ -> False
262−-- Adaptions and specializations of parsing utilities
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 }
275−-- | Like @Text.Parsec.Char.newline@, but causes additional state changes.
276−newline :: OrgParser Char
279− <* updateLastPreCharPos
280− <* updateLastForbiddenCharPos
282−-- | Like @Text.Parsec.Char.blanklines@, but causes additional state changes.
283−blanklines :: OrgParser [Char]
286− <* updateLastPreCharPos
287− <* updateLastForbiddenCharPos
289−anyLine :: OrgParser String
292− <* updateLastPreCharPos
293− <* updateLastForbiddenCharPos
295−-- | Succeeds when we're in list context.
296−inList :: OrgParser ()
298− ctx <- orgStateParserContext <$> getState
299− guard (ctx == ListItemState)
301−-- | Parse in different context
302−withContext :: ParserContext -- ^ New parser context
303− -> OrgParser a -- ^ Parser to run in that context
305−withContext context parser = do
306− oldContext <- orgStateParserContext <$> getState
307− updateState $ \s -> s{ orgStateParserContext = context }
309− updateState $ \s -> s{ orgStateParserContext = oldContext }
313-- parsing blocks
247-- parsing blocks
@@ -398,7 +332,7 @@ keyValues = try $
398 endOfValue :: OrgParser ()
332 endOfValue :: OrgParser ()
399 endOfValue =
333 endOfValue =
400 lookAhead $ (() <$ try (many1 spaceChar <* key))
334 lookAhead $ (() <$ try (many1 spaceChar <* key))
401− <|> () <$ P.newline
335+ <|> () <$ newline
@@ -675,7 +609,7 @@ propertiesDrawer = try $ do
675 key = try $ skipSpaces *> char ':' *> many1Till nonspaceChar (char ':')
609 key = try $ skipSpaces *> char ':' *> many1Till nonspaceChar (char ':')
677 value :: OrgParser String
611 value :: OrgParser String
678− value = try $ skipSpaces *> manyTill anyChar (try $ skipSpaces *> P.newline)
612+ value = try $ skipSpaces *> manyTill anyChar (try $ skipSpaces *> newline)
680keyValuesToAttr :: [(String, String)] -> Attr
614keyValuesToAttr :: [(String, String)] -> Attr
681keyValuesToAttr kvs =
615keyValuesToAttr kvs =
@@ -696,7 +630,7 @@ keyValuesToAttr kvs =
696figure :: OrgParser (F Blocks)
630figure :: OrgParser (F Blocks)
697figure = try $ do
631figure = try $ do
698 figAttrs <- blockAttributes
632 figAttrs <- blockAttributes
699− src <- skipSpaces *> selfTarget <* skipSpaces <* P.newline
633+ src <- skipSpaces *> selfTarget <* skipSpaces <* newline
700 guard . not . isNothing . blockAttrCaption $ figAttrs
634 guard . not . isNothing . blockAttrCaption $ figAttrs
701 guard (isImageFilename src)
635 guard (isImageFilename src)
702 let figName = fromMaybe mempty $ blockAttrName figAttrs
636 let figName = fromMaybe mempty $ blockAttrName figAttrs
@@ -785,7 +719,7 @@ parseFormat = try $ do
785header :: OrgParser (F Blocks)
719header :: OrgParser (F Blocks)
786header = try $ do
720header = try $ do
787 level <- headerStart
721 level <- headerStart
788− title <- manyTill inline (lookAhead $ optional headerTags <* P.newline)
722+ title <- manyTill inline (lookAhead $ optional headerTags <* newline)
789 tags <- option [] headerTags
723 tags <- option [] headerTags
791 propAttr <- option nullAttr (keyValuesToAttr <$> propertiesDrawer)
725 propAttr <- option nullAttr (keyValuesToAttr <$> propertiesDrawer)
@@ -1083,7 +1017,7 @@ definitionListItem parseMarkerGetLength = try $ do
1083 return $ (,) <$> term' <*> fmap (:[]) contents'
1017 return $ (,) <$> term' <*> fmap (:[]) contents'
1085 definitionMarker =
1019 definitionMarker =
1086− spaceChar *> string "::" <* (spaceChar <|> lookAhead P.newline)
1020+ spaceChar *> string "::" <* (spaceChar <|> lookAhead newline)
1089-- parse raw text for one list item, excluding start marker and continuations
1023-- parse raw text for one list item, excluding start marker and continuations
@@ -1559,7 +1493,7 @@ many1TillNOrLessNewlines n p end = try $
1559 nMoreLines k cs = try $ (final k cs <|> rest k cs)
1493 nMoreLines k cs = try $ (final k cs <|> rest k cs)
1560 >>= uncurry nMoreLines
1494 >>= uncurry nMoreLines
1561 final _ cs = (\x -> (Nothing, cs ++ x)) <$> try finalLine
1495 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)
1563 finalLine = try $ manyTill p end
1497 finalLine = try $ manyTill p end
1564 minus1 k = k - 1
1498 minus1 k = k - 1
1565 oneOrMore cs = guard (not $ null cs) *> return cs
1499 oneOrMore cs = guard (not $ null cs) *> return cs