Commit dc4642653b
Verified · cmc
Layout: unified · split
Sources/OrgCore/Export/LaTeXExport.swift +25 −33
| @@ -504,13 +504,8 @@ final class LaTeXRenderer { | |||
| 504 | let subsections = children.filter { $0.kind == .section } | 504 | let subsections = children.filter { $0.kind == .section } |
| 505 | var contents: String? | 505 | var contents: String? |
| 506 | if export == .all { | 506 | if export == .all { |
| 507 | var out = Self.normalize(try elements(body, end: subsections.first?.range.lowerBound ?? node.range.upperBound)) | 507 | let out = Self.normalize(try elements(body, end: subsections.first?.range.lowerBound ?? node.range.upperBound)) |
| 508 | let exported = try subsections.compactMap { try section($0) } | 508 | contents = Self.normalize(out + (try subsections.compactMap { try section($0) }.joined())) |
| 509 | for (index, sub) in exported.enumerated() { | ||
| 510 | out += sub | ||
| 511 | _ = index | ||
| 512 | } | ||
| 513 | contents = Self.normalize(out) | ||
| 514 | } | 509 | } |
| 515 | let first = children.first?.range.lowerBound ?? node.range.upperBound | 510 | let first = children.first?.range.lowerBound ?? node.range.upperBound |
| 516 | let preBlank = children.isEmpty ? 0 : blankLines(from: heading.range.lowerBound, to: first) | 511 | let preBlank = children.isEmpty ? 0 : blankLines(from: heading.range.lowerBound, to: first) |
| @@ -566,19 +561,10 @@ final class LaTeXRenderer { | |||
| 566 | /// headline opens its list when it is the first and closes it when it is the last. | 561 | /// headline opens its list when it is the first and closes it when it is the last. |
| 567 | private func exportedSiblings(_ section: SyntaxNode) -> [Int] { | 562 | private func exportedSiblings(_ section: SyntaxNode) -> [Int] { |
| 568 | guard let parent = section.parent else { return [section.range.lowerBound] } | 563 | guard let parent = section.parent else { return [section.range.lowerBound] } |
| 569 | // A section with a body before it means the headline isn't first. | 564 | return parent.children.filter { child in |
| 570 | var starts: [Int] = [] | 565 | guard child.kind == .section, let heading = child.firstChild(.heading) else { return false } |
| 571 | var bodyBefore = false | 566 | return HTMLRenderer.sectionExport(heading, options: options, selected: selected) != .nothing |
| 572 | for child in parent.children where child.kind != .heading { | 567 | }.map(\.range.lowerBound) |
| 573 | if child.kind != .section { | ||
| 574 | if starts.isEmpty { bodyBefore = true } | ||
| 575 | continue | ||
| 576 | } | ||
| 577 | guard let heading = child.firstChild(.heading), HTMLRenderer.sectionExport(heading, options: options, selected: selected) != .nothing else { continue } | ||
| 578 | starts.append(child.range.lowerBound) | ||
| 579 | } | ||
| 580 | _ = bodyBefore | ||
| 581 | return starts | ||
| 582 | } | 568 | } |
| 583 | 569 | ||
| 584 | // MARK: - Elements | 570 | // MARK: - Elements |
| @@ -719,8 +705,6 @@ final class LaTeXRenderer { | |||
| 719 | return try wrapLabel(affiliated, "\\begin{\(type)}\(affiliated.value("options") ?? "")\n\(Self.normalize(contents))\\end{\(type)}") | 705 | return try wrapLabel(affiliated, "\\begin{\(type)}\(affiliated.value("options") ?? "")\n\(Self.normalize(contents))\\end{\(type)}") |
| 720 | } | 706 | } |
| 721 | 707 | ||
| 722 | private var listDepth = 0 | ||
| 723 | |||
| 724 | /// `org-latex-item`. | 708 | /// `org-latex-item`. |
| 725 | private func item(_ item: SyntaxNode, ordered: Bool, depth: Int) throws -> String { | 709 | private func item(_ item: SyntaxNode, ordered: Bool, depth: Int) throws -> String { |
| 726 | var counter = "" | 710 | var counter = "" |
| @@ -962,13 +946,14 @@ final class LaTeXRenderer { | |||
| 962 | /// `org-export-format-code-default` for a src or example block's lines. | 946 | /// `org-export-format-code-default` for a src or example block's lines. |
| 963 | private func formatCode(_ lines: [String]) throws -> String { | 947 | private func formatCode(_ lines: [String]) throws -> String { |
| 964 | guard let parsed = Coderef.block(lines) else { return "" } | 948 | guard let parsed = Coderef.block(lines) else { return "" } |
| 949 | let blockLoc = try blockLoc(lines, numbered: parsed.switches.numberLines != nil) | ||
| 965 | var value = parsed.body.joined(separator: "\n") | 950 | var value = parsed.body.joined(separator: "\n") |
| 966 | if !value.isEmpty { value += "\n" } | 951 | if !value.isEmpty { value += "\n" } |
| 967 | let preserve = lines[0].firstMatch(of: /^[ \t]*#\+(?i:begin_(?:src|example))\b.*\s-i\b/) != nil | 952 | let preserve = lines[0].firstMatch(of: /^[ \t]*#\+(?i:begin_(?:src|example))\b.*\s-i\b/) != nil |
| 968 | var code = preserve ? value : try Self.removeIndentation(value) | 953 | var code = preserve ? value : try Self.removeIndentation(value) |
| 969 | if code.hasSuffix("\n") { code.removeLast() } | 954 | if code.hasSuffix("\n") { code.removeLast() } |
| 970 | let (lines, refs) = Coderef.unravel(code.components(separatedBy: "\n"), format: parsed.switches.labelFormat) | 955 | let (lines, refs) = Coderef.unravel(code.components(separatedBy: "\n"), format: parsed.switches.labelFormat) |
| 971 | let loc = parsed.switches.numberLines == nil ? nil : blockLoc(lines: lines) | 956 | let loc = parsed.switches.numberLines == nil ? nil : blockLoc |
| 972 | guard loc != nil || (parsed.switches.retainLabels && !refs.isEmpty) else { return lines.joined(separator: "\n") + "\n" } | 957 | guard loc != nil || (parsed.switches.retainLabels && !refs.isEmpty) else { return lines.joined(separator: "\n") + "\n" } |
| 973 | let width = loc.map { String($0 + lines.count).count } ?? 0 | 958 | let width = loc.map { String($0 + lines.count).count } ?? 0 |
| 974 | let numberWidth = loc == nil ? 0 : width + 2 | 959 | let numberWidth = loc == nil ? 0 : width + 2 |
| @@ -983,18 +968,25 @@ final class LaTeXRenderer { | |||
| 983 | }.joined(separator: "\n") + "\n" | 968 | }.joined(separator: "\n") + "\n" |
| 984 | } | 969 | } |
| 985 | 970 | ||
| 986 | private var blockLocs: [Int?]? | 971 | /// Each src and example block's `org-export-get-loc`, by the block's text, in order. |
| 987 | private var blockIndex = 0 | 972 | private lazy var blockLocs: [String: [Int?]] = { |
| 973 | let blocks = Coderef.blocks(tree) | ||
| 974 | var locs: [String: [Int?]] = [:] | ||
| 975 | for (lines, loc) in zip(blocks, Coderef.locs(blocks.compactMap(Coderef.block))) { | ||
| 976 | locs[lines.joined(separator: "\n"), default: []].append(loc) | ||
| 977 | } | ||
| 978 | return locs | ||
| 979 | }() | ||
| 988 | 980 | ||
| 989 | /// `org-export-get-loc` of the next numbered block. | 981 | /// `org-export-get-loc` of the block with `lines`, the next of those with the same text. |
| 990 | private func blockLoc(lines: [String]) -> Int? { | 982 | private func blockLoc(_ lines: [String], numbered: Bool) throws -> Int? { |
| 991 | if blockLocs == nil { | 983 | let key = lines.joined(separator: "\n") |
| 992 | let blocks = Coderef.blocks(tree).compactMap(Coderef.block) | 984 | guard let queue = blockLocs[key], !queue.isEmpty else { |
| 993 | blockLocs = Coderef.locs(blocks) | 985 | if numbered { throw Unsupported(reason: "Numbered blocks inside other blocks") } |
| 986 | return nil | ||
| 994 | } | 987 | } |
| 995 | _ = lines | 988 | blockLocs[key] = Array(queue.dropFirst()) |
| 996 | defer { blockIndex += 1 } | 989 | return queue[0] |
| 997 | return blockIndex < blockLocs!.count ? blockLocs![blockIndex] : nil | ||
| 998 | } | 990 | } |
| 999 | 991 | ||
| 1000 | /// `org-latex-verse-block` and the verse rules of `org-latex-plain-text`. | 992 | /// `org-latex-verse-block` and the verse rules of `org-latex-plain-text`. |
Tests/OrgCoreTests/LaTeXExportTests.swift +2
| @@ -154,6 +154,8 @@ struct LaTeXExportTests { | |||
| 154 | "| a | =b\\\\|c= |\n\nThe {{{title}}} macro and ={{{title}}}= as written.\n", | 154 | "| a | =b\\\\|c= |\n\nThe {{{title}}} macro and ={{{title}}}= as written.\n", |
| 155 | "#+begin_src emacs-lisp :exports none\n(hidden)\n#+end_src\n\n#+RESULTS:\n: gone\n\nafter\n\n#+RESULTS: orphan\n: kept\n", | 155 | "#+begin_src emacs-lisp :exports none\n(hidden)\n#+end_src\n\n#+RESULTS:\n: gone\n\nafter\n\n#+RESULTS: orphan\n: kept\n", |
| 156 | "Plain LaTeX's own words and TeXbook.\n", | 156 | "Plain LaTeX's own words and TeXbook.\n", |
| 157 | "#+begin_example\nplain\n#+end_example\n\n#+begin_src sh -n\none\ntwo\n#+end_src\n\n#+begin_example\nplain\n#+end_example\n\n#+begin_src sh +n\nthree\n#+end_src\n", | ||
| 158 | "* Deep lists\n** two\n*** three\n**** four a\n**** COMMENT skipped\n**** four b :noexport:\n**** four c\n", | ||
| 157 | ] | 159 | ] |
| 158 | 160 | ||
| 159 | @Test(.enabled(if: oracle)) | 161 | @Test(.enabled(if: oracle)) |