summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2022-03-12 11:14:28 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2022-03-12 12:18:51 -0800
commit699336cf5bf787b6e2766c6133852bbcff0c8264 (patch)
treeae34959bf299c53fdbe8285d5eda7c1c3bb7ac1c /src
parentd128f5ee15e4bffd3a8bee9d0ecd4a496e6fd4fc (diff)
LaTeX reader: better handling of `\usepackage`.
If the package is local but causes parse errors, parse everything up to the error and skip the rest. Issue a CouldNotParseIncludeFile warning indicating that parsing failed at that point. T.P.Logging: add CouldNotParseIncludeFile constructor.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Logging.hs9
-rw-r--r--src/Text/Pandoc/Readers/LaTeX.hs44
2 files changed, 42 insertions, 11 deletions
diff --git a/src/Text/Pandoc/Logging.hs b/src/Text/Pandoc/Logging.hs
index 44bc24968..b994b1820 100644
--- a/src/Text/Pandoc/Logging.hs
+++ b/src/Text/Pandoc/Logging.hs
@@ -63,6 +63,7 @@ data LogMessage =
| UndefinedToggle Text SourcePos
| ParsingUnescaped Text SourcePos
| CouldNotLoadIncludeFile Text SourcePos
+ | CouldNotParseIncludeFile Text SourcePos
| MacroAlreadyDefined Text SourcePos
| InlineNotRendered Inline
| BlockNotRendered Block
@@ -156,6 +157,11 @@ instance ToJSON LogMessage where
"source" .= sourceName pos,
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
+ CouldNotParseIncludeFile fp pos ->
+ ["path" .= fp,
+ "source" .= sourceName pos,
+ "line" .= toJSON (sourceLine pos),
+ "column" .= toJSON (sourceColumn pos)]
MacroAlreadyDefined name pos ->
["name" .= name,
"source" .= sourceName pos,
@@ -280,6 +286,8 @@ showLogMessage msg =
"Parsing unescaped '" <> s <> "' at " <> showPos pos
CouldNotLoadIncludeFile fp pos ->
"Could not load include file " <> fp <> " at " <> showPos pos
+ CouldNotParseIncludeFile fp pos ->
+ "Parsing include file " <> fp <> " failed at " <> showPos pos
MacroAlreadyDefined name pos ->
"Macro '" <> name <> "' already defined, ignoring at " <> showPos pos
InlineNotRendered il ->
@@ -383,6 +391,7 @@ messageVerbosity msg =
CouldNotLoadIncludeFile f _
| ".sty" `Text.isSuffixOf` f -> INFO
| otherwise -> WARNING
+ CouldNotParseIncludeFile{} -> WARNING
MacroAlreadyDefined{} -> WARNING
ParsingUnescaped{} -> INFO
InlineNotRendered{} -> INFO
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index da2af80e7..bb6c38352 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -708,15 +708,29 @@ doSubfile = do
return bs
include :: (PandocMonad m, Monoid a) => Text -> LP m a
-include name = do
+include _name = do
skipMany opt
fs <- map (T.unpack . removeDoubleQuotes . T.strip) . T.splitOn "," .
untokenize <$> braced
- let defaultExt | name == "usepackage" = ".sty"
- | otherwise = ".tex"
+ let defaultExt = ".tex"
mapM_ (insertIncluded defaultExt) fs
return mempty
+usepackage :: (PandocMonad m, Monoid a) => LP m a
+usepackage = do
+ skipMany opt
+ fs <- map (T.unpack . removeDoubleQuotes . T.strip) . T.splitOn "," .
+ untokenize <$> braced
+ let parsePackage f = do
+ TokStream _ ts <- getIncludedToks ".sty" f
+ parseFromToks (do _ <- blocks
+ eof <|>
+ do pos <- getPosition
+ report $ CouldNotParseIncludeFile (T.pack f) pos)
+ ts
+ mapM_ parsePackage fs
+ return mempty
+
readFileFromTexinputs :: PandocMonad m => FilePath -> LP m (Maybe Text)
readFileFromTexinputs fp = do
fileContentsMap <- sFileContents <$> getState
@@ -727,11 +741,11 @@ readFileFromTexinputs fp = do
<$> lookupEnv "TEXINPUTS"
readFileFromDirs dirs fp
-insertIncluded :: PandocMonad m
- => FilePath
- -> FilePath
- -> LP m ()
-insertIncluded defaultExtension f' = do
+getIncludedToks :: PandocMonad m
+ => FilePath
+ -> FilePath
+ -> LP m TokStream
+getIncludedToks defaultExtension f' = do
let f = case takeExtension f' of
".tex" -> f'
".sty" -> f'
@@ -747,9 +761,17 @@ insertIncluded defaultExtension f' = do
Nothing -> do
report $ CouldNotLoadIncludeFile (T.pack f) pos
return ""
- TokStream _ ts <- getInput
- setInput $ TokStream False (tokenize (initialPos f) contents ++ ts)
updateState dropLatestIncludeFile
+ return $ TokStream False $ tokenize (initialPos f) contents
+
+insertIncluded :: PandocMonad m
+ => FilePath
+ -> FilePath
+ -> LP m ()
+insertIncluded defaultExtension f' = do
+ contents <- getIncludedToks defaultExtension f'
+ ts <- getInput
+ setInput $ contents <> ts
authors :: PandocMonad m => LP m ()
authors = try $ do
@@ -941,7 +963,7 @@ blockCommands = M.fromList
, ("include", rawBlockOr "include" $ include "include")
, ("input", rawBlockOr "input" $ include "input")
, ("subfile", rawBlockOr "subfile" doSubfile)
- , ("usepackage", rawBlockOr "usepackage" $ include "usepackage")
+ , ("usepackage", rawBlockOr "usepackage" usepackage)
-- preamble
, ("PackageError", mempty <$ (braced >> braced >> braced))
-- epigraph package