git.iko.soy/typeable/pandoc
on master help sign in
Revert "Checkpoint - custom LaTeXState."

This reverts commit f56382a83fcf9b59eb43247f8887da72e982879d.

commit3bda0d92eda01ea942ff42fe39717c0a2d3e234f authorJohn MacFarlane <jgm@berkeley.edu> dateMon 3 Jul 2017, 00:17 (9 years ago) parentf56382a
1 file changed+19−86
src/Text/Pandoc/Readers/LaTeX.hs+19−86
src/Text/Pandoc/Readers/LaTeX.hs+19−86
@@ -1,6 +1,4 @@
11{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE FlexibleInstances #-}
3−{-# LANGUAGE MultiParamTypeClasses #-}
42{-# LANGUAGE ScopedTypeVariables #-}
53{-
64Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
@@ -41,12 +39,10 @@ import Control.Applicative (many, optional, (<|>))
4139import Control.Monad
4240import Control.Monad.Except (throwError)
4341import Data.Char (chr, isAlphaNum, isLetter, ord, isDigit)
44−import Data.Default
4542import Data.Text (Text, unpack)
4643import qualified Data.Text as T
4744import Data.List (intercalate, isPrefixOf)
4845import qualified Data.Map as M
49−import qualified Data.Set as Set
5046import Data.Maybe (fromMaybe, maybeToList)
5147import Safe (minimumDef)
5248import System.FilePath (addExtension, replaceExtension, takeExtension)
@@ -74,7 +70,7 @@ readLaTeX :: PandocMonad m
7470 -> Text -- ^ String to parse (assumes @'\n'@ line endings)
7571 -> m Pandoc
7672readLaTeX opts ltx = do
77− parsed <- runParserT parseLaTeX def{ sOptions = opts } "source"
73+ parsed <- runParserT parseLaTeX def{ stateOptions = opts } "source"
7874 (tokenize (crFilter ltx))
7975 case parsed of
8076 Right result -> return result
@@ -85,7 +81,7 @@ parseLaTeX = do
8581 bs <- blocks
8682 eof
8783 st <- getState
88− let meta = sMeta st
84+ let meta = stateMeta st
8985 let doc' = doc bs
9086 let headerLevel (Header n _ _) = [n]
9187 headerLevel _ = []
@@ -101,85 +97,22 @@ parseLaTeX = do
10197
10298testParser :: LP PandocIO a -> Text -> IO a
10399testParser p t = do
104− res <- runIOorExplode (runParserT p defaultLaTeXState{
105− sOptions = def{ readerExtensions =
100+ res <- runIOorExplode (runParserT p defaultParserState{
101+ stateOptions = def{ readerExtensions =
106102 getDefaultExtensions "latex" }} "name" (tokenize t))
107103 case res of
108104 Left e -> error (show e)
109105 Right r -> return r
110106
111−data Macro = Macro -- TODO
112− deriving Show
113−
114−data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
115− , sMeta :: Meta
116− , sQuoteContext :: QuoteContext
117− , sMacros :: [Macro]
118− , sContainers :: [String]
119− , sHeaders :: M.Map Inlines String
120− , sLogMessages :: [LogMessage]
121− , sIdentifiers :: Set.Set String }
122− deriving Show
123−
124−defaultLaTeXState :: LaTeXState
125−defaultLaTeXState = LaTeXState{ sOptions = def
126− , sMeta = nullMeta
127− , sQuoteContext = NoQuote
128− , sMacros = []
129− , sContainers = []
130− , sHeaders = M.empty
131− , sLogMessages = []
132− , sIdentifiers = Set.empty }
133−
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 }
140− result <- parser
141− newState <- getState
142− setState newState { sQuoteContext = oldQuoteContext }
143− return result
144−
145−instance HasLogMessages LaTeXState where
146− addLogMessage msg st = st{ sLogMessages = msg : sLogMessages st }
147− getLogMessages st = reverse $ sLogMessages st
148−
149−instance HasIdentifierList LaTeXState where
150− extractIdentifierList = sIdentifiers
151− updateIdentifierList f st = st{ sIdentifiers = f $ sIdentifiers st }
152−
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 }
157−
158−instance HasHeaderMap LaTeXState where
159− extractHeaderMap = sHeaders
160− updateHeaderMap f st = st{ sHeaders = f $ sHeaders st }
161−
162−instance HasReaderOptions LaTeXState where
163− extractReaderOptions = sOptions
164−
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 }
170−
171−instance Default LaTeXState where
172− def = defaultLaTeXState
173−
174−type LP m = ParserT [Tok] LaTeXState m
175−
176−rawLaTeXBlock :: PandocMonad m => ParserT String LaTeXState m String
107+type LP m = ParserT [Tok] ParserState m
108+
109+rawLaTeXBlock :: PandocMonad m => ParserT String ParserState m String
177110rawLaTeXBlock = mzero
178111
179−rawLaTeXInline :: PandocMonad m => ParserT String LaTeXState m Inline
112+rawLaTeXInline :: PandocMonad m => ParserT String ParserState m Inline
180113rawLaTeXInline = mzero
181114
182−inlineCommand :: PandocMonad m => ParserT String LaTeXState m Inlines
115+inlineCommand :: PandocMonad m => ParserT String ParserState m Inlines
183116inlineCommand = mzero
184117
185118data TokType = CtrlSeq | Spaces | Newline | Symbol | Word | Comment |
@@ -273,11 +206,11 @@ satisfyTok f =
273206 setSourceColumn (setSourceLine spos lin) col
274207 updatePos spos _ [] = spos
275208
276−-- TODO use something other than LaTeXState
209+-- TODO use something other than ParserState
277210doMacros :: PandocMonad m => LP m ()
278211doMacros = do
279212 inp <- getInput
280− macros <- sMacros <$> getState
213+ macros <- stateMacros <$> getState
281214 case inp of
282215 t@(Tok spos CtrlSeq txt) : ts
283216 | txt == "\\macro" -> do -- TODO actually check macros
@@ -486,8 +419,8 @@ quoted' f starter ender = do
486419enquote :: PandocMonad m => LP m Inlines
487420enquote = do
488421 skipopts
489− quoteContext <- sQuoteContext <$> getState
490− if quoteContext == InDoubleQuote
422+ context <- stateQuoteContext <$> getState
423+ if context == InDoubleQuote
491424 then singleQuoted <$> withQuoteContext InSingleQuote tok
492425 else doubleQuoted <$> withQuoteContext InDoubleQuote tok
493426
@@ -1186,7 +1119,7 @@ ignoreBlocks name = (name, p)
11861119
11871120addMeta :: PandocMonad m => ToMetaValue a => String -> a -> LP m ()
11881121addMeta field val = updateState $ \st ->
1189− st{ sMeta = addMetaField field val $ sMeta st }
1122+ st{ stateMeta = addMetaField field val $ stateMeta st }
11901123
11911124authors :: PandocMonad m => LP m ()
11921125authors = try $ do
@@ -1232,7 +1165,7 @@ closing = do
12321165 let extractInlines (MetaBlocks [Plain ys]) = ys
12331166 extractInlines (MetaBlocks [Para ys ]) = ys
12341167 extractInlines _ = []
1235− let sigs = case lookupMeta "author" (sMeta st) of
1168+ let sigs = case lookupMeta "author" (stateMeta st) of
12361169 Just (MetaList xs) ->
12371170 para $ trimInlines $ fromList $
12381171 intercalate [LineBreak] $ map extractInlines xs
@@ -1438,7 +1371,7 @@ blocks = mconcat <$> many block
14381371--
14391372-- addMeta :: PandocMonad m => ToMetaValue a => String -> a -> LP m ()
14401373-- addMeta field val = updateState $ \st ->
1441−-- st{ sMeta = addMetaField field val $ sMeta st }
1374+-- st{ stateMeta = addMetaField field val $ stateMeta st }
14421375--
14431376-- splitBibs :: String -> [Inlines]
14441377-- splitBibs = map (str . flip replaceExtension "bib" . trim) . splitBy (==',')
@@ -1531,10 +1464,10 @@ blocks = mconcat <$> many block
15311464-- where strToCode (Str s) = Code nullAttr s
15321465-- strToCode x = x
15331466--
1534−-- rawLaTeXBlock :: PandocMonad m => ParserT String LaTeXState String
1467+-- rawLaTeXBlock :: PandocMonad m => ParserT String ParserState String
15351468-- rawLaTeXBlock = snd <$> try (withRaw (environment <|> blockCommand))
15361469--
1537−-- rawLaTeXInline :: PandocMonad m => ParserT String LaTeXState Inline
1470+-- rawLaTeXInline :: PandocMonad m => ParserT String ParserState Inline
15381471-- rawLaTeXInline = do
15391472-- raw <- (snd <$> withRaw inlineCommand)
15401473-- <|> (snd <$> withRaw inlineEnvironment)
@@ -1571,7 +1504,7 @@ blocks = mconcat <$> many block
15711504-- bs <- blocks
15721505-- st <- getState
15731506-- -- add signature (author) and address (title)
1574−-- let addr = case lookupMeta "address" (sMeta st) of
1507+-- let addr = case lookupMeta "address" (stateMeta st) of
15751508-- Just (MetaBlocks [Plain xs]) ->
15761509-- para $ trimInlines $ fromList xs
15771510-- _ -> mempty