summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2022-08-24 09:35:25 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2022-08-24 09:36:13 -0700
commit488ad80a20e71db187291ec32fd96c85814bf848 (patch)
treebb69c91775bc6ac37538e63cfaaea727fecaa86c
parent373a3255a0644dd6bf6ac978fa5e70be63d4ce2a (diff)
LaTeX reader: handle `##` macro arguments properly.
These turn into regular `#` arguments when expanded. Closes #8243.
-rw-r--r--src/Text/Pandoc/Readers/LaTeX/Macro.hs2
-rw-r--r--src/Text/Pandoc/Readers/LaTeX/Parsing.hs28
-rw-r--r--src/Text/Pandoc/Readers/LaTeX/Types.hs2
-rw-r--r--test/command/8243.md23
4 files changed, 45 insertions, 10 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX/Macro.hs b/src/Text/Pandoc/Readers/LaTeX/Macro.hs
index 6706fae14..4756c5381 100644
--- a/src/Text/Pandoc/Readers/LaTeX/Macro.hs
+++ b/src/Text/Pandoc/Readers/LaTeX/Macro.hs
@@ -216,7 +216,7 @@ newenvironment = do
let result = (name,
Macro GroupScope ExpandWhenUsed argspecs optarg
(bg:startcontents),
- Macro GroupScope ExpandWhenUsed [] Nothing
+ Macro GroupScope ExpandWhenUsed argspecs optarg
(endcontents ++ [eg]))
(do lookupMacro name
case mtype of
diff --git a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
index 8d7181689..7528e8623 100644
--- a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
+++ b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
@@ -414,14 +414,24 @@ tokenize = totoks
Tok pos (CtrlSeq (T.singleton d)) (T.pack [c,d])
: totoks (incSourceColumn pos 2) rest'
| c == '#' ->
- let (t1, t2) = T.span (\d -> d >= '0' && d <= '9') rest
- in case safeRead t1 of
- Just i ->
- Tok pos (Arg i) ("#" <> t1)
- : totoks (incSourceColumn pos (1 + T.length t1)) t2
- Nothing ->
- Tok pos Symbol "#"
- : totoks (incSourceColumn pos 1) t2
+ case T.uncons rest of
+ Just ('#', t3) ->
+ let (t1, t2) = T.span (\d -> d >= '0' && d <= '9') t3
+ in case safeRead t1 of
+ Just i ->
+ Tok pos (DeferredArg i) ("##" <> t1)
+ : totoks (incSourceColumn pos (2 + T.length t1)) t2
+ Nothing -> Tok pos Symbol "#"
+ : Tok (incSourceColumn pos 1) Symbol "#"
+ : totoks (incSourceColumn pos 1) t2
+ _ ->
+ let (t1, t2) = T.span (\d -> d >= '0' && d <= '9') rest
+ in case safeRead t1 of
+ Just i ->
+ Tok pos (Arg i) ("#" <> t1)
+ : totoks (incSourceColumn pos (1 + T.length t1)) t2
+ Nothing -> Tok pos Symbol "#"
+ : totoks (incSourceColumn pos 1) rest
| c == '^' ->
case T.uncons rest of
Just ('^', rest') ->
@@ -571,6 +581,8 @@ doMacros' n inp =
x <- try $ spaces >> bracedOrToken
getargs (M.insert i x argmap) rest
+ addTok False _args spos (Tok _ (DeferredArg i) txt) acc =
+ Tok spos (Arg i) txt : acc
addTok False args spos (Tok _ (Arg i) _) acc =
case M.lookup i args of
Nothing -> mzero
diff --git a/src/Text/Pandoc/Readers/LaTeX/Types.hs b/src/Text/Pandoc/Readers/LaTeX/Types.hs
index 7157c5dee..ce85fc43a 100644
--- a/src/Text/Pandoc/Readers/LaTeX/Types.hs
+++ b/src/Text/Pandoc/Readers/LaTeX/Types.hs
@@ -25,7 +25,7 @@ import Text.Pandoc.Sources
import Data.List (groupBy)
data TokType = CtrlSeq Text | Spaces | Newline | Symbol | Word | Comment |
- Esc1 | Esc2 | Arg Int
+ Esc1 | Esc2 | Arg Int | DeferredArg Int
deriving (Eq, Ord, Show)
data Tok = Tok SourcePos TokType Text
diff --git a/test/command/8243.md b/test/command/8243.md
new file mode 100644
index 000000000..87c800973
--- /dev/null
+++ b/test/command/8243.md
@@ -0,0 +1,23 @@
+```
+% pandoc -f latex
+\newenvironment{topics}{
+\newcommand{\topic}[2]{ \item{##1 / ##2} }
+Topics:
+\begin{itemize}
+}
+{
+\end{itemize}
+}
+
+\begin{topics}
+\topic{Foo}{Bar}
+\topic{Baz}{Bim}
+\end{topics}
+^D
+<p>Topics:</p>
+<ul>
+<li><p><span>Foo / Bar</span></p></li>
+<li><p><span>Baz / Bim</span></p></li>
+</ul>
+```
+