git.iko.soy/typeable/pandoc
on master help sign in
Use Parsec directly in Biblio and Templates.
f79ed27John MacFarlane
src/Text/Pandoc/Biblio.hs+6−6
@@ -38,7 +38,7 @@ import qualified Text.CSL as CSL ( Cite(..) )
38import Text.Pandoc.Definition
38import Text.Pandoc.Definition
39import Text.Pandoc.Generic
39import Text.Pandoc.Generic
40import Text.Pandoc.Shared (stringify)
40import Text.Pandoc.Shared (stringify)
41−import Text.Pandoc.Parsing
41+import Text.Parsec
42import Control.Monad
42import Control.Monad
43
43
44-- | Process a 'Pandoc' document by adding citations formatted
44-- | Process a 'Pandoc' document by adding citations formatted
@@ -165,7 +165,7 @@ locatorWords inp =
165 breakup (x : xs) = x : breakup xs
165 breakup (x : xs) = x : breakup xs
166 splitup = groupBy (\x y -> x /= '\160' && y /= '\160')
166 splitup = groupBy (\x y -> x /= '\160' && y /= '\160')
167
167
168−pLocatorWords :: Parser [Inline] st (String, [Inline])
168+pLocatorWords :: Parsec [Inline] st (String, [Inline])
169pLocatorWords = do
169pLocatorWords = do
170 l <- pLocator
170 l <- pLocator
171 s <- getInput -- rest is suffix
171 s <- getInput -- rest is suffix
@@ -173,16 +173,16 @@ pLocatorWords = do
173 then return (init l, Str "," : s)
173 then return (init l, Str "," : s)
174 else return (l, s)
174 else return (l, s)
175
175
176−pMatch :: (Inline -> Bool) -> Parser [Inline] st Inline
176+pMatch :: (Inline -> Bool) -> Parsec [Inline] st Inline
177pMatch condition = try $ do
177pMatch condition = try $ do
178 t <- anyToken
178 t <- anyToken
179 guard $ condition t
179 guard $ condition t
180 return t
180 return t
181
181
182−pSpace :: Parser [Inline] st Inline
182+pSpace :: Parsec [Inline] st Inline
183pSpace = pMatch (\t -> t == Space || t == Str "\160")
183pSpace = pMatch (\t -> t == Space || t == Str "\160")
184
184
185−pLocator :: Parser [Inline] st String
185+pLocator :: Parsec [Inline] st String
186pLocator = try $ do
186pLocator = try $ do
187 optional $ pMatch (== Str ",")
187 optional $ pMatch (== Str ",")
188 optional pSpace
188 optional pSpace
@@ -190,7 +190,7 @@ pLocator = try $ do
190 gs <- many1 pWordWithDigits
190 gs <- many1 pWordWithDigits
191 return $ stringify f ++ (' ' : unwords gs)
191 return $ stringify f ++ (' ' : unwords gs)
192
192
193−pWordWithDigits :: Parser [Inline] st String
193+pWordWithDigits :: Parsec [Inline] st String
194pWordWithDigits = try $ do
194pWordWithDigits = try $ do
195 pSpace
195 pSpace
196 r <- many1 (notFollowedBy pSpace >> anyToken)
196 r <- many1 (notFollowedBy pSpace >> anyToken)
src/Text/Pandoc/Templates.hs+10−10
@@ -68,7 +68,7 @@ module Text.Pandoc.Templates ( renderTemplate
68 , TemplateTarget
68 , TemplateTarget
69 , getDefaultTemplate ) where
69 , getDefaultTemplate ) where
70
70
71−import Text.Pandoc.Parsing
71+import Text.Parsec
72import Control.Monad (liftM, when, forM, mzero)
72import Control.Monad (liftM, when, forM, mzero)
73import System.FilePath
73import System.FilePath
74import Data.List (intercalate, intersperse)
74import Data.List (intercalate, intersperse)
@@ -98,7 +98,7 @@ getDefaultTemplate user writer = do
98
98
99data TemplateState = TemplateState Int [(String,String)]
99data TemplateState = TemplateState Int [(String,String)]
100
100
101−adjustPosition :: String -> Parser [Char] TemplateState String
101+adjustPosition :: String -> Parsec [Char] TemplateState String
102adjustPosition str = do
102adjustPosition str = do
103 let lastline = takeWhile (/= '\n') $ reverse str
103 let lastline = takeWhile (/= '\n') $ reverse str
104 updateState $ \(TemplateState pos x) ->
104 updateState $ \(TemplateState pos x) ->
@@ -132,21 +132,21 @@ renderTemplate vals templ =
132reservedWords :: [String]
132reservedWords :: [String]
133reservedWords = ["else","endif","for","endfor","sep"]
133reservedWords = ["else","endif","for","endfor","sep"]
134
134
135−parseTemplate :: Parser [Char] TemplateState [String]
135+parseTemplate :: Parsec [Char] TemplateState [String]
136parseTemplate =
136parseTemplate =
137 many $ (plaintext <|> escapedDollar <|> conditional <|> for <|> variable)
137 many $ (plaintext <|> escapedDollar <|> conditional <|> for <|> variable)
138 >>= adjustPosition
138 >>= adjustPosition
139
139
140−plaintext :: Parser [Char] TemplateState String
140+plaintext :: Parsec [Char] TemplateState String
141plaintext = many1 $ noneOf "$"
141plaintext = many1 $ noneOf "$"
142
142
143−escapedDollar :: Parser [Char] TemplateState String
143+escapedDollar :: Parsec [Char] TemplateState String
144escapedDollar = try $ string "$$" >> return "$"
144escapedDollar = try $ string "$$" >> return "$"
145
145
146−skipEndline :: Parser [Char] st ()
146+skipEndline :: Parsec [Char] st ()
147skipEndline = try $ skipMany (oneOf " \t") >> newline >> return ()
147skipEndline = try $ skipMany (oneOf " \t") >> newline >> return ()
148
148
149−conditional :: Parser [Char] TemplateState String
149+conditional :: Parsec [Char] TemplateState String
150conditional = try $ do
150conditional = try $ do
151 TemplateState pos vars <- getState
151 TemplateState pos vars <- getState
152 string "$if("
152 string "$if("
@@ -170,7 +170,7 @@ conditional = try $ do
170 then ifContents
170 then ifContents
171 else elseContents
171 else elseContents
172
172
173−for :: Parser [Char] TemplateState String
173+for :: Parsec [Char] TemplateState String
174for = try $ do
174for = try $ do
175 TemplateState pos vars <- getState
175 TemplateState pos vars <- getState
176 string "$for("
176 string "$for("
@@ -193,7 +193,7 @@ for = try $ do
193 setState $ TemplateState pos vars
193 setState $ TemplateState pos vars
194 return $ concat $ intersperse sep contents
194 return $ concat $ intersperse sep contents
195
195
196−ident :: Parser [Char] TemplateState String
196+ident :: Parsec [Char] TemplateState String
197ident = do
197ident = do
198 first <- letter
198 first <- letter
199 rest <- many (alphaNum <|> oneOf "_-")
199 rest <- many (alphaNum <|> oneOf "_-")
@@ -202,7 +202,7 @@ ident = do
202 then mzero
202 then mzero
203 else return id'
203 else return id'
204
204
205−variable :: Parser [Char] TemplateState String
205+variable :: Parsec [Char] TemplateState String
206variable = try $ do
206variable = try $ do
207 char '$'
207 char '$'
208 id' <- ident
208 id' <- ident