git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1240 from neilmayhew/master

Docbook output of Line Blocks

commit857fcff7d61100e0ce0872b7e2318c3ad0f9e2fb authorJohn MacFarlane <jgm@berkeley.edu> dateSun 13 Apr 2014, 14:37 (13 years ago) parent86b4da9f22ce4f
4 files changed+72−7
src/Text/Pandoc/Writers/Docbook.hs+16−3 tests/Tests/Writers/Docbook.hs+52 tests/test-pandoc.hs+2 tests/writer.docbook+2−4
src/Text/Pandoc/Writers/Docbook.hs+16−3
@@ -32,12 +32,14 @@ module Text.Pandoc.Writers.Docbook ( writeDocbook) where
3232import Text.Pandoc.Definition
3333import Text.Pandoc.XML
3434import Text.Pandoc.Shared
35+import Text.Pandoc.Walk
3536import Text.Pandoc.Writers.Shared
3637import Text.Pandoc.Options
3738import Text.Pandoc.Templates (renderTemplate')
3839import Text.Pandoc.Readers.TeXMath
3940import Data.List ( isPrefixOf, intercalate, isSuffixOf )
4041import Data.Char ( toLower )
42+import Data.Monoid ( Any(..) )
4143import Text.Pandoc.Highlighting ( languages, languagesByExtension )
4244import Text.Pandoc.Pretty
4345import qualified Text.Pandoc.Builder as B
@@ -165,8 +167,9 @@ blockToDocbook opts (Para [Image txt (src,'f':'i':'g':':':_)]) =
165167 (inTagsIndented "imageobject"
166168 (selfClosingTag "imagedata" [("fileref",src)])) $$
167169 inTagsSimple "textobject" (inTagsSimple "phrase" alt))
168−blockToDocbook opts (Para lst) =
169− inTagsIndented "para" $ inlinesToDocbook opts lst
170+blockToDocbook opts (Para lst)
171+ | hasLineBreaks lst = flush $ nowrap $ inTagsSimple "literallayout" $ inlinesToDocbook opts lst
172+ | otherwise = inTagsIndented "para" $ inlinesToDocbook opts lst
170173blockToDocbook opts (BlockQuote blocks) =
171174 inTagsIndented "blockquote" $ blocksToDocbook opts blocks
172175blockToDocbook _ (CodeBlock (_,classes,_) str) =
@@ -226,6 +229,16 @@ blockToDocbook opts (Table caption aligns widths headers rows) =
226229 (inTags True "tgroup" [("cols", show (length headers))] $
227230 coltags $$ head' $$ body')
228231
232+hasLineBreaks :: [Inline] -> Bool
233+hasLineBreaks = getAny . query isLineBreak . walk removeNote
234+ where
235+ removeNote :: Inline -> Inline
236+ removeNote (Note _) = Str ""
237+ removeNote x = x
238+ isLineBreak :: Inline -> Any
239+ isLineBreak LineBreak = Any True
240+ isLineBreak _ = Any False
241+
229242alignmentToString :: Alignment -> [Char]
230243alignmentToString alignment = case alignment of
231244 AlignLeft -> "left"
@@ -293,7 +306,7 @@ inlineToDocbook opts (Math t str)
293306 fixNS = everywhere (mkT fixNS')
294307inlineToDocbook _ (RawInline f x) | f == "html" || f == "docbook" = text x
295308 | otherwise = empty
296−inlineToDocbook _ LineBreak = flush $ inTagsSimple "literallayout" (text "\n")
309+inlineToDocbook _ LineBreak = text "\n"
297310inlineToDocbook _ Space = space
298311inlineToDocbook opts (Link txt (src, _)) =
299312 if isPrefixOf "mailto:" src
tests/Tests/Writers/Docbook.hsnew file+52
@@ -0,0 +1,52 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Tests.Writers.Docbook (tests) where
3+
4+import Test.Framework
5+import Text.Pandoc.Builder
6+import Text.Pandoc
7+import Tests.Helpers
8+import Tests.Arbitrary()
9+
10+docbook :: (ToString a, ToPandoc a) => a -> String
11+docbook = writeDocbook def{ writerWrapText = False } . toPandoc
12+
13+{-
14+ "my test" =: X =?> Y
15+
16+is shorthand for
17+
18+ test docbook "my test" $ X =?> Y
19+
20+which is in turn shorthand for
21+
22+ test docbook "my test" (X,Y)
23+-}
24+
25+infix 4 =:
26+(=:) :: (ToString a, ToPandoc a)
27+ => String -> (a, String) -> Test
28+(=:) = test docbook
29+
30+lineblock :: Blocks
31+lineblock = para ("some text" <> linebreak <>
32+ "and more lines" <> linebreak <>
33+ "and again")
34+lineblock_out :: String
35+lineblock_out = "<literallayout>some text\n" ++
36+ "and more lines\n" ++
37+ "and again</literallayout>"
38+
39+tests :: [Test]
40+tests = [ testGroup "line blocks"
41+ [ "none" =: para "This is a test"
42+ =?> "<para>\n This is a test\n</para>"
43+ , "basic" =: lineblock
44+ =?> lineblock_out
45+ , "blockquote" =: blockQuote lineblock
46+ =?> ("<blockquote>\n" ++ lineblock_out ++ "\n</blockquote>")
47+ , "footnote" =: para ("This is a test" <> note lineblock <> " of footnotes")
48+ =?> ("<para>\n This is a test<footnote>\n" ++
49+ lineblock_out ++
50+ "\n </footnote> of footnotes\n</para>")
51+ ]
52+ ]
tests/test-pandoc.hs+2
@@ -12,6 +12,7 @@ import qualified Tests.Readers.RST
1212import qualified Tests.Writers.ConTeXt
1313import qualified Tests.Writers.LaTeX
1414import qualified Tests.Writers.HTML
15+import qualified Tests.Writers.Docbook
1516import qualified Tests.Writers.Native
1617import qualified Tests.Writers.Markdown
1718import qualified Tests.Shared
@@ -27,6 +28,7 @@ tests = [ testGroup "Old" Tests.Old.tests
2728 , testGroup "ConTeXt" Tests.Writers.ConTeXt.tests
2829 , testGroup "LaTeX" Tests.Writers.LaTeX.tests
2930 , testGroup "HTML" Tests.Writers.HTML.tests
31+ , testGroup "Docbook" Tests.Writers.Docbook.tests
3032 , testGroup "Markdown" Tests.Writers.Markdown.tests
3133 ]
3234 , testGroup "Readers"
tests/writer.docbook+2−4
@@ -68,10 +68,8 @@
6868 <para>
6969 Here’s one with a bullet. * criminey.
7070 </para>
71− <para>
72− There should be a hard line break<literallayout>
73−</literallayout>here.
74− </para>
71+<literallayout>There should be a hard line break
72+here.</literallayout>
7573</sect1>
7674<sect1 id="block-quotes">
7775 <title>Block Quotes</title>