Commit bcf0a0e2cb
Verified · cmc
Layout: unified · split
Sources/OrgCore/Export/ExportScope.swift +10 −1
| @@ -32,7 +32,16 @@ public enum ExportScope { | ||
| 32 | 32 | body = body.replacing(#/^(?:[ \t]*(?:SCHEDULED|DEADLINE|CLOSED):[^\n]*\n)?(?:[ \t]*:PROPERTIES:[ \t]*\n(?:[^\n]*\n)*?[ \t]*:END:[ \t]*(?:\n|$))?/#, with: "", maxReplacements: 1) |
| 33 | 33 | body = ExportPreprocess.shift(body, to: 1) |
| 34 | 34 | |
| 35 | let property = { (key: String) in heading.properties.last { $0.key.uppercased() == key }?.value } | |
| 35 | // `org-entry-get`: the last `KEY` value, then each `KEY+` value, joined with spaces; | |
| 36 | // "nil" is no value. | |
| 37 | let property = { (key: String) -> String? in | |
| 38 | let matching = heading.properties.filter { $0.key.uppercased() == key } | |
| 39 | let base = matching.last { !$0.additive }?.value | |
| 40 | let values = [base == "nil" ? nil : base] + matching.filter(\.additive).map(\.value) | |
| 41 | guard values.contains(where: { $0 != nil }) else { return nil } | |
| 42 | let value = values.compactMap { $0 }.joined(separator: " ") | |
| 43 | return value == "nil" ? nil : value | |
| 44 | } | |
| 36 | 45 | // What the subtree sets replaces the file's keywords; `EXPORT_OPTIONS` only the items |
| 37 | 46 | // it names. |
| 38 | 47 | let replaced = Set(overrides.filter { $0.property == "EXPORT_TITLE" || ($0.property != "EXPORT_OPTIONS" && property($0.property) != nil) }.map(\.keyword)) |
Sources/OrgCore/Export/LaTeXExport.swift +26 −6
| @@ -183,7 +183,8 @@ final class LaTeXRenderer { | ||
| 183 | 183 | let date = try keywords["DATE"].map { try parsed($0) } ?? (options.tex ? "\\today" : "") |
| 184 | 184 | out += "\\date{\(options.date ? date : "")}\n" |
| 185 | 185 | let title = try keywords["TITLE"].map { try parsed($0) } ?? "" |
| 186 | let subtitle = try keywords["SUBTITLE"].map { "\\\\\\medskip\n\\large " + (try parsed($0)) } ?? "" | |
| 186 | // An empty `#+SUBTITLE` parses to nil, which ox-latex leaves out. | |
| 187 | let subtitle = try keywords["SUBTITLE"].flatMap({ $0 == [""] ? nil : $0 }).map { "\\\\\\medskip\n\\large " + (try parsed($0)) } ?? "" | |
| 187 | 188 | out += "\\title{\(title)\(subtitle)}\n" |
| 188 | 189 | let lang = Self.languageNames[self.language] ?? self.language |
| 189 | 190 | out += "\\hypersetup{\n pdfauthor={\(options.author ? authorText ?? "" : "")},\n pdftitle={\(options.title ? title : "")},\n" |
| @@ -200,7 +201,8 @@ final class LaTeXRenderer { | ||
| 200 | 201 | |
| 201 | 202 | /// A keyword parsed as Org text, its lines joined with spaces. |
| 202 | 203 | private func parsed(_ values: [String]) throws -> String { |
| 203 | try inlineString(values.joined(separator: " ")) | |
| 204 | if values.count > 1, values.contains("") { throw Unsupported(reason: "Repeated keywords with an empty value") } | |
| 205 | return try inlineString(values.joined(separator: " ")) | |
| 204 | 206 | } |
| 205 | 207 | |
| 206 | 208 | /// `org-latex-make-preamble` with the class, its options and the header lines. |
| @@ -661,9 +663,16 @@ final class LaTeXRenderer { | ||
| 661 | 663 | |
| 662 | 664 | /// Whether `nodes` hold an element the export keeps. |
| 663 | 665 | private func hasContents(_ nodes: [SyntaxNode]) -> Bool { |
| 664 | nodes.contains { node in | |
| 666 | nodes.indices.contains { index in | |
| 667 | let node = nodes[index] | |
| 665 | 668 | switch node.kind { |
| 666 | case .comment, .affiliatedKeyword: return false | |
| 669 | case .comment: return false | |
| 670 | case .affiliatedKeyword: | |
| 671 | // Org parses affiliated keywords with no element right after them as keywords. | |
| 672 | var last = index | |
| 673 | while last + 1 < nodes.count, nodes[last + 1].kind == .affiliatedKeyword, | |
| 674 | nodes[last].range.upperBound == nodes[last + 1].range.lowerBound { last += 1 } | |
| 675 | return last + 1 == nodes.count || nodes[last].range.upperBound != nodes[last + 1].range.lowerBound | |
| 667 | 676 | case .block: |
| 668 | 677 | let type = blockType(node) |
| 669 | 678 | if type == "comment" { return false } |
| @@ -977,8 +986,19 @@ final class LaTeXRenderer { | ||
| 977 | 986 | let environment = affiliated.value("environment") ?? "verbatim" |
| 978 | 987 | return try wrapLabel(affiliated, "\\begin{\(environment)}\n\(try formatCode(lines))\\end{\(environment)}") |
| 979 | 988 | case "quote", "center": |
| 980 | // Emacs formats nil contents as `nil`. Blank lines alone are a paragraph of one newline. | |
| 981 | let contents = body.isEmpty ? nil : body.allSatisfy({ $0.allSatisfy(\.isWhitespace) }) ? "\n" : try blockContents(text) | |
| 989 | // Emacs formats nil contents as `nil`. | |
| 990 | var contents: String? | |
| 991 | if let first = body.first, first.allSatisfy(\.isWhitespace) { | |
| 992 | // Org parses a blank first line as a paragraph of that line, which runs on to | |
| 993 | // the next blank line or element when it holds spaces. | |
| 994 | let blanks = body.prefix { $0.allSatisfy(\.isWhitespace) }.count | |
| 995 | if !first.isEmpty, blanks == 1, blanks < body.count { throw Unsupported(reason: "A line of spaces opening a \(type) block") } | |
| 996 | let paragraph = first.isEmpty ? "\n\n" : first + "\n" | |
| 997 | let rest = unescaped.dropFirst(blanks).joined(separator: "\n") | |
| 998 | contents = paragraph + String(repeating: "\n", count: blanks - 1) + (rest.isEmpty ? "" : try blockContents(rest) ?? "") | |
| 999 | } else if !body.isEmpty { | |
| 1000 | contents = try blockContents(text) | |
| 1001 | } | |
| 982 | 1002 | let inner = contents.map(Self.normalize) ?? "nil" |
| 983 | 1003 | if type == "center" { return try wrapLabel(affiliated, "\\begin{center}\n\(inner)\\end{center}") } |
| 984 | 1004 | let environment = affiliated.value("environment") ?? "quote" |
Tests/OrgCoreTests/LaTeXExportTests.swift +32
| @@ -178,6 +178,20 @@ struct LaTeXExportTests { | ||
| 178 | 178 | "a\n:DR:\n:END:\n\nb\n\na\n#+begin_src sh\n#+end_src\n\nb\n\na\n:DR:\n:END:\nb\n\na\n#+BEGIN: x\n#+END:\n\nb\n", |
| 179 | 179 | "*b*\tx =v=\t\ty \\alpha\tz [[https://a.b]]\tw x^2\tq $m$\tr <2024-01-15 Mon>\tt *b*\t\n", |
| 180 | 180 | "a @@html:x@@\tb a@@html:x@@\tb [fn::n]\t[fn::m] c\n", |
| 181 | "a\n\n:DR:\n#+CAPTION: x\n:END:\n\nb\n", | |
| 182 | "a\n\n:DR:\n#+NAME: x\n:END:\n\nb\n", | |
| 183 | "a\n\n:DR:\n#+NAME: x\n\n:END:\n\nb\n", | |
| 184 | "a\n\n:DR:\n#+CAPTION: x\n#+NAME: y\n:END:\n\nb\n", | |
| 185 | "a\n\n#+BEGIN: foo\n#+CAPTION: x\n#+END:\n\nb\n", | |
| 186 | "a\n\n#+begin_quote\n#+CAPTION: x\n#+end_quote\n\nb\n", | |
| 187 | "a\n\n#+begin_quote\n\n \nfoo\n#+end_quote\n", | |
| 188 | "a\n\n#+begin_quote\n\nfoo\n#+end_quote\n", | |
| 189 | "a\n\n#+begin_quote\n \n\nfoo\n#+end_quote\n", | |
| 190 | "a\n\n#+begin_quote\n\n\nfoo\n#+end_quote\n", | |
| 191 | "a\n\n#+begin_quote\n\n# c\n#+end_quote\n", | |
| 192 | "a\n\n#+begin_center\n\n#+CAPTION: x\n#+end_center\n", | |
| 193 | "#+begin_quote\n \n#+end_quote\n\n#+begin_center\n \n#+end_center\n\n#+begin_quote\n \n \n#+end_quote\n\n#+begin_quote\n\n \n#+end_quote\n\n#+begin_center\n \t \n\n#+end_center\n", | |
| 194 | "#+TITLE: T\n#+SUBTITLE:\nx\n", | |
| 181 | 195 | ] |
| 182 | 196 | |
| 183 | 197 | @Test(.enabled(if: oracle)) |
| @@ -254,6 +268,20 @@ struct LaTeXExportTests { | ||
| 254 | 268 | try compare(cases, names: ["subtree properties", "subtree subtitle"]) |
| 255 | 269 | } |
| 256 | 270 | |
| 271 | /// `PROP+` lines add to a subtree property, and an empty or "nil" value is no value. | |
| 272 | @Test(.enabled(if: oracle)) | |
| 273 | func accumulatedSubtreePropertiesMatchEmacs() throws { | |
| 274 | let docs = [ | |
| 275 | "#+SUBTITLE: File\n* Top\n:PROPERTIES:\n:EXPORT_LATEX_HEADER: \\usepackage{a}\n:EXPORT_SUBTITLE: s1\n:EXPORT_LATEX_HEADER+: \\usepackage{b}\n:EXPORT_SUBTITLE+: s2\n:EXPORT_SUBTITLE+: s3\n:END:\nx\n", | |
| 276 | "#+SUBTITLE: File\n* Top\n:PROPERTIES:\n:EXPORT_SUBTITLE+: s2\n:EXPORT_KEYWORDS: k1\n:EXPORT_KEYWORDS: k2\n:END:\nx\n", | |
| 277 | "#+SUBTITLE: File\n#+KEYWORDS: k\n* Top\n:PROPERTIES:\n:EXPORT_SUBTITLE:\n:EXPORT_KEYWORDS:\n:END:\nx\n", | |
| 278 | "#+SUBTITLE: File\n* Top\n:PROPERTIES:\n:EXPORT_SUBTITLE: nil\n:EXPORT_TITLE: nil\n:END:\nx\n", | |
| 279 | "#+SUBTITLE: File\n* Top\n:PROPERTIES:\n:EXPORT_SUBTITLE: nil\n:EXPORT_SUBTITLE+: s\n:END:\nx\n", | |
| 280 | ] | |
| 281 | let cases = docs.map { LaTeXOracle.Case(text: $0, subtree: EmacsOracle.point(($0 as NSString).range(of: "\nx\n").location + 1, in: $0)) } | |
| 282 | try compare(cases, names: docs.indices.map { "accumulated \($0)" }) | |
| 283 | } | |
| 284 | ||
| 257 | 285 | /// Links to what the export leaves out stop both exports. |
| 258 | 286 | @Test(.enabled(if: oracle)) |
| 259 | 287 | func abortsWhereEmacsAborts() throws { |
| @@ -314,6 +342,10 @@ struct LaTeXExportTests { | ||
| 314 | 342 | #expect(reason("#+LATEX_HEADER: \\usepackage[AUTO]{babel}\nx\n") != nil) |
| 315 | 343 | #expect(reason("#+begin_src sh :exports results\necho\n#+end_src\n") == ":exports results") |
| 316 | 344 | #expect(reason("[cite:@key]\n") == "Citations") |
| 345 | for body in [" \n#+CAPTION: x\n", " \n#+FOO: x\n", " \n# c\n", " \nfoo\n", " \n#+CAPTION: x\n\nfoo\n", " \n x\n"] { | |
| 346 | #expect(reason("#+begin_quote\n\(body)#+end_quote\n") == "A line of spaces opening a quote block") | |
| 347 | } | |
| 348 | #expect(reason("#+TITLE: T\n#+SUBTITLE: a\n#+SUBTITLE:\nx\n") == "Repeated keywords with an empty value") | |
| 317 | 349 | #expect(reason("#+OPTIONS: tex:verbatim\nMath \\(a_1\\).\n") == "#+OPTIONS: tex:verbatim") |
| 318 | 350 | let pre = "* Top\n:PROPERTIES:\n:EXPORT_LATEX_CLASS_PRE: \\x\n:END:\nBody.\n" |
| 319 | 351 | #expect(ExportScope.subtree(pre, at: 0).flatMap(reason) == "#+LATEX_CLASS_PRE") |
docs/manual/guide/12-export.org +2 −2
| @@ -232,9 +232,9 @@ It reads: | ||
| 232 | 232 | | =#+LATEX_COMPILER:= | =pdflatex= (the default), =xelatex= or =lualatex=: the packages for that engine | |
| 233 | 233 | | =#+ATTR_LATEX:= | On tables (=:environment=, =:align=, =:width=, =:float=, =:placement=, =:center=, =:font=, =:caption=, =:options=), images (=:width=, =:height=, =:scale=, =:options=, =:float=, =:placement=, =:center=), lists, quote, example and special blocks | |
| 234 | 234 | |
| 235 | =#+TITLE=, =#+SUBTITLE=, =#+AUTHOR=, =#+DATE=, =#+KEYWORDS=, =#+DESCRIPTION=, =#+LANGUAGE= and =#+OPTIONS= work as in Emacs, as do the tags and =COMMENT= that leave headings out (see /Which headings are exported/). Body-only and subtree export are in the export dialog. In a subtree export, the heading's =EXPORT_LATEX_CLASS=, =EXPORT_LATEX_CLASS_OPTIONS=, =EXPORT_LATEX_HEADER=, =EXPORT_LATEX_HEADER_EXTRA=, =EXPORT_LATEX_COMPILER=, =EXPORT_SUBTITLE=, =EXPORT_KEYWORDS=, =EXPORT_DESCRIPTION= and =EXPORT_LANGUAGE= properties replace the file's keywords. | |
| 235 | =#+TITLE=, =#+SUBTITLE=, =#+AUTHOR=, =#+DATE=, =#+KEYWORDS=, =#+DESCRIPTION=, =#+LANGUAGE= and =#+OPTIONS= work as in Emacs, as do the tags and =COMMENT= that leave headings out (see /Which headings are exported/). Body-only and subtree export are in the export dialog. In a subtree export, the heading's =EXPORT_LATEX_CLASS=, =EXPORT_LATEX_CLASS_OPTIONS=, =EXPORT_LATEX_HEADER=, =EXPORT_LATEX_HEADER_EXTRA=, =EXPORT_LATEX_COMPILER=, =EXPORT_SUBTITLE=, =EXPORT_KEYWORDS=, =EXPORT_DESCRIPTION= and =EXPORT_LANGUAGE= properties replace the file's keywords. As in Emacs, a =PROPERTY+= line adds its value to the property's, after a space. | |
| 236 | 236 | |
| 237 | A file with something this export doesn't handle stops with "Can't export to LaTeX: " and what it was, rather than giving a =.tex= that differs from Emacs's: a LaTeX class other than the three, Babel or Polyglossia in the header, citations, radio targets, inline tasks, table.el tables, inline source blocks and calls, =#+CALL=, source blocks exported with their results, footnote definitions of several paragraphs, footnotes in headings, tables or item tags, =:mode math= and long tables, =UNNUMBERED= and =ALT_TITLE=, =tex:verbatim=, and =#+OPTIONS= items other than =toc=, =num=, =H=, =tags=, =todo=, =pri=, =^=, =tex=, =title=, =author=, =date=, =-= and =timestamp= set to something other than Org's default. A link Emacs can't resolve stops the export in both, including a link to a heading, target or named element the export leaves out. On iOS the share sheet stops and the message shows at the bottom of the screen. Publishing to LaTeX stops with the file's name in the message. In the download version, when Emacs is installed, such a file is exported by Emacs instead (see /Exporting through Emacs/). | |
| 237 | A file with something this export doesn't handle stops with "Can't export to LaTeX: " and what it was, rather than giving a =.tex= that differs from Emacs's: a LaTeX class other than the three, Babel or Polyglossia in the header, citations, radio targets, inline tasks, table.el tables, inline source blocks and calls, =#+CALL=, source blocks exported with their results, footnote definitions of several paragraphs, footnotes in headings, tables or item tags, =:mode math= and long tables, =UNNUMBERED= and =ALT_TITLE=, =tex:verbatim=, a quote or center block whose first line holds only spaces and whose second line is not blank, a keyword such as =#+SUBTITLE= repeated with an empty value, and =#+OPTIONS= items other than =toc=, =num=, =H=, =tags=, =todo=, =pri=, =^=, =tex=, =title=, =author=, =date=, =-= and =timestamp= set to something other than Org's default. A link Emacs can't resolve stops the export in both, including a link to a heading, target or named element the export leaves out. On iOS the share sheet stops and the message shows at the bottom of the screen. Publishing to LaTeX stops with the file's name in the message. In the download version, when Emacs is installed, such a file is exported by Emacs instead (see /Exporting through Emacs/). | |
| 238 | 238 | |
| 239 | 239 | * Word, RTF and plain text |
| 240 | 240 | |