Fix LaTeX export of elisp: and plain links, multi-line link paths !428
14 files changed, +145 −31
Layout: unified · split
Sources/OrgCore/Export/ASCIIExport.swift +2 −2
| @@ -1231,7 +1231,7 @@ final class ASCIIRenderer { | |||
| 1231 | 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"] |
| 1232 | 1232 | ||
| 1233 | /// `org-link-plain-re`, in any case, 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. |
| 1234 | private static let plainLink: NSRegularExpression = { | 1234 | static let plainLink: NSRegularExpression = { |
| 1235 | let plain = #"[^\[\]\s()<>]"# | 1235 | let plain = #"[^\[\]\s()<>]"# |
| 1236 | let parens = #"[<(\[](?:\#(plain)|[<(\[]\#(plain)*[\])>])*[\])>]"# | 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" | 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" |
| @@ -1244,7 +1244,7 @@ final class ASCIIRenderer { | |||
| 1244 | if node.firstChild(.linkDescription)?.descendants().contains(where: { $0.kind == .link }) == true { | 1244 | if node.firstChild(.linkDescription)?.descendants().contains(where: { $0.kind == .link }) == true { |
| 1245 | throw Unsupported(reason: "Links in link descriptions") | 1245 | throw Unsupported(reason: "Links in link descriptions") |
| 1246 | } | 1246 | } |
| 1247 | let pathToken = node.tokens.first { $0.kind == .linkPath }?.text | 1247 | let pathToken = node.linkPath |
| 1248 | var raw = pathToken ?? node.text | 1248 | var raw = pathToken ?? node.text |
| 1249 | if pathToken == nil, raw.hasPrefix("<"), raw.hasSuffix(">") { raw = String(raw.dropFirst().dropLast()) } | 1249 | if pathToken == nil, raw.hasPrefix("<"), raw.hasSuffix(">") { raw = String(raw.dropFirst().dropLast()) } |
| 1250 | var description: String? = node.firstChild(.linkDescription) == nil ? nil : try transcode(children) | 1250 | var description: String? = node.firstChild(.linkDescription) == nil ? nil : try transcode(children) |
Sources/OrgCore/Export/ASCIIText.swift +1 −1
| @@ -461,7 +461,7 @@ struct ASCIIText { | |||
| 461 | 461 | ||
| 462 | func link(_ node: SyntaxNode) -> String { | 462 | func link(_ node: SyntaxNode) -> String { |
| 463 | let description = node.firstChild(.linkDescription).map { inline($0) } | 463 | let description = node.firstChild(.linkDescription).map { inline($0) } |
| 464 | guard let path = node.tokens.first(where: { $0.kind == .linkPath })?.text else { | 464 | guard let path = node.linkPath else { |
| 465 | var target = node.text | 465 | var target = node.text |
| 466 | if target.hasPrefix("<"), target.hasSuffix(">") { target = String(target.dropFirst().dropLast()) } | 466 | if target.hasPrefix("<"), target.hasSuffix(">") { target = String(target.dropFirst().dropLast()) } |
| 467 | // A radio link is its text. | 467 | // A radio link is its text. |
Sources/OrgCore/Export/ExportOptions.swift +1 −1
| @@ -205,7 +205,7 @@ public enum ExportPreprocess { | |||
| 205 | let rebased = rebasingLinks(description.text, from: directory, to: includer, plain: true) | 205 | let rebased = rebasingLinks(description.text, from: directory, to: includer, plain: true) |
| 206 | if rebased != description.text { edits.append((description.range, rebased)) } | 206 | if rebased != description.text { edits.append((description.range, rebased)) } |
| 207 | } | 207 | } |
| 208 | if let path = node.tokens.first(where: { $0.kind == .linkPath }), let rebased = rebasedPath(path.text, from: directory, to: includer, bracket: true) { | 208 | if let path = node.tokens.first(where: { $0.kind == .linkPath }), let text = node.linkPath, let rebased = rebasedPath(text, from: directory, to: includer, bracket: true) { |
| 209 | edits.append((path.range, rebased)) | 209 | edits.append((path.range, rebased)) |
| 210 | } | 210 | } |
| 211 | } else if whole.hasPrefix("<"), whole.hasSuffix(">") { | 211 | } else if whole.hasPrefix("<"), whole.hasSuffix(">") { |
Sources/OrgCore/Export/HTMLExport.swift +1 −1
| @@ -856,7 +856,7 @@ final class HTMLRenderer { | |||
| 856 | static let imageExtensions: Set<String> = ["png", "jpg", "jpeg", "gif", "svg", "webp", "bmp", "tif", "tiff", "avif"] | 856 | static let imageExtensions: Set<String> = ["png", "jpg", "jpeg", "gif", "svg", "webp", "bmp", "tif", "tiff", "avif"] |
| 857 | 857 | ||
| 858 | private func link(_ node: SyntaxNode) -> String { | 858 | private func link(_ node: SyntaxNode) -> String { |
| 859 | let path = node.tokens.first { $0.kind == .linkPath }?.text | 859 | let path = node.linkPath |
| 860 | let description = node.firstChild(.linkDescription) | 860 | let description = node.firstChild(.linkDescription) |
| 861 | guard let path else { | 861 | guard let path else { |
| 862 | // A radio link goes to its target. | 862 | // A radio link goes to its target. |
Sources/OrgCore/Export/LaTeXExport.swift +85 −7
| @@ -64,7 +64,15 @@ public enum LaTeXExport { | |||
| 64 | if included.contains("{{{") { | 64 | if included.contains("{{{") { |
| 65 | if included.contains(marker) { throw LaTeXExport.Unsupported(reason: "U+E000 in the text") } | 65 | if included.contains(marker) { throw LaTeXExport.Unsupported(reason: "U+E000 in the text") } |
| 66 | let ns = included as NSString | 66 | let ns = included as NSString |
| 67 | let kept = OrgParser.parse(included, defaults: settings).root.descendants() | 67 | let nodes = OrgParser.parse(included, defaults: settings).root.descendants() |
| 68 | // A macro a plain link runs on into is part of the link in Org, not expanded. | ||
| 69 | let links = ASCIIRenderer.plainLink.matches(in: included, range: NSRange(location: 0, length: ns.length)).map(\.range) | ||
| 70 | for macro in nodes where macro.kind == .macro { | ||
| 71 | if links.contains(where: { $0.location < macro.range.lowerBound && NSMaxRange($0) > macro.range.lowerBound }) { | ||
| 72 | throw LaTeXExport.Unsupported(reason: "Plain links running into other objects") | ||
| 73 | } | ||
| 74 | } | ||
| 75 | let kept = nodes | ||
| 68 | .filter { [.verbatim, .code, .block, .fixedWidth, .comment, .latexEnvironment, .exportSnippet].contains($0.kind) }.map(\.range) | 76 | .filter { [.verbatim, .code, .block, .fixedWidth, .comment, .latexEnvironment, .exportSnippet].contains($0.kind) }.map(\.range) |
| 69 | var out = "" | 77 | var out = "" |
| 70 | var at = 0 | 78 | var at = 0 |
| @@ -1293,10 +1301,13 @@ final class LaTeXRenderer { | |||
| 1293 | } | 1301 | } |
| 1294 | 1302 | ||
| 1295 | private var inTitle = false | 1303 | private var inTitle = false |
| 1304 | /// Whether text is a link's description, where Org finds no plain links. | ||
| 1305 | private var inDescription = false | ||
| 1296 | 1306 | ||
| 1297 | /// `org-export-data` over objects: math objects merged (`org-latex--wrap-latex-math-block`), | 1307 | /// `org-export-data` over objects: math objects merged (`org-latex--wrap-latex-math-block`), |
| 1298 | /// objects that export nothing taking their blanks with them unless they are needed. | 1308 | /// objects that export nothing taking their blanks with them unless they are needed. |
| 1299 | func transcode(_ items: [Inline]) throws -> String { | 1309 | func transcode(_ items: [Inline]) throws -> String { |
| 1310 | if !inDescription { try refusePlainLinksIntoObjects(items) } | ||
| 1300 | var out = "" | 1311 | var out = "" |
| 1301 | var index = 0 | 1312 | var index = 0 |
| 1302 | var dropBlanks = false | 1313 | var dropBlanks = false |
| @@ -1314,7 +1325,7 @@ final class LaTeXRenderer { | |||
| 1314 | } | 1325 | } |
| 1315 | dropBlanks = false | 1326 | dropBlanks = false |
| 1316 | afterObject = false | 1327 | afterObject = false |
| 1317 | out += plainText(t) | 1328 | out += try plainTextAndLinks(t) |
| 1318 | index += 1 | 1329 | index += 1 |
| 1319 | case .object(let node, let children): | 1330 | case .object(let node, let children): |
| 1320 | dropBlanks = false | 1331 | dropBlanks = false |
| @@ -1525,7 +1536,7 @@ final class LaTeXRenderer { | |||
| 1525 | // MARK: Links | 1536 | // MARK: Links |
| 1526 | 1537 | ||
| 1527 | private func linkTarget(_ node: SyntaxNode) -> (type: String, path: String) { | 1538 | private func linkTarget(_ node: SyntaxNode) -> (type: String, path: String) { |
| 1528 | var raw = node.tokens.first { $0.kind == .linkPath }?.text ?? node.text | 1539 | var raw = node.linkPath ?? node.text |
| 1529 | if node.tokens.first(where: { $0.kind == .linkPath }) == nil, raw.hasPrefix("<"), raw.hasSuffix(">") { | 1540 | if node.tokens.first(where: { $0.kind == .linkPath }) == nil, raw.hasPrefix("<"), raw.hasSuffix(">") { |
| 1530 | raw = String(raw.dropFirst().dropLast()) | 1541 | raw = String(raw.dropFirst().dropLast()) |
| 1531 | } | 1542 | } |
| @@ -1569,7 +1580,11 @@ final class LaTeXRenderer { | |||
| 1569 | 1580 | ||
| 1570 | private func link(_ node: SyntaxNode, _ children: [Inline]) throws -> String? { | 1581 | private func link(_ node: SyntaxNode, _ children: [Inline]) throws -> String? { |
| 1571 | let (type, path) = linkTarget(node) | 1582 | let (type, path) = linkTarget(node) |
| 1583 | let saved = inDescription | ||
| 1584 | inDescription = true | ||
| 1585 | defer { inDescription = saved } | ||
| 1572 | var description: String? = node.firstChild(.linkDescription) == nil ? nil : try transcode(children) | 1586 | var description: String? = node.firstChild(.linkDescription) == nil ? nil : try transcode(children) |
| 1587 | inDescription = saved | ||
| 1573 | if description == "" { description = nil } | 1588 | if description == "" { description = nil } |
| 1574 | if node.firstChild(.linkDescription)?.descendants().contains(where: { $0.kind == .link }) == true { | 1589 | if node.firstChild(.linkDescription)?.descendants().contains(where: { $0.kind == .link }) == true { |
| 1575 | throw Unsupported(reason: "Links in link descriptions") | 1590 | throw Unsupported(reason: "Links in link descriptions") |
| @@ -1604,11 +1619,9 @@ final class LaTeXRenderer { | |||
| 1604 | if let range = format.range(of: "(\(path))") { format.replaceSubrange(range, with: "%s") } | 1619 | if let range = format.range(of: "(\(path))") { format.replaceSubrange(range, with: "%s") } |
| 1605 | return format.replacingOccurrences(of: "%s", with: resolved).replacingOccurrences(of: "%%", with: "%") | 1620 | return format.replacingOccurrences(of: "%s", with: resolved).replacingOccurrences(of: "%%", with: "%") |
| 1606 | case "file": | 1621 | case "file": |
| 1607 | if path.hasPrefix("~") { throw Unsupported(reason: "File links under ~") } | 1622 | let protected = Self.protect(try fileURI(path)) |
| 1608 | let uri = path.hasPrefix("//") ? "file:" + path : path.hasPrefix("/") ? "file://" + path : path | ||
| 1609 | let protected = Self.protect(uri) | ||
| 1610 | return description.map { "\\href{\(protected)}{\($0)}" } ?? "\\url{\(protected)}" | 1623 | return description.map { "\\href{\(protected)}{\($0)}" } ?? "\\url{\(protected)}" |
| 1611 | case "http", "https", "ftp", "mailto", "news": | 1624 | case "http", "https", "ftp", "mailto", "news", "elisp", "shell", "help", "shortdoc": |
| 1612 | let protected = Self.protect(type + ":" + path) | 1625 | let protected = Self.protect(type + ":" + path) |
| 1613 | return description.map { "\\href{\(protected)}{\($0)}" } ?? "\\url{\(protected)}" | 1626 | return description.map { "\\href{\(protected)}{\($0)}" } ?? "\\url{\(protected)}" |
| 1614 | default: | 1627 | default: |
| @@ -1616,6 +1629,71 @@ final class LaTeXRenderer { | |||
| 1616 | } | 1629 | } |
| 1617 | } | 1630 | } |
| 1618 | 1631 | ||
| 1632 | private func fileURI(_ path: String) throws -> String { | ||
| 1633 | if path.hasPrefix("~") { throw Unsupported(reason: "File links under ~") } | ||
| 1634 | return path.hasPrefix("//") ? "file:" + path : path.hasPrefix("/") ? "file://" + path : path | ||
| 1635 | } | ||
| 1636 | |||
| 1637 | /// Org matches `org-link-plain-re` on the source, so a plain link the parser leaves as text | ||
| 1638 | /// can run on into what the parser reads as an object after it, such as `shell:a_b`. | ||
| 1639 | private func refusePlainLinksIntoObjects(_ items: [Inline]) throws { | ||
| 1640 | var raw = "" | ||
| 1641 | var textRuns: [(start: Int, end: Int)] = [] | ||
| 1642 | for item in items { | ||
| 1643 | let start = (raw as NSString).length | ||
| 1644 | switch item { | ||
| 1645 | case .text(let t): | ||
| 1646 | raw += t | ||
| 1647 | textRuns.append((start, (raw as NSString).length)) | ||
| 1648 | case .object(let node, _): | ||
| 1649 | raw += node.text | ||
| 1650 | } | ||
| 1651 | } | ||
| 1652 | guard textRuns.count < items.count else { return } | ||
| 1653 | for m in ASCIIRenderer.plainLink.matches(in: raw, range: NSRange(location: 0, length: (raw as NSString).length)) { | ||
| 1654 | if let text = textRuns.first(where: { $0.start <= m.range.location && m.range.location < $0.end }), NSMaxRange(m.range) > text.end { | ||
| 1655 | throw Unsupported(reason: "Plain links running into other objects") | ||
| 1656 | } | ||
| 1657 | } | ||
| 1658 | } | ||
| 1659 | |||
| 1660 | /// Text with the plain links the parser leaves in it, `shell:ls` and the like, written as Emacs does. | ||
| 1661 | private func plainTextAndLinks(_ t: String) throws -> String { | ||
| 1662 | guard !inDescription else { return plainText(t) } | ||
| 1663 | let ns = t as NSString | ||
| 1664 | var out = "" | ||
| 1665 | var last = 0 | ||
| 1666 | for m in ASCIIRenderer.plainLink.matches(in: t, range: NSRange(location: 0, length: ns.length)) { | ||
| 1667 | let type = ns.substring(with: m.range(at: 1)) | ||
| 1668 | var path = ns.substring(with: NSRange(location: NSMaxRange(m.range(at: 1)) + 1, length: NSMaxRange(m.range) - NSMaxRange(m.range(at: 1)) - 1)) | ||
| 1669 | var before = ns.substring(with: NSRange(location: last, length: m.range.location - last)) | ||
| 1670 | if last > 0 { | ||
| 1671 | // The previous link's post-blank, which Emacs writes as spaces. | ||
| 1672 | let blanks = before.prefix { $0 == " " || $0 == "\t" } | ||
| 1673 | before = String(repeating: " ", count: blanks.count) + before.dropFirst(blanks.count) | ||
| 1674 | } | ||
| 1675 | out += plainText(before) | ||
| 1676 | if type.lowercased() == "file", !path.hasPrefix("//"), let range = path.range(of: "::") { path = String(path[..<range.lowerBound]) } | ||
| 1677 | let ext = (path as NSString).pathExtension.lowercased() | ||
| 1678 | switch type.lowercased() { | ||
| 1679 | case "file": | ||
| 1680 | if ["pdf", "jpeg", "jpg", "png", "ps", "eps", "tikz", "pgf", "svg"].contains(ext) { throw Unsupported(reason: "Plain image links") } | ||
| 1681 | out += "\\url{\(Self.protect(try fileURI(path)))}" | ||
| 1682 | case "http", "https", "ftp", "mailto", "news", "elisp", "shell", "help", "shortdoc": | ||
| 1683 | if ["jpeg", "jpg", "png", "ps", "eps", "tikz", "pgf", "svg"].contains(ext) { throw Unsupported(reason: "Plain image links") } | ||
| 1684 | out += "\\url{\(Self.protect(type + ":" + path))}" | ||
| 1685 | default: | ||
| 1686 | throw Unsupported(reason: "Plain links of type \(type)") | ||
| 1687 | } | ||
| 1688 | last = NSMaxRange(m.range) | ||
| 1689 | } | ||
| 1690 | guard last > 0 else { return plainText(t) } | ||
| 1691 | var rest = ns.substring(from: last) | ||
| 1692 | let blanks = rest.prefix { $0 == " " || $0 == "\t" } | ||
| 1693 | rest = String(repeating: " ", count: blanks.count) + rest.dropFirst(blanks.count) | ||
| 1694 | return out + plainText(rest) | ||
| 1695 | } | ||
| 1696 | |||
| 1619 | /// Words of a fuzzy link or a title, as `org-export-search-cells` compares them. | 1697 | /// Words of a fuzzy link or a title, as `org-export-search-cells` compares them. |
| 1620 | static func words(_ s: String) -> [String] { | 1698 | static func words(_ s: String) -> [String] { |
| 1621 | s.uppercased().split(whereSeparator: \.isWhitespace).map(String.init) | 1699 | s.uppercased().split(whereSeparator: \.isWhitespace).map(String.init) |
Sources/OrgCore/Export/MarkdownExport.swift +1 −1
| @@ -358,7 +358,7 @@ final class MarkdownRenderer { | |||
| 358 | } | 358 | } |
| 359 | 359 | ||
| 360 | private func link(_ node: SyntaxNode) -> String { | 360 | private func link(_ node: SyntaxNode) -> String { |
| 361 | guard let path = node.tokens.first(where: { $0.kind == .linkPath })?.text else { | 361 | guard let path = node.linkPath else { |
| 362 | // A radio link is its text. | 362 | // A radio link is its text. |
| 363 | if !node.text.hasPrefix("<"), !node.text.contains(":") { return node.text } | 363 | if !node.text.hasPrefix("<"), !node.text.contains(":") { return node.text } |
| 364 | var target = node.text | 364 | var target = node.text |
Sources/OrgCore/Export/ODTExport.swift +1 −1
| @@ -1026,7 +1026,7 @@ final class ODTRenderer { | |||
| 1026 | 1026 | ||
| 1027 | /// A bracket link's path, or a plain or angle `file:` link's. | 1027 | /// A bracket link's path, or a plain or angle `file:` link's. |
| 1028 | static func linkPath(_ node: SyntaxNode) -> String? { | 1028 | static func linkPath(_ node: SyntaxNode) -> String? { |
| 1029 | if let token = node.tokens.first(where: { $0.kind == .linkPath }) { return token.text } | 1029 | if let path = node.linkPath { return path } |
| 1030 | var text = node.text | 1030 | var text = node.text |
| 1031 | if text.hasPrefix("<"), text.hasSuffix(">") { text = String(text.dropFirst().dropLast()) } | 1031 | if text.hasPrefix("<"), text.hasSuffix(">") { text = String(text.dropFirst().dropLast()) } |
| 1032 | return text.hasPrefix("file:") ? text : nil | 1032 | return text.hasPrefix("file:") ? text : nil |
Sources/OrgCore/Links/Links.swift +2 −2
| @@ -136,8 +136,8 @@ public enum Links { | |||
| 136 | guard let node = found else { return nil } | 136 | guard let node = found else { return nil } |
| 137 | let whole = node.text | 137 | let whole = node.text |
| 138 | var link: OrgLink | 138 | var link: OrgLink |
| 139 | if whole.hasPrefix("[["), let path = node.tokens.first(where: { $0.kind == .linkPath }) { | 139 | if whole.hasPrefix("[["), let path = node.linkPath { |
| 140 | link = parse(bracket: path.text, abbreviations: abbreviations(in: tree.settings.setupKeywords + text)) | 140 | link = parse(bracket: path, abbreviations: abbreviations(in: tree.settings.setupKeywords + text)) |
| 141 | } else if whole.hasPrefix("<") && whole.hasSuffix(">") { | 141 | } else if whole.hasPrefix("<") && whole.hasSuffix(">") { |
| 142 | link = parse(plain: String(whole.dropFirst().dropLast())) | 142 | link = parse(plain: String(whole.dropFirst().dropLast())) |
| 143 | } else if tree.settings.radioTargets.contains(where: { words($0) == words(whole) }) { | 143 | } else if tree.settings.radioTargets.contains(where: { words($0) == words(whole) }) { |
Sources/OrgCore/Parser/Inline.swift +5 −13
| @@ -304,7 +304,7 @@ struct InlineScanner { | |||
| 304 | guard i + 1 < limit, chars[i + 1] == "[" else { return nil } | 304 | guard i + 1 < limit, chars[i + 1] == "[" else { return nil } |
| 305 | var j = i + 2 | 305 | var j = i + 2 |
| 306 | while j < limit, chars[j] != "]" { | 306 | while j < limit, chars[j] != "]" { |
| 307 | if chars[j] == "[" || isNewline(chars[j]) { return nil } | 307 | if chars[j] == "[" { return nil } |
| 308 | if chars[j] == "\\", j + 1 < limit { j += 1 } | 308 | if chars[j] == "\\", j + 1 < limit { j += 1 } |
| 309 | j += 1 | 309 | j += 1 |
| 310 | } | 310 | } |
| @@ -312,19 +312,11 @@ struct InlineScanner { | |||
| 312 | let path = (i + 2)..<j | 312 | let path = (i + 2)..<j |
| 313 | if chars[j + 1] == "]" { return .link(path: path, description: nil, whole: i..<(j + 2)) } | 313 | if chars[j + 1] == "]" { return .link(path: path, description: nil, whole: i..<(j + 2)) } |
| 314 | guard chars[j + 1] == "[" else { return nil } | 314 | guard chars[j + 1] == "[" else { return nil } |
| 315 | // `org-link-bracket-re`: the description is at least one character, up to the first `]]`. | ||
| 315 | let descriptionStart = j + 2 | 316 | let descriptionStart = j + 2 |
| 316 | var depth = 0 | 317 | var k = descriptionStart + 1 |
| 317 | var k = descriptionStart | 318 | while k + 1 < limit, !(chars[k] == "]" && chars[k + 1] == "]") { k += 1 } |
| 318 | while k < limit { | 319 | guard k + 1 < limit else { return nil } |
| 319 | if chars[k] == "[" { | ||
| 320 | depth += 1 | ||
| 321 | } else if chars[k] == "]" { | ||
| 322 | if depth == 0 { break } | ||
| 323 | depth -= 1 | ||
| 324 | } | ||
| 325 | k += 1 | ||
| 326 | } | ||
| 327 | guard k > descriptionStart, k + 1 < limit, chars[k + 1] == "]" else { return nil } | ||
| 328 | return .link(path: path, description: descriptionStart..<k, whole: i..<(k + 2)) | 320 | return .link(path: path, description: descriptionStart..<k, whole: i..<(k + 2)) |
| 329 | } | 321 | } |
| 330 | 322 | ||
Sources/OrgCore/Semantic/DocumentModel.swift +1 −1
| @@ -202,7 +202,7 @@ public struct DocumentModel: Sendable { | |||
| 202 | } | 202 | } |
| 203 | 203 | ||
| 204 | private func linkTarget(_ node: SyntaxNode) -> String { | 204 | private func linkTarget(_ node: SyntaxNode) -> String { |
| 205 | if let path = node.tokens.first(where: { $0.kind == .linkPath }) { return path.text } | 205 | if let path = node.linkPath { return path } |
| 206 | let text = node.text | 206 | let text = node.text |
| 207 | if text.hasPrefix("<"), text.hasSuffix(">") { return String(text.dropFirst().dropLast()) } | 207 | if text.hasPrefix("<"), text.hasSuffix(">") { return String(text.dropFirst().dropLast()) } |
| 208 | return text | 208 | return text |
Sources/OrgCore/Syntax/SyntaxNode.swift +6
| @@ -103,6 +103,12 @@ public final class SyntaxNode: Sendable { | |||
| 103 | public func descendants() -> [SyntaxNode] { | 103 | public func descendants() -> [SyntaxNode] { |
| 104 | [self] + children.flatMap { $0.descendants() } | 104 | [self] + children.flatMap { $0.descendants() } |
| 105 | } | 105 | } |
| 106 | |||
| 107 | /// A bracket link's path as `org-element-link-parser` reads it: each run of blanks | ||
| 108 | /// holding a newline made one space. | ||
| 109 | var linkPath: String? { | ||
| 110 | tokens.first { $0.kind == .linkPath }.map { $0.text.replacing(/[ \t]*\r?\n[ \t]*/.matchingSemantics(.unicodeScalar), with: " ") } | ||
| 111 | } | ||
| 106 | } | 112 | } |
| 107 | 113 | ||
| 108 | public struct SyntaxToken: Sendable, Equatable { | 114 | public struct SyntaxToken: Sendable, Equatable { |
Tests/OrgCoreTests/InlineTests.swift +15
| @@ -46,6 +46,21 @@ struct InlineTests { | |||
| 46 | #expect(link.children.map(\.kind) == [.linkDescription]) | 46 | #expect(link.children.map(\.kind) == [.linkDescription]) |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | /// `org-element-link-parser`: a path may run onto the next line, read with its line break as a space. | ||
| 50 | @Test func linkPathsSpanLines() { | ||
| 51 | let links = OrgParser.parse("[[elisp:(+ 1\n 2)]] and [[a \n\tb][c]]\n\n[[x\n\ny]]\n").root.descendants().filter { $0.kind == .link } | ||
| 52 | #expect(links.map(\.linkPath) == ["elisp:(+ 1 2)", "a b"]) | ||
| 53 | let crlf = OrgParser.parse("[[elisp:(+ 1\r\n 2)]] ok\r\n").root.descendants().filter { $0.kind == .link } | ||
| 54 | #expect(crlf.map(\.linkPath) == ["elisp:(+ 1 2)"]) | ||
| 55 | } | ||
| 56 | |||
| 57 | /// `org-link-bracket-re`: a description runs to the first `]]`, brackets in it balanced or not. | ||
| 58 | @Test func linkDescriptionsEndAtFirstDoubleBracket() { | ||
| 59 | let text = "[[a][x]b]] [[a][x [y]] [[a][]x]] [[a][[z]]]\n" | ||
| 60 | let descriptions = OrgParser.parse(text).root.descendants().filter { $0.kind == .linkDescription }.map(\.text) | ||
| 61 | #expect(descriptions == ["x]b", "x [y", "]x", "[z"]) | ||
| 62 | } | ||
| 63 | |||
| 49 | @Test func noLinksInsideLinkDescriptions() { | 64 | @Test func noLinksInsideLinkDescriptions() { |
| 50 | let link = OrgParser.parse("[[a][see https://b.c]]\n").root.descendants().first { $0.kind == .link }! | 65 | let link = OrgParser.parse("[[a][see https://b.c]]\n").root.descendants().first { $0.kind == .link }! |
| 51 | #expect(link.descendants().filter { $0.kind == .link }.count == 1) | 66 | #expect(link.descendants().filter { $0.kind == .link }.count == 1) |
Tests/OrgCoreTests/LaTeXExportTests.swift +23
| @@ -106,6 +106,14 @@ struct LaTeXExportTests { | |||
| 106 | "Snippets @@latex:\\newpage@@ and @@html:<br>@@ gone. Also @@html:x@@ @@html:y@@ end.\n@@html:z@@ start\n", | 106 | "Snippets @@latex:\\newpage@@ and @@html:<br>@@ gone. Also @@html:x@@ @@html:y@@ end.\n@@html:z@@ start\n", |
| 107 | "Line one\\\\\nline two \\\\\n\\\\\nafter\n", | 107 | "Line one\\\\\nline two \\\\\n\\\\\nafter\n", |
| 108 | "Links: [[https://orgmode.org][Org & co]] <https://a.b/c_d> https://x.y/%20 [[mailto:me@x.org]] [[file:doc.pdf][the doc]] [[./rel/file.txt]] [[file:/abs/path.org]]\n", | 108 | "Links: [[https://orgmode.org][Org & co]] <https://a.b/c_d> https://x.y/%20 [[mailto:me@x.org]] [[file:doc.pdf][the doc]] [[./rel/file.txt]] [[file:/abs/path.org]]\n", |
| 109 | "Run [[elisp:(+ 1 2)][add]] and [[elisp:(message \"50% #x\")]], [[shell:ls ~/x_y][list]] [[shell:echo $HOME]], [[help:car][car]] [[shortdoc:string]].\n", | ||
| 110 | "Plain shell:ls and elisp:foo and help:car here.\nGet ftp://x.org/f now, news:comp.x and mailto:a@b.c, shortdoc:string HTTPS://a.b/c.\nshell:echo_$x% and shell:ls\tx shell:ls[x elisp:(foo) FILE:a.txt File:b.org::s FILE:/abs FILE://h/p.\n[[https://a.b][see shell:ls]] *shell:ls* and\n| news:x |\n", | ||
| 111 | "* Heading shell:ls\nx\n", | ||
| 112 | "[[https://a.b][[x] y]] and [[https://a.b][x [y] z]] after.\n", | ||
| 113 | "A [[https://a.b][a]b]] z [[https://a.b][x [y]] z [[https://a.b][=v]=]] z [[https://a.b][]x]] z [[https://a.b][a\\]b]] z.\n", | ||
| 114 | "[[elisp:foo][[[file:a.png]]]]\n\n[[https://a.b][[[x]] y]]\n\n[[https://a.b][[x]]]\n", | ||
| 115 | "See FILE:a.org::foo.png here.\n", | ||
| 116 | "Run [[elisp:(+ 1\n 2)]] and see [[https://a.b/c\n d]] ok, [[shell:a \n\tb][two\n lines]] and [[elisp:a\tb\n c]].\n", | ||
| 109 | "* Target section\nA <<anchor>> here and [[anchor]] and [[anchor][desc]].\n* Other\nSee [[*Target section]] and [[Target section][named]] and [[#cid]].\n* Third\n:PROPERTIES:\n:CUSTOM_ID: cid\n:END:\nx\n", | 117 | "* Target section\nA <<anchor>> here and [[anchor]] and [[anchor][desc]].\n* Other\nSee [[*Target section]] and [[Target section][named]] and [[#cid]].\n* Third\n:PROPERTIES:\n:CUSTOM_ID: cid\n:END:\nx\n", |
| 110 | "#+OPTIONS: num:nil\n* Head\nSee [[*Head]].\n", | 118 | "#+OPTIONS: num:nil\n* Head\nSee [[*Head]].\n", |
| 111 | "Footnotes[fn:1] twice[fn:1] and[fn:2][fn:3] and inline[fn::anon /x/] and[fn:named:inline def].\n\n[fn:1] First.\n\n[fn:2] Second\n with two lines.\n\n[fn:3] Third.\n", | 119 | "Footnotes[fn:1] twice[fn:1] and[fn:2][fn:3] and inline[fn::anon /x/] and[fn:named:inline def].\n\n[fn:1] First.\n\n[fn:2] Second\n with two lines.\n\n[fn:3] Third.\n", |
| @@ -321,6 +329,12 @@ struct LaTeXExportTests { | |||
| 321 | } | 329 | } |
| 322 | } | 330 | } |
| 323 | 331 | ||
| 332 | /// A bracket link path broken across a CRLF line end is read as Emacs reads it once it decodes the file. | ||
| 333 | @Test func linkPathsAcrossCRLF() throws { | ||
| 334 | let body = try LaTeXExport.document("Text [[elisp:(+ 1\r\n 2)]] and [[https://a.b/c\r\n d]] ok.\r\n", bodyOnly: true) | ||
| 335 | #expect(body.hasPrefix("Text \\url{elisp:(+ 1 2)} and \\url{https://a.b/c d} ok.")) | ||
| 336 | } | ||
| 337 | |||
| 324 | /// What Emacs exports differently or refuses is refused, naming it. | 338 | /// What Emacs exports differently or refuses is refused, naming it. |
| 325 | @Test func refusesWhatItCannotMatch() { | 339 | @Test func refusesWhatItCannotMatch() { |
| 326 | func reason(_ org: String) -> String? { | 340 | func reason(_ org: String) -> String? { |
| @@ -350,6 +364,15 @@ struct LaTeXExportTests { | |||
| 350 | let pre = "* Top\n:PROPERTIES:\n:EXPORT_LATEX_CLASS_PRE: \\x\n:END:\nBody.\n" | 364 | let pre = "* Top\n:PROPERTIES:\n:EXPORT_LATEX_CLASS_PRE: \\x\n:END:\nBody.\n" |
| 351 | #expect(ExportScope.subtree(pre, at: 0).flatMap(reason) == "#+LATEX_CLASS_PRE") | 365 | #expect(ExportScope.subtree(pre, at: 0).flatMap(reason) == "#+LATEX_CLASS_PRE") |
| 352 | #expect(reason("#+OPTIONS: tags:not-in-toc\n* 100% done :a:\n")?.hasPrefix("% in a heading") == true) | 366 | #expect(reason("#+OPTIONS: tags:not-in-toc\n* 100% done :a:\n")?.hasPrefix("% in a heading") == true) |
| 367 | for org in ["shell:ls^2 x\n", "x shell:a_b\n", "ftp://a/b_c\n", "elisp:x^{2}\n", "help:a_{b}\n", "shell:a\\alpha\n", "shell:a$x$ y\n", | ||
| 368 | "shell:a@@latex:Z@@\n", "shell:a{{{date}}}\n", "shell:ls[fn:1] x\n\n[fn:1] d\n", "*shell:ab_c*\n"] { | ||
| 369 | #expect(reason(org) == "Plain links running into other objects", "\(org)") | ||
| 370 | } | ||
| 371 | #expect(reason("See id:abc.\n") == "Plain links of type id") | ||
| 372 | #expect(reason("See info:emacs here.\n") == "Plain links of type info") | ||
| 373 | #expect(reason("See FILE:a.png here.\n") == "Plain image links") | ||
| 374 | #expect(reason("See FILE:a.png::x here.\n") == "Plain image links") | ||
| 375 | #expect(reason("See FILE:~/a here.\n") == "File links under ~") | ||
| 353 | } | 376 | } |
| 354 | 377 | ||
| 355 | @Test func labelsStayTheSame() throws { | 378 | @Test func labelsStayTheSame() throws { |
docs/manual/guide/12-export.org +1 −1
| @@ -236,7 +236,7 @@ It reads: | |||
| 236 | 236 | ||
| 237 | =#+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. | 237 | =#+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. |
| 238 | 238 | ||
| 239 | 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/). | 239 | 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, plain (unbracketed) links of types it doesn't write, such as =id:= and =info:=, plain links to images, a plain link that runs on into markup such as =shell:a_b=, 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/). |
| 240 | 240 | ||
| 241 | * Word, RTF and plain text | 241 | * Word, RTF and plain text |
| 242 | 242 | ||