Commit bcf0a0e2cb

bcf0a0e2cb532bd09ff705cd761f9818a6e0ec2c

parent: 19f9854b18

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-20 18:25 UTC

Match ox-latex on orphan affiliated keywords, blank-led quote blocks, PROP+ and empty subtitles

Layout: unified · split

Sources/OrgCore/Export/ExportScope.swift +10 −1
@@ -32,7 +32,16 @@ public enum ExportScope {
3232 body = body.replacing(#/^(?:[ \t]*(?:SCHEDULED|DEADLINE|CLOSED):[^\n]*\n)?(?:[ \t]*:PROPERTIES:[ \t]*\n(?:[^\n]*\n)*?[ \t]*:END:[ \t]*(?:\n|$))?/#, with: "", maxReplacements: 1)
3333 body = ExportPreprocess.shift(body, to: 1)
3434
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 }
3645 // What the subtree sets replaces the file's keywords; `EXPORT_OPTIONS` only the items
3746 // it names.
3847 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 {
183183 let date = try keywords["DATE"].map { try parsed($0) } ?? (options.tex ? "\\today" : "")
184184 out += "\\date{\(options.date ? date : "")}\n"
185185 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)) } ?? ""
187188 out += "\\title{\(title)\(subtitle)}\n"
188189 let lang = Self.languageNames[self.language] ?? self.language
189190 out += "\\hypersetup{\n pdfauthor={\(options.author ? authorText ?? "" : "")},\n pdftitle={\(options.title ? title : "")},\n"
@@ -200,7 +201,8 @@ final class LaTeXRenderer {
200201
201202 /// A keyword parsed as Org text, its lines joined with spaces.
202203 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: " "))
204206 }
205207
206208 /// `org-latex-make-preamble` with the class, its options and the header lines.
@@ -661,9 +663,16 @@ final class LaTeXRenderer {
661663
662664 /// Whether `nodes` hold an element the export keeps.
663665 private func hasContents(_ nodes: [SyntaxNode]) -> Bool {
664 nodes.contains { node in
666 nodes.indices.contains { index in
667 let node = nodes[index]
665668 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
667676 case .block:
668677 let type = blockType(node)
669678 if type == "comment" { return false }
@@ -977,8 +986,19 @@ final class LaTeXRenderer {
977986 let environment = affiliated.value("environment") ?? "verbatim"
978987 return try wrapLabel(affiliated, "\\begin{\(environment)}\n\(try formatCode(lines))\\end{\(environment)}")
979988 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 }
9821002 let inner = contents.map(Self.normalize) ?? "nil"
9831003 if type == "center" { return try wrapLabel(affiliated, "\\begin{center}\n\(inner)\\end{center}") }
9841004 let environment = affiliated.value("environment") ?? "quote"
Tests/OrgCoreTests/LaTeXExportTests.swift +32
@@ -178,6 +178,20 @@ struct LaTeXExportTests {
178178 "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",
179179 "*b*\tx =v=\t\ty \\alpha\tz [[https://a.b]]\tw x^2\tq $m$\tr <2024-01-15 Mon>\tt *b*\t\n",
180180 "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",
181195 ]
182196
183197 @Test(.enabled(if: oracle))
@@ -254,6 +268,20 @@ struct LaTeXExportTests {
254268 try compare(cases, names: ["subtree properties", "subtree subtitle"])
255269 }
256270
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
257285 /// Links to what the export leaves out stop both exports.
258286 @Test(.enabled(if: oracle))
259287 func abortsWhereEmacsAborts() throws {
@@ -314,6 +342,10 @@ struct LaTeXExportTests {
314342 #expect(reason("#+LATEX_HEADER: \\usepackage[AUTO]{babel}\nx\n") != nil)
315343 #expect(reason("#+begin_src sh :exports results\necho\n#+end_src\n") == ":exports results")
316344 #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")
317349 #expect(reason("#+OPTIONS: tex:verbatim\nMath \\(a_1\\).\n") == "#+OPTIONS: tex:verbatim")
318350 let pre = "* Top\n:PROPERTIES:\n:EXPORT_LATEX_CLASS_PRE: \\x\n:END:\nBody.\n"
319351 #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:
232232| =#+LATEX_COMPILER:= | =pdflatex= (the default), =xelatex= or =lualatex=: the packages for that engine |
233233| =#+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 |
234234
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.
236236
237A 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/).
237A 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/).
238238
239239* Word, RTF and plain text
240240