From fd4831dd03c8797af4bcb180dac775a08b7bf3d6 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Tue, 6 Sep 2016 10:09:52 +0200
Subject: [PATCH] DocBook writer: include an anchor element when a div or span
 has an id.

This closes #3102.  Note that DocBook does not have a class attribute,
but at least this provides an anchor for internal links.
---
 src/Text/Pandoc/Writers/Docbook.hs | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/Text/Pandoc/Writers/Docbook.hs b/src/Text/Pandoc/Writers/Docbook.hs
index 9b1c70166..8bb0810e4 100644
--- a/src/Text/Pandoc/Writers/Docbook.hs
+++ b/src/Text/Pandoc/Writers/Docbook.hs
@@ -176,7 +176,11 @@ blockToDocbook opts (Div (ident,_,_) [Para lst]) =
      then flush $ nowrap $ inTags False "literallayout" attribs
                          $ inlinesToDocbook opts lst
      else inTags True "para" attribs $ inlinesToDocbook opts lst
-blockToDocbook opts (Div _ bs) = blocksToDocbook opts $ map plainToPara bs
+blockToDocbook opts (Div (ident,_,_) bs) =
+  (if null ident
+      then mempty
+      else selfClosingTag "anchor" [("id", ident)]) $$
+  blocksToDocbook opts (map plainToPara bs)
 blockToDocbook _ (Header _ _ _) = empty -- should not occur after hierarchicalize
 blockToDocbook opts (Plain lst) = inlinesToDocbook opts lst
 -- title beginning with fig: indicates that the image is a figure
@@ -313,7 +317,10 @@ inlineToDocbook opts (Quoted _ lst) =
   inTagsSimple "quote" $ inlinesToDocbook opts lst
 inlineToDocbook opts (Cite _ lst) =
   inlinesToDocbook opts lst
-inlineToDocbook opts (Span _ ils) =
+inlineToDocbook opts (Span (ident,_,_) ils) =
+  (if null ident
+      then mempty
+      else selfClosingTag "anchor" [("id", ident)]) <>
   inlinesToDocbook opts ils
 inlineToDocbook _ (Code _ str) =
   inTagsSimple "literal" $ text (escapeStringForXML str)