diff --git a/src/Text/Pandoc/Readers/Docx.hs b/src/Text/Pandoc/Readers/Docx.hs index efe104af7..3222e81b6 100644 --- a/src/Text/Pandoc/Readers/Docx.hs +++ b/src/Text/Pandoc/Readers/Docx.hs @@ -815,6 +815,8 @@ bodyPartToBlocks (Tbl mbsty cap grid look parts) = do cap' = caption shortCaption fullCaption (hdr, rows) = splitHeaderRows (firstRowFormatting look) parts + let rowHeadCols = if firstColumnFormatting look then 1 else 0 + let width = maybe 0 maximum $ nonEmpty $ map rowLength parts rowLength :: Docx.Row -> Int rowLength (Docx.Row _ c) = sum (fmap (\(Docx.Cell _ gridSpan _ _) -> fromIntegral gridSpan) c) @@ -838,7 +840,7 @@ bodyPartToBlocks (Tbl mbsty cap grid look parts) = do return $ tableWith attr cap' (zip alignments widths) (TableHead nullAttr headerCells) - [TableBody nullAttr 0 [] bodyCells] + [TableBody nullAttr (RowHeadColumns rowHeadCols) [] bodyCells] (TableFoot nullAttr []) bodyPartToBlocks HRule = pure Pandoc.horizontalRule diff --git a/src/Text/Pandoc/Readers/Docx/Parse.hs b/src/Text/Pandoc/Readers/Docx/Parse.hs index 99ffcaf09..43adf7930 100644 --- a/src/Text/Pandoc/Readers/Docx/Parse.hs +++ b/src/Text/Pandoc/Readers/Docx/Parse.hs @@ -294,11 +294,15 @@ data BodyPart = Paragraph ParagraphStyle [ParPart] type TblGrid = [Integer] -newtype TblLook = TblLook {firstRowFormatting::Bool} +data TblLook = TblLook { firstRowFormatting ::Bool + , firstColumnFormatting :: Bool + } deriving Show defaultTblLook :: TblLook -defaultTblLook = TblLook{firstRowFormatting = False} +defaultTblLook = TblLook{ firstRowFormatting = False + , firstColumnFormatting = False + } data Row = Row TblHeader [Cell] deriving Show @@ -691,17 +695,25 @@ elemToTblGrid _ _ = throwError WrongElem elemToTblLook :: NameSpaces -> Element -> D TblLook elemToTblLook ns element | isElem ns "w" "tblLook" element = - let firstRow = findAttrByName ns "w" "firstRow" element - val = findAttrByName ns "w" "val" element + let val = findAttrByName ns "w" "val" element firstRowFmt = - case firstRow of + case findAttrByName ns "w" "firstRow" element of Just "1" -> True Just _ -> False Nothing -> case val of Just bitMask -> testBitMask bitMask 0x020 Nothing -> False + firstColFmt = + case findAttrByName ns "w" "firstColumn" element of + Just "1" -> True + Just _ -> False + Nothing -> case val of + Just bitMask -> testBitMask bitMask 0x080 + Nothing -> False in - return TblLook{firstRowFormatting = firstRowFmt} + return TblLook{ firstRowFormatting = firstRowFmt + , firstColumnFormatting = firstColFmt + } elemToTblLook _ _ = throwError WrongElem elemToRow :: NameSpaces -> Element -> D Row diff --git a/src/Text/Pandoc/Writers/Docx/Table.hs b/src/Text/Pandoc/Writers/Docx/Table.hs index 0caab7cd5..6dcb3f027 100644 --- a/src/Text/Pandoc/Writers/Docx/Table.hs +++ b/src/Text/Pandoc/Writers/Docx/Table.hs @@ -63,6 +63,7 @@ import Text.Pandoc.XML.Light.Types import qualified Data.Text as T import qualified Text.Pandoc.Translations as Term import qualified Text.Pandoc.Writers.GridTable as Grid +import Data.Bits ((.|.)) tableToOpenXML :: PandocMonad m => WriterOptions @@ -71,7 +72,7 @@ tableToOpenXML :: PandocMonad m -> WS m [Content] tableToOpenXML opts blocksToOpenXML gridTable = do setFirstPara - let (Grid.Table (ident,_,tableAttr) caption colspecs _rowheads thead tbodies tfoot) = + let (Grid.Table (ident,_,tableAttr) caption colspecs rowheads thead tbodies tfoot) = gridTable let (Caption _maybeShortCaption captionBlocks) = caption tablenum <- gets stNextTableNum @@ -106,7 +107,8 @@ tableToOpenXML opts blocksToOpenXML gridTable = do -- 0×0100 Apply last column conditional formatting -- 0×0200 Do not apply row banding conditional formatting -- 0×0400 Do not apply column banding conditional formattin - let tblLookVal = if hasHeader then (0x20 :: Int) else 0 + let tblLookVal = (if hasHeader then (0x20 :: Int) else 0) .|. + (if rowheads > 0 then (0x80 :: Int) else 0) let (gridCols, tblWattr) = tableLayout (elems colspecs) listLevel <- asks envListLevel let tblStyle = fromMaybe "Table" (lookup "custom-style" tableAttr) @@ -122,7 +124,7 @@ tableToOpenXML opts blocksToOpenXML gridTable = do [ mknode "w:tblLayout" [("w:type", "fixed")] () | hasWidths ] ++ [ mknode "w:tblLook" [("w:firstRow",if hasHeader then "1" else "0") ,("w:lastRow",if hasFooter then "1" else "0") - ,("w:firstColumn","0") + ,("w:firstColumn",if rowheads > 0 then "1" else "0") ,("w:lastColumn","0") ,("w:noHBand","0") ,("w:noVBand","0") diff --git a/test/command/9358.docx b/test/command/9358.docx index c8d8798ce..97bf4abc0 100644 Binary files a/test/command/9358.docx and b/test/command/9358.docx differ diff --git a/test/command/9495.md b/test/command/9495.md new file mode 100644 index 000000000..6a3227be1 --- /dev/null +++ b/test/command/9495.md @@ -0,0 +1,98 @@ +``` +% pandoc -f native -t docx | pandoc -f docx -t native +[ Table + ( "" , [] , [] ) + (Caption Nothing []) + [ ( AlignDefault , ColWidth 0.5 ) + , ( AlignDefault , ColWidth 0.5 ) + ] + (TableHead + ( "" , [] , [] ) + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "2" ] ] + ] + ]) + [ TableBody + ( "" , [] , [] ) + (RowHeadColumns 1) + [] + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "4" ] ] + ] + ] + ] + (TableFoot ( "" , [] , [] ) []) +] +^D +[ Table + ( "" , [] , [] ) + (Caption Nothing []) + [ ( AlignDefault , ColWidth 0.5 ) + , ( AlignDefault , ColWidth 0.5 ) + ] + (TableHead + ( "" , [] , [] ) + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "2" ] ] + ] + ]) + [ TableBody + ( "" , [] , [] ) + (RowHeadColumns 1) + [] + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "4" ] ] + ] + ] + ] + (TableFoot ( "" , [] , [] ) []) +] +``` diff --git a/test/command/9603.docx b/test/command/9603.docx index 7d70a1249..16b3306f9 100644 Binary files a/test/command/9603.docx and b/test/command/9603.docx differ diff --git a/test/docx/table_captions_no_field.docx b/test/docx/table_captions_no_field.docx index 32d8ec1b5..e46149a2d 100644 Binary files a/test/docx/table_captions_no_field.docx and b/test/docx/table_captions_no_field.docx differ diff --git a/test/docx/table_captions_with_field.docx b/test/docx/table_captions_with_field.docx index db6de3088..7159213a3 100644 Binary files a/test/docx/table_captions_with_field.docx and b/test/docx/table_captions_with_field.docx differ diff --git a/test/docx/table_header_rowspan.docx b/test/docx/table_header_rowspan.docx index 1cc32a105..4bcc0014b 100644 Binary files a/test/docx/table_header_rowspan.docx and b/test/docx/table_header_rowspan.docx differ diff --git a/test/docx/table_one_header_row.docx b/test/docx/table_one_header_row.docx index db715dda8..e7ce0050f 100644 Binary files a/test/docx/table_one_header_row.docx and b/test/docx/table_one_header_row.docx differ diff --git a/test/docx/table_one_row.docx b/test/docx/table_one_row.docx index d05a856b5..b1316705b 100644 Binary files a/test/docx/table_one_row.docx and b/test/docx/table_one_row.docx differ diff --git a/test/docx/table_variable_width.docx b/test/docx/table_variable_width.docx index 899357968..3ac3f534b 100644 Binary files a/test/docx/table_variable_width.docx and b/test/docx/table_variable_width.docx differ diff --git a/test/docx/table_with_list_cell.docx b/test/docx/table_with_list_cell.docx index bf58c1abe..4b6cb7ac5 100644 Binary files a/test/docx/table_with_list_cell.docx and b/test/docx/table_with_list_cell.docx differ