git.iko.soy/typeable/pandoc
on master help sign in
Docbook writer: Updated to use Pretty.
commitebdbb06f94dec28eac480021c7784218cd9af43b authorJohn MacFarlane <jgm@berkeley.edu> dateTue 21 Dec 2010, 16:45 (16 years ago) parentce533ff
2 files changed+126−148
src/Text/Pandoc/Writers/Docbook.hs+20−21 tests/writer.docbook+106−127
src/Text/Pandoc/Writers/Docbook.hs+20−21
@@ -35,13 +35,13 @@ import Text.Pandoc.Templates (renderTemplate)
3535import Text.Pandoc.Readers.TeXMath
3636import Data.List ( isPrefixOf, intercalate )
3737import Data.Char ( toLower )
38−import Text.PrettyPrint.HughesPJ hiding ( Str )
3938import Text.Pandoc.Highlighting (languages, languagesByExtension)
39+import Text.Pandoc.Pretty
4040
4141-- | Convert list of authors to a docbook <author> section
4242authorToDocbook :: WriterOptions -> [Inline] -> Doc
4343authorToDocbook opts name' =
44− let name = render $ inlinesToDocbook opts name'
44+ let name = render Nothing $ inlinesToDocbook opts name'
4545 in if ',' `elem` name
4646 then -- last name first
4747 let (lastname, rest) = break (==',') name
@@ -61,16 +61,20 @@ authorToDocbook opts name' =
6161-- | Convert Pandoc document to string in Docbook format.
6262writeDocbook :: WriterOptions -> Pandoc -> String
6363writeDocbook opts (Pandoc (Meta tit auths dat) blocks) =
64− let title = wrap opts tit
64+ let title = inlinesToDocbook opts tit
6565 authors = map (authorToDocbook opts) auths
6666 date = inlinesToDocbook opts dat
6767 elements = hierarchicalize blocks
68− main = render $ vcat (map (elementToDocbook opts) elements)
68+ colwidth = if writerWrapText opts
69+ then Just $ writerColumns opts
70+ else Nothing
71+ render' = render colwidth
72+ main = render' $ vcat (map (elementToDocbook opts) elements)
6973 context = writerVariables opts ++
7074 [ ("body", main)
71− , ("title", render title)
72− , ("date", render date) ] ++
73− [ ("author", render a) | a <- authors ]
75+ , ("title", render' title)
76+ , ("date", render' date) ] ++
77+ [ ("author", render' a) | a <- authors ]
7478 in if writerStandalone opts
7579 then renderTemplate context $ writerTemplate opts
7680 else main
@@ -84,7 +88,7 @@ elementToDocbook opts (Sec _ _num id' title elements) =
8488 then [Blk (Para [])]
8589 else elements
8690 in inTags True "section" [("id",id')] $
87− inTagsSimple "title" (wrap opts title) $$
91+ inTagsSimple "title" (inlinesToDocbook opts title) $$
8892 vcat (map (elementToDocbook opts) elements')
8993
9094-- | Convert a list of Pandoc blocks to Docbook.
@@ -123,7 +127,7 @@ listItemToDocbook opts item =
123127blockToDocbook :: WriterOptions -> Block -> Doc
124128blockToDocbook _ Null = empty
125129blockToDocbook _ (Header _ _) = empty -- should not occur after hierarchicalize
126−blockToDocbook opts (Plain lst) = wrap opts lst
130+blockToDocbook opts (Plain lst) = inlinesToDocbook opts lst
127131blockToDocbook opts (Para [Image txt (src,_)]) =
128132 let capt = inlinesToDocbook opts txt
129133 in inTagsIndented "figure" $
@@ -132,12 +136,13 @@ blockToDocbook opts (Para [Image txt (src,_)]) =
132136 (inTagsIndented "imageobject"
133137 (selfClosingTag "imagedata" [("fileref",src)])) $$
134138 inTagsSimple "textobject" (inTagsSimple "phrase" capt))
135−blockToDocbook opts (Para lst) = inTagsIndented "para" $ wrap opts lst
139+blockToDocbook opts (Para lst) =
140+ inTagsIndented "para" $ inlinesToDocbook opts lst
136141blockToDocbook opts (BlockQuote blocks) =
137142 inTagsIndented "blockquote" $ blocksToDocbook opts blocks
138143blockToDocbook _ (CodeBlock (_,classes,_) str) =
139− text ("<screen" ++ lang ++ ">\n") <>
140− text (escapeStringForXML str) <> text "\n</screen>"
144+ text ("<screen" ++ lang ++ ">") <> cr <>
145+ flush (text (escapeStringForXML str) <> cr <> text "</screen>")
141146 where lang = if null langs
142147 then ""
143148 else " language=\"" ++ escapeStringForXML (head langs) ++
@@ -214,12 +219,6 @@ tableItemToDocbook opts tag align item =
214219 let attrib = [("align", align)]
215220 in inTags True tag attrib $ vcat $ map (blockToDocbook opts) item
216221
217−-- | Take list of inline elements and return wrapped doc.
218−wrap :: WriterOptions -> [Inline] -> Doc
219−wrap opts lst = if writerWrapText opts
220− then fsep $ map (inlinesToDocbook opts) (splitBy (== Space) lst)
221− else inlinesToDocbook opts lst
222−
223222-- | Convert a list of inline elements to Docbook.
224223inlinesToDocbook :: WriterOptions -> [Inline] -> Doc
225224inlinesToDocbook opts lst = hcat $ map (inlineToDocbook opts) lst
@@ -254,8 +253,8 @@ inlineToDocbook _ (Code str) =
254253inlineToDocbook opts (Math _ str) = inlinesToDocbook opts $ readTeXMath str
255254inlineToDocbook _ (TeX _) = empty
256255inlineToDocbook _ (HtmlInline _) = empty
257−inlineToDocbook _ LineBreak = text $ "<literallayout></literallayout>"
258−inlineToDocbook _ Space = char ' '
256+inlineToDocbook _ LineBreak = inTagsSimple "literallayout" empty
257+inlineToDocbook _ Space = space
259258inlineToDocbook opts (Link txt (src, _)) =
260259 if isPrefixOf "mailto:" src
261260 then let src' = drop 7 src
@@ -275,6 +274,6 @@ inlineToDocbook _ (Image _ (src, tit)) =
275274 else inTagsIndented "objectinfo" $
276275 inTagsIndented "title" (text $ escapeStringForXML tit)
277276 in inTagsIndented "inlinemediaobject" $ inTagsIndented "imageobject" $
278− titleDoc $$ selfClosingTag "imagedata" [("fileref", src)]
277+ titleDoc $$ selfClosingTag "imagedata" [("fileref", src)]
279278inlineToDocbook opts (Note contents) =
280279 inTagsIndented "footnote" $ blocksToDocbook opts contents
tests/writer.docbook+106−127
@@ -15,14 +15,13 @@
1515 <date>July 17, 2006</date>
1616 </articleinfo>
1717<para>
18− This is a set of tests for pandoc. Most of them are adapted from
19− John Gruber's markdown test suite.
18+ This is a set of tests for pandoc. Most of them are adapted from John
19+ Gruber's markdown test suite.
2020</para>
2121<section id="headers">
2222 <title>Headers</title>
2323 <section id="level-2-with-an-embedded-link">
24− <title>Level 2 with an
25− <ulink url="/url">embedded link</ulink></title>
24+ <title>Level 2 with an <ulink url="/url">embedded link</ulink></title>
2625 <section id="level-3-with-emphasis">
2726 <title>Level 3 with <emphasis>emphasis</emphasis></title>
2827 <section id="level-4">
@@ -60,16 +59,15 @@
6059 Here's a regular paragraph.
6160 </para>
6261 <para>
63− In Markdown 1.0.0 and earlier. Version 8. This line turns into a
64− list item. Because a hard-wrapped line in the middle of a paragraph
65− looked like a list item.
62+ In Markdown 1.0.0 and earlier. Version 8. This line turns into a list
63+ item. Because a hard-wrapped line in the middle of a paragraph looked like
64+ a list item.
6665 </para>
6766 <para>
6867 Here's one with a bullet. * criminey.
6968 </para>
7069 <para>
71− There should be a hard line
72− break<literallayout></literallayout>here.
70+ There should be a hard line break<literallayout></literallayout>here.
7371 </para>
7472</section>
7573<section id="block-quotes">
@@ -866,45 +864,41 @@ These should not be escaped: \$ \\ \&gt; \[ \{
866864 <div>
867865 foo
868866 </div>
869−
870867 <para>
871868 And nested without indentation:
872869 </para>
873870 <div>
874−<div>
875−<div>
871+ <div>
872+ <div>
876873 foo
877874 </div>
878−</div>
879−<div>
875+ </div>
876+ <div>
880877 bar
881878 </div>
882−</div>
883−
879+ </div>
884880 <para>
885881 Interpreted markdown in a table:
886882 </para>
887883 <table>
888−<tr>
889−<td>
884+ <tr>
885+ <td>
890886 This is <emphasis>emphasized</emphasis>
891887 </td>
892−<td>
888+ <td>
893889 And this is <emphasis role="strong">strong</emphasis>
894890 </td>
895−</tr>
896−</table>
897−
898−<script type="text/javascript">document.write('This *should not* be interpreted as markdown');</script>
891+ </tr>
892+ </table>
899893
894+ <script type="text/javascript">document.write('This *should not* be interpreted as markdown');</script>
900895 <para>
901896 Here's a simple block:
902897 </para>
903898 <div>
904−
899+
905900 foo
906901 </div>
907−
908902 <para>
909903 This should be a code block, though:
910904 </para>
@@ -923,31 +917,28 @@ These should not be escaped: \$ \\ \&gt; \[ \{
923917 Now, nested:
924918 </para>
925919 <div>
926− <div>
927− <div>
928−
920+ <div>
921+ <div>
922+
929923 foo
930924 </div>
931− </div>
932−</div>
933−
925+ </div>
926+ </div>
934927 <para>
935928 This should just be an HTML comment:
936929 </para>
937930 <!-- Comment -->
938−
939931 <para>
940932 Multiline:
941933 </para>
942934 <!--
943−Blah
944−Blah
945−-->
946−
947−<!--
948− This is another comment.
949−-->
935+ Blah
936+ Blah
937+ -->
950938
939+ <!--
940+ This is another comment.
941+ -->
951942 <para>
952943 Code block:
953944 </para>
@@ -958,7 +949,6 @@ Blah
958949 Just plain comment, with trailing spaces on the line:
959950 </para>
960951 <!-- foo -->
961−
962952 <para>
963953 Code:
964954 </para>
@@ -970,28 +960,27 @@ Blah
970960 </para>
971961 <hr>
972962
973−<hr />
963+ <hr />
974964
975−<hr />
965+ <hr />
976966
977−<hr>
967+ <hr>
978968
979−<hr />
969+ <hr />
980970
981−<hr />
971+ <hr />
982972
983−<hr class="foo" id="bar" />
973+ <hr class="foo" id="bar" />
984974
985−<hr class="foo" id="bar" />
986−
987−<hr class="foo" id="bar">
975+ <hr class="foo" id="bar" />
988976
977+ <hr class="foo" id="bar">
989978</section>
990979<section id="inline-markup">
991980 <title>Inline Markup</title>
992981 <para>
993− This is <emphasis>emphasized</emphasis>, and so
994− <emphasis>is this</emphasis>.
982+ This is <emphasis>emphasized</emphasis>, and so <emphasis>is
983+ this</emphasis>.
995984 </para>
996985 <para>
997986 This is <emphasis role="strong">strong</emphasis>, and so
@@ -1001,18 +990,18 @@ Blah
1001990 An <emphasis><ulink url="/url">emphasized link</ulink></emphasis>.
1002991 </para>
1003992 <para>
1004− <emphasis role="strong"><emphasis>This is strong and em.</emphasis></emphasis>
993+ <emphasis role="strong"><emphasis>This is strong and
994+ em.</emphasis></emphasis>
1005995 </para>
1006996 <para>
1007− So is <emphasis role="strong"><emphasis>this</emphasis></emphasis>
1008− word.
997+ So is <emphasis role="strong"><emphasis>this</emphasis></emphasis> word.
1009998 </para>
1010999 <para>
1011− <emphasis role="strong"><emphasis>This is strong and em.</emphasis></emphasis>
1000+ <emphasis role="strong"><emphasis>This is strong and
1001+ em.</emphasis></emphasis>
10121002 </para>
10131003 <para>
1014− So is <emphasis role="strong"><emphasis>this</emphasis></emphasis>
1015− word.
1004+ So is <emphasis role="strong"><emphasis>this</emphasis></emphasis> word.
10161005 </para>
10171006 <para>
10181007 This is code: <literal>&gt;</literal>, <literal>$</literal>,
@@ -1020,7 +1009,8 @@ Blah
10201009 <literal>&lt;html&gt;</literal>.
10211010 </para>
10221011 <para>
1023− <emphasis role="strikethrough">This is <emphasis>strikeout</emphasis>.</emphasis>
1012+ <emphasis role="strikethrough">This is
1013+ <emphasis>strikeout</emphasis>.</emphasis>
10241014 </para>
10251015 <para>
10261016 Superscripts: a<superscript>bc</superscript>d
@@ -1028,35 +1018,35 @@ Blah
10281018 a<superscript>hello there</superscript>.
10291019 </para>
10301020 <para>
1031− Subscripts: H<subscript>2</subscript>O,
1032− H<subscript>23</subscript>O, H<subscript>many of them</subscript>O.
1021+ Subscripts: H<subscript>2</subscript>O, H<subscript>23</subscript>O,
1022+ H<subscript>many of them</subscript>O.
10331023 </para>
10341024 <para>
1035− These should not be superscripts or subscripts, because of the
1036− unescaped spaces: a^b c^d, a~b c~d.
1025+ These should not be superscripts or subscripts, because of the unescaped
1026+ spaces: a^b c^d, a~b c~d.
10371027 </para>
10381028</section>
10391029<section id="smart-quotes-ellipses-dashes">
10401030 <title>Smart quotes, ellipses, dashes</title>
10411031 <para>
1042− <quote>Hello,</quote> said the spider.
1043− <quote><quote>Shelob</quote> is my name.</quote>
1032+ <quote>Hello,</quote> said the spider. <quote><quote>Shelob</quote> is my
1033+ name.</quote>
10441034 </para>
10451035 <para>
1046− <quote>A</quote>, <quote>B</quote>, and <quote>C</quote> are
1047− letters.
1036+ <quote>A</quote>, <quote>B</quote>, and <quote>C</quote> are letters.
10481037 </para>
10491038 <para>
1050− <quote>Oak,</quote> <quote>elm,</quote> and <quote>beech</quote>
1051− are names of trees. So is <quote>pine.</quote>
1039+ <quote>Oak,</quote> <quote>elm,</quote> and <quote>beech</quote> are names
1040+ of trees. So is <quote>pine.</quote>
10521041 </para>
10531042 <para>
1054− <quote>He said, <quote>I want to go.</quote></quote> Were you alive
1055− in the 70's?
1043+ <quote>He said, <quote>I want to go.</quote></quote> Were you alive in the
1044+ 70's?
10561045 </para>
10571046 <para>
10581047 Here is some quoted <quote><literal>code</literal></quote> and a
1059− <quote><ulink url="http://example.com/?foo=1&amp;bar=2">quoted link</ulink></quote>.
1048+ <quote><ulink url="http://example.com/?foo=1&amp;bar=2">quoted
1049+ link</ulink></quote>.
10601050 </para>
10611051 <para>
10621052 Some dashes: one—two — three—four — five.
@@ -1135,8 +1125,8 @@ Blah
11351125 </listitem>
11361126 <listitem>
11371127 <para>
1138− Escaped <literal>$</literal>: $73
1139− <emphasis>this should be emphasized</emphasis> 23$.
1128+ Escaped <literal>$</literal>: $73 <emphasis>this should be
1129+ emphasized</emphasis> 23$.
11401130 </para>
11411131 </listitem>
11421132 </itemizedlist>
@@ -1316,8 +1306,8 @@ Blah
13161306 <section id="with-ampersands">
13171307 <title>With ampersands</title>
13181308 <para>
1319− Here's a
1320− <ulink url="http://example.com/?foo=1&amp;bar=2">link with an ampersand in the URL</ulink>.
1309+ Here's a <ulink url="http://example.com/?foo=1&amp;bar=2">link with an
1310+ ampersand in the URL</ulink>.
13211311 </para>
13221312 <para>
13231313 Here's a link with an amersand in the link text:
@@ -1327,8 +1317,8 @@ Blah
13271317 Here's an <ulink url="/script?foo=1&amp;bar=2">inline link</ulink>.
13281318 </para>
13291319 <para>
1330− Here's an
1331− <ulink url="/script?foo=1&amp;bar=2">inline link in pointy braces</ulink>.
1320+ Here's an <ulink url="/script?foo=1&amp;bar=2">inline link in pointy
1321+ braces</ulink>.
13321322 </para>
13331323 </section>
13341324 <section id="autolinks">
@@ -1387,78 +1377,67 @@ or here: &lt;http://example.com/&gt;
13871377 </mediaobject>
13881378 </figure>
13891379 <para>
1390− Here is a movie
1391− <inlinemediaobject>
1380+ Here is a movie <inlinemediaobject>
13921381 <imageobject>
13931382 <imagedata fileref="movie.jpg" />
13941383 </imageobject>
1395− </inlinemediaobject>
1396− icon.
1384+ </inlinemediaobject> icon.
13971385 </para>
13981386</section>
13991387<section id="footnotes">
14001388 <title>Footnotes</title>
14011389 <para>
1402− Here is a footnote
1403− reference,<footnote>
1404− <para>
1405− Here is the footnote. It can go anywhere after the footnote
1406− reference. It need not be placed at the end of the document.
1407− </para>
1408− </footnote>
1409− and
1410− another.<footnote>
1411− <para>
1412− Here's the long note. This one contains multiple blocks.
1413− </para>
1414− <para>
1415− Subsequent blocks are indented to show that they belong to the
1416− footnote (as with list items).
1417− </para>
1418− <screen>
1390+ Here is a footnote reference,<footnote>
1391+ <para>
1392+ Here is the footnote. It can go anywhere after the footnote reference.
1393+ It need not be placed at the end of the document.
1394+ </para>
1395+ </footnote> and another.<footnote>
1396+ <para>
1397+ Here's the long note. This one contains multiple blocks.
1398+ </para>
1399+ <para>
1400+ Subsequent blocks are indented to show that they belong to the
1401+ footnote (as with list items).
1402+ </para>
1403+ <screen>
14191404 { &lt;code&gt; }
14201405</screen>
1421− <para>
1422− If you want, you can indent every line, but you can also be lazy
1423− and just indent the first line of each block.
1424− </para>
1425− </footnote>
1426− This should <emphasis>not</emphasis> be a footnote reference,
1427− because it contains a space.[^my note] Here is an inline
1428− note.<footnote>
1429− <para>
1430− This is <emphasis>easier</emphasis> to type. Inline notes may
1431− contain <ulink url="http://google.com">links</ulink> and
1432− <literal>]</literal> verbatim characters, as well as [bracketed
1433− text].
1434− </para>
1435− </footnote>
1406+ <para>
1407+ If you want, you can indent every line, but you can also be lazy and
1408+ just indent the first line of each block.
1409+ </para>
1410+ </footnote> This should <emphasis>not</emphasis> be a footnote reference,
1411+ because it contains a space.[^my note] Here is an inline note.<footnote>
1412+ <para>
1413+ This is <emphasis>easier</emphasis> to type. Inline notes may contain
1414+ <ulink url="http://google.com">links</ulink> and <literal>]</literal>
1415+ verbatim characters, as well as [bracketed text].
1416+ </para>
1417+ </footnote>
14361418 </para>
14371419 <blockquote>
14381420 <para>
1439− Notes can go in
1440− quotes.<footnote>
1441− <para>
1442− In quote.
1443− </para>
1444− </footnote>
1421+ Notes can go in quotes.<footnote>
1422+ <para>
1423+ In quote.
1424+ </para>
1425+ </footnote>
14451426 </para>
14461427 </blockquote>
14471428 <orderedlist numeration="arabic">
14481429 <listitem>
14491430 <para>
1450− And in list
1451− items.<footnote>
1452− <para>
1453− In list.
1454− </para>
1455− </footnote>
1431+ And in list items.<footnote>
1432+ <para>
1433+ In list.
1434+ </para>
1435+ </footnote>
14561436 </para>
14571437 </listitem>
14581438 </orderedlist>
14591439 <para>
1460− This paragraph should not be part of the note, as it is not
1461− indented.
1440+ This paragraph should not be part of the note, as it is not indented.
14621441 </para>
14631442</section>
14641443</article>