From 876859f9e95ba2dd2de2f197caf53184700ec335 Mon Sep 17 00:00:00 2001 From: Jan Tojnar <jtojnar@gmail.com> Date: Sun, 6 Feb 2022 23:05:20 +0100 Subject: [PATCH] Docbook writer: Interpret links without contents as cross-references (#7360) Links without text contents are converted to `<xref>` elements. DocBook processors will generate appropriate cross-reference text when presented with an xref element. --- src/Text/Pandoc/Writers/Docbook.hs | 4 +++- test/Tests/Writers/Docbook.hs | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Text/Pandoc/Writers/Docbook.hs b/src/Text/Pandoc/Writers/Docbook.hs index 2ab730a0c..1697b201c 100644 --- a/src/Text/Pandoc/Writers/Docbook.hs +++ b/src/Text/Pandoc/Writers/Docbook.hs @@ -429,7 +429,9 @@ inlineToDocbook opts (Link attr txt (src, _)) | otherwise = do version <- ask (if "#" `T.isPrefixOf` src - then inTags False "link" $ ("linkend", writerIdentifierPrefix opts <> T.drop 1 src) : idAndRole attr + then let tag = if null txt then "xref" else "link" + in inTags False tag $ + ("linkend", writerIdentifierPrefix opts <> T.drop 1 src) : idAndRole attr else if version == DocBook5 then inTags False "link" $ ("xlink:href", src) : idAndRole attr else inTags False "ulink" $ ("url", src) : idAndRole attr ) diff --git a/test/Tests/Writers/Docbook.hs b/test/Tests/Writers/Docbook.hs index f517f803a..a95140ac5 100644 --- a/test/Tests/Writers/Docbook.hs +++ b/test/Tests/Writers/Docbook.hs @@ -32,9 +32,11 @@ which is in turn shorthand for -} infix 4 =: -(=:) :: (ToString a, ToPandoc a) - => String -> (a, String) -> TestTree +(=:), testDb4, testDb5 :: (ToString a, ToPandoc a) + => String -> (a, String) -> TestTree (=:) = test docbook +testDb4 = test docbook +testDb5 = test docbook5 lineblock :: Blocks lineblock = para ("some text" <> linebreak <> @@ -47,7 +49,19 @@ lineblock_out = [ "<literallayout>some text" ] tests :: [TestTree] -tests = [ testGroup "line blocks" +tests = [ testGroup "inline elements" + [ testGroup "links" + [ testDb4 "db4 external link" $ link "https://example.com" "" "Hello" + =?> "<ulink url=\"https://example.com\">Hello</ulink>" + , testDb5 "db5 external link" $ link "https://example.com" "" "Hello" + =?> "<link xlink:href=\"https://example.com\">Hello</link>" + , testDb5 "anchor" $ link "#foo" "" "Hello" + =?> "<link linkend=\"foo\">Hello</link>" + , testDb5 "automatic anchor" $ link "#foo" "" "" + =?> "<xref linkend=\"foo\"></xref>" + ] + ] + , testGroup "line blocks" [ "none" =: para "This is a test" =?> unlines [ "<para>"