git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: Better handling of comments inside math environments.

This solves a problem with commented out `\end{eqnarray}` inside an eqnarray (among other things). Closes #3113.

commitf20c89e24380007a47f3e28889706a6f584bc6e0 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 6 May 2017, 22:15 (9 years ago) parentddf2524
2 files changed+56−30
src/Text/Pandoc/Readers/LaTeX.hs+43−30 test/command/3113.md+13
src/Text/Pandoc/Readers/LaTeX.hs+43−30
@@ -492,20 +492,20 @@ isBlockCommand s = s `M.member` (blockCommands :: M.Map String (LP PandocPure Bl
492492
493493inlineEnvironments :: PandocMonad m => M.Map String (LP m Inlines)
494494inlineEnvironments = M.fromList
495− [ ("displaymath", mathEnv id Nothing "displaymath")
496− , ("math", math <$> verbEnv "math")
497− , ("equation", mathEnv id Nothing "equation")
498− , ("equation*", mathEnv id Nothing "equation*")
499− , ("gather", mathEnv id (Just "gathered") "gather")
500− , ("gather*", mathEnv id (Just "gathered") "gather*")
501− , ("multline", mathEnv id (Just "gathered") "multline")
502− , ("multline*", mathEnv id (Just "gathered") "multline*")
503− , ("eqnarray", mathEnv id (Just "aligned") "eqnarray")
504− , ("eqnarray*", mathEnv id (Just "aligned") "eqnarray*")
505− , ("align", mathEnv id (Just "aligned") "align")
506− , ("align*", mathEnv id (Just "aligned") "align*")
507− , ("alignat", mathEnv id (Just "aligned") "alignat")
508− , ("alignat*", mathEnv id (Just "aligned") "alignat*")
495+ [ ("displaymath", mathEnvWith id Nothing "displaymath")
496+ , ("math", math <$> mathEnv "math")
497+ , ("equation", mathEnvWith id Nothing "equation")
498+ , ("equation*", mathEnvWith id Nothing "equation*")
499+ , ("gather", mathEnvWith id (Just "gathered") "gather")
500+ , ("gather*", mathEnvWith id (Just "gathered") "gather*")
501+ , ("multline", mathEnvWith id (Just "gathered") "multline")
502+ , ("multline*", mathEnvWith id (Just "gathered") "multline*")
503+ , ("eqnarray", mathEnvWith id (Just "aligned") "eqnarray")
504+ , ("eqnarray*", mathEnvWith id (Just "aligned") "eqnarray*")
505+ , ("align", mathEnvWith id (Just "aligned") "align")
506+ , ("align*", mathEnvWith id (Just "aligned") "align*")
507+ , ("alignat", mathEnvWith id (Just "aligned") "alignat")
508+ , ("alignat*", mathEnvWith id (Just "aligned") "alignat*")
509509 ]
510510
511511inlineCommands :: PandocMonad m => M.Map String (LP m Inlines)
@@ -1187,19 +1187,19 @@ environments = M.fromList
11871187 , ("obeylines", parseFromString
11881188 (para . trimInlines . mconcat <$> many inline) =<<
11891189 intercalate "\\\\\n" . lines <$> verbEnv "obeylines")
1190− , ("displaymath", mathEnv para Nothing "displaymath")
1191− , ("equation", mathEnv para Nothing "equation")
1192− , ("equation*", mathEnv para Nothing "equation*")
1193− , ("gather", mathEnv para (Just "gathered") "gather")
1194− , ("gather*", mathEnv para (Just "gathered") "gather*")
1195− , ("multline", mathEnv para (Just "gathered") "multline")
1196− , ("multline*", mathEnv para (Just "gathered") "multline*")
1197− , ("eqnarray", mathEnv para (Just "aligned") "eqnarray")
1198− , ("eqnarray*", mathEnv para (Just "aligned") "eqnarray*")
1199− , ("align", mathEnv para (Just "aligned") "align")
1200− , ("align*", mathEnv para (Just "aligned") "align*")
1201− , ("alignat", mathEnv para (Just "aligned") "alignat")
1202− , ("alignat*", mathEnv para (Just "aligned") "alignat*")
1190+ , ("displaymath", mathEnvWith para Nothing "displaymath")
1191+ , ("equation", mathEnvWith para Nothing "equation")
1192+ , ("equation*", mathEnvWith para Nothing "equation*")
1193+ , ("gather", mathEnvWith para (Just "gathered") "gather")
1194+ , ("gather*", mathEnvWith para (Just "gathered") "gather*")
1195+ , ("multline", mathEnvWith para (Just "gathered") "multline")
1196+ , ("multline*", mathEnvWith para (Just "gathered") "multline*")
1197+ , ("eqnarray", mathEnvWith para (Just "aligned") "eqnarray")
1198+ , ("eqnarray*", mathEnvWith para (Just "aligned") "eqnarray*")
1199+ , ("align", mathEnvWith para (Just "aligned") "align")
1200+ , ("align*", mathEnvWith para (Just "aligned") "align*")
1201+ , ("alignat", mathEnvWith para (Just "aligned") "alignat")
1202+ , ("alignat*", mathEnvWith para (Just "aligned") "alignat*")
12031203 ]
12041204
12051205figure :: PandocMonad m => LP m Blocks
@@ -1264,19 +1264,32 @@ listenv name p = try $ do
12641264 updateState $ \st -> st{ stateParserContext = oldCtx }
12651265 return res
12661266
1267−mathEnv :: PandocMonad m => (Inlines -> a) -> Maybe String -> String -> LP m a
1268−mathEnv f innerEnv name = f <$> mathDisplay (inner <$> verbEnv name)
1267+mathEnvWith :: PandocMonad m
1268+ => (Inlines -> a) -> Maybe String -> String -> LP m a
1269+mathEnvWith f innerEnv name = f <$> mathDisplay (inner <$> mathEnv name)
12691270 where inner x = case innerEnv of
12701271 Nothing -> x
12711272 Just y -> "\\begin{" ++ y ++ "}\n" ++ x ++
12721273 "\\end{" ++ y ++ "}"
12731274
1275+mathEnv :: PandocMonad m => String -> LP m String
1276+mathEnv name = do
1277+ skipopts
1278+ optional blankline
1279+ let endEnv = try $ controlSeq "end" *> braced >>= guard . (== name)
1280+ charMuncher = skipMany comment *>
1281+ (many1 (noneOf "\\%") <|> try (string "\\%")
1282+ <|> try (string "\\\\") <|> count 1 anyChar)
1283+ res <- concat <$> manyTill charMuncher endEnv
1284+ return $ stripTrailingNewlines res
1285+
12741286verbEnv :: PandocMonad m => String -> LP m String
12751287verbEnv name = do
12761288 skipopts
12771289 optional blankline
12781290 let endEnv = try $ controlSeq "end" *> braced >>= guard . (== name)
1279− res <- manyTill anyChar endEnv
1291+ charMuncher = anyChar
1292+ res <- manyTill charMuncher endEnv
12801293 return $ stripTrailingNewlines res
12811294
12821295fancyverbEnv :: PandocMonad m => String -> LP m Blocks
test/command/3113.mdnew file+13
@@ -0,0 +1,13 @@
1+```
2+% pandoc -f latex -t native
3+\begin{eqnarray}
4+A&=&B,\\
5+C&=&D,\\
6+%\end{eqnarray}
7+%\begin{eqnarray}
8+E&=&F
9+\end{eqnarray}
10+^D
11+[Para [Math DisplayMath "\\begin{aligned}\nA&=&B,\\\\\nC&=&D,\\\\\nE&=&F\\end{aligned}"]]
12+```
13+