Commit 36f91027c7

36f91027c70dff931872f2294bfd9b15773481c9

parent: 43255df900

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-20 22:19 UTC

Match ox-ascii on wide link notes and plain links in any case

Refuse a wide section reference in a link note only when it takes a
line past the fill width. Require a path character before the last
one in plain links, and write case variants of http, https, mailto and
file plain links in angle brackets.

Layout: unified · split

Sources/OrgCore/Export/ASCIIExport.swift +36 −12
@@ -399,7 +399,7 @@ final class ASCIIRenderer {
399399 let linkStart = noted.count
400400 var contents = try elements(nodes, end: end, context: Context(width: width))
401401 if heading == nil {
402 let links = describeLinks(from: linkStart, width: width)
402 let links = try describeLinks(from: linkStart, width: width)
403403 if !links.isEmpty { contents = Self.normalize(contents) + "\n\n" + links }
404404 }
405405 if heading != nil, low == false { contents = ASCIIText.indent(contents, Self.innerMargin) }
@@ -426,7 +426,7 @@ final class ASCIIRenderer {
426426 let end = subsections.first?.range.lowerBound ?? node.range.upperBound
427427 sectionText = try section(elements, end: end, heading: level)
428428 }
429 let links = describeLinks(from: linkStart, width: width)
429 let links = try describeLinks(from: linkStart, width: width)
430430 let sub = try subsections.compactMap { try headline($0) }.joined()
431431 if links.isEmpty {
432432 body = (sectionText ?? "") + sub
@@ -434,7 +434,7 @@ final class ASCIIRenderer {
434434 body = (sectionText.map { Self.normalize($0) + "\n\n" } ?? "") + links + sub
435435 }
436436 } else {
437 _ = describeLinks(from: linkStart, width: width)
437 _ = try? describeLinks(from: linkStart, width: width)
438438 }
439439 let out: String
440440 if let low {
@@ -1103,12 +1103,9 @@ final class ASCIIRenderer {
11031103 let blanks = t.prefix { $0 == " " || $0 == "\t" }
11041104 t = String(repeating: " ", count: blanks.count) + t.dropFirst(blanks.count)
11051105 }
1106 if let m = Self.plainLink.firstMatch(in: t, range: NSRange(t.startIndex..., in: t)) {
1107 throw Unsupported(reason: "Plain links of type \((t as NSString).substring(with: m.range(at: 1)))")
1108 }
11091106 dropBlanks = false
11101107 afterObject = false
1111 out += plainText(t)
1108 out += try plainTextAndLinks(t)
11121109 index += 1
11131110 case .object(let node, let children):
11141111 dropBlanks = false
@@ -1182,6 +1179,20 @@ final class ASCIIRenderer {
11821179 return text.replacingOccurrences(of: "---", with: "—").replacingOccurrences(of: "--", with: "–").replacingOccurrences(of: "...", with: "…")
11831180 }
11841181
1182 /// Text with the plain links the parser leaves in it, `FILE:x` and the like, written as Emacs does.
1183 private func plainTextAndLinks(_ t: String) throws -> String {
1184 let ns = t as NSString
1185 var out = ""
1186 var last = 0
1187 for m in Self.plainLink.matches(in: t, range: NSRange(location: 0, length: ns.length)) {
1188 let type = ns.substring(with: m.range(at: 1))
1189 guard ["http", "https", "mailto", "file"].contains(type.lowercased()) else { throw Unsupported(reason: "Plain links of type \(type)") }
1190 out += plainText(ns.substring(with: NSRange(location: last, length: m.range.location - last))) + "<" + ns.substring(with: m.range) + ">"
1191 last = NSMaxRange(m.range)
1192 }
1193 return out + plainText(ns.substring(from: last))
1194 }
1195
11851196 // MARK: Links
11861197
11871198 /// A link written in notes mode: its footprint and how `org-ascii--describe-links`
@@ -1197,13 +1208,20 @@ final class ASCIIRenderer {
11971208 private var noting = true
11981209
11991210 /// `org-ascii--describe-links` for the links noted since `start`, each once.
1200 private func describeLinks(from start: Int, width: Int) -> String {
1211 private func describeLinks(from start: Int, width: Int) throws -> String {
12011212 guard linksToNotes else { return "" }
12021213 var seen: Set<String> = []
12031214 var out = ""
12041215 for link in noted[start...] where !seen.contains(link.footprint) {
12051216 seen.insert(link.footprint)
1206 if let note = link.note { out += ASCIIText.fill(note, width: width) + "\n\n" }
1217 if let note = link.note {
1218 let filled = ASCIIText.fill(note, width: width)
1219 // Emacs fills by columns: the same unless wide characters take a line past them.
1220 if note.unicodeScalars.contains(where: Self.isZeroWidth) || filled.split(separator: "\n").contains(where: { Self.width(String($0)) > max(width, $0.count) }) {
1221 throw Unsupported(reason: "Wide or combining characters")
1222 }
1223 out += filled + "\n\n"
1224 }
12071225 }
12081226 noted.removeSubrange(start...)
12091227 return out
@@ -1212,8 +1230,13 @@ final class ASCIIRenderer {
12121230 /// Link types Org registers in an Org buffer that this export doesn't write as Emacs does.
12131231 private static let unrenderedTypes: Set<String> = ["attachment", "bbdb", "bibtex", "docview", "doi", "eww", "gnus", "info", "irc", "mhe", "rmail", "w3m"]
12141232
1215 /// `org-link-plain-re`, loosely, for the plain links the parser leaves as text.
1216 private static let plainLink = try! NSRegularExpression(pattern: #"(?i)(?<![\p{L}\p{N}_])(attachment|bbdb|bibtex|docview|doi|elisp|eww|file\+emacs|file\+sys|file|ftp|gnus|help|https?|id|info|irc|mailto|mhe|news|rmail|shell|shortdoc|w3m):\S*[\p{L}\p{N}/)\]>-]"#)
1233 /// `org-link-plain-re`, in any case, for the plain links the parser leaves as text.
1234 private static let plainLink: NSRegularExpression = {
1235 let plain = #"[^\[\]\s()<>]"#
1236 let parens = #"[<(\[](?:\#(plain)|[<(\[]\#(plain)*[\])>])*[\])>]"#
1237 let types = "attachment|bbdb|bibtex|docview|doi|elisp|eww|file\\+emacs|file\\+sys|file|ftp|gnus|help|https?|id|info|irc|mailto|mhe|news|rmail|shell|shortdoc|w3m"
1238 return try! NSRegularExpression(pattern: #"(?i)(?<![\p{L}\p{N}_])("# + types + #"):(?:\#(plain)|\#(parens))+(?:[^\p{P}\p{S}\s]|[/-]|\#(parens))"#)
1239 }()
12171240
12181241 private static let externalTypes: Set<String> = ["file+sys", "file+emacs", "shell", "news", "mailto", "https", "http", "ftp", "shortdoc", "help", "file", "elisp"]
12191242
@@ -1280,7 +1303,8 @@ final class ASCIIRenderer {
12801303
12811304 /// `org-ascii--describe-datum` for a heading `reference` names.
12821305 private func seeSection(_ reference: String) throws -> String {
1283 let format = try translateFilled("See section %s")
1306 // In notes, `describeLinks` checks the width.
1307 let format = linksToNotes ? translate("See section %s") : try translateFilled("See section %s")
12841308 // Emacs's `format` stops at `%d` given a string (Slovenian).
12851309 if format.contains("%d") { throw Unsupported(reason: "Format specifier doesn't match argument type") }
12861310 return format.replacingOccurrences(of: "%s", with: reference)
Tests/OrgCoreTests/ASCIIExportTests.swift +10 −1
@@ -211,6 +211,11 @@ struct ASCIIExportTests {
211211 "#+LANGUAGE: ja\n#+OPTIONS: toc:nil\n* A\nx[fn:1]\n\n[fn:1] n\n",
212212 "#+LANGUAGE: zh-CN\n* A\nx\n",
213213 "Types [[DOI:10.1/z][d]] [[Attachment:x.png]] [[ID:x]] [[INFO:emacs][i]] b_doi:10.1/x.\n",
214 "#+LANGUAGE: ja\n* A\nSee [[*B][b]].\n* B\ny\n",
215 "#+LANGUAGE: zh-CN\n* A\nSee [[*B][b]].\n* B\ny\n",
216 "#+LANGUAGE: ja\n* A\nSee [[*B][\(String(repeating: "b", count: 72))]].\n* B\ny\n",
217 "Text doi:- here, doi:a. and b.\n",
218 "x HTTPS://x.y/z and mailto:a@b.c and File:a.txt. FILE:foo-- \"q\" FILE:a(b) Mailto:a@b, y\n",
214219 "a\\\\\\\\\nb\n\nc\\\\\\\\\\\\ \nd\n\ne\\\\\nf\n",
215220 "#+OPTIONS: e:t\n\\alpha\\beta \\ast{} \\S \\para \\deg \\plusmn \\micro \\middot \\cedil \\ordf \\iexcl \\pound \\yen \\euro \\trade\n",
216221 ]
@@ -358,7 +363,11 @@ struct ASCIIExportTests {
358363 #expect(reason("<info:emacs#Top>\n") == "Links of type info")
359364 #expect(reason("Case DOI:10.1/z.\n") == "Plain links of type DOI")
360365 #expect(reason("#+LANGUAGE: ja\n#+CAPTION: c\n| a |\n") == "Wide or combining characters")
361 #expect(reason("#+LANGUAGE: ja\n* A\nSee [[*B][b]].\n* B\n") == "Wide or combining characters")
366 #expect(reason("#+LANGUAGE: ja\n* A\nSee [[*B][\(String(repeating: "b", count: 56))]].\n* B\n") == "Wide or combining characters")
367 #expect(reason("#+LANGUAGE: ja\nx[fn:1]\n* B\n\n[fn:1] See [[*B][b]].\n") == "Wide or combining characters")
368 #expect(reason("Text doi:- here.\n") == nil)
369 #expect(reason("Plain FILE:foo.txt and HTTPS://x.y here.\n") == nil)
370 #expect(reason("Plain FILE:foo.txt and SHELL:ls here.\n") == "Plain links of type SHELL")
362371 #expect(reason("#+LANGUAGE: sl\n* A\nSee [[*B][b]].\n* B\n") == "Format specifier doesn't match argument type")
363372 #expect(reason("#+LANGUAGE: sl\n* A\nSee [[*B]].\n* B\n") == nil)
364373 }
docs/manual/guide/12-export.org +1 −1
@@ -271,7 +271,7 @@ Export to Plain Text (ASCII) writes the =.txt= file that Emacs's =org-ascii-expo
271271
272272=#+TITLE=, =#+SUBTITLE=, =#+AUTHOR=, =#+DATE=, =#+LANGUAGE=, =#+TOC: headlines=, =#+ASCII:= lines, =#+BEGIN_EXPORT ascii= blocks, =@@ascii:...@@= snippets, =#+ATTR_ASCII: :width= on horizontal rules, and the =center=, =justifyleft= and =justifyright= blocks 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_TITLE=, =EXPORT_SUBTITLE=, =EXPORT_AUTHOR=, =EXPORT_DATE=, =EXPORT_LANGUAGE=, =EXPORT_OPTIONS=, =EXPORT_SELECT_TAGS= and =EXPORT_EXCLUDE_TAGS= properties replace the file's keywords. Leading tabs in a source block become spaces, as in Emacs.
273273
274A file with something this export doesn't handle stops with "Can't export to plain text: " and what it was, rather than giving a =.txt= that differs from Emacs's: citations and =#+BIBLIOGRAPHY=, radio targets, inline tasks, table.el tables, diary sexps, inline source blocks and calls, =#+CALL=, source blocks exported with their results, =#+RESULTS= without a source block, wide or combining characters (such as CJK text, emoji and combining accents), also in the =#+LANGUAGE= words for captions and section references, tabs outside source and example blocks or in the indentation of an indented source block, footnote definitions of several paragraphs, footnotes in headings, keywords, captions or other footnotes, =UNNUMBERED= and =ALT_TITLE=, tables with special columns or column groups, empty list items, alphabetical counters, affiliated keywords in list items, blocks whose first line is blank, headings inside blocks, numbered source blocks inside other blocks, verse blocks with more than text, links inside link descriptions, =doi=, =info=, =irc=, =attachment=, =docview=, =bbdb=, =bibtex=, =gnus=, =rmail=, =mhe=, =w3m= and =eww= links, plain links other than =http=, =https=, =mailto= and =file=, links to =<<targets>>= and named elements, short or repeated captions, unknown entities, a keyword such as =#+SUBTITLE= repeated with an empty value, =#+TOC:= other than =headlines= or with =local= or =:target=, 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 the export leaves out, as does a described link to a heading with =#+LANGUAGE: sl=. On iOS the share sheet stops and the message shows at the bottom of the screen. Publishing to plain text 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/).
274A file with something this export doesn't handle stops with "Can't export to plain text: " and what it was, rather than giving a =.txt= that differs from Emacs's: citations and =#+BIBLIOGRAPHY=, radio targets, inline tasks, table.el tables, diary sexps, inline source blocks and calls, =#+CALL=, source blocks exported with their results, =#+RESULTS= without a source block, wide or combining characters (such as CJK text, emoji and combining accents), also in the =#+LANGUAGE= words for captions, for section references in footnotes, and for a link's note when they take a line past the text width, tabs outside source and example blocks or in the indentation of an indented source block, footnote definitions of several paragraphs, footnotes in headings, keywords, captions or other footnotes, =UNNUMBERED= and =ALT_TITLE=, tables with special columns or column groups, empty list items, alphabetical counters, affiliated keywords in list items, blocks whose first line is blank, headings inside blocks, numbered source blocks inside other blocks, verse blocks with more than text, links inside link descriptions, =doi=, =info=, =irc=, =attachment=, =docview=, =bbdb=, =bibtex=, =gnus=, =rmail=, =mhe=, =w3m= and =eww= links, plain links other than =http=, =https=, =mailto= and =file= in any case, links to =<<targets>>= and named elements, short or repeated captions, unknown entities, a keyword such as =#+SUBTITLE= repeated with an empty value, =#+TOC:= other than =headlines= or with =local= or =:target=, 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 the export leaves out, as does a described link to a heading with =#+LANGUAGE: sl=. On iOS the share sheet stops and the message shows at the bottom of the screen. Publishing to plain text 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/).
275275
276276* Markdown export
277277