git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: implement \newtoggle, \iftoggle, \toggletrue|false
bfbdfa6John MacFarlane
src/Text/Pandoc/Logging.hs+9
@@ -75,6 +75,7 @@ data LogMessage =
75 | DuplicateIdentifier String SourcePos
75 | DuplicateIdentifier String SourcePos
76 | ReferenceNotFound String SourcePos
76 | ReferenceNotFound String SourcePos
77 | CircularReference String SourcePos
77 | CircularReference String SourcePos
78+ | UndefinedToggle String SourcePos
78 | ParsingUnescaped String SourcePos
79 | ParsingUnescaped String SourcePos
79 | CouldNotLoadIncludeFile String SourcePos
80 | CouldNotLoadIncludeFile String SourcePos
80 | MacroAlreadyDefined String SourcePos
81 | MacroAlreadyDefined String SourcePos
@@ -144,6 +145,11 @@ instance ToJSON LogMessage where
144 "source" .= Text.pack (sourceName pos),
145 "source" .= Text.pack (sourceName pos),
145 "line" .= toJSON (sourceLine pos),
146 "line" .= toJSON (sourceLine pos),
146 "column" .= toJSON (sourceColumn pos)]
147 "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)]
147 ParsingUnescaped s pos ->
153 ParsingUnescaped s pos ->
148 ["contents" .= Text.pack s,
154 ["contents" .= Text.pack s,
149 "source" .= Text.pack (sourceName pos),
155 "source" .= Text.pack (sourceName pos),
@@ -238,6 +244,8 @@ showLogMessage msg =
238 "Reference not found for '" ++ s ++ "' at " ++ showPos pos
244 "Reference not found for '" ++ s ++ "' at " ++ showPos pos
239 CircularReference s pos ->
245 CircularReference s pos ->
240 "Circular reference '" ++ s ++ "' at " ++ showPos pos
246 "Circular reference '" ++ s ++ "' at " ++ showPos pos
247+ UndefinedToggle s pos ->
248+ "Undefined toggle '" ++ s ++ "' at " ++ showPos pos
241 ParsingUnescaped s pos ->
249 ParsingUnescaped s pos ->
242 "Parsing unescaped '" ++ s ++ "' at " ++ showPos pos
250 "Parsing unescaped '" ++ s ++ "' at " ++ showPos pos
243 CouldNotLoadIncludeFile fp pos ->
251 CouldNotLoadIncludeFile fp pos ->
@@ -306,6 +314,7 @@ messageVerbosity msg =
306 DuplicateIdentifier{} -> WARNING
314 DuplicateIdentifier{} -> WARNING
307 ReferenceNotFound{} -> WARNING
315 ReferenceNotFound{} -> WARNING
308 CircularReference{} -> WARNING
316 CircularReference{} -> WARNING
317+ UndefinedToggle{} -> WARNING
309 CouldNotLoadIncludeFile{} -> WARNING
318 CouldNotLoadIncludeFile{} -> WARNING
310 MacroAlreadyDefined{} -> WARNING
319 MacroAlreadyDefined{} -> WARNING
311 ParsingUnescaped{} -> INFO
320 ParsingUnescaped{} -> INFO
src/Text/Pandoc/Readers/LaTeX.hs+47−5
@@ -159,6 +159,7 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
159 , sInTableCell :: Bool
159 , sInTableCell :: Bool
160 , sLastHeaderNum :: HeaderNum
160 , sLastHeaderNum :: HeaderNum
161 , sLabels :: M.Map String [Inline]
161 , sLabels :: M.Map String [Inline]
162+ , sToggles :: M.Map String Bool
162 }
163 }
163 deriving Show
164 deriving Show
164
165
@@ -177,6 +178,7 @@ defaultLaTeXState = LaTeXState{ sOptions = def
177 , sInTableCell = False
178 , sInTableCell = False
178 , sLastHeaderNum = HeaderNum []
179 , sLastHeaderNum = HeaderNum []
179 , sLabels = M.empty
180 , sLabels = M.empty
181+ , sToggles = M.empty
180 }
182 }
181
183
182instance PandocMonad m => HasQuoteContext LaTeXState m where
184instance PandocMonad m => HasQuoteContext LaTeXState m where
@@ -704,16 +706,16 @@ enquote = do
704doAcronym :: PandocMonad m => String -> LP m Inlines
706doAcronym :: PandocMonad m => String -> LP m Inlines
705doAcronym form = do
707doAcronym form = do
706 acro <- braced
708 acro <- braced
707− return . mconcat $ [spanWith ("",[],[("acronym-label", toksToString acro),
709+ return . mconcat $ [spanWith ("",[],[("acronym-label", toksToString acro),
708− ("acronym-form", "singular+" ++ form)])
710+ ("acronym-form", "singular+" ++ form)])
709 $ str $ toksToString acro]
711 $ str $ toksToString acro]
710
712
711doAcronymPlural :: PandocMonad m => String -> LP m Inlines
713doAcronymPlural :: PandocMonad m => String -> LP m Inlines
712doAcronymPlural form = do
714doAcronymPlural form = do
713 acro <- braced
715 acro <- braced
714 plural <- lit "s"
716 plural <- lit "s"
715− return . mconcat $ [spanWith ("",[],[("acronym-label", toksToString acro),
717+ return . mconcat $ [spanWith ("",[],[("acronym-label", toksToString acro),
716− ("acronym-form", "plural+" ++ form)]) $ mconcat
718+ ("acronym-form", "plural+" ++ form)]) $ mconcat
717 $ [str $ toksToString acro, plural]]
719 $ [str $ toksToString acro, plural]]
718
720
719doverb :: PandocMonad m => LP m Inlines
721doverb :: PandocMonad m => LP m Inlines
@@ -1440,12 +1442,46 @@ inlineCommands = M.fromList $
1440 , ("xspace", doxspace)
1442 , ("xspace", doxspace)
1441 -- etoolbox
1443 -- etoolbox
1442 , ("ifstrequal", ifstrequal)
1444 , ("ifstrequal", ifstrequal)
1445+ , ("newtoggle", braced >>= newToggle)
1446+ , ("toggletrue", braced >>= setToggle True)
1447+ , ("togglefalse", braced >>= setToggle False)
1448+ , ("iftoggle", try $ ifToggle >> inline)
1443 ]
1449 ]
1444
1450
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+
1445doTerm :: PandocMonad m => Translations.Term -> LP m Inlines
1481doTerm :: PandocMonad m => Translations.Term -> LP m Inlines
1446doTerm term = str <$> translateTerm term
1482doTerm term = str <$> translateTerm term
1447
1483
1448−ifstrequal :: PandocMonad m => LP m Inlines
1484+ifstrequal :: (PandocMonad m, Monoid a) => LP m a
1449ifstrequal = do
1485ifstrequal = do
1450 str1 <- tok
1486 str1 <- tok
1451 str2 <- tok
1487 str2 <- tok
@@ -1964,6 +2000,12 @@ environments = M.fromList
1964 , ("alignat", mathEnvWith para (Just "aligned") "alignat")
2000 , ("alignat", mathEnvWith para (Just "aligned") "alignat")
1965 , ("alignat*", mathEnvWith para (Just "aligned") "alignat*")
2001 , ("alignat*", mathEnvWith para (Just "aligned") "alignat*")
1966 , ("tikzpicture", rawVerbEnv "tikzpicture")
2002 , ("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)
1967 ]
2009 ]
1968
2010
1969environment :: PandocMonad m => LP m Blocks
2011environment :: 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+```