Commit b28cc71ca3
Verified · cmc
Layout: unified · split
Sources/OrgCore/Export/ASCIIExport.swift +31 −13
| @@ -486,7 +486,12 @@ final class ASCIIRenderer { | ||
| 486 | 486 | |
| 487 | 487 | /// `elements`, each `org-element-normalize-string`'d and followed by its blank lines, as |
| 488 | 488 | /// `org-export-data` joins a greater element's contents. |
| 489 | func elements(_ nodes: [SyntaxNode], end: Int, context: Context, firstParagraph: ((SyntaxNode) -> Bool)? = nil) throws -> String { | |
| 489 | /// `firstParagraph` says which paragraph's first line follows a bullet; `written` hears | |
| 490 | /// what each element wrote. | |
| 491 | func elements( | |
| 492 | _ nodes: [SyntaxNode], end: Int, context: Context, | |
| 493 | firstParagraph: ((SyntaxNode) -> Bool)? = nil, written: ((SyntaxNode, String) -> Void)? = nil | |
| 494 | ) throws -> String { | |
| 490 | 495 | var out = "" |
| 491 | 496 | var affiliated = Affiliated() |
| 492 | 497 | // The element before and its blank lines, which a comment after it may add to; they |
| @@ -557,6 +562,7 @@ final class ASCIIRenderer { | ||
| 557 | 562 | previous = node |
| 558 | 563 | previousWrote = true |
| 559 | 564 | pendingBlank = blankLines(from: node.range.lowerBound, to: next) |
| 565 | written?(node, result) | |
| 560 | 566 | out += Self.normalize(result) + String(repeating: "\n", count: pendingBlank) |
| 561 | 567 | } |
| 562 | 568 | return out |
| @@ -658,9 +664,13 @@ final class ASCIIRenderer { | ||
| 658 | 664 | // MARK: Paragraphs |
| 659 | 665 | |
| 660 | 666 | private func paragraph(_ node: SyntaxNode, context: Context, ignoreFirst: Bool, prefix: String = "") throws -> String { |
| 667 | // `:with-timestamps` decides on timestamps alone in a paragraph. | |
| 668 | isolatedTimestamps = ASCIIText.isolatedTimestamps(node) | |
| 669 | defer { isolatedTimestamps = false } | |
| 661 | 670 | let items = LaTeXRenderer.normalizeIndentation(inlineItems(node), ignoreFirst: ignoreFirst) |
| 662 | 671 | // `org-remove-blank-lines`, whose newline isn't a hard one. |
| 663 | 672 | let contents = (prefix + (try transcode(items))).replacing(#/\x{1}?\n(?:[ \t]*\x{1}?\n)+/#, with: "\n") |
| 673 | guard contents.contains(where: { !$0.isWhitespace && $0 != "\u{1}" }) else { return contents } | |
| 664 | 674 | return Self.fill(contents.replacingOccurrences(of: Self.hardNewline, with: ASCIIText.hardNewline), context: context) |
| 665 | 675 | } |
| 666 | 676 | |
| @@ -707,19 +717,15 @@ final class ASCIIRenderer { | ||
| 707 | 717 | let indentation = descriptive ? Self.quoteMargin : bullet.count |
| 708 | 718 | let inner = Context(width: context.width - (descriptive ? Self.quoteMargin : bullet.count + checkbox.count), justify: context.justify) |
| 709 | 719 | let children = item.children.filter { $0.kind != .itemTag } |
| 720 | if children.isEmpty { throw Unsupported(reason: "Empty list items") } | |
| 710 | 721 | let bulletLine = source.lineRange(for: NSRange(location: item.range.lowerBound, length: 0)) |
| 711 | // Each child alone, to find the first that writes something. | |
| 712 | var contents = "" | |
| 713 | 722 | var firstWriter: SyntaxNode? |
| 714 | for (i, child) in children.enumerated() { | |
| 715 | let childEnd = i + 1 < children.count ? children[i + 1].range.lowerBound : item.range.upperBound | |
| 716 | let text = try elements([child], end: childEnd, context: inner) { paragraph in | |
| 717 | i == 0 && paragraph.range.lowerBound < NSMaxRange(bulletLine) | |
| 718 | } | |
| 719 | if firstWriter == nil, child.kind != .affiliatedKeyword, text.contains(where: { !$0.isWhitespace }) { firstWriter = child } | |
| 720 | contents += text | |
| 721 | } | |
| 722 | if children.contains(where: { $0.kind == .affiliatedKeyword || $0.kind == .comment }) { throw Unsupported(reason: "Affiliated keywords and comments in list items") } | |
| 723 | let contents = try elements(children, end: item.range.upperBound, context: inner, firstParagraph: { paragraph in | |
| 724 | paragraph.range == children[0].range && paragraph.range.lowerBound < NSMaxRange(bulletLine) | |
| 725 | }, written: { node, text in | |
| 726 | if firstWriter == nil, text.contains(where: { !$0.isWhitespace }) { firstWriter = node } | |
| 727 | }) | |
| 728 | if children.contains(where: { $0.kind == .affiliatedKeyword }) { throw Unsupported(reason: "Affiliated keywords in list items") } | |
| 723 | 729 | let indented = ASCIIText.indent(children.isEmpty ? "" : Self.normalize(contents), indentation) |
| 724 | 730 | var text = bullet + checkbox |
| 725 | 731 | if !descriptive, indented.contains(where: { !$0.isWhitespace }), firstWriter?.kind == .paragraph { |
| @@ -819,6 +825,7 @@ final class ASCIIRenderer { | ||
| 819 | 825 | return Self.justifyBlock(out + (caption ?? ""), context: context) |
| 820 | 826 | } |
| 821 | 827 | |
| 828 | private var isolatedTimestamps = false | |
| 822 | 829 | private var tableCount = 0 |
| 823 | 830 | private var listingCount = 0 |
| 824 | 831 | |
| @@ -898,6 +905,15 @@ final class ASCIIRenderer { | ||
| 898 | 905 | if !value.isEmpty { value += "\n" } |
| 899 | 906 | let preserve = lines[0].firstMatch(of: /^[ \t]*#\+(?i:begin_(?:src|example))\b.*\s-i\b/) != nil |
| 900 | 907 | var code = preserve ? value : try LaTeXRenderer.removeIndentation(value) |
| 908 | // Emacs writes a src block's leading tabs as spaces. | |
| 909 | if !preserve, lines[0].firstMatch(of: /^[ \t]*#\+(?i:begin_src)\b/) != nil { | |
| 910 | code = code.components(separatedBy: "\n").map { line in | |
| 911 | var column = 0 | |
| 912 | let indentation = line.prefix { $0 == " " || $0 == "\t" } | |
| 913 | for c in indentation { column = c == "\t" ? (column / 8 + 1) * 8 : column + 1 } | |
| 914 | return String(repeating: " ", count: column) + line.dropFirst(indentation.count) | |
| 915 | }.joined(separator: "\n") | |
| 916 | } | |
| 901 | 917 | if code.hasSuffix("\n") { code.removeLast() } |
| 902 | 918 | let (lines, refs) = Coderef.unravel(code.components(separatedBy: "\n"), format: parsed.switches.labelFormat) |
| 903 | 919 | let loc = parsed.switches.numberLines == nil ? nil : blockLoc |
| @@ -1103,7 +1119,9 @@ final class ASCIIRenderer { | ||
| 1103 | 1119 | case .verbatim, .code: return "`\(node.text.dropFirst().dropLast())'" |
| 1104 | 1120 | case .link: return try link(node, children) |
| 1105 | 1121 | case .linkDescription: return try transcode(children) |
| 1106 | case .timestamp: return plainText(ASCIIText.timestamp(node.text)) | |
| 1122 | case .timestamp: | |
| 1123 | if isolatedTimestamps, ICalendarExport.skipsTimestamp(node.text, with: options.timestamps ?? "t") { return nil } | |
| 1124 | return plainText(ASCIIText.timestamp(node.text)) | |
| 1107 | 1125 | case .footnoteReference: return try footnoteReference(node) |
| 1108 | 1126 | case .entity: |
| 1109 | 1127 | let name = String(node.text.dropFirst()).replacingOccurrences(of: "{}", with: "") |
Tests/OrgCoreTests/ASCIIExportTests.swift +119
| @@ -175,6 +175,40 @@ struct ASCIIExportTests { | ||
| 175 | 175 | "#+begin_JUSTIFYLEFT\nleft\n#+end_JUSTIFYLEFT\n", |
| 176 | 176 | "#+ATTR_ASCII: :width 20\n-----\n", |
| 177 | 177 | "* A\n#+TOC: headlines 2\n** B\n** C\n", |
| 178 | "Quoted mail:\n> first line of the quote that goes on long enough to need a second line\n> second line\n", | |
| 179 | "-- a dash opens this paragraph which goes on long enough to need a second line of text.\n", | |
| 180 | "| a | b | c |\n| 1 |\n| 2 | 3 |\n", | |
| 181 | "- [X] term :: with a checkbox\n- plain :: def\n", | |
| 182 | "#+begin_quote\n#+begin_quote\nInner quote that goes on long enough to need filling at the narrower width.\n#+end_quote\n#+end_quote\n", | |
| 183 | "- - nested first\n - second\n- after\n", | |
| 184 | "- line one\\\\\n line two\n\n#+begin_quote\nq one\\\\\nq two\n#+end_quote\n", | |
| 185 | "#+OPTIONS: H:1\n* A\n** B\n*** C\n**** D\n***** E\ntext under E that is long enough to be filled at the narrower width of it.\n", | |
| 186 | "#+OPTIONS: num:nil\n* /Styled/ title\n* Other\nSee [[*/Styled/ title][it]] and [[*/Styled/ title]].\n", | |
| 187 | "* A\n:PROPERTIES:\n:CUSTOM_ID: a\n:END:\n* B\nNote.[fn:1]\n\n[fn:1] See [[#a][A]] and [[https://x.y][X]].\n", | |
| 188 | "#+OPTIONS: <:nil\n<2024-01-15 Mon>\n\nText <2024-01-15 Mon> kept.\n", | |
| 189 | "#+OPTIONS: stat:t\n* Tasks [1/2]\n- [X] a\n- [ ] b\n", | |
| 190 | "Averyveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongword and more.\n", | |
| 191 | "\n", | |
| 192 | "#+TITLE: Only a title\n", | |
| 193 | "#+begin_center\n#+ASCII: raw\n\\begin{x}\ny\n\\end{x}\n#+end_center\n", | |
| 194 | "#+begin_justifyright\nlower\n#+end_justifyright\n", | |
| 195 | "* A\n- item\n [fn:1]\n\n#+begin_quote\nq[fn:2]\n#+end_quote\n\n[fn:1] One.\n\n[fn:2] Two.\n", | |
| 196 | "#+OPTIONS: H:2\n* A\n** B\n*** C\n#+TOC: headlines\n", | |
| 197 | "#+OPTIONS: toc:1\n* A\n** B\n* C [[https://a.b][link]]\n", | |
| 198 | "- a\n # comment\n b\n- c\n\n # comment\n\n d\n- # only\n e\n", | |
| 199 | "* Tasks [1/3] [33%]\n** DONE One\nCLOSED: [2024-01-01 Mon 10:00]\n** TODO Two\n** Three\n", | |
| 200 | "Some *bold\ntext* across /lines of\nitalic/ text.\n", | |
| 201 | "#+begin_src emacs-lisp\n(message \"hi\")\n#+end_src\n\n#+begin_src python :exports code\nprint(1)\n#+end_src\n", | |
| 202 | "Para.\n\n\n\nAfter three blank lines.\n\n- item\n\n\n- item after two\n", | |
| 203 | "* A\n\n\nBody after blanks.\n\n\n* B\n", | |
| 204 | "1. one\n\n para\n\n #+begin_example\n ex\n #+end_example\n2. two\n", | |
| 205 | "- a\n | x | y |\n |---+---|\n | 1 | 2 |\n- b\n", | |
| 206 | "#+TITLE: T\n#+AUTHOR: A\n#+DATE: <2024-01-02 Tue>\n#+OPTIONS: toc:nil\nx\n", | |
| 207 | "#+AUTHOR: Just an author\n#+DATE: 2024-01-02\nx\n", | |
| 208 | "#+DATE: 2024-01-02\nx\n", | |
| 209 | "#+begin_src makefile\nall: x\n\tcc -o x x.c\n#+end_src\n", | |
| 210 | "#+begin_src makefile -n -r\nall: x\n \t\tcc\tx (ref:a)\n#+end_src\n\n#+begin_example\nall: x\n\tcc\n#+end_example\n", | |
| 211 | "#+OPTIONS: e:t\n\\alpha\\beta \\ast{} \\S \\para \\deg \\plusmn \\micro \\middot \\cedil \\ordf \\iexcl \\pound \\yen \\euro \\trade\n", | |
| 178 | 212 | ] |
| 179 | 213 | |
| 180 | 214 | static var cases: [(String, ASCIIOracle.Case)] { |
| @@ -217,6 +251,91 @@ struct ASCIIExportTests { | ||
| 217 | 251 | try compare(cases, names: names) |
| 218 | 252 | } |
| 219 | 253 | |
| 254 | /// A subtree's `EXPORT_*` properties replace the file's keywords. | |
| 255 | @Test(.enabled(if: oracle)) | |
| 256 | func subtreePropertiesMatchEmacs() throws { | |
| 257 | let doc = """ | |
| 258 | #+TITLE: Doc | |
| 259 | #+SUBTITLE: File sub | |
| 260 | #+AUTHOR: File author | |
| 261 | #+LANGUAGE: fr | |
| 262 | #+EXCLUDE_TAGS: drop | |
| 263 | * Top | |
| 264 | :PROPERTIES: | |
| 265 | :EXPORT_SUBTITLE: Sub | |
| 266 | :EXPORT_AUTHOR: Someone | |
| 267 | :EXPORT_DATE: 2024-05-06 | |
| 268 | :EXPORT_LANGUAGE: de | |
| 269 | :EXPORT_EXCLUDE_TAGS: gone | |
| 270 | :EXPORT_OPTIONS: toc:nil | |
| 271 | :END: | |
| 272 | Body[fn:1]. | |
| 273 | ** Child | |
| 274 | x | |
| 275 | ** Dropped :gone: | |
| 276 | z | |
| 277 | ** Kept :drop: | |
| 278 | w | |
| 279 | ||
| 280 | [fn:1] Note. | |
| 281 | ||
| 282 | """ | |
| 283 | let top = (doc as NSString).range(of: "Body").location | |
| 284 | let cases = ASCIIExport.Charset.allCases.map { ASCIIOracle.Case(text: doc, charset: $0, subtree: EmacsOracle.point(top, in: doc)) } | |
| 285 | try compare(cases, names: ASCIIExport.Charset.allCases.map { "subtree properties \($0.rawValue)" }) | |
| 286 | } | |
| 287 | ||
| 288 | /// Links to what the export leaves out stop both exports. | |
| 289 | @Test(.enabled(if: oracle)) | |
| 290 | func abortsWhereEmacsAborts() throws { | |
| 291 | let docs = [ | |
| 292 | "* A :noexport:\n* B\nSee [[*A]].\n", | |
| 293 | "* A :noexport:\n:PROPERTIES:\n:CUSTOM_ID: a\n:END:\n* B\nSee [[#a]].\n", | |
| 294 | "* COMMENT A\n* B\nSee [[*A]].\n", | |
| 295 | "See [[nowhere]].\n", | |
| 296 | "See [[id:nowhere]].\n", | |
| 297 | "#+OPTIONS: num:nil\n* /Styled/ title\nSee [[*Styled title]].\n", | |
| 298 | ] | |
| 299 | let emacs = try ASCIIOracle.export(docs.map { ASCIIOracle.Case(text: $0) }) | |
| 300 | for (doc, theirs) in zip(docs, emacs) { | |
| 301 | let ours = ASCIIOracle.native(ASCIIOracle.Case(text: doc)) | |
| 302 | #expect(theirs.hasPrefix("ERROR: "), "\(doc)\n--- emacs\n\(theirs)") | |
| 303 | #expect(ours.hasPrefix("ERROR: Unable to resolve link"), "\(doc)\n--- ours\n\(ours)") | |
| 304 | } | |
| 305 | } | |
| 306 | ||
| 307 | /// What Emacs exports differently or refuses is refused, naming it. | |
| 308 | @Test func refusesWhatItCannotMatch() { | |
| 309 | func reason(_ org: String) -> String? { | |
| 310 | do { | |
| 311 | _ = try ASCIIExport.document(org) | |
| 312 | return nil | |
| 313 | } catch let error as ASCIIExport.Unsupported { | |
| 314 | return error.reason | |
| 315 | } catch { | |
| 316 | return "\(error)" | |
| 317 | } | |
| 318 | } | |
| 319 | #expect(reason("Ref[fn:9].\n") == "Definition not found for footnote 9") | |
| 320 | #expect(reason("#+OPTIONS: \\n:t\nx\n") == "#+OPTIONS: \\n:t") | |
| 321 | #expect(reason("#+OPTIONS: tex:verbatim\nx\n") == "#+OPTIONS: tex:verbatim") | |
| 322 | #expect(reason("#+begin_src sh :exports results\necho\n#+end_src\n") == ":exports results") | |
| 323 | #expect(reason("[cite:@key]\n") == "Citations") | |
| 324 | #expect(reason("src_sh{echo 1}\n") == "Inline source blocks") | |
| 325 | #expect(reason("A <<here>> target.\nSee [[here]].\n") == "Links to targets and named elements") | |
| 326 | #expect(reason("#+NAME: t\n| a |\nSee [[t]].\n") == "Links to targets and named elements") | |
| 327 | #expect(reason("| / | < | > |\n| | a | b |\n") == "Tables with special columns or column groups") | |
| 328 | #expect(reason("Text[fn:1].\n\n[fn:1] One.\n\nTwo.\n") == "Footnote definitions of several paragraphs") | |
| 329 | #expect(reason("漢字\n") == "Wide or combining characters") | |
| 330 | #expect(reason("a\tb\n") == "Tabs outside code") | |
| 331 | #expect(reason("#+begin_src make\n\tcc\n#+end_src\n") == "Tabs in indented code") | |
| 332 | #expect(reason("#+TOC: tables\n") == "#+TOC: tables") | |
| 333 | #expect(reason("-\n- x\n") == "Empty list items") | |
| 334 | #expect(reason("* A\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n") == "UNNUMBERED and ALT_TITLE properties") | |
| 335 | #expect(reason("* A [fn:1]\n\n[fn:1] x\n") == "Footnotes in headings") | |
| 336 | #expect(reason("#+begin_quote\n\nq\n#+end_quote\n") == "Blocks opening with a blank line") | |
| 337 | } | |
| 338 | ||
| 220 | 339 | private func compare(_ cases: [ASCIIOracle.Case], names: [String]) throws { |
| 221 | 340 | let emacs = try ASCIIOracle.export(cases) |
| 222 | 341 | for (index, c) in cases.enumerated() { |