git.iko.soy/typeable/pandoc
on master help sign in
Added proper support for DocBook 'xref' elements
35f12b5Frerich Raabe
pandoc.cabal+1
@@ -125,6 +125,7 @@ Extra-Source-Files:
125 tests/bodybg.gif
125 tests/bodybg.gif
126 tests/*.native
126 tests/*.native
127 tests/docbook-reader.docbook
127 tests/docbook-reader.docbook
128+ tests/docbook-xref.docbook
128 tests/html-reader.html
129 tests/html-reader.html
129 tests/opml-reader.opml
130 tests/opml-reader.opml
130 tests/haddock-reader.haddock
131 tests/haddock-reader.haddock
src/Text/Pandoc/Readers/DocBook.hs+18−2
@@ -18,6 +18,7 @@ import Text.TeXMath (readMathML, writeTeX)
18import Text.Pandoc.Error (PandocError)
18import Text.Pandoc.Error (PandocError)
19import Text.Pandoc.Compat.Except
19import Text.Pandoc.Compat.Except
20import Data.Default
20import Data.Default
21+import Data.Foldable (asum)
21
22
22{-
23{-
23
24
@@ -498,7 +499,7 @@ List of all DocBook tags, with [x] indicating implemented,
498[x] warning - An admonition set off from the text
499[x] warning - An admonition set off from the text
499[x] wordasword - A word meant specifically as a word and not representing
500[x] wordasword - A word meant specifically as a word and not representing
500 anything else
501 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
502[ ] year - The year of publication of a document
503[ ] year - The year of publication of a document
503[x] ?asciidoc-br? - line break from asciidoc docbook output
504[x] ?asciidoc-br? - line break from asciidoc docbook output
504-}
505-}
@@ -952,7 +953,13 @@ parseInline (Elem e) =
952 "keycombo" -> keycombo <$> (mapM parseInline $ elContent e)
953 "keycombo" -> keycombo <$> (mapM parseInline $ elContent e)
953 "menuchoice" -> menuchoice <$> (mapM parseInline $
954 "menuchoice" -> menuchoice <$> (mapM parseInline $
954 filter isGuiMenu $ elContent e)
955 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))
956 "email" -> return $ link ("mailto:" ++ strContent e) ""
963 "email" -> return $ link ("mailto:" ++ strContent e) ""
957 $ str $ strContent e
964 $ str $ strContent e
958 "uri" -> return $ link (strContent e) "" $ str $ strContent e
965 "uri" -> return $ link (strContent e) "" $ str $ strContent e
@@ -1013,3 +1020,12 @@ parseInline (Elem e) =
1013 isGuiMenu (Elem x) = named "guimenu" x || named "guisubmenu" x ||
1020 isGuiMenu (Elem x) = named "guimenu" x || named "guisubmenu" x ||
1014 named "guimenuitem" x
1021 named "guimenuitem" x
1015 isGuiMenu _ = False
1022 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"
105 [ testGroup "writer" $ writerTests "docbook"
105 [ testGroup "writer" $ writerTests "docbook"
106 , test "reader" ["-r", "docbook", "-w", "native", "-s"]
106 , test "reader" ["-r", "docbook", "-w", "native", "-s"]
107 "docbook-reader.docbook" "docbook-reader.native"
107 "docbook-reader.docbook" "docbook-reader.native"
108+ , test "reader" ["-r", "docbook", "-w", "native", "-s"]
109+ "docbook-xref.docbook" "docbook-xref.native"
108 ]
110 ]
109 , testGroup "native"
111 , testGroup "native"
110 [ testGroup "writer" $ writerTests "native"
112 [ 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"]]