git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: Pick table width from the longest row or header
e6ff7f7danse
src/Text/Pandoc/Readers/Docx.hs+9−5
@@ -642,7 +642,7 @@ bodyPartToBlocks (ListItem pPr _ _ _ parparts) =
642 bodyPartToBlocks $ Paragraph pPr' parparts
642 bodyPartToBlocks $ Paragraph pPr' parparts
643bodyPartToBlocks (Tbl _ _ _ []) =
643bodyPartToBlocks (Tbl _ _ _ []) =
644 return $ para mempty
644 return $ para mempty
645−bodyPartToBlocks (Tbl cap _ look (r:rs)) = do
645+bodyPartToBlocks (Tbl cap _ look parts@(r:rs)) = do
646 let caption = text cap
646 let caption = text cap
647 (hdr, rows) = case firstRowFormatting look of
647 (hdr, rows) = case firstRowFormatting look of
648 True | null rs -> (Nothing, [r])
648 True | null rs -> (Nothing, [r])
@@ -651,10 +651,14 @@ bodyPartToBlocks (Tbl cap _ look (r:rs)) = do
651
651
652 cells <- mapM rowToBlocksList rows
652 cells <- mapM rowToBlocksList rows
653
653
654− let width = case cells of
654+ let width = maybe 0 maximum $ nonEmpty $ map rowLength parts
655− r':_ -> length r'
655+ -- Data.List.NonEmpty is not available with ghc 7.10 so we roll out
656− -- shouldn't happen
656+ -- our own, see
657− [] -> 0
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
658
662
659 hdrCells <- case hdr of
663 hdrCells <- case hdr of
660 Just r' -> rowToBlocksList r'
664 Just r' -> rowToBlocksList r'
test/Tests/Readers/Docx.hs+4
@@ -272,6 +272,10 @@ tests = [ testGroup "inlines"
272 "tables with one row"
272 "tables with one row"
273 "docx/table_one_row.docx"
273 "docx/table_one_row.docx"
274 "docx/table_one_row.native"
274 "docx/table_one_row.native"
275+ , testCompare
276+ "tables with variable width"
277+ "docx/table_variable_width.docx"
278+ "docx/table_variable_width.native"
275 , testCompare
279 , testCompare
276 "code block"
280 "code block"
277 "docx/codeblock.docx"
281 "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+ ,[]]]]