git.iko.soy/typeable/pandoc
on master help sign in
Move some code from T.P.R.LaTeX. to T.P.R.LaTeX.Parsing.

We need to reduce the size of the LaTeX reader to ease compilation on resource-limited systems. More can be done in this vein.

commitfe315a8290a9d192ef6fe707553f3baaafb3d035 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 20 Jul 2020, 23:36 (6 years ago) parente17b471
2 files changed+78−70
src/Text/Pandoc/Readers/LaTeX.hs+14−70 src/Text/Pandoc/Readers/LaTeX/Parsing.hs+64
src/Text/Pandoc/Readers/LaTeX.hs+14−70
@@ -586,44 +586,6 @@ nlToSpace :: Char -> Char
586586nlToSpace '\n' = ' '
587587nlToSpace x = x
588588
589−keyval :: PandocMonad m => LP m (Text, Text)
590−keyval = try $ do
591− Tok _ Word key <- satisfyTok isWordTok
592− sp
593− val <- option mempty $ do
594− symbol '='
595− sp
596− (untokenize <$> braced) <|>
597− (mconcat <$> many1 (
598− (untokenize . snd <$> withRaw braced)
599− <|>
600− (untokenize <$> (many1
601− (satisfyTok
602− (\t -> case t of
603− Tok _ Symbol "]" -> False
604− Tok _ Symbol "," -> False
605− Tok _ Symbol "{" -> False
606− Tok _ Symbol "}" -> False
607− _ -> True))))))
608− optional (symbol ',')
609− sp
610− return (key, T.strip val)
611−
612−keyvals :: PandocMonad m => LP m [(Text, Text)]
613−keyvals = try $ symbol '[' >> manyTill keyval (symbol ']') <* sp
614−
615−accent :: PandocMonad m => Char -> Maybe Char -> LP m Inlines
616−accent combiningAccent fallBack = try $ do
617− ils <- tok
618− case toList ils of
619− (Str (T.uncons -> Just (x, xs)) : ys) -> return $ fromList $
620− -- try to normalize to the combined character:
621− Str (Normalize.normalize Normalize.NFC
622− (T.pack [x, combiningAccent]) <> xs) : ys
623− [Space] -> return $ str $ T.singleton $ fromMaybe combiningAccent fallBack
624− [] -> return $ str $ T.singleton $ fromMaybe combiningAccent fallBack
625− _ -> return ils
626−
627589mathDisplay :: Text -> Inlines
628590mathDisplay = displayMath . trimMath
629591
@@ -782,6 +744,7 @@ inlineCommand' = try $ do
782744 <|> ignore rawcommand
783745 lookupListDefault raw names inlineCommands
784746
747+
785748tok :: PandocMonad m => LP m Inlines
786749tok = try $ spaces >> grouped inline <|> inlineCommand' <|> singleChar'
787750 where singleChar' = do
@@ -1178,6 +1141,19 @@ inlineCommands = M.union inlineLanguageCommands $ M.fromList
11781141 , ("ifdim", ifdim)
11791142 ]
11801143
1144+accent :: PandocMonad m => Char -> Maybe Char -> LP m Inlines
1145+accent combiningAccent fallBack = try $ do
1146+ ils <- tok
1147+ case toList ils of
1148+ (Str (T.uncons -> Just (x, xs)) : ys) -> return $ fromList $
1149+ -- try to normalize to the combined character:
1150+ Str (Normalize.normalize Normalize.NFC
1151+ (T.pack [x, combiningAccent]) <> xs) : ys
1152+ [Space] -> return $ str $ T.singleton $ fromMaybe combiningAccent fallBack
1153+ [] -> return $ str $ T.singleton $ fromMaybe combiningAccent fallBack
1154+ _ -> return ils
1155+
1156+
11811157lettrine :: PandocMonad m => LP m Inlines
11821158lettrine = do
11831159 optional opt
@@ -1471,20 +1447,6 @@ inlines = mconcat <$> many inline
14711447
14721448-- block elements:
14731449
1474−begin_ :: PandocMonad m => Text -> LP m ()
1475−begin_ t = try (do
1476− controlSeq "begin"
1477− spaces
1478− txt <- untokenize <$> braced
1479− guard (t == txt)) <?> ("\\begin{" ++ T.unpack t ++ "}")
1480−
1481−end_ :: PandocMonad m => Text -> LP m ()
1482−end_ t = try (do
1483− controlSeq "end"
1484− spaces
1485− txt <- untokenize <$> braced
1486− guard $ t == txt) <?> ("\\end{" ++ T.unpack t ++ "}")
1487−
14881450preamble :: PandocMonad m => LP m Blocks
14891451preamble = mconcat <$> many preambleBlock
14901452 where preambleBlock = (mempty <$ spaces1)
@@ -1997,24 +1959,6 @@ rawVerbEnv name = do
19971959 report $ SkippedContent raw' pos
19981960 return mempty
19991961
2000−verbEnv :: PandocMonad m => Text -> LP m Text
2001−verbEnv name = withVerbatimMode $ do
2002− optional blankline
2003− res <- manyTill anyTok (end_ name)
2004− return $ stripTrailingNewline
2005− $ untokenize
2006− $ res
2007−
2008−-- Strip single final newline and any spaces following it.
2009−-- Input is unchanged if it doesn't end with newline +
2010−-- optional spaces.
2011−stripTrailingNewline :: Text -> Text
2012−stripTrailingNewline t =
2013− let (b, e) = T.breakOnEnd "\n" t
2014− in if T.all (== ' ') e
2015− then T.dropEnd 1 b
2016− else t
2017−
20181962fancyverbEnv :: PandocMonad m => Text -> LP m Blocks
20191963fancyverbEnv name = do
20201964 options <- option [] keyvals
src/Text/Pandoc/Readers/LaTeX/Parsing.hs+64
@@ -2,6 +2,7 @@
22{-# LANGUAGE MultiParamTypeClasses #-}
33{-# LANGUAGE OverloadedStrings #-}
44{-# LANGUAGE ScopedTypeVariables #-}
5+{-# LANGUAGE ViewPatterns #-}
56{- |
67 Module : Text.Pandoc.Readers.LaTeX.Parsing
78 Copyright : Copyright (C) 2006-2020 John MacFarlane
@@ -66,6 +67,10 @@ module Text.Pandoc.Readers.LaTeX.Parsing
6667 , dimenarg
6768 , ignore
6869 , withRaw
70+ , keyvals
71+ , verbEnv
72+ , begin_
73+ , end_
6974 ) where
7075
7176import Control.Applicative (many, (<|>))
@@ -695,3 +700,62 @@ withRaw parser = do
695700 let raw = takeWhile (\(Tok pos _ _) -> maybe True
696701 (\p -> sourceName p /= sourceName pos || pos < p) nxtpos) inp
697702 return (result, raw)
703+
704+keyval :: PandocMonad m => LP m (Text, Text)
705+keyval = try $ do
706+ Tok _ Word key <- satisfyTok isWordTok
707+ sp
708+ val <- option mempty $ do
709+ symbol '='
710+ sp
711+ (untokenize <$> braced) <|>
712+ (mconcat <$> many1 (
713+ (untokenize . snd <$> withRaw braced)
714+ <|>
715+ (untokenize <$> (many1
716+ (satisfyTok
717+ (\t -> case t of
718+ Tok _ Symbol "]" -> False
719+ Tok _ Symbol "," -> False
720+ Tok _ Symbol "{" -> False
721+ Tok _ Symbol "}" -> False
722+ _ -> True))))))
723+ optional (symbol ',')
724+ sp
725+ return (key, T.strip val)
726+
727+keyvals :: PandocMonad m => LP m [(Text, Text)]
728+keyvals = try $ symbol '[' >> manyTill keyval (symbol ']') <* sp
729+
730+verbEnv :: PandocMonad m => Text -> LP m Text
731+verbEnv name = withVerbatimMode $ do
732+ optional blankline
733+ res <- manyTill anyTok (end_ name)
734+ return $ stripTrailingNewline
735+ $ untokenize
736+ $ res
737+
738+-- Strip single final newline and any spaces following it.
739+-- Input is unchanged if it doesn't end with newline +
740+-- optional spaces.
741+stripTrailingNewline :: Text -> Text
742+stripTrailingNewline t =
743+ let (b, e) = T.breakOnEnd "\n" t
744+ in if T.all (== ' ') e
745+ then T.dropEnd 1 b
746+ else t
747+
748+begin_ :: PandocMonad m => Text -> LP m ()
749+begin_ t = try (do
750+ controlSeq "begin"
751+ spaces
752+ txt <- untokenize <$> braced
753+ guard (t == txt)) <?> ("\\begin{" ++ T.unpack t ++ "}")
754+
755+end_ :: PandocMonad m => Text -> LP m ()
756+end_ t = try (do
757+ controlSeq "end"
758+ spaces
759+ txt <- untokenize <$> braced
760+ guard $ t == txt) <?> ("\\end{" ++ T.unpack t ++ "}")
761+