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