mirror of
https://github.com/jgm/pandoc.git
synced 2026-08-28 17:20:47 +08:00
LaTeX reader: handle ## macro arguments properly.
These turn into regular `#` arguments when expanded. Closes #8243.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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>
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user