| @@ -219,11 +219,9 @@ final class ODTRenderer { |
| 219 | 219 | } |
| 220 | 220 | // `org-odt--enumerate`: numbered within the closest numbered heading at level 2 or |
| 221 | 221 | // above, else through the document. |
| 222 | | var counts: [String: Int] = [:] |
| 223 | 222 | for (index, item) in enumerables.enumerated() { |
| 224 | 223 | let scope = item.ancestors.reversed().first { headings[$0].map { $0.relative <= 2 && $0.number != nil } ?? false } |
| 225 | 224 | let ordinal = enumerables[...index].filter { $0.category == item.category && (scope.map($0.ancestors.contains) ?? true) }.count |
| 226 | | counts[item.category, default: 0] += 1 |
| 227 | 225 | let sequence = (scope.flatMap { headings[$0]?.number }.map { $0.map(String.init).joined(separator: ".") + "." } ?? "") + String(ordinal) |
| 228 | 226 | let named = names.contains { $0.key == item.key } |
| 229 | 227 | let variable = ["Table": "Table", "Figure": "Illustration", "Listing": "Listing"][item.category]! |
| @@ -471,7 +469,7 @@ final class ODTRenderer { |
| 471 | 469 | return code(lines) |
| 472 | 470 | case .tableEl: |
| 473 | 471 | // ox-odt strips table.el tables, the list around them closed and opened again. |
| 474 | | return frames.isEmpty ? "" : "\n" + reopening(nil) |
| 472 | return frames.isEmpty ? "" : "\n" + reopening(nil, for: node) |
| 475 | 473 | default: |
| 476 | 474 | // Comments, property drawers, planning, clocks, footnote definitions, formulas. |
| 477 | 475 | return "" |
| @@ -529,7 +527,7 @@ final class ODTRenderer { |
| 529 | 527 | let parentLow = heading.ancestors.last.flatMap { headings[$0]?.lowLevel } ?? false |
| 530 | 528 | out += "\n<text:list text:style-name=\"\(style)\" text:continue-numbering=\"\(parentLow)\">" |
| 531 | 529 | } |
| 532 | | frames.append(Frame(style: style)) |
| 530 | frames.append(Frame(style: style, heading: true)) |
| 533 | 531 | let body = "\n<text:p text:style-name=\"Text_20_body\">\(extra)\(anchored)</text:p>" + (contents ? node.children.filter { $0.kind != .heading }.map(element).joined() : "") |
| 534 | 532 | let frame = frames.removeLast() |
| 535 | 533 | out += "\n<text:list-item>\n\(body)\n\(frame.header ? "</text:list-header>" : "</text:list-item>")" |
| @@ -596,9 +594,11 @@ final class ODTRenderer { |
| 596 | 594 | |
| 597 | 595 | // MARK: - Lists |
| 598 | 596 | |
| 599 | | /// An open list item, closed and opened again around a table. |
| 597 | /// An open list item, closed and opened again around a table; `heading` for a heading |
| 598 | /// deeper than `H`. |
| 600 | 599 | private struct Frame { |
| 601 | 600 | var style: String |
| 601 | var heading = false |
| 602 | 602 | var header = false |
| 603 | 603 | } |
| 604 | 604 | |
| @@ -664,9 +664,14 @@ final class ODTRenderer { |
| 664 | 664 | return "<text:span text:style-name=\"OrgCode\">\(mark)</text:span>" |
| 665 | 665 | } |
| 666 | 666 | |
| 667 | | /// `org-odt-table`'s list handling: the open lists closed, `inner` in an indented section, |
| 668 | | /// the lists opened again, the innermost item as a header. |
| 669 | | private func reopening(_ inner: String?) -> String { |
| 667 | /// `org-odt-table`'s list handling for `node`: the open lists closed, `inner` in an indented |
| 668 | /// section, the lists opened again, the innermost item as a header. |
| 669 | private func reopening(_ inner: String?, for node: SyntaxNode) -> String { |
| 670 | // ox-odt doesn't close the lists around a table in a block, and writes XML that doesn't |
| 671 | // nest. |
| 672 | if let parent = node.parent, parent.kind != .item, parent.kind != .section, frames.contains(where: { !$0.heading }) { |
| 673 | return fail("ODT export doesn't support a table in a block inside a list") |
| 674 | } |
| 670 | 675 | var closing: [String] = [] |
| 671 | 676 | for frame in frames.reversed() { |
| 672 | 677 | closing.append(frame.header ? "</text:list-header>" : "</text:list-item>") |
| @@ -790,7 +795,7 @@ final class ODTRenderer { |
| 790 | 795 | }.joined(separator: "\n") |
| 791 | 796 | var out = caption.map { "\n<text:p text:style-name=\"Table\">\($0)</text:p>" } ?? "" |
| 792 | 797 | out += "\n<table:table table:style-name=\"\(style)\">" + columnSpecs + "\n" + body + "</table:table>" |
| 793 | | return "\n" + reopening(out) |
| 798 | return "\n" + reopening(out, for: node) |
| 794 | 799 | } |
| 795 | 800 | |
| 796 | 801 | /// `org-odt-format-label` as a definition: a bookmark, then the category, sequence number |
| @@ -879,8 +884,7 @@ final class ODTRenderer { |
| 879 | 884 | |
| 880 | 885 | /// `org-odt-do-format-code`: one paragraph per line, the last one styled apart. |
| 881 | 886 | private func code(_ lines: [String], refs: [Int: String] = [:], retain: Bool = false, loc: Int? = nil, emptyLast: Bool = false) -> String { |
| 882 | | var lines = lines |
| 883 | | if emptyLast { lines.append("") } |
| 887 | let lines = emptyLast ? lines + [""] : lines |
| 884 | 888 | let out = lines.enumerated().map { index, line -> String in |
| 885 | 889 | var text = line |
| 886 | 890 | if let ref = refs[index + 1], retain { text += " (\(ref))" } |