git.iko.soy/typeable/pandoc
on master help sign in
Checkpoint.

In parsing, adjusted type of insertIncludedFile so it can be used with token parser.

commit0ee848d256241ea5c563722223593d27b5b2e15a authorJohn MacFarlane <jgm@berkeley.edu> dateSat 1 Jul 2017, 22:39 (9 years ago) parent9f6f4ab
2 files changed+85−76
src/Text/Pandoc/Parsing.hs+11−9 src/Text/Pandoc/Readers/LaTeX.hs+74−67
src/Text/Pandoc/Parsing.hs+11−9
@@ -1388,10 +1388,11 @@ extractIdClass (ident, cls, kvs) = (ident', cls', kvs')
13881388
13891389insertIncludedFile' :: (PandocMonad m, HasIncludeFiles st,
13901390 Functor mf, Applicative mf, Monad mf)
1391− => ParserT String st m (mf Blocks)
1391+ => ParserT [a] st m (mf Blocks)
1392+ -> (String -> [a])
13921393 -> [FilePath] -> FilePath
1393− -> ParserT String st m (mf Blocks)
1394−insertIncludedFile' blocks dirs f = do
1394+ -> ParserT [a] st m (mf Blocks)
1395+insertIncludedFile' blocks totoks dirs f = do
13951396 oldPos <- getPosition
13961397 oldInput <- getInput
13971398 containers <- getIncludeFiles <$> getState
@@ -1405,7 +1406,7 @@ insertIncludedFile' blocks dirs f = do
14051406 report $ CouldNotLoadIncludeFile f oldPos
14061407 return ""
14071408 setPosition $ newPos f 1 1
1408− setInput contents
1409+ setInput $ totoks contents
14091410 bs <- blocks
14101411 setInput oldInput
14111412 setPosition oldPos
@@ -1415,11 +1416,12 @@ insertIncludedFile' blocks dirs f = do
14151416-- | Parse content of include file as blocks. Circular includes result in an
14161417-- @PandocParseError@.
14171418insertIncludedFile :: (PandocMonad m, HasIncludeFiles st)
1418− => ParserT String st m Blocks
1419+ => ParserT [a] st m Blocks
1420+ -> (String -> [a])
14191421 -> [FilePath] -> FilePath
1420− -> ParserT String st m Blocks
1421−insertIncludedFile blocks dirs f =
1422− runIdentity <$> insertIncludedFile' (Identity <$> blocks) dirs f
1422+ -> ParserT [a] st m Blocks
1423+insertIncludedFile blocks totoks dirs f =
1424+ runIdentity <$> insertIncludedFile' (Identity <$> blocks) totoks dirs f
14231425
14241426-- | Parse content of include file as future blocks. Circular includes result in
14251427-- an @PandocParseError@.
@@ -1427,4 +1429,4 @@ insertIncludedFileF :: (PandocMonad m, HasIncludeFiles st)
14271429 => ParserT String st m (Future st Blocks)
14281430 -> [FilePath] -> FilePath
14291431 -> ParserT String st m (Future st Blocks)
1430−insertIncludedFileF = insertIncludedFile'
1432+insertIncludedFileF p = insertIncludedFile' p id
src/Text/Pandoc/Readers/LaTeX.hs+74−67
@@ -54,7 +54,7 @@ import Text.Pandoc.Highlighting (fromListingsLanguage, languagesByExtension)
5454import Text.Pandoc.ImageSize (numUnit, showFl)
5555import Text.Pandoc.Logging
5656import Text.Pandoc.Options
57−import Text.Pandoc.Parsing hiding (many, mathDisplay, mathInline, optional,
57+import Text.Pandoc.Parsing hiding (many, optional,
5858 space, (<|>), spaces, blankline)
5959import Text.Pandoc.Shared
6060import Text.Pandoc.Walk
@@ -79,13 +79,13 @@ testParser p t = do
7979
8080type LP m = ParserT [Tok] ParserState m
8181
82−rawLaTeXBlock :: PandocMonad m => LP m String
82+rawLaTeXBlock :: PandocMonad m => ParserT String ParserState m String
8383rawLaTeXBlock = mzero
8484
85−rawLaTeXInline :: PandocMonad m => LP m Inline
85+rawLaTeXInline :: PandocMonad m => ParserT String ParserState m Inline
8686rawLaTeXInline = mzero
8787
88−inlineCommand :: PandocMonad m => LP m Inlines
88+inlineCommand :: PandocMonad m => ParserT String ParserState m Inlines
8989inlineCommand = mzero
9090
9191data TokType = CtrlSeq | Spaces | Newline | Symbol | Word | Comment |
@@ -150,7 +150,8 @@ totoks (lin,col) t =
150150 : totoks (lin, col + 3) rest''
151151 _ -> [Tok (lin, col) Symbol (T.singleton '^'),
152152 Tok (lin, col + 1) Symbol (T.singleton '^')]
153− Nothing -> [Tok (lin, col) Symbol (T.singleton '^')]
153+ _ -> Tok (lin, col) Symbol (T.singleton '^')
154+ : totoks (lin, col + 1) rest
154155 | otherwise ->
155156 Tok (lin, col) Symbol (T.singleton c) : totoks (lin, col + 1) rest
156157
@@ -355,6 +356,15 @@ quoted' f starter ender = do
355356 cs -> cs)
356357 else lit startchs
357358
359+dollarsMath :: PandocMonad m => LP m Inlines
360+dollarsMath = do
361+ symbol '$'
362+ display <- option False (True <$ symbol '$')
363+ contents <- many1Till anyTok (symbol '$')
364+ when display (() <$ symbol '$')
365+ let constructor = if display then displayMath else math
366+ return $ constructor $ T.unpack $ untokenize contents
367+
358368inline :: PandocMonad m => LP m Inlines
359369inline = (mempty <$ comment)
360370 <|> (space <$ whitespace)
@@ -373,8 +383,7 @@ inline = (mempty <$ comment)
373383 <|> (str "’" <$ symbol '\'')
374384 <|> (str "’" <$ symbol '’')
375385 <|> (str "\160" <$ symbol '~')
376− -- <|> mathDisplay (string "$$" *> mathChars <* string "$$")
377− -- <|> mathInline (char '$' *> mathChars <* char '$')
386+ <|> dollarsMath
378387 <|> (guardEnabled Ext_literate_haskell *> symbol '|' *> doLHSverb)
379388 <|> (str . (:[]) <$> primEscape)
380389 <|> (do res <- symbolIn "#&~^'`\"[]"
@@ -397,10 +406,29 @@ paragraph = do
397406 then return mempty
398407 else return $ para x
399408
409+include :: PandocMonad m => LP m Blocks
410+include = do
411+ (Tok _ _ name) <- controlSeq "include" <|> controlSeq "input" <|>
412+ controlSeq "subfile" <|> controlSeq "usepackage"
413+ skipMany $ bracketed inline -- skip options
414+ fs <- (map trim . splitBy (==',') . T.unpack . untokenize) <$> braced
415+ let fs' = if name == "\\usepackage"
416+ then map (maybeAddExtension ".sty") fs
417+ else map (maybeAddExtension ".tex") fs
418+ dirs <- (splitBy (==':') . fromMaybe ".") <$> lookupEnv "TEXINPUTS"
419+ mconcat <$> mapM (insertIncludedFile blocks (tokenize . T.pack) dirs) fs'
420+
421+maybeAddExtension :: String -> FilePath -> FilePath
422+maybeAddExtension ext fp =
423+ if null (takeExtension fp)
424+ then addExtension fp ext
425+ else fp
426+
427+
400428block :: PandocMonad m => LP m Blocks
401429block = (mempty <$ spaces1)
402430 -- <|> environment
403− -- <|> include
431+ <|> include
404432 -- <|> macro
405433 -- <|> blockCommand
406434 <|> paragraph
@@ -1372,63 +1400,6 @@ rawVerbEnv name = do
13721400
13731401----
13741402
1375−maybeAddExtension :: String -> FilePath -> FilePath
1376−maybeAddExtension ext fp =
1377− if null (takeExtension fp)
1378− then addExtension fp ext
1379− else fp
1380−
1381−include :: PandocMonad m => LP m Blocks
1382−include = do
1383− fs' <- try $ do
1384− char '\\'
1385− name <- try (string "include")
1386− <|> try (string "input")
1387− <|> try (string "subfile")
1388− <|> string "usepackage"
1389− -- skip options
1390− skipMany $ try $ char '[' *> manyTill anyChar (char ']')
1391− fs <- (map trim . splitBy (==',')) <$> braced
1392− return $ if name == "usepackage"
1393− then map (maybeAddExtension ".sty") fs
1394− else map (maybeAddExtension ".tex") fs
1395− dirs <- (splitBy (==':') . fromMaybe ".") <$> lookupEnv "TEXINPUTS"
1396− mconcat <$> mapM (insertIncludedFile blocks dirs) fs'
1397−
1398−inputListing :: PandocMonad m => LP m Blocks
1399−inputListing = do
1400− pos <- getPosition
1401− options <- option [] keyvals
1402− f <- filter (/='"') <$> braced
1403− dirs <- (splitBy (==':') . fromMaybe ".") <$> lookupEnv "TEXINPUTS"
1404− mbCode <- readFileFromDirs dirs f
1405− codeLines <- case mbCode of
1406− Just s -> return $ lines s
1407− Nothing -> do
1408− report $ CouldNotLoadIncludeFile f pos
1409− return []
1410− let (ident,classes,kvs) = parseListingsOptions options
1411− let language = case lookup "language" options >>= fromListingsLanguage of
1412− Just l -> [l]
1413− Nothing -> take 1 $ languagesByExtension (takeExtension f)
1414− let firstline = fromMaybe 1 $ lookup "firstline" options >>= safeRead
1415− let lastline = fromMaybe (length codeLines) $
1416− lookup "lastline" options >>= safeRead
1417− let codeContents = intercalate "\n" $ take (1 + lastline - firstline) $
1418− drop (firstline - 1) codeLines
1419− return $ codeBlockWith (ident,ordNub (classes ++ language),kvs) codeContents
1420−
1421−parseListingsOptions :: [(String, String)] -> Attr
1422−parseListingsOptions options =
1423− let kvs = [ (if k == "firstnumber"
1424− then "startFrom"
1425− else k, v) | (k,v) <- options ]
1426− classes = [ "numberLines" |
1427− lookup "numbers" options == Just "left" ]
1428− ++ maybeToList (lookup "language" options
1429− >>= fromListingsLanguage)
1430− in (fromMaybe "" (lookup "label" options), classes, kvs)
1431−
14321403----
14331404
14341405keyval :: PandocMonad m => LP m (String, String)
@@ -1451,10 +1422,10 @@ alltt t = walk strToCode <$> parseFromString' blocks
14511422 where strToCode (Str s) = Code nullAttr s
14521423 strToCode x = x
14531424
1454−rawLaTeXBlock :: PandocMonad m => LP m String
1425+rawLaTeXBlock :: PandocMonad m => ParserT String ParserState String
14551426rawLaTeXBlock = snd <$> try (withRaw (environment <|> blockCommand))
14561427
1457−rawLaTeXInline :: PandocMonad m => LP m Inline
1428+rawLaTeXInline :: PandocMonad m => ParserT String ParserState Inline
14581429rawLaTeXInline = do
14591430 raw <- (snd <$> withRaw inlineCommand)
14601431 <|> (snd <$> withRaw inlineEnvironment)
@@ -1890,4 +1861,40 @@ removeDoubleQuotes ('"':xs) =
18901861 '"':ys -> reverse ys
18911862 _ -> '"':xs
18921863removeDoubleQuotes xs = xs
1864+
1865+inputListing :: PandocMonad m => LP m Blocks
1866+inputListing = do
1867+ pos <- getPosition
1868+ options <- option [] keyvals
1869+ f <- filter (/='"') <$> braced
1870+ dirs <- (splitBy (==':') . fromMaybe ".") <$> lookupEnv "TEXINPUTS"
1871+ mbCode <- readFileFromDirs dirs f
1872+ codeLines <- case mbCode of
1873+ Just s -> return $ lines s
1874+ Nothing -> do
1875+ report $ CouldNotLoadIncludeFile f pos
1876+ return []
1877+ let (ident,classes,kvs) = parseListingsOptions options
1878+ let language = case lookup "language" options >>= fromListingsLanguage of
1879+ Just l -> [l]
1880+ Nothing -> take 1 $ languagesByExtension (takeExtension f)
1881+ let firstline = fromMaybe 1 $ lookup "firstline" options >>= safeRead
1882+ let lastline = fromMaybe (length codeLines) $
1883+ lookup "lastline" options >>= safeRead
1884+ let codeContents = intercalate "\n" $ take (1 + lastline - firstline) $
1885+ drop (firstline - 1) codeLines
1886+ return $ codeBlockWith (ident,ordNub (classes ++ language),kvs) codeContents
1887+
1888+parseListingsOptions :: [(String, String)] -> Attr
1889+parseListingsOptions options =
1890+ let kvs = [ (if k == "firstnumber"
1891+ then "startFrom"
1892+ else k, v) | (k,v) <- options ]
1893+ classes = [ "numberLines" |
1894+ lookup "numbers" options == Just "left" ]
1895+ ++ maybeToList (lookup "language" options
1896+ >>= fromListingsLanguage)
1897+ in (fromMaybe "" (lookup "label" options), classes, kvs)
1898+
1899+
18931900-}