git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: implement \newtoggle, \iftoggle, \toggletrue|false

from etoolbox. Closes #3853.

commitbfbdfa646a48147cc7210679f6b17654eec35c5f authorJohn MacFarlane <jgm@berkeley.edu> dateFri 18 Aug 2017, 10:13 (9 years ago) parentd1444b4
3 files changed+82−5
src/Text/Pandoc/Logging.hs+9 src/Text/Pandoc/Readers/LaTeX.hs+47−5 test/command/3853.md+26
src/Text/Pandoc/Logging.hs+9
@@ -75,6 +75,7 @@ data LogMessage =
7575 | DuplicateIdentifier String SourcePos
7676 | ReferenceNotFound String SourcePos
7777 | CircularReference String SourcePos
78+ | UndefinedToggle String SourcePos
7879 | ParsingUnescaped String SourcePos
7980 | CouldNotLoadIncludeFile String SourcePos
8081 | MacroAlreadyDefined String SourcePos
@@ -144,6 +145,11 @@ instance ToJSON LogMessage where
144145 "source" .= Text.pack (sourceName pos),
145146 "line" .= toJSON (sourceLine pos),
146147 "column" .= toJSON (sourceColumn pos)]
148+ UndefinedToggle s pos ->
149+ ["contents" .= Text.pack s,
150+ "source" .= Text.pack (sourceName pos),
151+ "line" .= toJSON (sourceLine pos),
152+ "column" .= toJSON (sourceColumn pos)]
147153 ParsingUnescaped s pos ->
148154 ["contents" .= Text.pack s,
149155 "source" .= Text.pack (sourceName pos),
@@ -238,6 +244,8 @@ showLogMessage msg =
238244 "Reference not found for '" ++ s ++ "' at " ++ showPos pos
239245 CircularReference s pos ->
240246 "Circular reference '" ++ s ++ "' at " ++ showPos pos
247+ UndefinedToggle s pos ->
248+ "Undefined toggle '" ++ s ++ "' at " ++ showPos pos
241249 ParsingUnescaped s pos ->
242250 "Parsing unescaped '" ++ s ++ "' at " ++ showPos pos
243251 CouldNotLoadIncludeFile fp pos ->
@@ -306,6 +314,7 @@ messageVerbosity msg =
306314 DuplicateIdentifier{} -> WARNING
307315 ReferenceNotFound{} -> WARNING
308316 CircularReference{} -> WARNING
317+ UndefinedToggle{} -> WARNING
309318 CouldNotLoadIncludeFile{} -> WARNING
310319 MacroAlreadyDefined{} -> WARNING
311320 ParsingUnescaped{} -> INFO
src/Text/Pandoc/Readers/LaTeX.hs+47−5
@@ -159,6 +159,7 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
159159 , sInTableCell :: Bool
160160 , sLastHeaderNum :: HeaderNum
161161 , sLabels :: M.Map String [Inline]
162+ , sToggles :: M.Map String Bool
162163 }
163164 deriving Show
164165
@@ -177,6 +178,7 @@ defaultLaTeXState = LaTeXState{ sOptions = def
177178 , sInTableCell = False
178179 , sLastHeaderNum = HeaderNum []
179180 , sLabels = M.empty
181+ , sToggles = M.empty
180182 }
181183
182184instance PandocMonad m => HasQuoteContext LaTeXState m where
@@ -704,16 +706,16 @@ enquote = do
704706doAcronym :: PandocMonad m => String -> LP m Inlines
705707doAcronym form = do
706708 acro <- braced
707− return . mconcat $ [spanWith ("",[],[("acronym-label", toksToString acro),
708− ("acronym-form", "singular+" ++ form)])
709+ return . mconcat $ [spanWith ("",[],[("acronym-label", toksToString acro),
710+ ("acronym-form", "singular+" ++ form)])
709711 $ str $ toksToString acro]
710712
711713doAcronymPlural :: PandocMonad m => String -> LP m Inlines
712714doAcronymPlural form = do
713715 acro <- braced
714716 plural <- lit "s"
715− return . mconcat $ [spanWith ("",[],[("acronym-label", toksToString acro),
716− ("acronym-form", "plural+" ++ form)]) $ mconcat
717+ return . mconcat $ [spanWith ("",[],[("acronym-label", toksToString acro),
718+ ("acronym-form", "plural+" ++ form)]) $ mconcat
717719 $ [str $ toksToString acro, plural]]
718720
719721doverb :: PandocMonad m => LP m Inlines
@@ -1440,12 +1442,46 @@ inlineCommands = M.fromList $
14401442 , ("xspace", doxspace)
14411443 -- etoolbox
14421444 , ("ifstrequal", ifstrequal)
1445+ , ("newtoggle", braced >>= newToggle)
1446+ , ("toggletrue", braced >>= setToggle True)
1447+ , ("togglefalse", braced >>= setToggle False)
1448+ , ("iftoggle", try $ ifToggle >> inline)
14431449 ]
14441450
1451+newToggle :: (Monoid a, PandocMonad m) => [Tok] -> LP m a
1452+newToggle name = do
1453+ updateState $ \st ->
1454+ st{ sToggles = M.insert (toksToString name) False (sToggles st) }
1455+ return mempty
1456+
1457+setToggle :: (Monoid a, PandocMonad m) => Bool -> [Tok] -> LP m a
1458+setToggle on name = do
1459+ updateState $ \st ->
1460+ st{ sToggles = M.adjust (const on) (toksToString name) (sToggles st) }
1461+ return mempty
1462+
1463+ifToggle :: PandocMonad m => LP m ()
1464+ifToggle = do
1465+ name <- braced
1466+ spaces
1467+ yes <- braced
1468+ spaces
1469+ no <- braced
1470+ toggles <- sToggles <$> getState
1471+ inp <- getInput
1472+ let name' = toksToString name
1473+ case M.lookup name' toggles of
1474+ Just True -> setInput (yes ++ inp)
1475+ Just False -> setInput (no ++ inp)
1476+ Nothing -> do
1477+ pos <- getPosition
1478+ report $ UndefinedToggle name' pos
1479+ return ()
1480+
14451481doTerm :: PandocMonad m => Translations.Term -> LP m Inlines
14461482doTerm term = str <$> translateTerm term
14471483
1448−ifstrequal :: PandocMonad m => LP m Inlines
1484+ifstrequal :: (PandocMonad m, Monoid a) => LP m a
14491485ifstrequal = do
14501486 str1 <- tok
14511487 str2 <- tok
@@ -1964,6 +2000,12 @@ environments = M.fromList
19642000 , ("alignat", mathEnvWith para (Just "aligned") "alignat")
19652001 , ("alignat*", mathEnvWith para (Just "aligned") "alignat*")
19662002 , ("tikzpicture", rawVerbEnv "tikzpicture")
2003+ -- etoolbox
2004+ , ("ifstrequal", ifstrequal)
2005+ , ("newtoggle", braced >>= newToggle)
2006+ , ("toggletrue", braced >>= setToggle True)
2007+ , ("togglefalse", braced >>= setToggle False)
2008+ , ("iftoggle", try $ ifToggle >> block)
19672009 ]
19682010
19692011environment :: PandocMonad m => LP m Blocks
test/command/3853.mdnew file+26
@@ -0,0 +1,26 @@
1+```
2+% pandoc -f latex -t native
3+\newtoggle{ebook}
4+\toggletrue{ebook}
5+\iftoggle{ebook}{
6+ebook
7+}%
8+{
9+not ebook
10+}%
11+more
12+
13+\togglefalse{ebook}
14+\iftoggle{ebook}{%
15+ebook
16+}{
17+not ebook
18+}%
19+more
20+
21+hello \iftoggle{ebook}{ebook}{noebook}
22+^D
23+[Para [Str "ebook",SoftBreak,Str "more"]
24+,Para [Str "not",Space,Str "ebook",SoftBreak,Str "more"]
25+,Para [Str "hello",Space,Str "noebook"]]
26+```