git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: improved support for \hyperlink, \hypertarget.

Closes #2549.

commit1b3431a165309aad3a28a0e8a75755c299561280 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 25 Aug 2017, 22:04 (9 years ago) parentc9be2de
2 files changed+61−6
src/Text/Pandoc/Readers/LaTeX.hs+23−6 test/command/2549.md+38
src/Text/Pandoc/Readers/LaTeX.hs+23−6
@@ -1391,11 +1391,8 @@ inlineCommands = M.fromList $
13911391 <|> citation "citeauthor" AuthorInText False)
13921392 , ("nocite", mempty <$ (citation "nocite" NormalCitation False >>=
13931393 addMeta "nocite"))
1394− -- hyperlink: for now, we just preserve contents.
1395− -- we might add the actual links, but we need to avoid clashes
1396− -- with ids produced by label.
1397− , ("hypertarget", braced >> tok)
1398− , ("hyperlink", braced >> tok)
1394+ , ("hyperlink", hyperlink)
1395+ , ("hypertarget", hypertargetInline)
13991396 -- glossaries package
14001397 , ("gls", doAcronym "short")
14011398 , ("Gls", doAcronym "short")
@@ -1450,6 +1447,26 @@ inlineCommands = M.fromList $
14501447 , ("Rn", romanNumeralLower)
14511448 ]
14521449
1450+hyperlink :: PandocMonad m => LP m Inlines
1451+hyperlink = try $ do
1452+ src <- toksToString <$> braced
1453+ lab <- tok
1454+ return $ link ('#':src) "" lab
1455+
1456+hypertargetBlock :: PandocMonad m => LP m Blocks
1457+hypertargetBlock = try $ do
1458+ ref <- toksToString <$> braced
1459+ bs <- grouped block
1460+ case toList bs of
1461+ [Header 1 (ident,_,_) _] | ident == ref -> return bs
1462+ _ -> return $ divWith (ref, [], []) bs
1463+
1464+hypertargetInline :: PandocMonad m => LP m Inlines
1465+hypertargetInline = try $ do
1466+ ref <- toksToString <$> braced
1467+ ils <- grouped inline
1468+ return $ spanWith (ref, [], []) ils
1469+
14531470romanNumeralUpper :: (PandocMonad m) => LP m Inlines
14541471romanNumeralUpper =
14551472 str . toRomanNumeral <$> romanNumeralArg
@@ -1972,7 +1989,7 @@ blockCommands = M.fromList $
19721989 , ("setdefaultlanguage", setDefaultLanguage)
19731990 , ("setmainlanguage", setDefaultLanguage)
19741991 -- hyperlink
1975− , ("hypertarget", try $ braced >> grouped block)
1992+ , ("hypertarget", hypertargetBlock)
19761993 -- LaTeX colors
19771994 , ("textcolor", coloredBlock "color")
19781995 , ("colorbox", coloredBlock "background-color")
test/command/2549.mdnew file+38
@@ -0,0 +1,38 @@
1+```
2+% pandoc -f latex -t native
3+\hypertarget{foo}{%
4+\section{A section}\label{foo}
5+}
6+^D
7+[Header 1 ("foo",[],[]) [Str "A",Space,Str "section"]]
8+```
9+
10+```
11+% pandoc -f latex -t native
12+\hypertarget{bar}{%
13+\section{A section}\label{foo}
14+}
15+^D
16+[Div ("bar",[],[])
17+ [Header 1 ("foo",[],[]) [Str "A",Space,Str "section"]]]
18+```
19+
20+```
21+% pandoc -f latex -t native
22+Bar \hypertarget{foo}{Foo}
23+^D
24+[Para [Str "Bar",Space,Span ("foo",[],[]) [Str "Foo"]]]
25+```
26+
27+```
28+% pandoc -f latex -t native
29+\hypertarget{foo}{%
30+\begin{verbatim}
31+bar
32+\end{verbatim}
33+}
34+^D
35+[Div ("foo",[],[])
36+ [CodeBlock ("",[],[]) "bar"]]
37+```
38+