Sources/OrgCore/Export/TableEl.swift
69 lines · 3560 bytes
1import Foundation
2
3/// A table.el table's cells, read from its drawing: each cell's text lines and the grid rows
4/// and columns it spans, as `table-generate-source` finds them.
5enum TableEl {
6 struct Cell: Equatable {
7 var row: Int
8 var lines: [String]
9 var colspan: Int
10 var rowspan: Int
11 }
12
13 /// A cell's borders in the drawing: the lines of its top and bottom rules, and the
14 /// columns of its left and right bars.
15 struct Rect: Equatable {
16 var top: Int, left: Int, right: Int, bottom: Int
17 }
18
19 /// The cells' borders in reading order, with the grid rows and columns each spans, or nil
20 /// when the drawing isn't a well-formed table.
21 static func rects(_ grid: [[Character]]) -> [(rect: Rect, colspan: Int, rowspan: Int)]? {
22 guard grid.count >= 3 else { return nil }
23 func at(_ r: Int, _ c: Int) -> Character? { r >= 0 && r < grid.count && c >= 0 && c < grid[r].count ? grid[r][c] : nil }
24 func vertical(_ r: Int, _ c: Int) -> Bool { at(r, c) == "|" || at(r, c) == "+" }
25 func horizontal(_ r: Int, _ c: Int) -> Bool { at(r, c) == "-" || at(r, c) == "+" }
26 // Grid lines: rows holding a horizontal border, columns holding a corner.
27 var rows: [Int] = []
28 var columns: Set<Int> = []
29 for (r, line) in grid.enumerated() {
30 for (c, ch) in line.enumerated() where ch == "+" {
31 columns.insert(c)
32 if horizontal(r, c + 1) || horizontal(r, c - 1) { if rows.last != r { rows.append(r) } }
33 }
34 }
35 var found: [(rect: Rect, colspan: Int, rowspan: Int)] = []
36 for r in rows {
37 for c in grid[r].indices where grid[r][c] == "+" && at(r, c + 1) == "-" && vertical(r + 1, c) {
38 // Right edge: the next corner along the top with a border below it.
39 var x2 = c + 1
40 while x2 < grid[r].count, !(grid[r][x2] == "+" && vertical(r + 1, x2)) { x2 += 1 }
41 guard x2 < grid[r].count else { return nil }
42 // Bottom edge: the next corner down the left side with a border to its right.
43 var r2 = r + 1
44 while r2 < grid.count, !(at(r2, c) == "+" && horizontal(r2, c + 1)) { r2 += 1 }
45 guard r2 < grid.count, r2 > r + 1 else { return nil }
46 found.append((Rect(top: r, left: c, right: x2, bottom: r2), columns.filter { $0 > c && $0 < x2 }.count + 1,
47 rows.filter { $0 > r && $0 < r2 }.count + 1))
48 }
49 }
50 return found.isEmpty ? nil : found
51 }
52
53 /// The cells in reading order, or nil when the drawing isn't a well-formed table.
54 static func cells(_ text: String) -> [Cell]? {
55 let raw = text.components(separatedBy: "\n").filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty }
56 let indent = raw.map { $0.prefix { $0 == " " || $0 == "\t" }.count }.min() ?? 0
57 let grid = raw.map { Array($0.dropFirst(indent)) }
58 guard let rects = rects(grid) else { return nil }
59 return rects.map { found in
60 let rect = found.rect
61 let lines = (rect.top + 1..<rect.bottom).map { line -> String in
62 let row = grid[line]
63 let end = min(rect.right, row.count)
64 return rect.left + 1 < end ? String(row[(rect.left + 1)..<end]).trimmingCharacters(in: .whitespaces) : ""
65 }
66 return Cell(row: rect.top, lines: lines, colspan: found.colspan, rowspan: found.rowspan)
67 }
68 }
69}