git.iko.soy/typeable/pandoc
on master help sign in
[odt] Infer table's caption from the paragraph (#3224)

ODT's reader always put empty captions for the parsed tables. This commit 1) checks paragraphs that follow the table definition 2) treats specially a paragraph with a style named 'Table' 3) does some postprocessing of the paragraphs that combines tables followed immediately by captions The ODT writer used 'TableCaption' style name for the caption paragraph. This commit follows the open office approach which allows for appending captions to table but uses a built-in style named 'Table' instead of 'TableCaption'. Any users of odt format (both writer and reader) are therefore required to change the style's name to 'Table', if necessary.

commit015dead0bb2bb5cea06a0fa366fdd651c8e07889 authorhubertp-lshift <hubertp@lshift.de> dateSat 26 Nov 2016, 21:45 (10 years ago) parentbaa2536
7 files changed+29−14
data/odt/styles.xml+1−1 src/Text/Pandoc/Readers/Odt/ContentReader.hs+21−6 src/Text/Pandoc/Writers/OpenDocument.hs+1−1 tests/Tests/Readers/Odt.hs+1 tests/odt/native/simpleTableWithCaption.native+1 tests/odt/odt/simpleTableWithCaption.odt tests/tables.opendocument+4−6
data/odt/styles.xml+1−1
@@ -133,7 +133,7 @@ xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
133133 style:font-size-complex="12pt"
134134 style:font-style-complex="italic" />
135135 </style:style>
136− <style:style style:name="TableCaption" style:family="paragraph"
136+ <style:style style:name="Table" style:family="paragraph"
137137 style:parent-style-name="Caption" style:class="extra">
138138 </style:style>
139139 <style:style style:name="FigureCaption" style:family="paragraph"
src/Text/Pandoc/Readers/Odt/ContentReader.hs+21−6
@@ -312,7 +312,7 @@ withNewStyle a = proc x -> do
312312 where
313313 isCodeStyle :: StyleName -> Bool
314314 isCodeStyle "Source_Text" = True
315− isCodeStyle _ = False
315+ isCodeStyle _ = False
316316
317317 inlineCode :: Inlines -> Inlines
318318 inlineCode = code . intercalate "" . map stringify . toList
@@ -415,12 +415,18 @@ constructPara reader = proc blocks -> do
415415 fStyle <- readStyleByName -< blocks
416416 case fStyle of
417417 Left _ -> reader -< blocks
418+ Right (styleName, _) | isTableCaptionStyle styleName -> do
419+ blocks' <- reader -< blocks
420+ arr tableCaptionP -< blocks'
418421 Right (_, style) -> do
419422 let modifier = getParaModifier style
420− blocks' <- reader -< blocks
421− arr modifier -<< blocks'
422−
423−
423+ blocks' <- reader -< blocks
424+ arr modifier -<< blocks'
425+ where
426+ isTableCaptionStyle :: StyleName -> Bool
427+ isTableCaptionStyle "Table" = True
428+ isTableCaptionStyle _ = False
429+ tableCaptionP b = divWith ("", ["caption"], []) b
424430
425431type ListConstructor = [Blocks] -> Blocks
426432
@@ -904,6 +910,15 @@ read_text = matchChildContent' [ read_header
904910 ]
905911 >>^ doc
906912
913+post_process :: Pandoc -> Pandoc
914+post_process (Pandoc m blocks) =
915+ Pandoc m (post_process' blocks)
916+
917+post_process' :: [Block] -> [Block]
918+post_process' ((Table _ a w h r) : (Div ("", ["caption"], _) [Para inlines] ) : xs) =
919+ (Table inlines a w h r) : ( post_process' xs )
920+post_process' bs = bs
921+
907922read_body :: OdtReader _x (Pandoc, MediaBag)
908923read_body = executeIn NsOffice "body"
909924 $ executeIn NsOffice "text"
@@ -911,4 +926,4 @@ read_body = executeIn NsOffice "body"
911926 $ proc inlines -> do
912927 txt <- read_text -< inlines
913928 state <- getExtraState -< ()
914− returnA -< (txt, getMediaBag state)
929+ returnA -< (post_process txt, getMediaBag state)
src/Text/Pandoc/Writers/OpenDocument.hs+1−1
@@ -336,7 +336,7 @@ blockToOpenDocument o bs
336336 mapM_ addParaStyle . newPara $ paraHStyles ++ paraStyles
337337 captionDoc <- if null c
338338 then return empty
339− else withParagraphStyle o "TableCaption" [Para c]
339+ else withParagraphStyle o "Table" [Para c]
340340 th <- if all null h
341341 then return empty
342342 else colHeadsToOpenDocument o name (map fst paraHStyles) h
tests/Tests/Readers/Odt.hs+1
@@ -157,6 +157,7 @@ namesOfTestsComparingToNative = [ "blockquote"
157157 , "referenceToListItem"
158158 , "referenceToText"
159159 , "simpleTable"
160+ , "simpleTableWithCaption"
160161-- , "table"
161162 , "unicode"
162163 , "unorderedList"
tests/odt/native/simpleTableWithCaption.nativenew file+1
@@ -0,0 +1 @@
1+[Table [Str "Table",Space,Str "1:",Space,Str "Some",Space,Str "caption",Space,Str "for",Space,Str "a",Space,Str "table"] [AlignDefault,AlignDefault] [0.0,0.0] [[],[]] [[[Plain [Str "Content"]],[Plain [Str "More",Space,Str "content"]]]],Para []]
tests/odt/odt/simpleTableWithCaption.odtnew file

Binary file not shown.

tests/tables.opendocument+4−6
@@ -63,8 +63,7 @@
6363 </table:table-cell>
6464 </table:table-row>
6565</table:table>
66−<text:p text:style-name="TableCaption">Demonstration of simple table
67−syntax.</text:p>
66+<text:p text:style-name="Table">Demonstration of simple table syntax.</text:p>
6867<text:p text:style-name="First_20_paragraph">Simple table without
6968caption:</text:p>
7069<table:table table:name="Table2" table:style-name="Table2">
@@ -197,8 +196,7 @@ spaces:</text:p>
197196 </table:table-cell>
198197 </table:table-row>
199198</table:table>
200−<text:p text:style-name="TableCaption">Demonstration of simple table
201−syntax.</text:p>
199+<text:p text:style-name="Table">Demonstration of simple table syntax.</text:p>
202200<text:p text:style-name="First_20_paragraph">Multiline table with
203201caption:</text:p>
204202<table:table table:name="Table4" table:style-name="Table4">
@@ -253,8 +251,8 @@ caption:</text:p>
253251 </table:table-cell>
254252 </table:table-row>
255253</table:table>
256−<text:p text:style-name="TableCaption">Here's the caption. It may span
257−multiple lines.</text:p>
254+<text:p text:style-name="Table">Here's the caption. It may span multiple
255+lines.</text:p>
258256<text:p text:style-name="First_20_paragraph">Multiline table without
259257caption:</text:p>
260258<table:table table:name="Table5" table:style-name="Table5">