Skip to content

Commit

Permalink
LaTeX reader: support \expandafter.
Browse files Browse the repository at this point in the history
Closes #3983.
  • Loading branch information
jgm committed Oct 19, 2017
1 parent e941ba0 commit 28bb5d6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/Text/Pandoc/Readers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,14 @@ totoks pos t =
| otherwise ->
Tok pos Symbol (T.singleton c) : totoks (incSourceColumn pos 1) rest

where isSpaceOrTab ' ' = True
isSpaceOrTab '\t' = True
isSpaceOrTab _ = False
isLetterOrAt '@' = True
isLetterOrAt c = isLetter c
isSpaceOrTab :: Char -> Bool
isSpaceOrTab ' ' = True
isSpaceOrTab '\t' = True
isSpaceOrTab _ = False

isLetterOrAt :: Char -> Bool
isLetterOrAt '@' = True
isLetterOrAt c = isLetter c

isLowerHex :: Char -> Bool
isLowerHex x = x >= '0' && x <= '9' || x >= 'a' && x <= 'f'
Expand Down Expand Up @@ -411,10 +414,19 @@ doMacros n = do
Tok spos (CtrlSeq "end") _ : Tok _ Symbol "{" :
Tok _ Word name : Tok _ Symbol "}" : ts
-> handleMacros spos ("end" <> name) ts
Tok _ (CtrlSeq "expandafter") _ : t : ts
-> do setInput ts
doMacros n
getInput >>= setInput . combineTok t
Tok spos (CtrlSeq name) _ : ts
-> handleMacros spos name ts
_ -> return ()
where handleMacros spos name ts = do
where combineTok (Tok spos (CtrlSeq name) x) (Tok _ Word w : ts)
| T.all isLetterOrAt w =
Tok spos (CtrlSeq (name <> w)) (x1 <> w <> x2) : ts
where (x1, x2) = T.break isSpaceOrTab x
combineTok t ts = t:ts
handleMacros spos name ts = do
macros <- sMacros <$> getState
case M.lookup name macros of
Nothing -> return ()
Expand All @@ -439,6 +451,7 @@ doMacros n = do
else doMacros (n + 1)
ExpandWhenDefined -> return ()


setpos :: SourcePos -> Tok -> Tok
setpos spos (Tok _ tt txt) = Tok spos tt txt

Expand Down
29 changes: 29 additions & 0 deletions test/command/3983.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
```
pandoc -f latex -t native
\def\filename@area{foo:bar:baz}
\makeatletter
\graphicspath\expandafter{\expandafter{\filename@area}}%
\makeatother
^D
[RawBlock (Format "latex") "\\makeatletter"
,RawBlock (Format "latex") "\\makeatother"]
```

```
pandoc -f latex -t native
\makeatletter
\newcommand\urlfootnote@[1]{\footnote{\url@{#1}}}
\DeclareRobustCommand{\urlfootnote}{\hyper@normalise\urlfootnote@}
\makeatother
^D
[RawBlock (Format "latex") "\\makeatletter"
,RawBlock (Format "latex") "\\makeatother"]
```

```
pandoc -f latex -t native
\def\foo{bar}
\expandafter\bam\foo
^D
[RawBlock (Format "latex") "\\bambar"]
```

0 comments on commit 28bb5d6

Please sign in to comment.