LaTeX reader: handle ## macro arguments properly.

These turn into regular `#` arguments when expanded.

Closes #8243.
This commit is contained in:
John MacFarlane
2022-08-24 09:35:25 -07:00
parent 373a3255a0
commit 488ad80a20
4 changed files with 45 additions and 10 deletions
+1 -1
View File
@@ -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
+20 -8
View File
@@ -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
+1 -1
View File
@@ -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
+23
View File
@@ -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>
```