Sources/OrgCore/Export/ODTTemplate.swift
231 lines · 16794 bytes
11 symbols in this file
1import Foundation
2
3/// The parts of an `.odt` file around the exported text: the content wrapper with its
4/// automatic styles, `styles.xml` with the style names ox-odt (Org 9.8.7) uses, `meta.xml` and
5/// the manifest. Written here rather than taken from ox-odt's templates.
6enum ODTTemplate {
7 static let mimetype = "application/vnd.oasis.opendocument.text"
8
9 static let namespaces = """
10 xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
11 xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
12 xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
13 xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
14 xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
15 xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
16 xmlns:xlink="http://www.w3.org/1999/xlink"
17 xmlns:dc="http://purl.org/dc/elements/1.1/"
18 xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
19 xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
20 xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
21 xmlns:ooow="http://openoffice.org/2004/writer"
22 office:version="1.2"
23 """
24
25 /// `content.xml`: table cell borders by row and column group, indented sections for tables
26 /// in lists, then `body`.
27 static func content(automaticStyles: String, body: String) -> String {
28 let border = "0.002cm solid #000000"
29 var styles = """
30 <style:style style:name="OrgTable" style:family="table">
31 <style:table-properties style:rel-width="96%" fo:margin-top="0cm" fo:margin-bottom="0.20cm" table:align="center"/>
32 </style:style>
33 <style:style style:name="OrgTableColumn" style:family="table-column">
34 <style:table-column-properties style:rel-column-width="1*"/>
35 </style:style>
36 <style:style style:name="OrgIndexSection" style:family="section">
37 <style:section-properties fo:background-color="#c0c0c0" style:editable="false"/>
38 </style:style>
39
40 """
41 for sides in ["", "T", "B", "L", "TB", "TL", "BL", "TBL"] {
42 func edge(_ side: Character) -> String { sides.contains(side) ? border : "none" }
43 styles += """
44 <style:style style:name="OrgTblCell\(sides)" style:family="table-cell">
45 <style:table-cell-properties style:vertical-align="top" fo:padding="0.159cm" fo:border-top="\(edge("T"))" fo:border-bottom="\(edge("B"))" fo:border-left="\(edge("L"))" fo:border-right="none"/>
46 </style:style>
47
48 """
49 }
50 for level in 0...10 {
51 styles += """
52 <style:style style:name="OrgIndentedSection-Level-\(level)" style:family="section">
53 <style:section-properties fo:margin-left="\(String(format: "%.3fcm", level == 0 ? 0 : 0.635 * Double(level + 1)))" fo:margin-right="0cm" style:editable="false"/>
54 </style:style>
55
56 """
57 }
58 styles += """
59 <style:style style:name="Sect1" style:family="section">
60 <style:section-properties style:editable="false"/>
61 </style:style>
62
63 """
64 let sequences = ["Table", "Illustration", "Text", "Equation", "Listing"].map {
65 "<text:sequence-decl text:display-outline-level=\"2\" text:name=\"\($0)\"/>"
66 }.joined(separator: "\n")
67 return """
68 <?xml version="1.0" encoding="UTF-8"?>
69 <office:document-content
70 \(namespaces)>
71 <office:automatic-styles>
72 \(styles)\(automaticStyles) </office:automatic-styles>
73 <office:body>
74 <office:text>
75
76 <text:sequence-decls>
77 \(sequences)
78 </text:sequence-decls>\(body)
79 </office:text>
80 </office:body>
81 </office:document-content>
82
83 """
84 }
85
86 /// `author`, `title` and `subtitle` are XML text already.
87 static func meta(author: String, date: String?, keywords: String, description: String, title: String, subtitle: String) -> String {
88 let e = { ODTRenderer.escape($0) }
89 var out = """
90 <?xml version="1.0" encoding="UTF-8"?>
91 <office:document-meta
92 \(namespaces)>
93 <office:meta>
94 <dc:creator>\(author)</dc:creator>
95 <meta:initial-creator>\(author)</meta:initial-creator>
96
97 """
98 if let date { out += "<dc:date>\(date)</dc:date>\n<meta:creation-date>\(date)</meta:creation-date>\n" }
99 out += "<meta:generator>orgstar</meta:generator>\n<meta:keyword>\(e(keywords))</meta:keyword>\n<dc:subject>\(e(description))</dc:subject>\n<dc:title>\(title)</dc:title>\n"
100 if !subtitle.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
101 out += "<meta:user-defined meta:name=\"subtitle\">\(subtitle)</meta:user-defined>\n"
102 }
103 return out + "\n </office:meta>\n</office:document-meta>\n"
104 }
105
106 /// The manifest, which lists every part but `mimetype` (the package itself, `/`).
107 static func manifest(images: [String]) -> String {
108 var entries = [(mimetype, "/", " manifest:version=\"1.2\""), ("text/xml", "content.xml", ""), ("text/xml", "styles.xml", ""), ("text/xml", "meta.xml", "")]
109 if !images.isEmpty { entries.append(("", "Images/", "")) }
110 // ox-odt names the type after the extension.
111 entries += images.map { ("image/" + ($0 as NSString).pathExtension, $0, "") }
112 return """
113 <?xml version="1.0" encoding="UTF-8"?>
114 <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">
115 \(entries.map { "<manifest:file-entry manifest:media-type=\"\($0.0)\" manifest:full-path=\"\($0.1)\"\($0.2)/>" }.joined(separator: "\n"))
116 </manifest:manifest>
117
118 """
119 }
120
121 /// `styles.xml`: headings numbered to `numbered` levels, A4 pages with 2 cm margins.
122 static func styles(numbered: Int) -> String {
123 func paragraph(_ name: String, display: String? = nil, parent: String? = "Standard", _ properties: String = "", text: String = "") -> String {
124 var out = " <style:style style:name=\"\(name)\" style:display-name=\"\(display ?? name)\" style:family=\"paragraph\""
125 if let parent { out += " style:parent-style-name=\"\(parent)\"" }
126 out += ">"
127 if !properties.isEmpty { out += "<style:paragraph-properties \(properties)/>" }
128 if !text.isEmpty { out += "<style:text-properties \(text)/>" }
129 return out + "</style:style>\n"
130 }
131 func span(_ name: String, display: String? = nil, _ text: String) -> String {
132 " <style:style style:name=\"\(name)\" style:display-name=\"\(display ?? name)\" style:family=\"text\"><style:text-properties \(text)/></style:style>\n"
133 }
134 func graphic(_ name: String, _ properties: String) -> String {
135 " <style:style style:name=\"\(name)\" style:family=\"graphic\"><style:graphic-properties \(properties)/></style:style>\n"
136 }
137 let mono = "fo:font-family=\"Courier, monospace\" fo:font-size=\"10pt\""
138 let body = "fo:margin-top=\"0cm\" fo:margin-bottom=\"0.212cm\""
139 var styles = """
140 <style:default-style style:family="paragraph"><style:paragraph-properties style:tab-stop-distance="1.251cm"/><style:text-properties fo:font-size="12pt" fo:language="en" fo:country="US"/></style:default-style>
141 <style:style style:name="Standard" style:family="paragraph" style:class="text"/>
142
143 """
144 styles += paragraph("Text_20_body", display: "Text body", body)
145 styles += paragraph("Text_20_body_20_bold", display: "Text body bold", parent: "Text_20_body", text: "fo:font-weight=\"bold\"")
146 styles += paragraph("Heading", parent: "Standard", "fo:margin-top=\"0.423cm\" fo:margin-bottom=\"0.212cm\" fo:keep-with-next=\"always\"", text: "fo:font-size=\"14pt\"")
147 let sizes = ["", "130%", "115%", "101%", "95%", "85%", "85%", "75%", "75%", "65%", "65%"]
148 for level in 1...10 {
149 let bold = "fo:font-size=\"\(sizes[level])\" fo:font-weight=\"bold\""
150 styles += " <style:style style:name=\"Heading_20_\(level)\" style:display-name=\"Heading \(level)\" style:family=\"paragraph\" style:parent-style-name=\"Heading\" style:next-style-name=\"Text_20_body\" style:default-outline-level=\"\(level)\" style:class=\"text\"><style:text-properties \(bold)/></style:style>\n"
151 styles += " <style:style style:name=\"Heading_20_\(level)_unnumbered\" style:display-name=\"Heading \(level) unnumbered\" style:family=\"paragraph\" style:parent-style-name=\"Heading_20_\(level)\" style:class=\"text\"/>\n"
152 }
153 styles += paragraph("OrgTitle", "fo:text-align=\"center\" fo:margin-bottom=\"0.212cm\"", text: "fo:font-size=\"24pt\" fo:font-weight=\"bold\"")
154 styles += paragraph("OrgSubtitle", "fo:text-align=\"center\" fo:margin-bottom=\"0.212cm\"", text: "fo:font-size=\"14pt\"")
155 styles += paragraph("Quotations", parent: "Text_20_body", "fo:margin-left=\"1cm\" fo:margin-right=\"1cm\"")
156 styles += paragraph("OrgCenter", parent: "Text_20_body", "fo:text-align=\"center\"")
157 styles += paragraph("OrgVerse", parent: "Text_20_body", "fo:margin-left=\"1cm\"")
158 styles += paragraph("OrgSrcBlock", parent: "Standard", "fo:margin-top=\"0cm\" fo:margin-bottom=\"0cm\" fo:background-color=\"#f0f0f0\"", text: mono)
159 styles += paragraph("OrgSrcBlockLastLine", parent: "OrgSrcBlock", "fo:margin-bottom=\"0.212cm\"")
160 styles += paragraph("OrgFixedWidthBlock", parent: "Standard", "fo:margin-top=\"0cm\" fo:margin-bottom=\"0cm\" fo:background-color=\"#f0f0f0\"", text: mono)
161 styles += paragraph("OrgFixedWidthBlockLastLine", parent: "OrgFixedWidthBlock", "fo:margin-bottom=\"0.212cm\"")
162 styles += paragraph("Footnote", parent: "Standard", "fo:margin-left=\"0.5cm\" fo:text-indent=\"-0.5cm\"", text: "fo:font-size=\"10pt\"")
163 styles += paragraph("OrgFootnoteCenter", parent: "Footnote", "fo:text-align=\"center\"")
164 styles += paragraph("OrgFootnoteQuotations", parent: "Footnote", "fo:margin-left=\"1cm\" fo:margin-right=\"1cm\"")
165 styles += paragraph("Table_20_Contents", display: "Table Contents", parent: "Standard")
166 for (name, align) in [("Left", "start"), ("Right", "end"), ("Center", "center")] {
167 styles += paragraph("OrgTableContents\(name)", parent: "Table_20_Contents", "fo:text-align=\"\(align)\"")
168 styles += paragraph("OrgTableHeading\(name)", parent: "Table_20_Contents", "fo:text-align=\"\(align)\"", text: "fo:font-weight=\"bold\"")
169 }
170 styles += paragraph("Contents_20_Heading", display: "Contents Heading", parent: "Heading", text: "fo:font-size=\"16pt\" fo:font-weight=\"bold\"")
171 for level in 1...10 {
172 styles += paragraph("Contents_20_\(level)", display: "Contents \(level)", parent: "Standard", "fo:margin-left=\"\(String(format: "%.3f", 0.499 * Double(level - 1)))cm\"")
173 }
174 styles += paragraph("Horizontal_20_Line", display: "Horizontal Line", parent: "Standard", "fo:margin-bottom=\"0.5cm\" fo:border-bottom=\"0.002cm solid #808080\"")
175 styles += paragraph("Caption", parent: "Standard", "fo:margin-top=\"0.212cm\" fo:margin-bottom=\"0.212cm\"", text: "fo:font-style=\"italic\" fo:font-size=\"10pt\"")
176 for name in ["Illustration", "Table", "Listing"] { styles += paragraph(name, parent: "Caption") }
177 styles += span("Emphasis", "fo:font-style=\"italic\"")
178 styles += span("Bold", "fo:font-weight=\"bold\"")
179 styles += span("Underline", "style:text-underline-style=\"solid\" style:text-underline-width=\"auto\" style:text-underline-color=\"font-color\"")
180 styles += span("Strikethrough", "style:text-line-through-style=\"solid\"")
181 styles += span("OrgCode", mono)
182 styles += span("OrgSubscript", "style:text-position=\"sub 58%\"")
183 styles += span("OrgSuperscript", "style:text-position=\"super 58%\"")
184 styles += span("OrgTodo", "fo:color=\"#b00000\" fo:font-weight=\"bold\"")
185 styles += span("OrgDone", "fo:color=\"#008000\" fo:font-weight=\"bold\"")
186 styles += span("OrgTags", "fo:font-size=\"85%\"")
187 styles += span("OrgTag", "fo:font-weight=\"bold\"")
188 styles += span("OrgPriority", "fo:font-weight=\"bold\"")
189 for priority in ["A", "B", "C"] { styles += span("OrgPriority-\(priority)", "fo:font-weight=\"bold\"") }
190 styles += span("OrgActiveTimestamp", "fo:color=\"#555555\"")
191 styles += span("OrgInactiveTimestamp", "fo:color=\"#888888\"")
192 styles += span("Internet_20_link", display: "Internet link", "fo:color=\"#000080\" style:text-underline-style=\"solid\" style:text-underline-width=\"auto\" style:text-underline-color=\"font-color\"")
193 styles += graphic("OrgDisplayImage", "style:wrap=\"none\" style:vertical-pos=\"top\" style:vertical-rel=\"paragraph\" style:horizontal-pos=\"center\" style:horizontal-rel=\"paragraph\"")
194 styles += graphic("OrgInlineImage", "style:vertical-pos=\"middle\" style:vertical-rel=\"text\"")
195 styles += graphic("OrgCaptionedImage", "style:wrap=\"none\" style:vertical-pos=\"top\" style:vertical-rel=\"paragraph\" style:horizontal-pos=\"center\" style:horizontal-rel=\"paragraph\"")
196 styles += graphic("OrgImageCaptionFrame", "style:wrap=\"none\" style:vertical-pos=\"top\" style:vertical-rel=\"paragraph\" style:horizontal-pos=\"center\" style:horizontal-rel=\"paragraph\" fo:padding=\"0cm\" fo:border=\"none\"")
197 func list(_ name: String, _ level: (Int) -> String) -> String {
198 " <text:list-style style:name=\"\(name)\">\n" + (1...10).map(level).joined() + " </text:list-style>\n"
199 }
200 func position(_ level: Int) -> String {
201 let indent = String(format: "%.3fcm", 0.635 * Double(level))
202 return "<style:list-level-properties text:list-level-position-and-space-mode=\"label-alignment\"><style:list-level-label-alignment text:label-followed-by=\"listtab\" text:list-tab-stop-position=\"\(indent)\" fo:text-indent=\"-0.635cm\" fo:margin-left=\"\(indent)\"/></style:list-level-properties>"
203 }
204 styles += list("OrgBulletedList") { " <text:list-level-style-bullet text:level=\"\($0)\" text:bullet-char=\"•\">\(position($0))</text:list-level-style-bullet>\n" }
205 styles += list("OrgNumberedList") { " <text:list-level-style-number text:level=\"\($0)\" style:num-suffix=\".\" style:num-format=\"1\">\(position($0))</text:list-level-style-number>\n" }
206 styles += list("OrgDescriptionList") { " <text:list-level-style-number text:level=\"\($0)\" style:num-format=\"\">\(position($0))</text:list-level-style-number>\n" }
207 styles += list("OrgSrcBlockNumberedLine") { " <text:list-level-style-number text:level=\"\($0)\" style:num-suffix=\" \" style:num-format=\"1\">\(position($0))</text:list-level-style-number>\n" }
208 styles += " <text:outline-style style:name=\"Outline\">\n"
209 for level in 1...10 {
210 let format = level <= numbered ? "style:num-format=\"1\" style:num-suffix=\".\" text:display-levels=\"\(level)\"" : "style:num-format=\"\""
211 styles += " <text:outline-level-style text:level=\"\(level)\" \(format)><style:list-level-properties text:list-level-position-and-space-mode=\"label-alignment\"><style:list-level-label-alignment text:label-followed-by=\"space\"/></style:list-level-properties></text:outline-level-style>\n"
212 }
213 styles += " </text:outline-style>\n"
214 styles += " <text:notes-configuration text:note-class=\"footnote\" style:num-format=\"1\" text:start-value=\"0\" text:footnotes-position=\"page\" text:start-numbering-at=\"document\"/>\n"
215 return """
216 <?xml version="1.0" encoding="UTF-8"?>
217 <office:document-styles
218 \(namespaces)>
219 <office:styles>
220 \(styles) </office:styles>
221 <office:automatic-styles>
222 <style:page-layout style:name="OrgPage"><style:page-layout-properties fo:page-width="21cm" fo:page-height="29.7cm" style:print-orientation="portrait" fo:margin-top="2cm" fo:margin-bottom="2cm" fo:margin-left="2cm" fo:margin-right="2cm"/></style:page-layout>
223 </office:automatic-styles>
224 <office:master-styles>
225 <style:master-page style:name="Standard" style:page-layout-name="OrgPage"/>
226 </office:master-styles>
227 </office:document-styles>
228
229 """
230 }
231}