Sources/OrgCore/Export/ODTTemplate.swift

2df4420ff51f7e97a9c06eacf814f64cdb5a4351
orgstar/Sources/OrgCore/Export/ODTTemplate.swift

222 lines · 16186 bytes

  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        let sequences = ["Table", "Illustration", "Text", "Equation", "Listing"].map {
 59            "<text:sequence-decl text:display-outline-level=\"2\" text:name=\"\($0)\"/>"
 60        }.joined(separator: "\n")
 61        return """
 62            <?xml version="1.0" encoding="UTF-8"?>
 63            <office:document-content
 64            \(namespaces)>
 65              <office:automatic-styles>
 66            \(styles)\(automaticStyles)  </office:automatic-styles>
 67              <office:body>
 68                <office:text>
 69
 70            <text:sequence-decls>
 71            \(sequences)
 72            </text:sequence-decls>\(body)
 73            </office:text>
 74              </office:body>
 75            </office:document-content>
 76
 77            """
 78    }
 79
 80    /// `author`, `title` and `subtitle` are XML text already.
 81    static func meta(author: String, date: String?, keywords: String, description: String, title: String, subtitle: String) -> String {
 82        let e = { ODTRenderer.escape($0) }
 83        var out = """
 84            <?xml version="1.0" encoding="UTF-8"?>
 85            <office:document-meta
 86            \(namespaces)>
 87              <office:meta>
 88            <dc:creator>\(author)</dc:creator>
 89            <meta:initial-creator>\(author)</meta:initial-creator>
 90
 91            """
 92        if let date { out += "<dc:date>\(date)</dc:date>\n<meta:creation-date>\(date)</meta:creation-date>\n" }
 93        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"
 94        if !subtitle.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
 95            out += "<meta:user-defined meta:name=\"subtitle\">\(subtitle)</meta:user-defined>\n"
 96        }
 97        return out + "\n  </office:meta>\n</office:document-meta>\n"
 98    }
 99
