git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: Pick table width from the longest row or header

This change is intended to preserve as much of the table content as possible Closes #4360

commite6ff7f79861d3088f8cba2b22d86d9f75db73f6a authordanse <f.occhipinti@gmail.com> dateMon 12 Feb 2018, 17:10 (9 years ago) parent82a0cea
4 files changed+26−5
src/Text/Pandoc/Readers/Docx.hs+9−5 test/Tests/Readers/Docx.hs+4 test/docx/table_variable_width.docx test/docx/table_variable_width.native+13
src/Text/Pandoc/Readers/Docx.hs+9−5
@@ -642,7 +642,7 @@ bodyPartToBlocks (ListItem pPr _ _ _ parparts) =
642642 bodyPartToBlocks $ Paragraph pPr' parparts
643643bodyPartToBlocks (Tbl _ _ _ []) =
644644 return $ para mempty
645−bodyPartToBlocks (Tbl cap _ look (r:rs)) = do
645+bodyPartToBlocks (Tbl cap _ look parts@(r:rs)) = do
646646 let caption = text cap
647647 (hdr, rows) = case firstRowFormatting look of
648648 True | null rs -> (Nothing, [r])
@@ -651,10 +651,14 @@ bodyPartToBlocks (Tbl cap _ look (r:rs)) = do
651651
652652 cells <- mapM rowToBlocksList rows
653653
654− let width = case cells of
655− r':_ -> length r'
656− -- shouldn't happen
657− [] -> 0
654+ let width = maybe 0 maximum $ nonEmpty $ map rowLength parts
655+ -- Data.List.NonEmpty is not available with ghc 7.10 so we roll out
656+ -- our own, see
657+ -- https://github.com/jgm/pandoc/pull/4361#issuecomment-365416155
658+ nonEmpty [] = Nothing
659+ nonEmpty l = Just l
660+ rowLength :: Row -> Int
661+ rowLength (Row c) = length c
658662
659663 hdrCells <- case hdr of
660664 Just r' -> rowToBlocksList r'
test/Tests/Readers/Docx.hs+4
@@ -272,6 +272,10 @@ tests = [ testGroup "inlines"
272272 "tables with one row"
273273 "docx/table_one_row.docx"
274274 "docx/table_one_row.native"
275+ , testCompare
276+ "tables with variable width"
277+ "docx/table_variable_width.docx"
278+ "docx/table_variable_width.native"
275279 , testCompare
276280 "code block"
277281 "docx/codeblock.docx"
test/docx/table_variable_width.docxnew file

Binary file not shown.

test/docx/table_variable_width.nativenew file+13
@@ -0,0 +1,13 @@
1+[Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.0,0.0,0.0,0.0,0.0]
2+ [[]
3+ ,[]
4+ ,[Plain [Str "h3"]]
5+ ,[Plain [Str "h4"]]
6+ ,[Plain [Str "h5"]]]
7+ [[[Plain [Str "c11"]]
8+ ,[]
9+ ,[]]
10+ ,[[]
11+ ,[Plain [Str "c22"]]
12+ ,[Plain [Str "c23"]]
13+ ,[]]]]