@@ -35,7 +35,6 @@ import Data.List (intercalate)
35import qualified Data.Map as M
35import qualified Data.Map as M
36import Data.Maybe (fromMaybe, maybeToList)
36import Data.Maybe (fromMaybe, maybeToList)
37import qualified Data.Set as Set
37import qualified Data.Set as Set
38−import qualified Data.Sequence as Seq
39import Data.Text (Text)
38import Data.Text (Text)
40import qualified Data.Text as T
39import qualified Data.Text as T
41import System.FilePath (addExtension, replaceExtension, takeExtension)
40import System.FilePath (addExtension, replaceExtension, takeExtension)
@@ -57,6 +56,11 @@ import Text.Pandoc.Readers.LaTeX.Types (ExpansionPoint (..), Macro (..),
57import Text.Pandoc.Readers.LaTeX.Parsing
56import Text.Pandoc.Readers.LaTeX.Parsing
58import Text.Pandoc.Readers.LaTeX.Accent (accentCommands)
57import Text.Pandoc.Readers.LaTeX.Accent (accentCommands)
59import Text.Pandoc.Readers.LaTeX.Citation (citationCommands, cites)
58import Text.Pandoc.Readers.LaTeX.Citation (citationCommands, cites)
59+import Text.Pandoc.Readers.LaTeX.Math (dollarsMath, inlineEnvironments,
61+ mathDisplay, mathInline,
62+ newtheorem, theoremstyle, proof,
60import Text.Pandoc.Readers.LaTeX.Table (tableEnvironments)
64import Text.Pandoc.Readers.LaTeX.Table (tableEnvironments)
61import Text.Pandoc.Readers.LaTeX.Lang (polyglossiaLangToBCP47,
65import Text.Pandoc.Readers.LaTeX.Lang (polyglossiaLangToBCP47,
62 babelLangToBCP47, setDefaultLanguage)
66 babelLangToBCP47, setDefaultLanguage)
@@ -387,39 +391,6 @@ nlToSpace :: Char -> Char
387nlToSpace '\n' = ' '
391nlToSpace '\n' = ' '
388nlToSpace x = x
392nlToSpace x = x
390−mathDisplay :: Text -> Inlines
391−mathDisplay = displayMath . trimMath
393−mathInline :: Text -> Inlines
394−mathInline = math . trimMath
396−dollarsMath :: PandocMonad m => LP m Inlines
399− display <- option False (True <$ symbol '$')
400− (do contents <- try $ untokenize <$> pDollarsMath 0
402− then mathDisplay contents <$ symbol '$'
403− else return $ mathInline contents)
404− <|> (guard display >> return (mathInline ""))
406−-- Int is number of embedded groupings
407−pDollarsMath :: PandocMonad m => Int -> LP m [Tok]
409− tk@(Tok _ toktype t) <- anyTok
412− , n == 0 -> return []
415− (tk :) . (tk' :) <$> pDollarsMath n
416− | t == "{" -> (tk :) <$> pDollarsMath (n+1)
419− then (tk :) <$> pDollarsMath (n-1)
421− _ -> (tk :) <$> pDollarsMath n
423inlineCommand' :: PandocMonad m => LP m Inlines
394inlineCommand' :: PandocMonad m => LP m Inlines
424inlineCommand' = try $ do
395inlineCommand' = try $ do
425 Tok _ (CtrlSeq name) cmd <- anyControlSeq
396 Tok _ (CtrlSeq name) cmd <- anyControlSeq
@@ -452,51 +423,6 @@ unescapeURL = T.concat . go . T.splitOn "\\"
452 , isEscapable c = t
423 , isEscapable c = t
453 | otherwise = "\\" <> t
424 | otherwise = "\\" <> t
455−mathEnvWith :: PandocMonad m
456− => (Inlines -> a) -> Maybe Text -> Text -> LP m a
457−mathEnvWith f innerEnv name = f . mathDisplay . inner <$> mathEnv name
458− where inner x = case innerEnv of
460− Just y -> "\\begin{" <> y <> "}\n" <> x <>
461− "\\end{" <> y <> "}"
463−mathEnv :: PandocMonad m => Text -> LP m Text
467− res <- manyTill anyTok (end_ name)
468− return $ stripTrailingNewlines $ untokenize res
470−inlineEnvironment :: PandocMonad m => LP m Inlines
471−inlineEnvironment = try $ do
473− name <- untokenize <$> braced
474− M.findWithDefault mzero name inlineEnvironments
476−inlineEnvironments :: PandocMonad m => M.Map Text (LP m Inlines)
477−inlineEnvironments = M.fromList [
478− ("displaymath", mathEnvWith id Nothing "displaymath")
479− , ("math", math <$> mathEnv "math")
480− , ("equation", mathEnvWith id Nothing "equation")
481− , ("equation*", mathEnvWith id Nothing "equation*")
482− , ("gather", mathEnvWith id (Just "gathered") "gather")
483− , ("gather*", mathEnvWith id (Just "gathered") "gather*")
484− , ("multline", mathEnvWith id (Just "gathered") "multline")
485− , ("multline*", mathEnvWith id (Just "gathered") "multline*")
486− , ("eqnarray", mathEnvWith id (Just "aligned") "eqnarray")
487− , ("eqnarray*", mathEnvWith id (Just "aligned") "eqnarray*")
488− , ("align", mathEnvWith id (Just "aligned") "align")
489− , ("align*", mathEnvWith id (Just "aligned") "align*")
490− , ("alignat", mathEnvWith id (Just "aligned") "alignat")
491− , ("alignat*", mathEnvWith id (Just "aligned") "alignat*")
492− , ("dmath", mathEnvWith id Nothing "dmath")
493− , ("dmath*", mathEnvWith id Nothing "dmath*")
494− , ("dgroup", mathEnvWith id (Just "aligned") "dgroup")
495− , ("dgroup*", mathEnvWith id (Just "aligned") "dgroup*")
496− , ("darray", mathEnvWith id (Just "aligned") "darray")
497− , ("darray*", mathEnvWith id (Just "aligned") "darray*")
500inlineCommands :: PandocMonad m => M.Map Text (LP m Inlines)
426inlineCommands :: PandocMonad m => M.Map Text (LP m Inlines)
501inlineCommands =
427inlineCommands =
502 M.union inlineLanguageCommands $
428 M.union inlineLanguageCommands $
@@ -1354,7 +1280,7 @@ blockCommands = M.fromList
1354 , ("address", mempty <$ (skipopts *> tok >>= addMeta "address"))
1280 , ("address", mempty <$ (skipopts *> tok >>= addMeta "address"))
1355 , ("signature", mempty <$ (skipopts *> authors))
1281 , ("signature", mempty <$ (skipopts *> authors))
1356 , ("date", mempty <$ (skipopts *> tok >>= addMeta "date"))
1282 , ("date", mempty <$ (skipopts *> tok >>= addMeta "date"))
1357− , ("newtheorem", newtheorem)
1283+ , ("newtheorem", newtheorem inline)
1358 , ("theoremstyle", theoremstyle)
1284 , ("theoremstyle", theoremstyle)
1359 -- KOMA-Script metadata commands
1285 -- KOMA-Script metadata commands
1360 , ("extratitle", mempty <$ (skipopts *> tok >>= addMeta "extratitle"))
1286 , ("extratitle", mempty <$ (skipopts *> tok >>= addMeta "extratitle"))
@@ -1473,7 +1399,7 @@ environments = M.union (tableEnvironments blocks inline) $
1473 , ("lilypond", rawVerbEnv "lilypond")
1399 , ("lilypond", rawVerbEnv "lilypond")
1474 , ("ly", rawVerbEnv "ly")
1400 , ("ly", rawVerbEnv "ly")
1475 -- amsthm
1401 -- amsthm
1476− , ("proof", proof)
1402+ , ("proof", proof blocks opt)
1477 -- etoolbox
1403 -- etoolbox
1478 , ("ifstrequal", ifstrequal)
1404 , ("ifstrequal", ifstrequal)
1479 , ("newtoggle", braced >>= newToggle)
1405 , ("newtoggle", braced >>= newToggle)
@@ -1494,128 +1420,17 @@ filecontents = try $ do
1494 st{ sFileContents = M.insert fp txt (sFileContents st) }
1420 st{ sFileContents = M.insert fp txt (sFileContents st) }
1495 return mempty
1421 return mempty
1497−theoremstyle :: PandocMonad m => LP m Blocks
1499− stylename <- untokenize <$> braced
1500− let mbstyle = case stylename of
1501− "plain" -> Just PlainStyle
1502− "definition" -> Just DefinitionStyle
1503− "remark" -> Just RemarkStyle
1506− Nothing -> return ()
1507− Just sty -> updateState $ \s -> s{ sLastTheoremStyle = sty }
1510−newtheorem :: PandocMonad m => LP m Blocks
1512− number <- option True (False <$ symbol '*' <* sp)
1513− name <- untokenize <$> braced
1515− series <- option Nothing $ Just . untokenize <$> bracketedToks
1519− syncTo <- option Nothing $ Just . untokenize <$> bracketedToks
1520− sty <- sLastTheoremStyle <$> getState
1521− let spec = TheoremSpec { theoremName = showName
1522− , theoremStyle = sty
1523− , theoremSeries = series
1524− , theoremSyncTo = syncTo
1525− , theoremNumber = number
1526− , theoremLastNum = DottedNum [0] }
1527− tmap <- sTheoremMap <$> getState
1528− updateState $ \s -> s{ sTheoremMap =
1529− M.insert name spec tmap }
1532−proof :: PandocMonad m => LP m Blocks
1534− title <- option (B.text "Proof") opt
1535− bs <- env "proof" blocks
1537− B.divWith ("", ["proof"], []) $
1538− addQed $ addTitle (B.emph (title <> ".")) bs
1540−addTitle :: Inlines -> Blocks -> Blocks
1542− case B.toList bs of
1544− -> B.fromList (Para (B.toList ils ++ (Space : xs)) : rest)
1545− _ -> B.para ils <> bs
1547−addQed :: Blocks -> Blocks
1549− case Seq.viewr (B.unMany bs) of
1551− -> B.Many (s Seq.|> Para (ils ++ B.toList qedSign))
1552− _ -> bs <> B.para qedSign
1554− qedSign = B.str "\xa0\x25FB"
1556environment :: PandocMonad m => LP m Blocks
1423environment :: PandocMonad m => LP m Blocks
1557environment = try $ do
1424environment = try $ do
1558 controlSeq "begin"
1425 controlSeq "begin"
1559 name <- untokenize <$> braced
1426 name <- untokenize <$> braced
1560 M.findWithDefault mzero name environments <|>
1427 M.findWithDefault mzero name environments <|>
1561− theoremEnvironment name <|>
1428+ theoremEnvironment blocks opt name <|>
1562 if M.member name (inlineEnvironments
1429 if M.member name (inlineEnvironments
1563 :: M.Map Text (LP PandocPure Inlines))
1430 :: M.Map Text (LP PandocPure Inlines))
1564 then mzero
1431 then mzero
1565 else try (rawEnv name) <|> rawVerbEnv name
1432 else try (rawEnv name) <|> rawVerbEnv name
1567−theoremEnvironment :: PandocMonad m => Text -> LP m Blocks
1568−theoremEnvironment name = do
1569− tmap <- sTheoremMap <$> getState
1570− case M.lookup name tmap of
1573− optTitle <- option mempty $ (\x -> space <> "(" <> x <> ")") <$> opt
1574− mblabel <- option Nothing $ Just . untokenize <$>
1575− try (spaces >> controlSeq "label" >> spaces >> braced)
1576− bs <- env name blocks
1578− if theoremNumber tspec
1580− let name' = fromMaybe name $ theoremSeries tspec
1581− num <- getNextNumber
1582− (maybe (DottedNum [0]) theoremLastNum .
1583− M.lookup name' . sTheoremMap)
1584− updateState $ \s ->
1587− (\spec -> spec{ theoremLastNum = num })
1594− updateState $ \s ->
1595− s{ sLabels = M.insert ident
1597− theoremName tspec <> "\160" <>
1598− str (renderDottedNum num)) (sLabels s) }
1599− Nothing -> return ()
1600− return $ space <> B.text (renderDottedNum num)
1602− let titleEmph = case theoremStyle tspec of
1603− PlainStyle -> B.strong
1604− DefinitionStyle -> B.strong
1605− RemarkStyle -> B.emph
1606− let title = titleEmph (theoremName tspec <> number)
1607− <> optTitle <> "." <> space
1608− return $ divWith (fromMaybe "" mblabel, [name], []) $ addTitle title
1609− $ case theoremStyle tspec of
1610− PlainStyle -> walk italicize bs
1613−italicize :: Block -> Block
1614−italicize x@(Para [Image{}]) = x -- see #6925
1615−italicize (Para ils) = Para [Emph ils]
1616−italicize (Plain ils) = Plain [Emph ils]
1619rawEnv :: PandocMonad m => Text -> LP m Blocks
1434rawEnv :: PandocMonad m => Text -> LP m Blocks
1620rawEnv name = do
1435rawEnv name = do
1621 exts <- getOption readerExtensions
1436 exts <- getOption readerExtensions