git.iko.soy/typeable/pandoc
on master help sign in
fig, table-wrap & caption Divs for JATS writer

Support writing <fig> and <table-wrap> elements with <title> and <caption> inside them by using Divs with class set to on of fig, table-wrap or cation. The title is included as a Heading so the constraint on where Heading can occur is also relaxed. Also leaves out empty alt attributes on links.

commitec1693505c65ef5dfca1df09d415f852a2787c15 authorHamish Mackenzie <Hamish.K.Mackenzie@googlemail.com> dateWed 13 Dec 2017, 12:06 (9 years ago) parent7d23031
4 files changed+152−6
pandoc.cabal+1 src/Text/Pandoc/Writers/JATS.hs+30−6 test/Tests/Writers/JATS.hs+119 test/test-pandoc.hs+2
pandoc.cabal+1
@@ -635,6 +635,7 @@ test-suite test-pandoc
635635 Tests.Writers.ConTeXt
636636 Tests.Writers.Docbook
637637 Tests.Writers.HTML
638+ Tests.Writers.JATS
638639 Tests.Writers.Markdown
639640 Tests.Writers.Org
640641 Tests.Writers.Plain
src/Text/Pandoc/Writers/JATS.hs+30−6
@@ -168,6 +168,13 @@ blockToJATS opts (Div ('r':'e':'f':'-':_,_,_) [Para lst]) =
168168blockToJATS opts (Div ("refs",_,_) xs) = do
169169 contents <- blocksToJATS opts xs
170170 return $ inTagsIndented "ref-list" contents
171+blockToJATS opts (Div (ident,[cls],kvs) bs) | cls `elem` ["fig", "caption", "table-wrap"] = do
172+ contents <- blocksToJATS opts bs
173+ let attr = [("id", ident) | not (null ident)] ++
174+ [("xml:lang",l) | ("lang",l) <- kvs] ++
175+ [(k,v) | (k,v) <- kvs, k `elem` ["specific-use",
176+ "content-type", "orientation", "position"]]
177+ return $ inTags True cls attr contents
171178blockToJATS opts (Div (ident,_,kvs) bs) = do
172179 contents <- blocksToJATS opts bs
173180 let attr = [("id", ident) | not (null ident)] ++
@@ -175,10 +182,9 @@ blockToJATS opts (Div (ident,_,kvs) bs) = do
175182 [(k,v) | (k,v) <- kvs, k `elem` ["specific-use",
176183 "content-type", "orientation", "position"]]
177184 return $ inTags True "boxed-text" attr contents
178−blockToJATS _ h@(Header{}) = do
179− -- should not occur after hierarchicalize, except inside lists/blockquotes
180− report $ BlockNotRendered h
181− return empty
185+blockToJATS opts (Header _ _ title) = do
186+ title' <- inlinesToJATS opts title
187+ return $ inTagsSimple "title" title'
182188-- No Plain, everything needs to be in a block-level tag
183189blockToJATS opts (Plain lst) = blockToJATS opts (Para lst)
184190-- title beginning with fig: indicates that the image is a figure
@@ -204,6 +210,24 @@ blockToJATS opts (Para [Image (ident,_,kvs) txt
204210 ("xlink:title",tit)]
205211 return $ inTags True "fig" attr $
206212 capt $$ selfClosingTag "graphic" graphicattr
213+blockToJATS _ (Para [Image (ident,_,kvs) _ (src, tit)]) = do
214+ let mbMT = getMimeType src
215+ let maintype = fromMaybe "image" $
216+ lookup "mimetype" kvs `mplus`
217+ (takeWhile (/='/') <$> mbMT)
218+ let subtype = fromMaybe "" $
219+ lookup "mime-subtype" kvs `mplus`
220+ ((drop 1 . dropWhile (/='/')) <$> mbMT)
221+ let attr = [("id", ident) | not (null ident)] ++
222+ [("mimetype", maintype),
223+ ("mime-subtype", subtype),
224+ ("xlink:href", src)] ++
225+ [("xlink:title", tit) | not (null tit)] ++
226+ [(k,v) | (k,v) <- kvs, k `elem` ["baseline-shift",
227+ "content-type", "specific-use", "xlink:actuate",
228+ "xlink:href", "xlink:role", "xlink:show",
229+ "xlink:type"]]
230+ return $ selfClosingTag "graphic" attr
207231blockToJATS opts (Para lst) =
208232 inTagsIndented "p" <$> inlinesToJATS opts lst
209233blockToJATS opts (LineBlock lns) =
@@ -379,8 +403,8 @@ inlineToJATS _ (Link _attr [Str t] ('m':'a':'i':'l':'t':'o':':':email, _))
379403 return $ inTagsSimple "email" $ text (escapeStringForXML email)
380404inlineToJATS opts (Link (ident,_,kvs) txt ('#':src, _)) = do
381405 let attr = [("id", ident) | not (null ident)] ++
382− [("alt", stringify txt),
383− ("rid", src)] ++
406+ [("alt", stringify txt) | not (null txt)] ++
407+ [("rid", src)] ++
384408 [(k,v) | (k,v) <- kvs, k `elem` ["ref-type", "specific-use"]]
385409 contents <- inlinesToJATS opts txt
386410 return $ inTags False "xref" attr contents
test/Tests/Writers/JATS.hsnew file+119
@@ -0,0 +1,119 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Tests.Writers.JATS (tests) where
3+
4+import Data.Text (unpack)
5+import Test.Tasty
6+import Tests.Helpers
7+import Text.Pandoc
8+import Text.Pandoc.Arbitrary ()
9+import Text.Pandoc.Builder
10+
11+jats :: (ToPandoc a) => a -> String
12+jats = unpack . purely (writeJATS def{ writerWrapText = WrapNone }) . toPandoc
13+
14+{-
15+ "my test" =: X =?> Y
16+
17+is shorthand for
18+
19+ test jats "my test" $ X =?> Y
20+
21+which is in turn shorthand for
22+
23+ test jats "my test" (X,Y)
24+-}
25+
26+infix 4 =:
27+(=:) :: (ToString a, ToPandoc a)
28+ => String -> (a, String) -> TestTree
29+(=:) = test jats
30+
31+tests :: [TestTree]
32+tests = [ testGroup "inline code"
33+ [ "basic" =: code "@&" =?> "<p>\n <monospace>@&amp;</monospace>\n</p>"
34+ ]
35+ , testGroup "images"
36+ [ "basic" =:
37+ image "/url" "title" mempty
38+ =?> "<graphic mimetype=\"image\" mime-subtype=\"\" xlink:href=\"/url\" xlink:title=\"title\" />"
39+ ]
40+ , testGroup "inlines"
41+ [ "Emphasis" =: emph ("emphasized")
42+ =?> "<p>\n <italic>emphasized</italic>\n</p>"
43+ ]
44+ , "bullet list" =: bulletList [ plain $ text "first"
45+ , plain $ text "second"
46+ , plain $ text "third"
47+ ]
48+ =?> "<list list-type=\"bullet\">\n\
49+ \ <list-item>\n\
50+ \ <p>\n\
51+ \ first\n\
52+ \ </p>\n\
53+ \ </list-item>\n\
54+ \ <list-item>\n\
55+ \ <p>\n\
56+ \ second\n\
57+ \ </p>\n\
58+ \ </list-item>\n\
59+ \ <list-item>\n\
60+ \ <p>\n\
61+ \ third\n\
62+ \ </p>\n\
63+ \ </list-item>\n\
64+ \</list>"
65+ , testGroup "definition lists"
66+ [ "with internal link" =: definitionList [(link "#go" "" (str "testing"),
67+ [plain (text "hi there")])] =?>
68+ "<def-list>\n\
69+ \ <def-item>\n\
70+ \ <term>\n\
71+ \ <xref alt=\"testing\" rid=\"go\">testing</xref>\n\
72+ \ </term>\n\
73+ \ <def>\n\
74+ \ <p>\n\
75+ \ hi there\n\
76+ \ </p>\n\
77+ \ </def>\n\
78+ \ </def-item>\n\
79+ \</def-list>"
80+ ]
81+ , testGroup "math"
82+ [ "escape |" =: para (math "\\sigma|_{\\{x\\}}") =?>
83+ "<p>\n\
84+ \ <inline-formula><alternatives>\n\
85+ \ <tex-math><![CDATA[\\sigma|_{\\{x\\}}]]></tex-math>\n\
86+ \ <mml:math display=\"inline\" xmlns:mml=\"http://www.w3.org/1998/Math/MathML\"><mml:mrow><mml:mi>σ</mml:mi><mml:msub><mml:mo stretchy=\"false\" form=\"prefix\">|</mml:mo><mml:mrow><mml:mo stretchy=\"false\" form=\"prefix\">{</mml:mo><mml:mi>x</mml:mi><mml:mo stretchy=\"false\" form=\"postfix\">}</mml:mo></mml:mrow></mml:msub></mml:mrow></mml:math></alternatives></inline-formula>\n\
87+ \</p>"
88+ ]
89+ , testGroup "headers"
90+ [ "unnumbered header" =:
91+ headerWith ("foo",["unnumbered"],[]) 1
92+ (text "Header 1" <> note (plain $ text "note")) =?>
93+ "<sec id=\"foo\">\n\
94+ \ <title>Header 1<fn>\n\
95+ \ <p>\n\
96+ \ note\n\
97+ \ </p>\n\
98+ \ </fn></title>\n\
99+ \</sec>"
100+ , "unnumbered sub header" =:
101+ headerWith ("foo",["unnumbered"],[]) 1
102+ (text "Header")
103+ <> headerWith ("foo",["unnumbered"],[]) 2
104+ (text "Sub-Header") =?>
105+ "<sec id=\"foo\">\n\
106+ \ <title>Header</title>\n\
107+ \ <sec id=\"foo\">\n\
108+ \ <title>Sub-Header</title>\n\
109+ \ </sec>\n\
110+ \</sec>"
111+ , "containing image" =:
112+ header 1 (image "imgs/foo.jpg" "" (text "Alt text")) =?>
113+ "<sec>\n\
114+ \ <title><inline-graphic mimetype=\"image\" mime-subtype=\"jpeg\" xlink:href=\"imgs/foo.jpg\" /></title>\n\
115+ \</sec>"
116+ ]
117+ ]
118+
119+
test/test-pandoc.hs+2
@@ -25,6 +25,7 @@ import qualified Tests.Writers.Docbook
2525import qualified Tests.Writers.Docx
2626import qualified Tests.Writers.FB2
2727import qualified Tests.Writers.HTML
28+import qualified Tests.Writers.JATS
2829import qualified Tests.Writers.LaTeX
2930import qualified Tests.Writers.Markdown
3031import qualified Tests.Writers.Muse
@@ -44,6 +45,7 @@ tests = testGroup "pandoc tests" [ Tests.Command.tests
4445 , testGroup "ConTeXt" Tests.Writers.ConTeXt.tests
4546 , testGroup "LaTeX" Tests.Writers.LaTeX.tests
4647 , testGroup "HTML" Tests.Writers.HTML.tests
48+ , testGroup "JATS" Tests.Writers.JATS.tests
4749 , testGroup "Docbook" Tests.Writers.Docbook.tests
4850 , testGroup "Markdown" Tests.Writers.Markdown.tests
4951 , testGroup "Org" Tests.Writers.Org.tests