Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reading DOCX, pick table width from the longest row -- closes #4360 #4361

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Text/Pandoc/Readers/Docx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions test/Tests/Readers/Docx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Binary file added test/docx/table_variable_width.docx
Binary file not shown.
13 changes: 13 additions & 0 deletions test/docx/table_variable_width.native
Original file line number Diff line number Diff line change
@@ -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"]]
,[]]]]