Skip to content

Commit f34bb7e

Browse files
jgmchristopherkenny
authored andcommitted
Docx reader and writer: support row heads.
Reader: When `w:tblLook` has `w:firstColumn` set (or an equivalent bit mask), we set row heads = 1 in the AST. Writer: set `w:firstColumn` in `w:tblLook` when there are row heads. (Word only allows one, so this is triggered by any number of row heads > 0.) Closes jgm#9495.
1 parent 845cc9b commit f34bb7e

File tree

13 files changed

+124
-10
lines changed

13 files changed

+124
-10
lines changed

src/Text/Pandoc/Readers/Docx.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,8 @@ bodyPartToBlocks (Tbl mbsty cap grid look parts) = do
815815
cap' = caption shortCaption fullCaption
816816
(hdr, rows) = splitHeaderRows (firstRowFormatting look) parts
817817

818+
let rowHeadCols = if firstColumnFormatting look then 1 else 0
819+
818820
let width = maybe 0 maximum $ nonEmpty $ map rowLength parts
819821
rowLength :: Docx.Row -> Int
820822
rowLength (Docx.Row _ c) = sum (fmap (\(Docx.Cell _ gridSpan _ _) -> fromIntegral gridSpan) c)
@@ -838,7 +840,7 @@ bodyPartToBlocks (Tbl mbsty cap grid look parts) = do
838840
return $ tableWith attr cap'
839841
(zip alignments widths)
840842
(TableHead nullAttr headerCells)
841-
[TableBody nullAttr 0 [] bodyCells]
843+
[TableBody nullAttr (RowHeadColumns rowHeadCols) [] bodyCells]
842844
(TableFoot nullAttr [])
843845
bodyPartToBlocks HRule = pure Pandoc.horizontalRule
844846

src/Text/Pandoc/Readers/Docx/Parse.hs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,15 @@ data BodyPart = Paragraph ParagraphStyle [ParPart]
294294

295295
type TblGrid = [Integer]
296296

297-
newtype TblLook = TblLook {firstRowFormatting::Bool}
297+
data TblLook = TblLook { firstRowFormatting ::Bool
298+
, firstColumnFormatting :: Bool
299+
}
298300
deriving Show
299301

300302
defaultTblLook :: TblLook
301-
defaultTblLook = TblLook{firstRowFormatting = False}
303+
defaultTblLook = TblLook{ firstRowFormatting = False
304+
, firstColumnFormatting = False
305+
}
302306

303307
data Row = Row TblHeader [Cell] deriving Show
304308

@@ -691,17 +695,25 @@ elemToTblGrid _ _ = throwError WrongElem
691695

692696
elemToTblLook :: NameSpaces -> Element -> D TblLook
693697
elemToTblLook ns element | isElem ns "w" "tblLook" element =
694-
let firstRow = findAttrByName ns "w" "firstRow" element
695-
val = findAttrByName ns "w" "val" element
698+
let val = findAttrByName ns "w" "val" element
696699
firstRowFmt =
697-
case firstRow of
700+
case findAttrByName ns "w" "firstRow" element of
698701
Just "1" -> True
699702
Just _ -> False
700703
Nothing -> case val of
701704
Just bitMask -> testBitMask bitMask 0x020
702705
Nothing -> False
706+
firstColFmt =
707+
case findAttrByName ns "w" "firstColumn" element of
708+
Just "1" -> True
709+
Just _ -> False
710+
Nothing -> case val of
711+
Just bitMask -> testBitMask bitMask 0x080
712+
Nothing -> False
703713
in
704-
return TblLook{firstRowFormatting = firstRowFmt}
714+
return TblLook{ firstRowFormatting = firstRowFmt
715+
, firstColumnFormatting = firstColFmt
716+
}
705717
elemToTblLook _ _ = throwError WrongElem
706718

707719
elemToRow :: NameSpaces -> Element -> D Row

src/Text/Pandoc/Writers/Docx/Table.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import Text.Pandoc.XML.Light.Types
6363
import qualified Data.Text as T
6464
import qualified Text.Pandoc.Translations as Term
6565
import qualified Text.Pandoc.Writers.GridTable as Grid
66+
import Data.Bits ((.|.))
6667