100    /// The manifest, which lists every part but `mimetype` (the package itself, `/`).
101    static func manifest(images: [String]) -> String {
102        var entries = [(mimetype, "/", " manifest:version=\"1.2\""), ("text/xml", "content.xml", ""), ("text/xml", "styles.xml", ""), ("text/xml", "meta.xml", "")]
103        if !images.isEmpty { entries.append(("", "Images/", "")) }
104        // ox-odt names the type after the extension.
105        entries += images.map { ("image/" + ($0 as NSString).pathExtension, $0, "") }
106        return """
107            <?xml version="1.0" encoding="UTF-8"?>
108            <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">
109            \(entries.map { "<manifest:file-entry manifest:media-type=\"\($0.0)\" manifest:full-path=\"\($0.1)\"\($0.2)/>" }.joined(separator: "\n"))
110            </manifest:manifest>
111
112            """
113    }
114
115    /// `styles.xml`: headings numbered to `numbered` levels, A4 pages with 2 cm margins.
116    static func styles(numbered: Int) -> String {
117        func paragraph(_ name: String, display: String? = nil, parent: String? = "Standard", _ properties: String = "", text: String = "") -> String {
118            var out = "  <style:style style:name=\"\(name)\" style:display-name=\"\(display ?? name)\" style:family=\"paragraph\""
119            if let parent { out += " style:parent-style-name=\"\(parent)\"" }
120            out += ">"
121            if !properties.isEmpty { out += "<style:paragraph-properties \(properties)/>" }
122            if !text.isEmpty { out += "<style:text-properties \(text)/>" }
123            return out + "</style:style>\n"
124        }
125        func span(_ name: String, display: String? = nil, _ text: String) -> String {
126            "  <style:style style:name=\"\(name)\" style:display-name=\"\(display ?? name)\" style:family=\"text\"><style:text-properties \(text)/></style:style>\n"
127        }
128        func graphic(_ name: String, _ properties: String) -> String {
129            "  <style:style style:name=\"\(name)\" style:family=\"graphic\"><style:graphic-properties \(properties)/></style:style>\n"
130        }
131        let mono = "fo:font-family=\"Courier, monospace\" fo:font-size=\"10pt\""
132        let body = "fo:margin-top=\"0cm\" fo:margin-bottom=\"0.212cm\""
133        var styles = """
134              <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>
135              <style:style style:name="Standard" style:family="paragraph" style:class="text"/>
136
137            """
138        styles += paragraph("Text_20_body", display: "Text body", body)
139        styles += paragraph("Text_20_body_20_bold", display: "Text body bold", parent: "Text_20_body", text: "fo:font-weight=\"bold\"")
140        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\"")
141        let sizes = ["", "130%", "115%", "101%", "95%", "85%", "85%", "75%", "75%", "65%", "65%"]
142        for level in 1...10 {
143            let bold = "fo:font-size=\"\(sizes[level])\" fo:font-weight=\"bold\""
144            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"
145            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"
146        }
147        styles += paragraph("OrgTitle", "fo:text-align=\"center\" fo:margin-bottom=\"0.212cm\"", text: "fo:font-size=\"24pt\" fo:font-weight=\"bold\"")
148        styles += paragraph("OrgSubtitle", "fo:text-align=\"center\" fo:margin-bottom=\"0.212cm\"", text: "fo:font-size=\"14pt\"")
149        styles += paragraph("Quotations", parent: "Text_20_body", "fo:margin-left=\"1cm\" fo:margin-right=\"1cm\"")
150        styles += paragraph("OrgCenter", parent: "Text_20_body", "fo:text-align=\"center\"")
151        styles += paragraph("OrgVerse", parent: "Text_20_body", "fo:margin-left=\"1cm\"")
152        styles += paragraph("OrgSrcBlock", parent: "Standard", "fo:margin-top=\"0cm\" fo:margin-bottom=\"0cm\" fo:background-color=\"#f0f0f0\"", text: mono)
153        styles += paragraph("OrgSrcBlockLastLine", parent: "OrgSrcBlock", "fo:margin-bottom=\"0.212cm\"")
154        styles += paragraph("Footnote", parent: "Standard", "fo:margin-left=\"0.5cm\" fo:text-indent=\"-0.5cm\"", text: "fo:font-size=\"10pt\"")
155        styles += paragraph("OrgFootnoteCenter", parent: "Footnote", "fo:text-align=\"center\"")
156        styles += paragraph("OrgFootnoteQuotations", parent: "Footnote", "fo:margin-left=\"1cm\" fo:margin-right=\"1cm\"")
157        styles += paragraph("Table_20_Contents", display: "Table Contents", parent: "Standard")
158        for (name, align) in [("Left", "start"), ("Right", "end"), ("Center", "center")] {
159            styles += paragraph("OrgTableContents\(name)", parent: "Table_20_Contents", "fo:text-align=\"\(align)\"")
160            styles += paragraph("OrgTableHeading\(name)", parent: "Table_20_Contents", "fo:text-align=\"\(align)\"", text: "fo:font-weight=\"bold\"")
161        }
162        styles += paragraph("Contents_20_Heading", display: "Contents Heading", parent: "Heading", text: "fo:font-size=\"16pt\" fo:font-weight=\"bold\"")
163        for level in 1...10 {
164            styles += paragraph("Contents_20_\(level)", display: "Contents \(level)", parent: "Standard", "fo:margin-left=\"\(String(format: "%.3f", 0.499 * Double(level - 1)))cm\"")
165        }
166        styles += paragraph("Horizontal_20_Line", display: "Horizontal Line", parent: "Standard", "fo:margin-bottom=\"0.5cm\" fo:border-bottom=\"0.002cm solid #808080\"")
167        styles += paragraph("Caption", parent: "Standard", "fo:margin-top=\"0.212cm\" fo:margin-bottom=\"0.212cm\"", text: "fo:font-style=\"italic\" fo:font-size=\"10pt\"")
168        for name in ["Illustration", "Table", "Listing"] { styles += paragraph(name, parent: "Caption") }
169        styles += span("Emphasis", "fo:font-style=\"italic\"")
170        styles += span("Bold", "fo:font-weight=\"bold\"")
171        styles += span("Underline", "style:text-underline-style=\"solid\" style:text-underline-width=\"auto\" style:text-underline-color=\"font-color\"")
172        styles += span("Strikethrough", "style:text-line-through-style=\"solid\"")
173        styles += span("OrgCode", mono)
174        styles += span("OrgSubscript", "style:text-position=\"sub 58%\"")
175        styles += span("OrgSuperscript", "style:text-position=\"super 58%\"")
176        styles += span("OrgTodo", "fo:color=\"#b00000\" fo:font-weight=\"bold\"")
177        styles += span("OrgDone", "fo:color=\"#008000\" fo:font-weight=\"bold\"")
178        styles += span("OrgTags", "fo:font-size=\"85%\"")
179        styles += span("OrgTag", "fo:font-weight=\"bold\"")
180        styles += span("OrgPriority", "fo:font-weight=\"bold\"")
181        styles += span("OrgActiveTimestamp", "fo:color=\"#555555\"")
182        styles += span("OrgInactiveTimestamp", "fo:color=\"#888888\"")
183        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\"")
184        styles += graphic("OrgDisplayImage", "style:wrap=\"none\" style:vertical-pos=\"top\" style:vertical-rel=\"paragraph\" style:horizontal-pos=\"center\" style:horizontal-rel=\"paragraph\"")
185        styles += graphic("OrgInlineImage", "style:vertical-pos=\"middle\" style:vertical-rel=\"text\"")
186        styles += graphic("OrgCaptionedImage", "style:wrap=\"none\" style:vertical-pos=\"top\" style:vertical-rel=\"paragraph\" style:horizontal-pos=\"center\" style:horizontal-rel=\"paragraph\"")
187        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\"")
188        func list(_ name: String, _ level: (Int) -> String) -> String {
189            "  <text:list-style style:name=\"\(name)\">\n" + (1...10).map(level).joined() + "  </text:list-style>\n"
190        }
191        func position(_ level: Int) -> String {
192            let indent = String(format: "%.3fcm", 0.635 * Double(level))
193            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>"
194        }
195        styles += list("OrgBulletedList") { "    <text:list-level-style-bullet text:level=\"\($0)\" text:bullet-char=\"•\">\(position($0))</text:list-level-style-bullet>\n" }
196        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" }
197        styles += list("OrgDescriptionList") { "    <text:list-level-style-number text:level=\"\($0)\" style:num-format=\"\">\(position($0))</text:list-level-style-number>\n" }
198        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" }
199        styles += "  <text:outline-style style:name=\"Outline\">\n"
200        for level in 1...10 {
201            let format = level <= numbered ? "style:num-format=\"1\" style:num-suffix=\".\" text:display-levels=\"\(level)\"" : "style:num-format=\"\""
202            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"
203        }
204        styles += "  </text:outline-style>\n"
205        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"
206        return """
207            <?xml version="1.0" encoding="UTF-8"?>
208            <office:document-styles
209            \(namespaces)>
210              <office:styles>
211            \(styles)  </office:styles>
212              <office:automatic-styles>
213                <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>
214              </office:automatic-styles>
215              <office:master-styles>
216                <style:master-page style:name="Standard" style:page-layout-name="OrgPage"/>
217              </office:master-styles>
218            </office:document-styles>
219
220            """
221    }
222}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form