Skip to content

Commit

Permalink
OpenDocument writer: Table text width support
Browse files Browse the repository at this point in the history
Support for table width as a percentage of text width by summing
width of columns and verifying that the sum is > 0 and <= 1.
  • Loading branch information
pyssling committed Nov 20, 2020
1 parent d286242 commit e7f091a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Text/Pandoc/Writers/OpenDocument.hs
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,14 @@ blockToOpenDocument o bs
fromWidth (ColWidth w) | w > 0 = w
fromWidth _ = 0
widths = map fromWidth mwidths
textWidth = sum widths
columnIds = zip genIds widths
mkColumn n = selfClosingTag "table:table-column" [("table:style-name", name <> "." <> T.singleton (fst n))]
columns = map mkColumn columnIds
paraHStyles = paraTableStyles "Heading" pn aligns
paraStyles = paraTableStyles "Contents" (pn + length (newPara paraHStyles)) aligns
newPara = map snd . filter (not . isEmpty . snd)
addTableStyle $ tableStyle tn columnIds
addTableStyle $ tableStyle tn textWidth columnIds
mapM_ addParaStyle . newPara $ paraHStyles ++ paraStyles
captionDoc <- if null c
then return empty
Expand Down Expand Up @@ -684,14 +685,19 @@ listLevelStyle i =
, ("fo:margin-left", indent <> "in")
]

tableStyle :: Int -> [(Char,Double)] -> Doc Text
tableStyle num wcs =
tableStyle :: Int -> Double -> [(Char,Double)] -> Doc Text
tableStyle num textWidth wcs =
let tableId = "Table" <> tshow (num + 1)
tableWidthAttr :: [(Text,Text)]
tableWidthAttr
| textWidth <= 1 && textWidth > 0 = [("style:rel-width",
T.pack (show (round (textWidth * 100) :: Int) <> "%"))]
| otherwise = []
table = inTags True "style:style"
[("style:name", tableId)
,("style:family", "table")] $
selfClosingTag "style:table-properties"
[("table:align" , "center")]
(("table:align", "center") : tableWidthAttr)
colStyle (c,0) = selfClosingTag "style:style"
[ ("style:name" , tableId <> "." <> T.singleton c)
, ("style:family", "table-column" )]
Expand Down
63 changes: 63 additions & 0 deletions test/command/6792.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
```
% pandoc -f native -t opendocument --template command/6792.template --quiet
[Table ("",[],[]) (Caption Nothing
[])
[(AlignDefault,ColWidth 0.25)
,(AlignDefault,ColWidth 0.25)]
(TableHead ("",[],[])
[])
[(TableBody ("",[],[]) (RowHeadColumns 0)
[]
[Row ("",[],[])
[Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
[Para [Str "2"]]
,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
[Para [Str "1"]]]])]
(TableFoot ("",[],[])
[])]
^D
<?xml version="1.0" encoding="utf-8" ?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.2">
<office:automatic-styles>
<style:style style:name="fr2" style:family="graphic" style:parent-style-name="Formula"><style:graphic-properties style:vertical-pos="middle" style:vertical-rel="paragraph-content" style:horizontal-pos="center" style:horizontal-rel="paragraph-content" style:wrap="none" /></style:style>
<style:style style:name="fr1" style:family="graphic" style:parent-style-name="Formula"><style:graphic-properties style:vertical-pos="middle" style:vertical-rel="text" /></style:style>
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Table_20_Contents">
<style:paragraph-properties fo:text-align="center" style:justify-single-word="false" />
</style:style>
<style:style style:name="P2" style:family="paragraph" style:parent-style-name="Table_20_Contents">
<style:paragraph-properties fo:text-align="center" style:justify-single-word="false" />
</style:style>
<style:style style:name="TableHeaderRowCell" style:family="table-cell">
<style:table-cell-properties fo:border="none" />
</style:style>
<style:style style:name="TableRowCell" style:family="table-cell">
<style:table-cell-properties fo:border="none" />
</style:style>
<style:style style:name="Table1" style:family="table">
<style:table-properties table:align="center" style:rel-width="50%" />
</style:style>
<style:style style:name="Table1.A" style:family="table-column">
<style:table-column-properties style:rel-column-width="16383*" />
</style:style>
<style:style style:name="Table1.B" style:family="table-column">
<style:table-column-properties style:rel-column-width="16383*" />
</style:style>
</office:automatic-styles>
<office:body>
<office:text>
<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-row>
<table:table-cell table:style-name="TableRowCell" office:value-type="string">
<text:p text:style-name="P1">2</text:p>
</table:table-cell>
<table:table-cell table:style-name="TableRowCell" office:value-type="string">
<text:p text:style-name="P2">1</text:p>
</table:table-cell>
</table:table-row>
</table:table>
</office:text>
</office:body>
</office:document-content>
```
11 changes: 11 additions & 0 deletions test/command/6792.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.2">
<office:automatic-styles>
$automatic-styles$
</office:automatic-styles>
<office:body>
<office:text>
$body$
</office:text>
</office:body>
</office:document-content>

0 comments on commit e7f091a

Please sign in to comment.