@@ -1,6 +1,4 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE FlexibleInstances #-}
3−{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE ScopedTypeVariables #-}
6Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
4Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
@@ -41,12 +39,10 @@ import Control.Applicative (many, optional, (<|>))
41import Control.Monad
39import Control.Monad
42import Control.Monad.Except (throwError)
40import Control.Monad.Except (throwError)
43import Data.Char (chr, isAlphaNum, isLetter, ord, isDigit)
41import Data.Char (chr, isAlphaNum, isLetter, ord, isDigit)
45import Data.Text (Text, unpack)
42import Data.Text (Text, unpack)
46import qualified Data.Text as T
43import qualified Data.Text as T
47import Data.List (intercalate, isPrefixOf)
44import Data.List (intercalate, isPrefixOf)
48import qualified Data.Map as M
45import qualified Data.Map as M
49−import qualified Data.Set as Set
50import Data.Maybe (fromMaybe, maybeToList)
46import Data.Maybe (fromMaybe, maybeToList)
51import Safe (minimumDef)
47import Safe (minimumDef)
52import System.FilePath (addExtension, replaceExtension, takeExtension)
48import System.FilePath (addExtension, replaceExtension, takeExtension)
@@ -74,7 +70,7 @@ readLaTeX :: PandocMonad m
74 -> Text -- ^ String to parse (assumes @'\n'@ line endings)
70 -> Text -- ^ String to parse (assumes @'\n'@ line endings)
75 -> m Pandoc
71 -> m Pandoc
76readLaTeX opts ltx = do
72readLaTeX opts ltx = do
77− parsed <- runParserT parseLaTeX def{ sOptions = opts } "source"
73+ parsed <- runParserT parseLaTeX def{ stateOptions = opts } "source"
78 (tokenize (crFilter ltx))
74 (tokenize (crFilter ltx))
79 case parsed of
75 case parsed of
80 Right result -> return result
76 Right result -> return result
@@ -85,7 +81,7 @@ parseLaTeX = do
85 bs <- blocks
81 bs <- blocks
87 st <- getState
83 st <- getState
88− let meta = sMeta st
84+ let meta = stateMeta st
89 let doc' = doc bs
85 let doc' = doc bs
90 let headerLevel (Header n _ _) = [n]
86 let headerLevel (Header n _ _) = [n]
91 headerLevel _ = []
87 headerLevel _ = []
@@ -101,85 +97,22 @@ parseLaTeX = do
102testParser :: LP PandocIO a -> Text -> IO a
98testParser :: LP PandocIO a -> Text -> IO a
103testParser p t = do
99testParser p t = do
104− res <- runIOorExplode (runParserT p defaultLaTeXState{
100+ res <- runIOorExplode (runParserT p defaultParserState{
105− sOptions = def{ readerExtensions =
101+ stateOptions = def{ readerExtensions =
106 getDefaultExtensions "latex" }} "name" (tokenize t))
102 getDefaultExtensions "latex" }} "name" (tokenize t))
107 case res of
103 case res of
108 Left e -> error (show e)
104 Left e -> error (show e)
109 Right r -> return r
105 Right r -> return r
111−data Macro = Macro -- TODO
114−data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
116− , sQuoteContext :: QuoteContext
117− , sMacros :: [Macro]
118− , sContainers :: [String]
119− , sHeaders :: M.Map Inlines String
120− , sLogMessages :: [LogMessage]
121− , sIdentifiers :: Set.Set String }
124−defaultLaTeXState :: LaTeXState
125−defaultLaTeXState = LaTeXState{ sOptions = def
127− , sQuoteContext = NoQuote
130− , sHeaders = M.empty
132− , sIdentifiers = Set.empty }
134−instance PandocMonad m => HasQuoteContext LaTeXState m where
135− getQuoteContext = sQuoteContext <$> getState
136− withQuoteContext context parser = do
137− oldState <- getState
138− let oldQuoteContext = sQuoteContext oldState
139− setState oldState { sQuoteContext = context }
141− newState <- getState
142− setState newState { sQuoteContext = oldQuoteContext }
145−instance HasLogMessages LaTeXState where
146− addLogMessage msg st = st{ sLogMessages = msg : sLogMessages st }
147− getLogMessages st = reverse $ sLogMessages st
149−instance HasIdentifierList LaTeXState where
150− extractIdentifierList = sIdentifiers
151− updateIdentifierList f st = st{ sIdentifiers = f $ sIdentifiers st }
153−instance HasIncludeFiles LaTeXState where
154− getIncludeFiles = sContainers
155− addIncludeFile f s = s{ sContainers = f : sContainers s }
156− dropLatestIncludeFile s = s { sContainers = drop 1 $ sContainers s }
158−instance HasHeaderMap LaTeXState where
159− extractHeaderMap = sHeaders
160− updateHeaderMap f st = st{ sHeaders = f $ sHeaders st }
162−instance HasReaderOptions LaTeXState where
163− extractReaderOptions = sOptions
165−instance HasMeta LaTeXState where
166− setMeta field val st =
167− st{ sMeta = setMeta field val $ sMeta st }
168− deleteMeta field st =
169− st{ sMeta = deleteMeta field $ sMeta st }
171−instance Default LaTeXState where
172− def = defaultLaTeXState
174−type LP m = ParserT [Tok] LaTeXState m
107+type LP m = ParserT [Tok] ParserState m
176−rawLaTeXBlock :: PandocMonad m => ParserT String LaTeXState m String
109+rawLaTeXBlock :: PandocMonad m => ParserT String ParserState m String
177rawLaTeXBlock = mzero
110rawLaTeXBlock = mzero
179−rawLaTeXInline :: PandocMonad m => ParserT String LaTeXState m Inline
112+rawLaTeXInline :: PandocMonad m => ParserT String ParserState m Inline
180rawLaTeXInline = mzero
113rawLaTeXInline = mzero
182−inlineCommand :: PandocMonad m => ParserT String LaTeXState m Inlines
115+inlineCommand :: PandocMonad m => ParserT String ParserState m Inlines
183inlineCommand = mzero
116inlineCommand = mzero
185data TokType = CtrlSeq | Spaces | Newline | Symbol | Word | Comment |
118data TokType = CtrlSeq | Spaces | Newline | Symbol | Word | Comment |
@@ -273,11 +206,11 @@ satisfyTok f =
273 setSourceColumn (setSourceLine spos lin) col
206 setSourceColumn (setSourceLine spos lin) col
274 updatePos spos _ [] = spos
207 updatePos spos _ [] = spos
276−-- TODO use something other than LaTeXState
209+-- TODO use something other than ParserState
277doMacros :: PandocMonad m => LP m ()
210doMacros :: PandocMonad m => LP m ()
278doMacros = do
211doMacros = do
279 inp <- getInput
212 inp <- getInput
280− macros <- sMacros <$> getState
213+ macros <- stateMacros <$> getState
281 case inp of
214 case inp of
282 t@(Tok spos CtrlSeq txt) : ts
215 t@(Tok spos CtrlSeq txt) : ts
283 | txt == "\\macro" -> do -- TODO actually check macros
216 | txt == "\\macro" -> do -- TODO actually check macros
@@ -486,8 +419,8 @@ quoted' f starter ender = do
486enquote :: PandocMonad m => LP m Inlines
419enquote :: PandocMonad m => LP m Inlines
487enquote = do
420enquote = do
489− quoteContext <- sQuoteContext <$> getState
422+ context <- stateQuoteContext <$> getState
490− if quoteContext == InDoubleQuote
423+ if context == InDoubleQuote
491 then singleQuoted <$> withQuoteContext InSingleQuote tok
424 then singleQuoted <$> withQuoteContext InSingleQuote tok
492 else doubleQuoted <$> withQuoteContext InDoubleQuote tok
425 else doubleQuoted <$> withQuoteContext InDoubleQuote tok
@@ -1186,7 +1119,7 @@ ignoreBlocks name = (name, p)
1187addMeta :: PandocMonad m => ToMetaValue a => String -> a -> LP m ()
1120addMeta :: PandocMonad m => ToMetaValue a => String -> a -> LP m ()
1188addMeta field val = updateState $ \st ->
1121addMeta field val = updateState $ \st ->
1189− st{ sMeta = addMetaField field val $ sMeta st }
1122+ st{ stateMeta = addMetaField field val $ stateMeta st }
1191authors :: PandocMonad m => LP m ()
1124authors :: PandocMonad m => LP m ()
1192authors = try $ do
1125authors = try $ do
@@ -1232,7 +1165,7 @@ closing = do
1232 let extractInlines (MetaBlocks [Plain ys]) = ys
1165 let extractInlines (MetaBlocks [Plain ys]) = ys
1233 extractInlines (MetaBlocks [Para ys ]) = ys
1166 extractInlines (MetaBlocks [Para ys ]) = ys
1234 extractInlines _ = []
1167 extractInlines _ = []
1235− let sigs = case lookupMeta "author" (sMeta st) of
1168+ let sigs = case lookupMeta "author" (stateMeta st) of
1236 Just (MetaList xs) ->
1169 Just (MetaList xs) ->
1237 para $ trimInlines $ fromList $
1170 para $ trimInlines $ fromList $
1238 intercalate [LineBreak] $ map extractInlines xs
1171 intercalate [LineBreak] $ map extractInlines xs
@@ -1438,7 +1371,7 @@ blocks = mconcat <$> many block
1439-- addMeta :: PandocMonad m => ToMetaValue a => String -> a -> LP m ()
1372-- addMeta :: PandocMonad m => ToMetaValue a => String -> a -> LP m ()
1440-- addMeta field val = updateState $ \st ->
1373-- addMeta field val = updateState $ \st ->
1441−-- st{ sMeta = addMetaField field val $ sMeta st }
1374+-- st{ stateMeta = addMetaField field val $ stateMeta st }
1443-- splitBibs :: String -> [Inlines]
1376-- splitBibs :: String -> [Inlines]
1444-- splitBibs = map (str . flip replaceExtension "bib" . trim) . splitBy (==',')
1377-- splitBibs = map (str . flip replaceExtension "bib" . trim) . splitBy (==',')
@@ -1531,10 +1464,10 @@ blocks = mconcat <$> many block
1531-- where strToCode (Str s) = Code nullAttr s
1464-- where strToCode (Str s) = Code nullAttr s
1532-- strToCode x = x
1465-- strToCode x = x
1534−-- rawLaTeXBlock :: PandocMonad m => ParserT String LaTeXState String
1467+-- rawLaTeXBlock :: PandocMonad m => ParserT String ParserState String
1535-- rawLaTeXBlock = snd <$> try (withRaw (environment <|> blockCommand))
1468-- rawLaTeXBlock = snd <$> try (withRaw (environment <|> blockCommand))
1537−-- rawLaTeXInline :: PandocMonad m => ParserT String LaTeXState Inline
1470+-- rawLaTeXInline :: PandocMonad m => ParserT String ParserState Inline
1538-- rawLaTeXInline = do
1471-- rawLaTeXInline = do
1539-- raw <- (snd <$> withRaw inlineCommand)
1472-- raw <- (snd <$> withRaw inlineCommand)
1540-- <|> (snd <$> withRaw inlineEnvironment)
1473-- <|> (snd <$> withRaw inlineEnvironment)
@@ -1571,7 +1504,7 @@ blocks = mconcat <$> many block
1571-- bs <- blocks
1504-- bs <- blocks
1572-- st <- getState
1505-- st <- getState
1573-- -- add signature (author) and address (title)
1506-- -- add signature (author) and address (title)
1574−-- let addr = case lookupMeta "address" (sMeta st) of
1507+-- let addr = case lookupMeta "address" (stateMeta st) of
1575-- Just (MetaBlocks [Plain xs]) ->
1508-- Just (MetaBlocks [Plain xs]) ->
1576-- para $ trimInlines $ fromList xs
1509-- para $ trimInlines $ fromList xs
1577-- _ -> mempty
1510-- _ -> mempty