git.iko.soy/typeable/pandoc
on master help sign in
Added proper support for DocBook 'xref' elements

'xref' is used to create cross references to other parts of the document. It is an empty element - the cross reference text depends on various attributes. Quoting 'DocBook: The Definitive Guide': 1. If the endterm attribute is specified on xref, the content of the element pointed to by endterm will be used as the text of the cross-reference. 2. Otherwise, if the object pointed to has a specified XRefLabel, the content of that attribute will be used as the cross-reference text.

commit35f12b5095799e41b563f47a7923a1d01015c71c authorFrerich Raabe <raabe@froglogic.com> dateWed 23 Sep 2015, 22:53 (11 years ago) parentf653814
5 files changed+120−2
pandoc.cabal+1 src/Text/Pandoc/Readers/DocBook.hs+18−2 tests/Tests/Old.hs+2 tests/docbook-xref.docbook+70 tests/docbook-xref.native+29
pandoc.cabal+1
@@ -125,6 +125,7 @@ Extra-Source-Files:
125125 tests/bodybg.gif
126126 tests/*.native
127127 tests/docbook-reader.docbook
128+ tests/docbook-xref.docbook
128129 tests/html-reader.html
129130 tests/opml-reader.opml
130131 tests/haddock-reader.haddock
src/Text/Pandoc/Readers/DocBook.hs+18−2
@@ -18,6 +18,7 @@ import Text.TeXMath (readMathML, writeTeX)
1818import Text.Pandoc.Error (PandocError)
1919import Text.Pandoc.Compat.Except
2020import Data.Default
21+import Data.Foldable (asum)
2122
2223{-
2324
@@ -498,7 +499,7 @@ List of all DocBook tags, with [x] indicating implemented,
498499[x] warning - An admonition set off from the text
499500[x] wordasword - A word meant specifically as a word and not representing
500501 anything else
501−[ ] xref - A cross reference to another part of the document
502+[x] xref - A cross reference to another part of the document
502503[ ] year - The year of publication of a document
503504[x] ?asciidoc-br? - line break from asciidoc docbook output
504505-}
@@ -952,7 +953,13 @@ parseInline (Elem e) =
952953 "keycombo" -> keycombo <$> (mapM parseInline $ elContent e)
953954 "menuchoice" -> menuchoice <$> (mapM parseInline $
954955 filter isGuiMenu $ elContent e)
955− "xref" -> return $ str "?" -- so at least you know something is there
956+ "xref" -> do
957+ content <- dbContent <$> get
958+ let linkend = attrValue "linkend" e
959+ let title = case attrValue "endterm" e of
960+ "" -> maybe "???" xrefTitleByElem (findElementById linkend content)
961+ endterm -> maybe "???" strContent (findElementById endterm content)
962+ return $ link ('#' : linkend) "" (singleton (Str title))
956963 "email" -> return $ link ("mailto:" ++ strContent e) ""
957964 $ str $ strContent e
958965 "uri" -> return $ link (strContent e) "" $ str $ strContent e
@@ -1013,3 +1020,12 @@ parseInline (Elem e) =
10131020 isGuiMenu (Elem x) = named "guimenu" x || named "guisubmenu" x ||
10141021 named "guimenuitem" x
10151022 isGuiMenu _ = False
1023+
1024+ findElementById idString content
1025+ = asum [filterElement (\x -> attrValue "id" x == idString) el | Elem el <- content]
1026+
1027+ xrefTitleByElem el
1028+ | null xrefLabel = "???"
1029+ | otherwise = xrefLabel
1030+ where
1031+ xrefLabel = attrValue "xreflabel" el
tests/Tests/Old.hs+2
@@ -105,6 +105,8 @@ tests = [ testGroup "markdown"
105105 [ testGroup "writer" $ writerTests "docbook"
106106 , test "reader" ["-r", "docbook", "-w", "native", "-s"]
107107 "docbook-reader.docbook" "docbook-reader.native"
108+ , test "reader" ["-r", "docbook", "-w", "native", "-s"]
109+ "docbook-xref.docbook" "docbook-xref.native"
108110 ]
109111 , testGroup "native"
110112 [ testGroup "writer" $ writerTests "native"
tests/docbook-xref.docbooknew file+70
@@ -0,0 +1,70 @@
1+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
2+ "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
3+<book><title>An Example Book</title>
4+<chapter id="ch01"><title>XRef Samples</title>
5+<para>
6+This paragraph demonstrates several features of
7+<sgmltag>XRef</sgmltag>.
8+</para>
9+<itemizedlist>
10+<listitem><para>A straight link generates the
11+cross-reference text: <xref linkend="ch02"/>.
12+</para></listitem>
13+<listitem><para>A link to an element with an
14+<sgmltag class="attribute">XRefLabel</sgmltag>:
15+<xref linkend="ch03"/>.
16+</para></listitem>
17+<listitem><para>A link with an
18+<sgmltag class="attribute">EndTerm</sgmltag>:
19+<xref linkend="ch04" endterm="ch04short"/>.
20+</para></listitem>
21+<listitem><para>A link to an
22+<sgmltag>cmdsynopsis</sgmltag> element: <xref linkend="cmd01"/>.
23+</para></listitem>
24+<listitem><para>A link to an
25+<sgmltag>funcsynopsis</sgmltag> element: <xref linkend="func01"/>.
26+</para></listitem>
27+</itemizedlist>
28+</chapter>
29+
30+<chapter id="ch02">
31+ <title>The Second Chapter</title>
32+ <para>Some content here</para>
33+</chapter>
34+
35+<chapter id="ch03" xreflabel="Chapter the Third">
36+ <title>The Third Chapter</title>
37+ <para>Some content here</para>
38+</chapter>
39+
40+<chapter id="ch04">
41+ <title>The Fourth Chapter</title>
42+ <titleabbrev id="ch04short">Chapter 4</titleabbrev>
43+ <para>Some content here</para>
44+
45+<cmdsynopsis id="cmd01">
46+ <command>chgrp</command>
47+ <arg>-R
48+ <group>
49+ <arg>-H</arg>
50+ <arg>-L</arg>
51+ <arg>-P</arg>
52+ </group>
53+ </arg>
54+ <arg>-f</arg>
55+ <arg choice='plain'><replaceable>group</replaceable></arg>
56+ <arg rep='repeat' choice='plain'><replaceable>file</replaceable></arg>
57+</cmdsynopsis>
58+
59+
60+<funcsynopsis id="func01">
61+<funcprototype>
62+<funcdef>int <function>max</function></funcdef>
63+<paramdef>int <parameter>int1</parameter></paramdef>
64+<paramdef>int <parameter>int2</parameter></paramdef>
65+</funcprototype>
66+</funcsynopsis>
67+
68+</chapter>
69+</book>
70+
tests/docbook-xref.nativenew file+29
@@ -0,0 +1,29 @@
1+Pandoc (Meta {unMeta = fromList []})
2+[Header 1 ("ch01",[],[]) [Str "XRef",Space,Str "Samples"]
3+,Para [Str "This",Space,Str "paragraph",Space,Str "demonstrates",Space,Str "several",Space,Str "features",Space,Str "of",Space,Str "XRef."]
4+,BulletList
5+ [[Para [Str "A",Space,Str "straight",Space,Str "link",Space,Str "generates",Space,Str "the",Space,Str "cross-reference",Space,Str "text:",Space,Link [Str "???"] ("#ch02",""),Str "."]]
6+ ,[Para [Str "A",Space,Str "link",Space,Str "to",Space,Str "an",Space,Str "element",Space,Str "with",Space,Str "an",Space,Str "XRefLabel:",Space,Link [Str "Chapter the Third"] ("#ch03",""),Str "."]]
7+ ,[Para [Str "A",Space,Str "link",Space,Str "with",Space,Str "an",Space,Str "EndTerm:",Space,Link [Str "Chapter 4"] ("#ch04",""),Str "."]]
8+ ,[Para [Str "A",Space,Str "link",Space,Str "to",Space,Str "an",Space,Str "cmdsynopsis",Space,Str "element:",Space,Link [Str "???"] ("#cmd01",""),Str "."]]
9+ ,[Para [Str "A",Space,Str "link",Space,Str "to",Space,Str "an",Space,Str "funcsynopsis",Space,Str "element:",Space,Link [Str "???"] ("#func01",""),Str "."]]]
10+,Header 1 ("ch02",[],[]) [Str "The",Space,Str "Second",Space,Str "Chapter"]
11+,Para [Str "Some",Space,Str "content",Space,Str "here"]
12+,Header 1 ("ch03",[],[]) [Str "The",Space,Str "Third",Space,Str "Chapter"]
13+,Para [Str "Some",Space,Str "content",Space,Str "here"]
14+,Header 1 ("ch04",[],[]) [Str "The",Space,Str "Fourth",Space,Str "Chapter"]
15+,Para [Str "Some",Space,Str "content",Space,Str "here"]
16+,Plain [Str "chgrp"]
17+,Plain [Str "-R"]
18+,Plain [Str "-H"]
19+,Plain [Str "-L"]
20+,Plain [Str "-P"]
21+,Plain [Str "-f"]
22+,Plain [Str "group"]
23+,Plain [Str "file"]
24+,Plain [Str "int"]
25+,Plain [Str "max"]
26+,Plain [Str "int"]
27+,Plain [Str "int1"]
28+,Plain [Str "int"]
29+,Plain [Str "int2"]]