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
This commit is contained in:
danse 2018-02-12 17:10:29 +01:00 committed by Jesse Rosenthal
parent 82a0ceaf18
commit e6ff7f7986
4 changed files with 26 additions and 5 deletions

View file

@ -642,7 +642,7 @@ bodyPartToBlocks (ListItem pPr _ _ _ parparts) =
bodyPartToBlocks $ Paragraph pPr' parparts
bodyPartToBlocks (Tbl _ _ _ []) =
return $ para mempty
bodyPartToBlocks (Tbl cap _ look (r:rs)) = do
bodyPartToBlocks (Tbl cap _ look parts@(r:rs)) = do
let caption = text cap
(hdr, rows) = case firstRowFormatting look of
True | null rs -> (Nothing, [r])
@ -651,10 +651,14 @@ bodyPartToBlocks (Tbl cap _ look (r:rs)) = do
cells <- mapM rowToBlocksList rows
let width = case cells of
r':_ -> length r'
-- shouldn't happen
[] -> 0
let width = maybe 0 maximum $ nonEmpty $ map rowLength parts
-- Data.List.NonEmpty is not available with ghc 7.10 so we roll out
-- our own, see
-- https://github.com/jgm/pandoc/pull/4361#issuecomment-365416155
nonEmpty [] = Nothing
nonEmpty l = Just l
rowLength :: Row -> Int
rowLength (Row c) = length c
hdrCells <- case hdr of
Just r' -> rowToBlocksList r'

View file

@ -272,6 +272,10 @@ tests = [ testGroup "inlines"
"tables with one row"
"docx/table_one_row.docx"
"docx/table_one_row.native"
, testCompare
"tables with variable width"
"docx/table_variable_width.docx"
"docx/table_variable_width.native"
, testCompare
"code block"
"docx/codeblock.docx"

Binary file not shown.

View file

@ -0,0 +1,13 @@
[Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.0,0.0,0.0,0.0,0.0]
[[]
,[]
,[Plain [Str "h3"]]
,[Plain [Str "h4"]]
,[Plain [Str "h5"]]]
[[[Plain [Str "c11"]]
,[]
,[]]
,[[]
,[Plain [Str "c22"]]
,[Plain [Str "c23"]]
,[]]]]