6768
tableToOpenXML :: PandocMonad m
6869
=> WriterOptions
@@ -71,7 +72,7 @@ tableToOpenXML :: PandocMonad m
7172
-> WS m [Content]
7273
tableToOpenXML opts blocksToOpenXML gridTable = do
7374
setFirstPara
74-
let (Grid.Table (ident,_,tableAttr) caption colspecs _rowheads thead tbodies tfoot) =
75+
let (Grid.Table (ident,_,tableAttr) caption colspecs rowheads thead tbodies tfoot) =
7576
gridTable
7677
let (Caption _maybeShortCaption captionBlocks) = caption
7778
tablenum <- gets stNextTableNum
@@ -106,7 +107,8 @@ tableToOpenXML opts blocksToOpenXML gridTable = do
106107
-- 0×0100 Apply last column conditional formatting
107108
-- 0×0200 Do not apply row banding conditional formatting
108109
-- 0×0400 Do not apply column banding conditional formattin
109-
let tblLookVal = if hasHeader then (0x20 :: Int) else 0
110+
let tblLookVal = (if hasHeader then (0x20 :: Int) else 0) .|.
111+
(if rowheads > 0 then (0x80 :: Int) else 0)
110112
let (gridCols, tblWattr) = tableLayout (elems colspecs)
111113
listLevel <- asks envListLevel
112114
let tblStyle = fromMaybe "Table" (lookup "custom-style" tableAttr)
@@ -122,7 +124,7 @@ tableToOpenXML opts blocksToOpenXML gridTable = do
122124
[ mknode "w:tblLayout" [("w:type", "fixed")] () | hasWidths ] ++
123125
[ mknode "w:tblLook" [("w:firstRow",if hasHeader then "1" else "0")
124126
,("w:lastRow",if hasFooter then "1" else "0")
125-
,("w:firstColumn","0")
127+
,("w:firstColumn",if rowheads > 0 then "1" else "0")
126128
,("w:lastColumn","0")
127129
,("w:noHBand","0")
128130
,("w:noVBand","0")

test/command/9358.docx

-12 Bytes
Binary file not shown.

test/command/9495.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
```
2+
% pandoc -f native -t docx | pandoc -f docx -t native
3+
[ Table
4+
( "" , [] , [] )
5+
(Caption Nothing [])
6+
[ ( AlignDefault , ColWidth 0.5 )
7+
, ( AlignDefault , ColWidth 0.5 )
8+
]
9+
(TableHead
10+
( "" , [] , [] )
11+
[ Row
12+
( "" , [] , [] )
13+
[ Cell
14+
( "" , [] , [] )
15+
AlignDefault
16+
(RowSpan 1)
17+
(ColSpan 1)
18+
[ Plain [ Str "1" ] ]
19+
, Cell
20+
( "" , [] , [] )
21+
AlignDefault
22+
(RowSpan 1)
23+
(ColSpan 1)
24+
[ Plain [ Str "2" ] ]
25+
]
26+
])
27+
[ TableBody
28+
( "" , [] , [] )
29+
(RowHeadColumns 1)
30+
[]
31+
[ Row
32+
( "" , [] , [] )
33+
[ Cell
34+
( "" , [] , [] )
35+
AlignDefault
36+
(RowSpan 1)
37+
(ColSpan 1)
38+
[ Plain [ Str "3" ] ]
39+
, Cell
40+
( "" , [] , [] )
41+
AlignDefault
42+
(RowSpan 1)
43+
(ColSpan 1)
44+
[ Plain [ Str "4" ] ]
45+
]
46+
]
47+
]
48+
(TableFoot ( "" , [] , [] ) [])
49+
]
50+
^D
51+
[ Table
52+
( "" , [] , [] )
53+
(Caption Nothing [])
54+
[ ( AlignDefault , ColWidth 0.5 )
55+
, ( AlignDefault , ColWidth 0.5 )
56+
]
57+
(TableHead
58+
( "" , [] , [] )
59+
[ Row
60+
( "" , [] , [] )
61+
[ Cell
62+
( "" , [] , [] )
63+
AlignDefault
64+
(RowSpan 1)
65+
(ColSpan 1)
66+
[ Plain [ Str "1" ] ]
67+
, Cell
68+
( "" , [] , [] )
69+
AlignDefault
70+
(RowSpan 1)
71+
(ColSpan 1)
72+
[ Plain [ Str "2" ] ]
73+
]
74+
])
75+
[ TableBody
76+
( "" , [] , [] )
77+
(RowHeadColumns 1)
78+
[]
79+
[ Row
80+
( "" , [] , [] )
81+
[ Cell
82+
( "" , [] , [] )
83+
AlignDefault
84+
(RowSpan 1)
85+
(ColSpan 1)
86+
[ Plain [ Str "3" ] ]
87+
, Cell
88+
( "" , [] , [] )
89+
AlignDefault
90+
(RowSpan 1)
91+
(ColSpan 1)
92+
[ Plain [ Str "4" ] ]
93+
]
94+
]
95+
]
96+
(TableFoot ( "" , [] , [] ) [])
97+
]
98+
```

test/command/9603.docx

17 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
-22 Bytes
Binary file not shown.
-136 Bytes
Binary file not shown.
29 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)