git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1240 from neilmayhew/master
857fcffJohn MacFarlane
src/Text/Pandoc/Writers/Docbook.hs+16−3
@@ -32,12 +32,14 @@ module Text.Pandoc.Writers.Docbook ( writeDocbook) where
32import Text.Pandoc.Definition
32import Text.Pandoc.Definition
33import Text.Pandoc.XML
33import Text.Pandoc.XML
34import Text.Pandoc.Shared
34import Text.Pandoc.Shared
35+import Text.Pandoc.Walk
35import Text.Pandoc.Writers.Shared
36import Text.Pandoc.Writers.Shared
36import Text.Pandoc.Options
37import Text.Pandoc.Options
37import Text.Pandoc.Templates (renderTemplate')
38import Text.Pandoc.Templates (renderTemplate')
38import Text.Pandoc.Readers.TeXMath
39import Text.Pandoc.Readers.TeXMath
39import Data.List ( isPrefixOf, intercalate, isSuffixOf )
40import Data.List ( isPrefixOf, intercalate, isSuffixOf )
40import Data.Char ( toLower )
41import Data.Char ( toLower )
42+import Data.Monoid ( Any(..) )
41import Text.Pandoc.Highlighting ( languages, languagesByExtension )
43import Text.Pandoc.Highlighting ( languages, languagesByExtension )
42import Text.Pandoc.Pretty
44import Text.Pandoc.Pretty
43import qualified Text.Pandoc.Builder as B
45import qualified Text.Pandoc.Builder as B
@@ -165,8 +167,9 @@ blockToDocbook opts (Para [Image txt (src,'f':'i':'g':':':_)]) =
165 (inTagsIndented "imageobject"
167 (inTagsIndented "imageobject"
166 (selfClosingTag "imagedata" [("fileref",src)])) $$
168 (selfClosingTag "imagedata" [("fileref",src)])) $$
167 inTagsSimple "textobject" (inTagsSimple "phrase" alt))
169 inTagsSimple "textobject" (inTagsSimple "phrase" alt))
168−blockToDocbook opts (Para lst) =
170+blockToDocbook opts (Para lst)
171+ | hasLineBreaks lst = flush $ nowrap $ inTagsSimple "literallayout" $ inlinesToDocbook opts lst
169− inTagsIndented "para" $ inlinesToDocbook opts lst
172+ | otherwise = inTagsIndented "para" $ inlinesToDocbook opts lst
170blockToDocbook opts (BlockQuote blocks) =
173blockToDocbook opts (BlockQuote blocks) =
171 inTagsIndented "blockquote" $ blocksToDocbook opts blocks
174 inTagsIndented "blockquote" $ blocksToDocbook opts blocks
172blockToDocbook _ (CodeBlock (_,classes,_) str) =
175blockToDocbook _ (CodeBlock (_,classes,_) str) =
@@ -226,6 +229,16 @@ blockToDocbook opts (Table caption aligns widths headers rows) =
226 (inTags True "tgroup" [("cols", show (length headers))] $
229 (inTags True "tgroup" [("cols", show (length headers))] $
227 coltags $$ head' $$ body')
230 coltags $$ head' $$ body')
228
231
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+
229alignmentToString :: Alignment -> [Char]
242alignmentToString :: Alignment -> [Char]
230alignmentToString alignment = case alignment of
243alignmentToString alignment = case alignment of
231 AlignLeft -> "left"
244 AlignLeft -> "left"
@@ -293,7 +306,7 @@ inlineToDocbook opts (Math t str)
293 fixNS = everywhere (mkT fixNS')
306 fixNS = everywhere (mkT fixNS')
294inlineToDocbook _ (RawInline f x) | f == "html" || f == "docbook" = text x
307inlineToDocbook _ (RawInline f x) | f == "html" || f == "docbook" = text x
295 | otherwise = empty
308 | otherwise = empty
296−inlineToDocbook _ LineBreak = flush $ inTagsSimple "literallayout" (text "\n")
309+inlineToDocbook _ LineBreak = text "\n"
297inlineToDocbook _ Space = space
310inlineToDocbook _ Space = space
298inlineToDocbook opts (Link txt (src, _)) =
311inlineToDocbook opts (Link txt (src, _)) =
299 if isPrefixOf "mailto:" src
312 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
12import qualified Tests.Writers.ConTeXt
12import qualified Tests.Writers.ConTeXt
13import qualified Tests.Writers.LaTeX
13import qualified Tests.Writers.LaTeX
14import qualified Tests.Writers.HTML
14import qualified Tests.Writers.HTML
15+import qualified Tests.Writers.Docbook
15import qualified Tests.Writers.Native
16import qualified Tests.Writers.Native
16import qualified Tests.Writers.Markdown
17import qualified Tests.Writers.Markdown
17import qualified Tests.Shared
18import qualified Tests.Shared
@@ -27,6 +28,7 @@ tests = [ testGroup "Old" Tests.Old.tests
27 , testGroup "ConTeXt" Tests.Writers.ConTeXt.tests
28 , testGroup "ConTeXt" Tests.Writers.ConTeXt.tests
28 , testGroup "LaTeX" Tests.Writers.LaTeX.tests
29 , testGroup "LaTeX" Tests.Writers.LaTeX.tests
29 , testGroup "HTML" Tests.Writers.HTML.tests
30 , testGroup "HTML" Tests.Writers.HTML.tests
31+ , testGroup "Docbook" Tests.Writers.Docbook.tests
30 , testGroup "Markdown" Tests.Writers.Markdown.tests
32 , testGroup "Markdown" Tests.Writers.Markdown.tests
31 ]
33 ]
32 , testGroup "Readers"
34 , testGroup "Readers"
tests/writer.docbook+2−4
@@ -68,10 +68,8 @@
68 <para>
68 <para>
69 Here’s one with a bullet. * criminey.
69 Here’s one with a bullet. * criminey.
70 </para>
70 </para>
71− <para>
72− There should be a hard line break<literallayout>
71+<literallayout>There should be a hard line break
73−</literallayout>here.
72+here.</literallayout>
74− </para>
75</sect1>
73</sect1>
76<sect1 id="block-quotes">
74<sect1 id="block-quotes">
77 <title>Block Quotes</title>
75 <title>Block Quotes</title>