aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-08-25 22:04:57 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2017-08-25 22:04:57 -0700
commit1b3431a165309aad3a28a0e8a75755c299561280 (patch)
treefb9588dd9bb811d0c6c00368061fdc9d9c94d701
parentc9be2de5c1d6856531c497a1a0f919b030a5cb95 (diff)
downloadpandoc-1b3431a165309aad3a28a0e8a75755c299561280.tar.gz
LaTeX reader: improved support for \hyperlink, \hypertarget.
Closes #2549.
-rw-r--r--src/Text/Pandoc/Readers/LaTeX.hs29
-rw-r--r--test/command/2549.md38
2 files changed, 61 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index ac471bdb1..06e112cef 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -1391,11 +1391,8 @@ inlineCommands = M.fromList $
<|> citation "citeauthor" AuthorInText False)
, ("nocite", mempty <$ (citation "nocite" NormalCitation False >>=
addMeta "nocite"))
- -- hyperlink: for now, we just preserve contents.
- -- we might add the actual links, but we need to avoid clashes
- -- with ids produced by label.
- , ("hypertarget", braced >> tok)
- , ("hyperlink", braced >> tok)
+ , ("hyperlink", hyperlink)
+ , ("hypertarget", hypertargetInline)
-- glossaries package
, ("gls", doAcronym "short")
, ("Gls", doAcronym "short")
@@ -1450,6 +1447,26 @@ inlineCommands = M.fromList $
, ("Rn", romanNumeralLower)
]
+hyperlink :: PandocMonad m => LP m Inlines
+hyperlink = try $ do
+ src <- toksToString <$> braced
+ lab <- tok
+ return $ link ('#':src) "" lab
+
+hypertargetBlock :: PandocMonad m => LP m Blocks
+hypertargetBlock = try $ do
+ ref <- toksToString <$> braced
+ bs <- grouped block
+ case toList bs of
+ [Header 1 (ident,_,_) _] | ident == ref -> return bs
+ _ -> return $ divWith (ref, [], []) bs
+
+hypertargetInline :: PandocMonad m => LP m Inlines
+hypertargetInline = try $ do
+ ref <- toksToString <$> braced
+ ils <- grouped inline
+ return $ spanWith (ref, [], []) ils
+
romanNumeralUpper :: (PandocMonad m) => LP m Inlines
romanNumeralUpper =
str . toRomanNumeral <$> romanNumeralArg
@@ -1972,7 +1989,7 @@ blockCommands = M.fromList $
, ("setdefaultlanguage", setDefaultLanguage)
, ("setmainlanguage", setDefaultLanguage)
-- hyperlink
- , ("hypertarget", try $ braced >> grouped block)
+ , ("hypertarget", hypertargetBlock)
-- LaTeX colors
, ("textcolor", coloredBlock "color")
, ("colorbox", coloredBlock "background-color")
diff --git a/test/command/2549.md b/test/command/2549.md
new file mode 100644
index 000000000..8f4aea852
--- /dev/null
+++ b/test/command/2549.md
@@ -0,0 +1,38 @@
+```
+% pandoc -f latex -t native
+\hypertarget{foo}{%
+\section{A section}\label{foo}
+}
+^D
+[Header 1 ("foo",[],[]) [Str "A",Space,Str "section"]]
+```
+
+```
+% pandoc -f latex -t native
+\hypertarget{bar}{%
+\section{A section}\label{foo}
+}
+^D
+[Div ("bar",[],[])
+ [Header 1 ("foo",[],[]) [Str "A",Space,Str "section"]]]
+```
+
+```
+% pandoc -f latex -t native
+Bar \hypertarget{foo}{Foo}
+^D
+[Para [Str "Bar",Space,Span ("foo",[],[]) [Str "Foo"]]]
+```
+
+```
+% pandoc -f latex -t native
+\hypertarget{foo}{%
+\begin{verbatim}
+bar
+\end{verbatim}
+}
+^D
+[Div ("foo",[],[])
+ [CodeBlock ("",[],[]) "bar"]]
+```
+