git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: Read table column widths.
commit44484d0dee1bd095240b9faf26f8d1dad8e560ea authorEmily Bourke <undergroundquizscene@gmail.com> dateSun 11 Apr 2021, 21:42 (6 years ago) parent4842c5f
14 files changed+128−30
src/Text/Pandoc/Readers/Docx.hs+3−2 src/Text/Pandoc/Readers/Docx/Parse.hs+1−1 test/Tests/Writers/Docx.hs+5 test/docx/0_level_headers.native+2−2 test/docx/golden/table_one_row.docx test/docx/golden/table_with_list_cell.docx test/docx/golden/tables-default-widths.docx test/docx/golden/tables.docx test/docx/sdt_elements.native+3−3 test/docx/table_one_row.native+4−4 test/docx/table_variable_width.native+6−6 test/docx/table_with_list_cell.native+3−3 test/docx/tables-default-widths.native+92 test/docx/tables.native+9−9
src/Text/Pandoc/Readers/Docx.hs+3−2
@@ -639,7 +639,7 @@ bodyPartToBlocks (ListItem pPr _ _ _ parparts) =
639639 bodyPartToBlocks $ Paragraph pPr' parparts
640640bodyPartToBlocks (Tbl _ _ _ []) =
641641 return $ para mempty
642−bodyPartToBlocks (Tbl cap _ look parts@(r:rs)) = do
642+bodyPartToBlocks (Tbl cap grid look parts@(r:rs)) = do
643643 let cap' = simpleCaption $ plain $ text cap
644644 (hdr, rows) = case firstRowFormatting look of
645645 True | null rs -> (Nothing, [r])
@@ -669,7 +669,8 @@ bodyPartToBlocks (Tbl cap _ look parts@(r:rs)) = do
669669 -- so should be possible. Alignment might be more difficult,
670670 -- since there doesn't seem to be a column entity in docx.
671671 let alignments = replicate width AlignDefault
672− widths = replicate width ColWidthDefault
672+ totalWidth = sum grid
673+ widths = (\w -> ColWidth (fromInteger w / fromInteger totalWidth)) <$> grid
673674
674675 return $ table cap'
675676 (zip alignments widths)
src/Text/Pandoc/Readers/Docx/Parse.hs+1−1
@@ -563,7 +563,7 @@ elemToTblGrid :: NameSpaces -> Element -> D TblGrid
563563elemToTblGrid ns element | isElem ns "w" "tblGrid" element =
564564 let cols = findChildrenByName ns "w" "gridCol" element
565565 in
566− mapD (\e -> maybeToD (findAttrByName ns "w" "val" e >>= stringToInteger))
566+ mapD (\e -> maybeToD (findAttrByName ns "w" "w" e >>= stringToInteger))
567567 cols
568568elemToTblGrid _ _ = throwError WrongElem
569569
test/Tests/Writers/Docx.hs+5
@@ -111,6 +111,11 @@ tests = [ testGroup "inlines"
111111 def
112112 "docx/tables.native"
113113 "docx/golden/tables.docx"
114+ , docxTest
115+ "tables without explicit column widths"
116+ def
117+ "docx/tables-default-widths.native"
118+ "docx/golden/tables-default-widths.docx"
114119 , docxTest
115120 "tables with lists in cells"
116121 def
test/docx/0_level_headers.native+2−2
@@ -1,6 +1,6 @@
11[Table ("",[],[]) (Caption Nothing
22 [])
3− [(AlignDefault,ColWidthDefault)]
3+ [(AlignDefault,ColWidth 1.0)]
44 (TableHead ("",[],[])
55 [])
66 [(TableBody ("",[],[]) (RowHeadColumns 0)
@@ -49,4 +49,4 @@
4949,Para [Strong [Str "Table",Space,Str "Page"]]
5050,Para [Strong [Str "No",Space,Str "table",Space,Str "of",Space,Str "figures",Space,Str "entries",Space,Str "found."]]
5151,Header 1 ("introduction",[],[]) [Str "Introduction"]
52−,Para [Str "Nothing",Space,Str "to",Space,Str "introduce,",Space,Str "yet."]]
52+,Para [Str "Nothing",Space,Str "to",Space,Str "introduce,",Space,Str "yet."]]
test/docx/golden/table_one_row.docx

Binary file not shown.

test/docx/golden/table_with_list_cell.docx

Binary file not shown.

test/docx/golden/tables-default-widths.docxnew file

Binary file not shown.

test/docx/golden/tables.docx

Binary file not shown.

test/docx/sdt_elements.native+3−3
@@ -1,8 +1,8 @@
11[Table ("",[],[]) (Caption Nothing
22 [])
3− [(AlignDefault,ColWidthDefault)
4− ,(AlignDefault,ColWidthDefault)
5− ,(AlignDefault,ColWidthDefault)]
3+ [(AlignDefault,ColWidth 0.22069570301081556)
4+ ,(AlignDefault,ColWidth 0.22069570301081556)
5+ ,(AlignDefault,ColWidth 0.5586085939783689)]
66 (TableHead ("",[],[])
77 [])
88 [(TableBody ("",[],[]) (RowHeadColumns 0)
test/docx/table_one_row.native+4−4
@@ -1,8 +1,8 @@
11[Table ("",[],[]) (Caption Nothing
22 [])
3− [(AlignDefault,ColWidthDefault)
4− ,(AlignDefault,ColWidthDefault)
5− ,(AlignDefault,ColWidthDefault)]
3+ [(AlignDefault,ColWidth 0.3333333333333333)
4+ ,(AlignDefault,ColWidth 0.3333333333333333)
5+ ,(AlignDefault,ColWidth 0.3333333333333333)]
66 (TableHead ("",[],[])
77 [])
88 [(TableBody ("",[],[]) (RowHeadColumns 0)
@@ -15,4 +15,4 @@
1515 ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
1616 [Plain [Str "Table"]]]])]
1717 (TableFoot ("",[],[])
18− [])]
18+ [])]
test/docx/table_variable_width.native+6−6
@@ -1,10 +1,10 @@
11[Table ("",[],[]) (Caption Nothing
22 [])
3− [(AlignDefault,ColWidthDefault)
4− ,(AlignDefault,ColWidthDefault)
5− ,(AlignDefault,ColWidthDefault)
6− ,(AlignDefault,ColWidthDefault)
7− ,(AlignDefault,ColWidthDefault)]
3+ [(AlignDefault,ColWidth 2.0096205237840725e-2)
4+ ,(AlignDefault,ColWidth 1.9882415820416888e-2)
5+ ,(AlignDefault,ColWidth 0.22202030999465527)
6+ ,(AlignDefault,ColWidth 0.4761090326028862)
7+ ,(AlignDefault,ColWidth 1.0689470871191876e-4)]
88 (TableHead ("",[],[])
99 [Row ("",[],[])
1010 [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
@@ -42,4 +42,4 @@
4242 ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
4343 []]])]
4444 (TableFoot ("",[],[])
45− [])]
45+ [])]
test/docx/table_with_list_cell.native+3−3
@@ -1,7 +1,7 @@
11[Table ("",[],[]) (Caption Nothing
22 [])
3− [(AlignDefault,ColWidthDefault)
4− ,(AlignDefault,ColWidthDefault)]
3+ [(AlignDefault,ColWidth 0.5)
4+ ,(AlignDefault,ColWidth 0.5)]
55 (TableHead ("",[],[])
66 [Row ("",[],[])
77 [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
@@ -22,4 +22,4 @@
2222 ,[Para [Str "A"]]
2323 ,[Para [Str "Numbered",Space,Str "list."]]]]]])]
2424 (TableFoot ("",[],[])
25− [])]
25+ [])]
test/docx/tables-default-widths.nativenew file+92
@@ -0,0 +1,92 @@
1+[Header 2 ("a-table-with-and-without-a-header-row",[],[]) [Str "A",Space,Str "table,",Space,Str "with",Space,Str "and",Space,Str "without",Space,Str "a",Space,Str "header",Space,Str "row"]
2+,Table ("",[],[]) (Caption Nothing
3+ [])
4+ [(AlignDefault,ColWidthDefault)
5+ ,(AlignDefault,ColWidthDefault)
6+ ,(AlignDefault,ColWidthDefault)
7+ ,(AlignDefault,ColWidthDefault)]
8+ (TableHead ("",[],[])
9+ [Row ("",[],[])
10+ [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
11+ [Plain [Str "Name"]]
12+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
13+ [Plain [Str "Game"]]
14+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
15+ [Plain [Str "Fame"]]
16+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
17+ [Plain [Str "Blame"]]]])
18+ [(TableBody ("",[],[]) (RowHeadColumns 0)
19+ []
20+ [Row ("",[],[])
21+ [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
22+ [Plain [Str "Lebron",Space,Str "James"]]
23+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
24+ [Plain [Str "Basketball"]]
25+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
26+ [Plain [Str "Very",Space,Str "High"]]
27+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
28+ [Plain [Str "Leaving",Space,Str "Cleveland"]]]
29+ ,Row ("",[],[])
30+ [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
31+ [Plain [Str "Ryan",Space,Str "Braun"]]
32+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
33+ [Plain [Str "Baseball"]]
34+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
35+ [Plain [Str "Moderate"]]
36+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
37+ [Plain [Str "Steroids"]]]
38+ ,Row ("",[],[])
39+ [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
40+ [Plain [Str "Russell",Space,Str "Wilson"]]
41+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
42+ [Plain [Str "Football"]]
43+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
44+ [Plain [Str "High"]]
45+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
46+ [Plain [Str "Tacky",Space,Str "uniform"]]]])]
47+ (TableFoot ("",[],[])
48+ [])
49+,Table ("",[],[]) (Caption Nothing
50+ [])
51+ [(AlignDefault,ColWidthDefault)
52+ ,(AlignDefault,ColWidthDefault)]
53+ (TableHead ("",[],[])
54+ [])
55+ [(TableBody ("",[],[]) (RowHeadColumns 0)
56+ []
57+ [Row ("",[],[])
58+ [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
59+ [Plain [Str "Sinple"]]
60+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
61+ [Plain [Str "Table"]]]
62+ ,Row ("",[],[])
63+ [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
64+ [Plain [Str "Without"]]
65+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
66+ [Plain [Str "Header"]]]])]
67+ (TableFoot ("",[],[])
68+ [])
69+,Table ("",[],[]) (Caption Nothing
70+ [])
71+ [(AlignDefault,ColWidthDefault)
72+ ,(AlignDefault,ColWidthDefault)]
73+ (TableHead ("",[],[])
74+ [])
75+ [(TableBody ("",[],[]) (RowHeadColumns 0)
76+ []
77+ [Row ("",[],[])
78+ [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
79+ [Para [Str "Simple"]
80+ ,Para [Str "Multiparagraph"]]
81+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
82+ [Para [Str "Table"]
83+ ,Para [Str "Full"]]]
84+ ,Row ("",[],[])
85+ [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
86+ [Para [Str "Of"]
87+ ,Para [Str "Paragraphs"]]
88+ ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
89+ [Para [Str "In",Space,Str "each"]
90+ ,Para [Str "Cell."]]]])]
91+ (TableFoot ("",[],[])
92+ [])]
test/docx/tables.native+9−9
@@ -1,10 +1,10 @@
11[Header 2 ("a-table-with-and-without-a-header-row",[],[]) [Str "A",Space,Str "table,",Space,Str "with",Space,Str "and",Space,Str "without",Space,Str "a",Space,Str "header",Space,Str "row"]
22,Table ("",[],[]) (Caption Nothing
33 [])
4− [(AlignDefault,ColWidthDefault)
5− ,(AlignDefault,ColWidthDefault)
6− ,(AlignDefault,ColWidthDefault)
7− ,(AlignDefault,ColWidthDefault)]
4+ [(AlignDefault,ColWidth 0.25)
5+ ,(AlignDefault,ColWidth 0.25)
6+ ,(AlignDefault,ColWidth 0.25)
7+ ,(AlignDefault,ColWidth 0.25)]
88 (TableHead ("",[],[])
99 [Row ("",[],[])
1010 [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
@@ -48,8 +48,8 @@
4848 [])
4949,Table ("",[],[]) (Caption Nothing
5050 [])
51− [(AlignDefault,ColWidthDefault)
52− ,(AlignDefault,ColWidthDefault)]
51+ [(AlignDefault,ColWidth 0.5)
52+ ,(AlignDefault,ColWidth 0.5)]
5353 (TableHead ("",[],[])
5454 [])
5555 [(TableBody ("",[],[]) (RowHeadColumns 0)
@@ -68,8 +68,8 @@
6868 [])
6969,Table ("",[],[]) (Caption Nothing
7070 [])
71− [(AlignDefault,ColWidthDefault)
72− ,(AlignDefault,ColWidthDefault)]
71+ [(AlignDefault,ColWidth 0.5)
72+ ,(AlignDefault,ColWidth 0.5)]
7373 (TableHead ("",[],[])
7474 [])
7575 [(TableBody ("",[],[]) (RowHeadColumns 0)
@@ -89,4 +89,4 @@
8989 [Para [Str "In",Space,Str "each"]
9090 ,Para [Str "Cell."]]]])]
9191 (TableFoot ("",[],[])
92− [])]
92+ [])]