Commit 36f91027c7
Verified · cmc
Layout: unified · split
Sources/OrgCore/Export/ASCIIExport.swift +36 −12
| @@ -399,7 +399,7 @@ final class ASCIIRenderer { | |||
| 399 | let linkStart = noted.count | 399 | let linkStart = noted.count |
| 400 | var contents = try elements(nodes, end: end, context: Context(width: width)) | 400 | var contents = try elements(nodes, end: end, context: Context(width: width)) |
| 401 | if heading == nil { | 401 | if heading == nil { |
| 402 | let links = describeLinks(from: linkStart, width: width) | 402 | let links = try describeLinks(from: linkStart, width: width) |
| 403 | if !links.isEmpty { contents = Self.normalize(contents) + "\n\n" + links } | 403 | if !links.isEmpty { contents = Self.normalize(contents) + "\n\n" + links } |
| 404 | } | 404 | } |
| 405 | if heading != nil, low == false { contents = ASCIIText.indent(contents, Self.innerMargin) } | 405 | if heading != nil, low == false { contents = ASCIIText.indent(contents, Self.innerMargin) } |
| @@ -426,7 +426,7 @@ final class ASCIIRenderer { | |||
| 426 | let end = subsections.first?.range.lowerBound ?? node.range.upperBound | 426 | let end = subsections.first?.range.lowerBound ?? node.range.upperBound |
| 427 | sectionText = try section(elements, end: end, heading: level) | 427 | sectionText = try section(elements, end: end, heading: level) |
| 428 | } | 428 | } |
| 429 | let links = describeLinks(from: linkStart, width: width) | 429 | let links = try describeLinks(from: linkStart, width: width) |
| 430 | let sub = try subsections.compactMap { try headline($0) }.joined() | 430 | let sub = try subsections.compactMap { try headline($0) }.joined() |
| 431 | if links.isEmpty { | 431 | if links.isEmpty { |
| 432 | body = (sectionText ?? "") + sub | 432 | body = (sectionText ?? "") + sub |
| @@ -434,7 +434,7 @@ final class ASCIIRenderer { | |||
| 434 | body = (sectionText.map { Self.normalize($0) + "\n\n" } ?? "") + links + sub | 434 | body = (sectionText.map { Self.normalize($0) + "\n\n" } ?? "") + links + sub |
| 435 | } | 435 | } |
| 436 | } else { | 436 | } else { |
| 437 | _ = describeLinks(from: linkStart, width: width) | 437 | _ = try? describeLinks(from: linkStart, width: width) |
| 438 | } | 438 | } |
| 439 | let out: String | 439 | let out: String |
| 440 | if let low { | 440 | if let low { |
| @@ -1103,12 +1103,9 @@ final class ASCIIRenderer { | |||
| 1103 | let blanks = t.prefix { $0 == " " || $0 == "\t" } | 1103 | let blanks = t.prefix { $0 == " " || $0 == "\t" } |
| 1104 | t = String(repeating: " ", count: blanks.count) + t.dropFirst(blanks.count) | 1104 | t = String(repeating: " ", count: blanks.count) + t.dropFirst(blanks.count) |
| 1105 | } | 1105 | } |
| 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 | } | ||
| 1109 | dropBlanks = false | 1106 | dropBlanks = false |
| 1110 | afterObject = false | 1107 | afterObject = false |
| 1111 | out += plainText(t) | 1108 | out += try plainTextAndLinks(t) |
| 1112 | index += 1 | 1109 | index += 1 |
| 1113 | case .object(let node, let children): | 1110 | case .object(let node, let children): |
| 1114 | dropBlanks = false | 1111 | dropBlanks = false |
| @@ -1182,6 +1179,20 @@ final class ASCIIRenderer { | |||
| 1182 | return text.replacingOccurrences(of: "---", with: "—").replacingOccurrences(of: "--", with: "–").replacingOccurrences(of: "...", with: "…") | 1179 | return text.replacingOccurrences(of: "---", with: "—").replacingOccurrences(of: "--", with: "–").replacingOccurrences(of: "...", with: "…") |
| 1183 | } | 1180 | } |
| 1184 | 1181 | ||
| 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 | |||
| 1185 | // MARK: Links | 1196 | // MARK: Links |
| 1186 | 1197 | ||
| 1187 | /// A link written in notes mode: its footprint and how `org-ascii--describe-links` | 1198 | /// A link written in notes mode: its footprint and how `org-ascii--describe-links` |
| @@ -1197,13 +1208,20 @@ final class ASCIIRenderer { | |||
| 1197 | private var noting = true | 1208 | private var noting = true |
| 1198 | 1209 | ||
| 1199 | /// `org-ascii--describe-links` for the links noted since `start`, each once. | 1210 | /// `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 { |
| 1201 | guard linksToNotes else { return "" } | 1212 | guard linksToNotes else { return "" } |
| 1202 | var seen: Set<String> = [] | 1213 | var seen: Set<String> = [] |
| 1203 | var out = "" | 1214 | var out = "" |
| 1204 | for link in noted[start...] where !seen.contains(link.footprint) { | 1215 | for link in noted[start...] where !seen.contains(link.footprint) { |
| 1205 | seen.insert(link.footprint) | 1216 | 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 | } | ||
| 1207 | } | 1225 | } |
| 1208 | noted.removeSubrange(start...) | 1226 | noted.removeSubrange(start...) |
| 1209 | return out | 1227 | return out |
| @@ -1212,8 +1230,13 @@ final class ASCIIRenderer { | |||
| 1212 | /// Link types Org registers in an Org buffer that this export doesn't write as Emacs does. | 1230 | /// Link types Org registers in an Org buffer that this export doesn't write as Emacs does. |
| 1213 | private static let unrenderedTypes: Set<String> = ["attachment", "bbdb", "bibtex", "docview", "doi", "eww", "gnus", "info", "irc", "mhe", "rmail", "w3m"] | 1231 | private static let unrenderedTypes: Set<String> = ["attachment", "bbdb", "bibtex", "docview", "doi", "eww", "gnus", "info", "irc", "mhe", "rmail", "w3m"] |
| 1214 | 1232 | ||
| 1215 | /// `org-link-plain-re`, loosely, for the plain links the parser leaves as text. | 1233 | /// `org-link-plain-re`, in any case, 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}/)\]>-]"#) | 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 | }() | ||
| 1217 | 1240 | ||
| 1218 | private static let externalTypes: Set<String> = ["file+sys", "file+emacs", "shell", "news", "mailto", "https", "http", "ftp", "shortdoc", "help", "file", "elisp"] | 1241 | private static let externalTypes: Set<String> = ["file+sys", "file+emacs", "shell", "news", "mailto", "https", "http", "ftp", "shortdoc", "help", "file", "elisp"] |
| 1219 | 1242 | ||
| @@ -1280,7 +1303,8 @@ final class ASCIIRenderer { | |||
| 1280 | 1303 | ||
| 1281 | /// `org-ascii--describe-datum` for a heading `reference` names. | 1304 | /// `org-ascii--describe-datum` for a heading `reference` names. |
| 1282 | private func seeSection(_ reference: String) throws -> String { | 1305 | 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") | ||
| 1284 | // Emacs's `format` stops at `%d` given a string (Slovenian). | 1308 | // Emacs's `format` stops at `%d` given a string (Slovenian). |
| 1285 | if format.contains("%d") { throw Unsupported(reason: "Format specifier doesn't match argument type") } | 1309 | if format.contains("%d") { throw Unsupported(reason: "Format specifier doesn't match argument type") } |
| 1286 | return format.replacingOccurrences(of: "%s", with: reference) | 1310 | return format.replacingOccurrences(of: "%s", with: reference) |
Tests/OrgCoreTests/ASCIIExportTests.swift +10 −1
| @@ -211,6 +211,11 @@ struct ASCIIExportTests { | |||
| 211 | "#+LANGUAGE: ja\n#+OPTIONS: toc:nil\n* A\nx[fn:1]\n\n[fn:1] n\n", | 211 | "#+LANGUAGE: ja\n#+OPTIONS: toc:nil\n* A\nx[fn:1]\n\n[fn:1] n\n", |
| 212 | "#+LANGUAGE: zh-CN\n* A\nx\n", | 212 | "#+LANGUAGE: zh-CN\n* A\nx\n", |
| 213 | "Types [[DOI:10.1/z][d]] [[Attachment:x.png]] [[ID:x]] [[INFO:emacs][i]] b_doi:10.1/x.\n", | 213 | "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", | ||
| 214 | "a\\\\\\\\\nb\n\nc\\\\\\\\\\\\ \nd\n\ne\\\\\nf\n", | 219 | "a\\\\\\\\\nb\n\nc\\\\\\\\\\\\ \nd\n\ne\\\\\nf\n", |
| 215 | "#+OPTIONS: e:t\n\\alpha\\beta \\ast{} \\S \\para \\deg \\plusmn \\micro \\middot \\cedil \\ordf \\iexcl \\pound \\yen \\euro \\trade\n", | 220 | "#+OPTIONS: e:t\n\\alpha\\beta \\ast{} \\S \\para \\deg \\plusmn \\micro \\middot \\cedil \\ordf \\iexcl \\pound \\yen \\euro \\trade\n", |
| 216 | ] | 221 | ] |
| @@ -358,7 +363,11 @@ struct ASCIIExportTests { | |||
| 358 | #expect(reason("<info:emacs#Top>\n") == "Links of type info") | 363 | #expect(reason("<info:emacs#Top>\n") == "Links of type info") |
| 359 | #expect(reason("Case DOI:10.1/z.\n") == "Plain links of type DOI") | 364 | #expect(reason("Case DOI:10.1/z.\n") == "Plain links of type DOI") |
| 360 | #expect(reason("#+LANGUAGE: ja\n#+CAPTION: c\n| a |\n") == "Wide or combining characters") | 365 | #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") | ||
| 362 | #expect(reason("#+LANGUAGE: sl\n* A\nSee [[*B][b]].\n* B\n") == "Format specifier doesn't match argument type") | 371 | #expect(reason("#+LANGUAGE: sl\n* A\nSee [[*B][b]].\n* B\n") == "Format specifier doesn't match argument type") |
| 363 | #expect(reason("#+LANGUAGE: sl\n* A\nSee [[*B]].\n* B\n") == nil) | 372 | #expect(reason("#+LANGUAGE: sl\n* A\nSee [[*B]].\n* B\n") == nil) |
| 364 | } | 373 | } |
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 | |||
| 271 | 271 | ||
| 272 | =#+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. | 272 | =#+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. |
| 273 | 273 | ||
| 274 | A 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/). | 274 | A 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/). |
| 275 | 275 | ||
| 276 | * Markdown export | 276 | * Markdown export |
| 277 | 277 | ||