git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: support `\expandafter`.

Closes #3983.

commit28bb5d610dc8c96a014f610d53b937ea7b9d977e authorJohn MacFarlane <jgm@berkeley.edu> dateThu 19 Oct 2017, 12:52 (9 years ago) parente941ba0
2 files changed+48−6
src/Text/Pandoc/Readers/LaTeX.hs+19−6 test/command/3983.md+29
src/Text/Pandoc/Readers/LaTeX.hs+19−6
@@ -372,11 +372,14 @@ totoks pos t =
372372 | otherwise ->
373373 Tok pos Symbol (T.singleton c) : totoks (incSourceColumn pos 1) rest
374374
375− where isSpaceOrTab ' ' = True
376− isSpaceOrTab '\t' = True
377− isSpaceOrTab _ = False
378− isLetterOrAt '@' = True
379− isLetterOrAt c = isLetter c
375+isSpaceOrTab :: Char -> Bool
376+isSpaceOrTab ' ' = True
377+isSpaceOrTab '\t' = True
378+isSpaceOrTab _ = False
379+
380+isLetterOrAt :: Char -> Bool
381+isLetterOrAt '@' = True
382+isLetterOrAt c = isLetter c
380383
381384isLowerHex :: Char -> Bool
382385isLowerHex x = x >= '0' && x <= '9' || x >= 'a' && x <= 'f'
@@ -411,10 +414,19 @@ doMacros n = do
411414 Tok spos (CtrlSeq "end") _ : Tok _ Symbol "{" :
412415 Tok _ Word name : Tok _ Symbol "}" : ts
413416 -> handleMacros spos ("end" <> name) ts
417+ Tok _ (CtrlSeq "expandafter") _ : t : ts
418+ -> do setInput ts
419+ doMacros n
420+ getInput >>= setInput . combineTok t
414421 Tok spos (CtrlSeq name) _ : ts
415422 -> handleMacros spos name ts
416423 _ -> return ()
417− where handleMacros spos name ts = do
424+ where combineTok (Tok spos (CtrlSeq name) x) (Tok _ Word w : ts)
425+ | T.all isLetterOrAt w =
426+ Tok spos (CtrlSeq (name <> w)) (x1 <> w <> x2) : ts
427+ where (x1, x2) = T.break isSpaceOrTab x
428+ combineTok t ts = t:ts
429+ handleMacros spos name ts = do
418430 macros <- sMacros <$> getState
419431 case M.lookup name macros of
420432 Nothing -> return ()
@@ -439,6 +451,7 @@ doMacros n = do
439451 else doMacros (n + 1)
440452 ExpandWhenDefined -> return ()
441453
454+
442455setpos :: SourcePos -> Tok -> Tok
443456setpos spos (Tok _ tt txt) = Tok spos tt txt
444457
test/command/3983.mdnew file+29
@@ -0,0 +1,29 @@
1+```
2+pandoc -f latex -t native
3+\def\filename@area{foo:bar:baz}
4+\makeatletter
5+\graphicspath\expandafter{\expandafter{\filename@area}}%
6+\makeatother
7+^D
8+[RawBlock (Format "latex") "\\makeatletter"
9+,RawBlock (Format "latex") "\\makeatother"]
10+```
11+
12+```
13+pandoc -f latex -t native
14+\makeatletter
15+ \newcommand\urlfootnote@[1]{\footnote{\url@{#1}}}
16+ \DeclareRobustCommand{\urlfootnote}{\hyper@normalise\urlfootnote@}
17+\makeatother
18+^D
19+[RawBlock (Format "latex") "\\makeatletter"
20+,RawBlock (Format "latex") "\\makeatother"]
21+```
22+
23+```
24+pandoc -f latex -t native
25+\def\foo{bar}
26+\expandafter\bam\foo
27+^D
28+[RawBlock (Format "latex") "\\bambar"]
29+```