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

OpenDocument Writer: Implement table cell alignment #6700

Merged
merged 2 commits into from
Sep 27, 2020
Merged
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
38 changes: 33 additions & 5 deletions src/Text/Pandoc/Writers/OpenDocument.hs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ tableRowToOpenDocument o ns r =
in inTagsIndented "table:table-row" . vcat <$>
mapM (tableItemToOpenDocument o "TableRowCell") (zip ns c)


colspanAttrib :: ColSpan -> [(Text, Text)]
colspanAttrib cs =
case cs of
Expand All @@ -505,17 +504,32 @@ rowspanAttrib rs =
RowSpan 1 -> mempty
RowSpan n -> [("table:number-rows-spanned", tshow n)]

alignAttrib :: Alignment -> [(Text,Text)]
alignAttrib a = case a of
AlignRight -> ("fo:text-align","end") : style
AlignCenter -> ("fo:text-align","center") : style
_ -> []
where
style = [("style:justify-single-word","false")]

tableItemToOpenDocument :: PandocMonad m
=> WriterOptions -> Text -> (Text,Ann.Cell)
-> OD m (Doc Text)
tableItemToOpenDocument o s (n,c) =
let (Ann.Cell _colspecs _colnum (Cell _ _ rs cs i) ) = c
tableItemToOpenDocument o s (n,c) = do
let (Ann.Cell _colspecs _colnum (Cell _ align rs cs i) ) = c
csa = colspanAttrib cs
rsa = rowspanAttrib rs
aa = alignAttrib align
a = [ ("table:style-name" , s )
, ("office:value-type", "string" ) ] ++ csa ++ rsa
in inTags True "table:table-cell" a <$>
withParagraphStyle o n (map plainToPara i)
itemParaStyle <- case aa of
[] -> return 0
_ -> paraStyleFromParent n aa
let itemParaStyle' = case itemParaStyle of
0 -> n
x -> "P" <> tshow x
inTags True "table:table-cell" a <$>
withParagraphStyle o itemParaStyle' (map plainToPara i)

-- | Convert a list of inline elements to OpenDocument.
inlinesToOpenDocument :: PandocMonad m => WriterOptions -> [Inline] -> OD m (Doc Text)
Expand Down Expand Up @@ -713,6 +727,20 @@ paraStyle attrs = do
addParaStyle $ inTags True "style:style" (styleAttr <> attrs) paraProps
return pn

paraStyleFromParent :: PandocMonad m => Text -> [(Text,Text)] -> OD m Int
paraStyleFromParent parent attrs = do
pn <- (+) 1 . length <$> gets stParaStyles
let styleAttr = [ ("style:name" , "P" <> tshow pn)
, ("style:family" , "paragraph")
, ("style:parent-style-name", parent)]
paraProps = if null attrs
then mempty
else selfClosingTag
"style:paragraph-properties" attrs
addParaStyle $ inTags True "style:style" styleAttr paraProps
return pn


paraListStyle :: PandocMonad m => Int -> OD m Int
paraListStyle l = paraStyle
[("style:parent-style-name","Text_20_body")
Expand Down
45 changes: 45 additions & 0 deletions test/command/table-with-cell-align.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,48 @@
(TableFoot ("",[],[])
[])]
```
```
% pandoc -f native -t opendocument --quiet
[Table ("",[],[]) (Caption Nothing
[])
[(AlignDefault,ColWidthDefault)
,(AlignDefault,ColWidthDefault)
,(AlignDefault,ColWidthDefault)
,(AlignDefault,ColWidthDefault)]
(TableHead ("",[],[])
[])
[(TableBody ("",[],[]) (RowHeadColumns 0)
[]
[Row ("",[],[])
[Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
[Para [Str "1"]]
,Cell ("",[],[]) AlignLeft (RowSpan 1) (ColSpan 1)
[Para [Str "2"]]
,Cell ("",[],[]) AlignRight (RowSpan 1) (ColSpan 1)
[Para [Str "3"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "4"]]]])]
(TableFoot ("",[],[])
[])]
^D
<table:table table:name="Table1" table:style-name="Table1">
<table:table-column table:style-name="Table1.A" />
<table:table-column table:style-name="Table1.B" />
<table:table-column table:style-name="Table1.C" />
<table:table-column table:style-name="Table1.D" />
<table:table-row>
<table:table-cell table:style-name="TableRowCell" office:value-type="string">
<text:p text:style-name="P1">1</text:p>
</table:table-cell>
<table:table-cell table:style-name="TableRowCell" office:value-type="string">
<text:p text:style-name="Table_20_Contents">2</text:p>
</table:table-cell>
<table:table-cell table:style-name="TableRowCell" office:value-type="string">
<text:p text:style-name="P2">3</text:p>
</table:table-cell>
<table:table-cell table:style-name="TableRowCell" office:value-type="string">
<text:p text:style-name="Table_20_Contents">4</text:p>
</table:table-cell>
</table:table-row>
</table:table>
```
40 changes: 20 additions & 20 deletions test/command/table-with-column-span.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,47 +142,47 @@
[(TableBody ("",[],[]) (RowHeadColumns 0)
[]
[Row ("",[],[])
[Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 8)
[Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 8)
[Para [Strong [Str "Octet",Space,Str "no.",Space,Str "1"]]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 8)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 8)
[Para [Strong [Str "Octet",Space,Str "no.",Space,Str "2"]]]]
,Row ("",[],[])
[Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
[Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "16"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "15"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "14"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "13"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "12"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "11"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "10"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "9"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "8"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "7"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "6"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "5"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "4"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "3"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "2"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Para [Str "1"]]]
,Row ("",[],[])
[Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 8)
[Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 8)
[Para [Str "Code",Space,Str "A"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 8)
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 8)
[Para [Str "Code",Space,Str "B"]]]])]
(TableFoot ("",[],[])
[])]
Expand Down