git.iko.soy/typeable/pandoc
on master help sign in
JATS writer: reduce unnecessary use of <p> elements for wrapping

The `<p>` element is used for wrapping in cases were the contents would otherwise not be allowed in a certain context. Unnecessary wrapping is avoided, especially around quotes (`<disp-quote>` elements). Closes: #7227

commit5f79a66ed64e9b0cc326e467dcb17239f1596fcc authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 16 Apr 2021, 22:13 (6 years ago) parent2e7fee9
7 files changed+166−144
src/Text/Pandoc/Writers/JATS.hs+12−10 src/Text/Pandoc/Writers/JATS/Table.hs+23−3 src/Text/Pandoc/Writers/JATS/Types.hs+12−3 test/command/7041.md+23 test/writer.jats_archiving+28−40 test/writer.jats_articleauthoring+40−48 test/writer.jats_publishing+28−40
src/Text/Pandoc/Writers/JATS.hs+12−10
@@ -1,6 +1,7 @@
1+{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE OverloadedStrings #-}
13{-# LANGUAGE ScopedTypeVariables #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−{-# LANGUAGE ViewPatterns #-}
4+{-# LANGUAGE ViewPatterns #-}
45{- |
56 Module : Text.Pandoc.Writers.JATS
67 Copyright : 2017-2021 John MacFarlane
@@ -80,7 +81,7 @@ writeJats tagSet opts d = do
8081 let environment = JATSEnv
8182 { jatsTagSet = tagSet
8283 , jatsInlinesWriter = inlinesToJATS
83− , jatsBlockWriter = blockToJATS
84+ , jatsBlockWriter = wrappedBlocksToJATS
8485 , jatsReferences = refs
8586 }
8687 let initialState = JATSState { jatsNotes = [] }
@@ -162,11 +163,9 @@ wrappedBlocksToJATS needsWrap opts =
162163 wrappedBlockToJATS b = do
163164 inner <- blockToJATS opts b
164165 return $
165− if needsWrap b || isBlockQuote b -- see #7041
166+ if needsWrap b
166167 then inTags True "p" [("specific-use","wrapper")] inner
167168 else inner
168− isBlockQuote (BlockQuote _) = True
169− isBlockQuote _ = False
170169
171170-- | Auxiliary function to convert Plain block to Para.
172171plainToPara :: Block -> Block
@@ -324,10 +323,13 @@ blockToJATS opts (LineBlock lns) =
324323 blockToJATS opts $ linesToPara lns
325324blockToJATS opts (BlockQuote blocks) = do
326325 tagSet <- asks jatsTagSet
327− let blocksToJats' = if tagSet == TagSetArticleAuthoring
328− then wrappedBlocksToJATS (not . isPara)
329− else blocksToJATS
330− inTagsIndented "disp-quote" <$> blocksToJats' opts blocks
326+ let needsWrap = if tagSet == TagSetArticleAuthoring
327+ then not . isPara
328+ else \case
329+ Header{} -> True
330+ HorizontalRule -> True
331+ _ -> False
332+ inTagsIndented "disp-quote" <$> wrappedBlocksToJATS needsWrap opts blocks
331333blockToJATS _ (CodeBlock a str) = return $
332334 inTags False tag attr (flush (text (T.unpack $ escapeStringForXML str)))
333335 where (lang, attr) = codeAttr a
src/Text/Pandoc/Writers/JATS/Table.hs+23−3
@@ -34,13 +34,19 @@ tableToJATS :: PandocMonad m
3434 -> JATS m (Doc Text)
3535tableToJATS opts (Ann.Table attr caption colspecs thead tbodies tfoot) = do
3636 let (Caption _maybeShortCaption captionBlocks) = caption
37+ -- Only paragraphs are allowed in captions, all other blocks must be
38+ -- wrapped in @<p>@ elements.
39+ let needsWrapping = \case
40+ Plain{} -> False
41+ Para{} -> False
42+ _ -> True
3743 tbl <- captionlessTable opts attr colspecs thead tbodies tfoot
3844 captionDoc <- if null captionBlocks
3945 then return empty
4046 else do
4147 blockToJATS <- asks jatsBlockWriter
42− inTagsIndented "caption" . vcat <$>
43− mapM (blockToJATS opts) captionBlocks
48+ inTagsIndented "caption" <$>
49+ blockToJATS needsWrapping opts captionBlocks
4450 return $ inTags True "table-wrap" [] $ captionDoc $$ tbl
4551
4652captionlessTable :: PandocMonad m
@@ -230,7 +236,7 @@ tableCellToJats opts ctype colAlign (Cell attr align rowspan colspan item) = do
230236 inlinesToJats <- asks jatsInlinesWriter
231237 let cellContents = \case
232238 [Plain inlines] -> inlinesToJats opts inlines
233− blocks -> vcat <$> mapM (blockToJats opts) blocks
239+ blocks -> blockToJats needsWrapInCell opts blocks
234240 let tag' = case ctype of
235241 BodyCell -> "td"
236242 HeaderCell -> "th"
@@ -246,3 +252,17 @@ tableCellToJats opts ctype colAlign (Cell attr align rowspan colspan item) = do
246252 . maybeCons (colspanAttrib colspan)
247253 $ toAttribs attr validAttribs
248254 inTags False tag' attribs <$> cellContents item
255+
256+-- | Whether the JATS produced from this block should be wrapped in a
257+-- @<p>@ element when put directly below a @<td>@ element.
258+needsWrapInCell :: Block -> Bool
259+needsWrapInCell = \case
260+ Plain{} -> False -- should be unwrapped anyway
261+ Para{} -> False
262+ BulletList{} -> False
263+ OrderedList{} -> False
264+ DefinitionList{} -> False
265+ HorizontalRule -> False
266+ CodeBlock{} -> False
267+ RawBlock{} -> False -- responsibility of the user
268+ _ -> True
src/Text/Pandoc/Writers/JATS/Types.hs+12−3
@@ -37,11 +37,20 @@ newtype JATSState = JATSState
3737 { jatsNotes :: [(Int, Doc Text)]
3838 }
3939
40+-- | Environment containing all information relevant for rendering.
4041data JATSEnv m = JATSEnv
41− { jatsTagSet :: JATSTagSet
42+ { jatsTagSet :: JATSTagSet -- ^ The tag set that's being ouput
43+
44+ , jatsBlockWriter :: (Block -> Bool)
45+ -> WriterOptions -> [Block] -> JATS m (Doc Text)
46+ -- ^ Converts a block list to JATS, wrapping top-level blocks into a
47+ -- @<p>@ element if the property evaluates to @True@.
48+ -- See #7227.
49+
4250 , jatsInlinesWriter :: WriterOptions -> [Inline] -> JATS m (Doc Text)
43− , jatsBlockWriter :: WriterOptions -> Block -> JATS m (Doc Text)
44− , jatsReferences :: [Reference Inlines]
51+ -- ^ Converts an inline list to JATS.
52+
53+ , jatsReferences :: [Reference Inlines] -- ^ List of references
4554 }
4655
4756-- | JATS writer type
test/command/7041.mdnew file+23
@@ -0,0 +1,23 @@
1+```
2+% pandoc -f html -t jats
3+<table>
4+ <tr><td><blockquote>Fly, you fools!</blockquote></td></tr>
5+</table>
6+^D
7+<table-wrap>
8+ <table>
9+ <colgroup>
10+ <col width="100%" />
11+ </colgroup>
12+ <tbody>
13+ <tr>
14+ <td><p specific-use="wrapper">
15+ <disp-quote>
16+ <p>Fly, you fools!</p>
17+ </disp-quote>
18+ </p></td>
19+ </tr>
20+ </tbody>
21+ </table>
22+</table-wrap>
23+```
test/writer.jats_archiving+28−40
@@ -78,39 +78,31 @@ Gruber’s markdown test suite.</p>
7878<sec id="block-quotes">
7979 <title>Block Quotes</title>
8080 <p>E-mail style:</p>
81− <p specific-use="wrapper">
81+ <disp-quote>
82+ <p>This is a block quote. It is pretty short.</p>
83+ </disp-quote>
84+ <disp-quote>
85+ <p>Code in a block quote:</p>
86+ <preformat>sub status {
87+ print &quot;working&quot;;
88+}</preformat>
89+ <p>A list:</p>
90+ <list list-type="order">
91+ <list-item>
92+ <p>item one</p>
93+ </list-item>
94+ <list-item>
95+ <p>item two</p>
96+ </list-item>
97+ </list>
98+ <p>Nested block quotes:</p>
8299 <disp-quote>
83− <p>This is a block quote. It is pretty short.</p>
100+ <p>nested</p>
84101 </disp-quote>
85− </p>
86− <p specific-use="wrapper">
87102 <disp-quote>
88− <p>Code in a block quote:</p>
89− <preformat>sub status {
90− print &quot;working&quot;;
91−}</preformat>
92− <p>A list:</p>
93− <list list-type="order">
94− <list-item>
95− <p>item one</p>
96− </list-item>
97− <list-item>
98− <p>item two</p>
99− </list-item>
100− </list>
101− <p>Nested block quotes:</p>
102− <p specific-use="wrapper">
103− <disp-quote>
104− <p>nested</p>
105− </disp-quote>
106− </p>
107− <p specific-use="wrapper">
108− <disp-quote>
109− <p>nested</p>
110− </disp-quote>
111− </p>
103+ <p>nested</p>
112104 </disp-quote>
113− </p>
105+ </disp-quote>
114106 <p>This should not be a block quote: 2 &gt; 1.</p>
115107 <p>And a following paragraph.</p>
116108</sec>
@@ -837,12 +829,10 @@ These should not be escaped: \$ \\ \&gt; \[ \{</preformat>
837829 </list-item>
838830 </list>
839831 <p>An e-mail address: <email>nobody@nowhere.net</email></p>
840− <p specific-use="wrapper">
841− <disp-quote>
842− <p>Blockquoted:
843− <ext-link ext-link-type="uri" xlink:href="http://example.com/">http://example.com/</ext-link></p>
844− </disp-quote>
845− </p>
832+ <disp-quote>
833+ <p>Blockquoted:
834+ <ext-link ext-link-type="uri" xlink:href="http://example.com/">http://example.com/</ext-link></p>
835+ </disp-quote>
846836 <p>Auto-links should not occur here:
847837 <monospace>&lt;http://example.com/&gt;</monospace></p>
848838 <preformat>or here: &lt;http://example.com/&gt;</preformat>
@@ -866,11 +856,9 @@ These should not be escaped: \$ \\ \&gt; \[ \{</preformat>
866856 <italic>not</italic> be a footnote reference, because it contains a
867857 space.[^my note] Here is an inline
868858 note.<xref ref-type="fn" rid="fn3">3</xref></p>
869− <p specific-use="wrapper">
870− <disp-quote>
871− <p>Notes can go in quotes.<xref ref-type="fn" rid="fn4">4</xref></p>
872− </disp-quote>
873− </p>
859+ <disp-quote>
860+ <p>Notes can go in quotes.<xref ref-type="fn" rid="fn4">4</xref></p>
861+ </disp-quote>
874862 <list list-type="order">
875863 <list-item>
876864 <p>And in list items.<xref ref-type="fn" rid="fn5">5</xref></p>
test/writer.jats_articleauthoring+40−48
@@ -67,43 +67,39 @@ Gruber’s markdown test suite.</p>
6767<sec id="block-quotes">
6868 <title>Block Quotes</title>
6969 <p>E-mail style:</p>
70− <p specific-use="wrapper">
71− <disp-quote>
72− <p>This is a block quote. It is pretty short.</p>
73− </disp-quote>
74− </p>
75− <p specific-use="wrapper">
76− <disp-quote>
77− <p>Code in a block quote:</p>
78− <p specific-use="wrapper">
79− <preformat>sub status {
70+ <disp-quote>
71+ <p>This is a block quote. It is pretty short.</p>
72+ </disp-quote>
73+ <disp-quote>
74+ <p>Code in a block quote:</p>
75+ <p specific-use="wrapper">
76+ <preformat>sub status {
8077 print &quot;working&quot;;
8178}</preformat>
82− </p>
83− <p>A list:</p>
84− <p specific-use="wrapper">
85− <list list-type="order">
86− <list-item>
87− <p>item one</p>
88− </list-item>
89− <list-item>
90− <p>item two</p>
91− </list-item>
92− </list>
93− </p>
94− <p>Nested block quotes:</p>
95− <p specific-use="wrapper">
96− <disp-quote>
97− <p>nested</p>
98− </disp-quote>
99− </p>
100− <p specific-use="wrapper">
101− <disp-quote>
102− <p>nested</p>
103− </disp-quote>
104− </p>
105− </disp-quote>
106− </p>
79+ </p>
80+ <p>A list:</p>
81+ <p specific-use="wrapper">
82+ <list list-type="order">
83+ <list-item>
84+ <p>item one</p>
85+ </list-item>
86+ <list-item>
87+ <p>item two</p>
88+ </list-item>
89+ </list>
90+ </p>
91+ <p>Nested block quotes:</p>
92+ <p specific-use="wrapper">
93+ <disp-quote>
94+ <p>nested</p>
95+ </disp-quote>
96+ </p>
97+ <p specific-use="wrapper">
98+ <disp-quote>
99+ <p>nested</p>
100+ </disp-quote>
101+ </p>
102+ </disp-quote>
107103 <p>This should not be a block quote: 2 &gt; 1.</p>
108104 <p>And a following paragraph.</p>
109105</sec>
@@ -817,12 +813,10 @@ These should not be escaped: \$ \\ \&gt; \[ \{</preformat>
817813 </list-item>
818814 </list>
819815 <p>An e-mail address: <email>nobody@nowhere.net</email></p>
820− <p specific-use="wrapper">
821− <disp-quote>
822− <p>Blockquoted:
823− <ext-link ext-link-type="uri" xlink:href="http://example.com/">http://example.com/</ext-link></p>
824− </disp-quote>
825− </p>
816+ <disp-quote>
817+ <p>Blockquoted:
818+ <ext-link ext-link-type="uri" xlink:href="http://example.com/">http://example.com/</ext-link></p>
819+ </disp-quote>
826820 <p>Auto-links should not occur here:
827821 <monospace>&lt;http://example.com/&gt;</monospace></p>
828822 <preformat>or here: &lt;http://example.com/&gt;</preformat>
@@ -860,13 +854,11 @@ These should not be escaped: \$ \\ \&gt; \[ \{</preformat>
860854 and <monospace>]</monospace> verbatim characters, as well as [bracketed
861855 text].</p>
862856 </fn></p>
863− <p specific-use="wrapper">
864− <disp-quote>
865− <p>Notes can go in quotes.<fn>
866− <p>In quote.</p>
867− </fn></p>
868− </disp-quote>
869− </p>
857+ <disp-quote>
858+ <p>Notes can go in quotes.<fn>
859+ <p>In quote.</p>
860+ </fn></p>
861+ </disp-quote>
870862 <list list-type="order">
871863 <list-item>
872864 <p>And in list items.<fn>
test/writer.jats_publishing+28−40
@@ -78,39 +78,31 @@ Gruber’s markdown test suite.</p>
7878<sec id="block-quotes">
7979 <title>Block Quotes</title>
8080 <p>E-mail style:</p>
81− <p specific-use="wrapper">
81+ <disp-quote>
82+ <p>This is a block quote. It is pretty short.</p>
83+ </disp-quote>
84+ <disp-quote>
85+ <p>Code in a block quote:</p>
86+ <preformat>sub status {
87+ print &quot;working&quot;;
88+}</preformat>
89+ <p>A list:</p>
90+ <list list-type="order">
91+ <list-item>
92+ <p>item one</p>
93+ </list-item>
94+ <list-item>
95+ <p>item two</p>
96+ </list-item>
97+ </list>
98+ <p>Nested block quotes:</p>
8299 <disp-quote>
83− <p>This is a block quote. It is pretty short.</p>
100+ <p>nested</p>
84101 </disp-quote>
85− </p>
86− <p specific-use="wrapper">
87102 <disp-quote>
88− <p>Code in a block quote:</p>
89− <preformat>sub status {
90− print &quot;working&quot;;
91−}</preformat>
92− <p>A list:</p>
93− <list list-type="order">
94− <list-item>
95− <p>item one</p>
96− </list-item>
97− <list-item>
98− <p>item two</p>
99− </list-item>
100− </list>
101− <p>Nested block quotes:</p>
102− <p specific-use="wrapper">
103− <disp-quote>
104− <p>nested</p>
105− </disp-quote>
106− </p>
107− <p specific-use="wrapper">
108− <disp-quote>
109− <p>nested</p>
110− </disp-quote>
111− </p>
103+ <p>nested</p>
112104 </disp-quote>
113− </p>
105+ </disp-quote>
114106 <p>This should not be a block quote: 2 &gt; 1.</p>
115107 <p>And a following paragraph.</p>
116108</sec>
@@ -837,12 +829,10 @@ These should not be escaped: \$ \\ \&gt; \[ \{</preformat>
837829 </list-item>
838830 </list>
839831 <p>An e-mail address: <email>nobody@nowhere.net</email></p>
840− <p specific-use="wrapper">
841− <disp-quote>
842− <p>Blockquoted:
843− <ext-link ext-link-type="uri" xlink:href="http://example.com/">http://example.com/</ext-link></p>
844− </disp-quote>
845− </p>
832+ <disp-quote>
833+ <p>Blockquoted:
834+ <ext-link ext-link-type="uri" xlink:href="http://example.com/">http://example.com/</ext-link></p>
835+ </disp-quote>
846836 <p>Auto-links should not occur here:
847837 <monospace>&lt;http://example.com/&gt;</monospace></p>
848838 <preformat>or here: &lt;http://example.com/&gt;</preformat>
@@ -866,11 +856,9 @@ These should not be escaped: \$ \\ \&gt; \[ \{</preformat>
866856 <italic>not</italic> be a footnote reference, because it contains a
867857 space.[^my note] Here is an inline
868858 note.<xref ref-type="fn" rid="fn3">3</xref></p>
869− <p specific-use="wrapper">
870− <disp-quote>
871− <p>Notes can go in quotes.<xref ref-type="fn" rid="fn4">4</xref></p>
872− </disp-quote>
873− </p>
859+ <disp-quote>
860+ <p>Notes can go in quotes.<xref ref-type="fn" rid="fn4">4</xref></p>
861+ </disp-quote>
874862 <list list-type="order">
875863 <list-item>
876864 <p>And in list items.<xref ref-type="fn" rid="fn5">5</xref></p>