git.iko.soy/typeable/pandoc
on master help sign in
OpenDocument writer: Table text width support (#6792)

Support for table width as a percentage of text width by summing width of columns and verifying that the sum is > 0 and <= 1.

commitae52918faa36ad59deca3561acce79ad6d3ea0c2 authorNils Carlson <nils@nilscarlson.se> dateSat 21 Nov 2020, 20:42 (6 years ago) parent7db2cf5
2 files changed+76−4
src/Text/Pandoc/Writers/OpenDocument.hs+10−4 test/command/6792.md+66
src/Text/Pandoc/Writers/OpenDocument.hs+10−4
@@ -407,13 +407,14 @@ blockToOpenDocument o bs
407407 fromWidth (ColWidth w) | w > 0 = w
408408 fromWidth _ = 0
409409 widths = map fromWidth mwidths
410+ textWidth = sum widths
410411 columnIds = zip genIds widths
411412 mkColumn n = selfClosingTag "table:table-column" [("table:style-name", name <> "." <> T.singleton (fst n))]
412413 columns = map mkColumn columnIds
413414 paraHStyles = paraTableStyles "Heading" pn aligns
414415 paraStyles = paraTableStyles "Contents" (pn + length (newPara paraHStyles)) aligns
415416 newPara = map snd . filter (not . isEmpty . snd)
416− addTableStyle $ tableStyle tn columnIds
417+ addTableStyle $ tableStyle tn textWidth columnIds
417418 mapM_ addParaStyle . newPara $ paraHStyles ++ paraStyles
418419 captionDoc <- if null c
419420 then return empty
@@ -684,14 +685,19 @@ listLevelStyle i =
684685 , ("fo:margin-left", indent <> "in")
685686 ]
686687
687−tableStyle :: Int -> [(Char,Double)] -> Doc Text
688−tableStyle num wcs =
688+tableStyle :: Int -> Double -> [(Char,Double)] -> Doc Text
689+tableStyle num textWidth wcs =
689690 let tableId = "Table" <> tshow (num + 1)
691+ tableWidthAttr :: [(Text,Text)]
692+ tableWidthAttr
693+ | textWidth <= 1 && textWidth > 0 = [("style:rel-width",
694+ T.pack (show (round (textWidth * 100) :: Int) <> "%"))]
695+ | otherwise = []
690696 table = inTags True "style:style"
691697 [("style:name", tableId)
692698 ,("style:family", "table")] $
693699 selfClosingTag "style:table-properties"
694− [("table:align" , "center")]
700+ (("table:align", "center") : tableWidthAttr)
695701 colStyle (c,0) = selfClosingTag "style:style"
696702 [ ("style:name" , tableId <> "." <> T.singleton c)
697703 , ("style:family", "table-column" )]
test/command/6792.mdnew file+66
@@ -0,0 +1,66 @@
1+```
2+% pandoc -f native -t opendocument -s --quiet
3+[Table ("",[],[]) (Caption Nothing
4+ [])
5+ [(AlignDefault,ColWidth 0.25)
6+ ,(AlignDefault,ColWidth 0.25)]
7+ (TableHead ("",[],[])
8+ [])
9+ [(TableBody ("",[],[]) (RowHeadColumns 0)
10+ []
11+ [Row ("",[],[])
12+ [Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
13+ [Para [Str "2"]]
14+ ,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1)
15+ [Para [Str "1"]]]])]
16+ (TableFoot ("",[],[])
17+ [])]
18+^D
19+<?xml version="1.0" encoding="utf-8" ?>
20+<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">
21+ <office:font-face-decls>
22+ <style:font-face style:name="Courier New" style:font-family-generic="modern" style:font-pitch="fixed" svg:font-family="'Courier New'" />
23+ </office:font-face-decls>
24+ <office:automatic-styles>
25+ <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>
26+ <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>
27+ <style:style style:name="P1" style:family="paragraph" style:parent-style-name="Table_20_Contents">
28+ <style:paragraph-properties fo:text-align="center" style:justify-single-word="false" />
29+ </style:style>
30+ <style:style style:name="P2" style:family="paragraph" style:parent-style-name="Table_20_Contents">
31+ <style:paragraph-properties fo:text-align="center" style:justify-single-word="false" />
32+ </style:style>
33+ <style:style style:name="TableHeaderRowCell" style:family="table-cell">
34+ <style:table-cell-properties fo:border="none" />
35+ </style:style>
36+ <style:style style:name="TableRowCell" style:family="table-cell">
37+ <style:table-cell-properties fo:border="none" />
38+ </style:style>
39+ <style:style style:name="Table1" style:family="table">
40+ <style:table-properties table:align="center" style:rel-width="50%" />
41+ </style:style>
42+ <style:style style:name="Table1.A" style:family="table-column">
43+ <style:table-column-properties style:rel-column-width="16383*" />
44+ </style:style>
45+ <style:style style:name="Table1.B" style:family="table-column">
46+ <style:table-column-properties style:rel-column-width="16383*" />
47+ </style:style>
48+ </office:automatic-styles>
49+<office:body>
50+<office:text>
51+<table:table table:name="Table1" table:style-name="Table1">
52+ <table:table-column table:style-name="Table1.A" />
53+ <table:table-column table:style-name="Table1.B" />
54+ <table:table-row>
55+ <table:table-cell table:style-name="TableRowCell" office:value-type="string">
56+ <text:p text:style-name="P1">2</text:p>
57+ </table:table-cell>
58+ <table:table-cell table:style-name="TableRowCell" office:value-type="string">
59+ <text:p text:style-name="P2">1</text:p>
60+ </table:table-cell>
61+ </table:table-row>
62+</table:table>
63+</office:text>
64+</office:body>
65+</office:document-content>
66+```