Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple table headers #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 54 additions & 16 deletions Sources/Ink/Internal/Table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
struct Table: Fragment {
var modifierTarget: Modifier.Target { .tables }

private var header: Row?
private var headers: [Row]? // There can be multiple header rows in a table.
private var rows = [Row]()
private var columnCount = 0
private var columnAlignments = [ColumnAlignment]()
Expand Down Expand Up @@ -37,15 +37,16 @@ struct Table: Fragment {
var html = ""
let render: () -> String = { "<table>\(html)</table>" }

if let header = header {
let rowHTML = self.html(
forRow: header,
cellElementName: "th",
urls: urls,
modifiers: modifiers
)

html.append("<thead>\(rowHTML)</thead>")
if let headers = headers {
for row in headers {
let rowHTML = self.html(
forRow: row,
cellElementName: "th",
urls: urls,
modifiers: modifiers
)
html.append("<thead>\(rowHTML)</thead>")
}
}

guard !rows.isEmpty else {
Expand All @@ -70,7 +71,14 @@ struct Table: Fragment {
}

func plainText() -> String {
var text = header.map(plainText) ?? ""
var text = ""

if let headers = headers {
for row in headers {
if !text.isEmpty { text.append("\n") }
text.append(plainText(forRow: row))
}
}

for row in rows {
if !text.isEmpty { text.append("\n") }
Expand Down Expand Up @@ -107,15 +115,43 @@ private extension Table {
}
}
}

/// This method is to determine where the format row is located in the Table.
/// - Returns: Int? If a value is returned it should be the index for the formatting row.
func findHeaderAlignmentRow() -> Int? {
var result : Int? = nil

let textPredicate = Self.allowedHeaderCharacters.contains

for (index, row) in rows.enumerated() {
if index > 0 {
// see if the current row is all column alignment cells
var goodRow = true
for cell in row {
let text = cell.plainText()

if !text.allSatisfy(textPredicate) {
goodRow = false
break
}
}
if goodRow {
result = index
}
}
}
return result
}

mutating func formHeaderAndColumnAlignmentsIfNeeded() {
guard rows.count > 1 else { return }
guard rows[0].count == rows[1].count else { return }

guard let alignmentRow = findHeaderAlignmentRow(), rows[0].count == rows[alignmentRow].count else { return }

let textPredicate = Self.allowedHeaderCharacters.contains
var alignments = [ColumnAlignment]()

for cell in rows[1] {
for cell in rows[alignmentRow] {
let text = cell.plainText()

guard text.allSatisfy(textPredicate) else {
Expand All @@ -124,10 +160,12 @@ private extension Table {

alignments.append(parseColumnAlignment(from: text))
}

header = rows[0]

var headers = [Row]()
headers.append(contentsOf: rows[0..<alignmentRow])
self.headers = headers
columnAlignments = alignments
rows.removeSubrange(0...1)
rows.removeSubrange(0...alignmentRow)
}

func parseColumnAlignment(from text: String) -> ColumnAlignment {
Expand Down
23 changes: 23 additions & 0 deletions Tests/InkTests/TableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ final class TableTests: XCTestCase {
""")
}

func testTableWithMultipleHeaderRows() {
let html = MarkdownParser().html(from: """
| HeaderA | HeaderB | HeaderC |
| HeaderD | HeaderE | HeaderF |
| ------- | ------- | ------- |
| CellA1 | CellB1 | CellC1 |
| CellA2 | CellB2 | CellC2 |
""")

XCTAssertEqual(html, """
<table>\
<thead><tr><th>HeaderA</th><th>HeaderB</th><th>HeaderC</th></tr></thead>\
<thead><tr><th>HeaderD</th><th>HeaderE</th><th>HeaderF</th></tr></thead>\
<tbody>\
<tr><td>CellA1</td><td>CellB1</td><td>CellC1</td></tr>\
<tr><td>CellA2</td><td>CellB2</td><td>CellC2</td></tr>\
</tbody>\
</table>
""")
}


func testTableWithUnalignedColumns() {
let html = MarkdownParser().html(from: """
| HeaderA | HeaderB | HeaderC |
Expand Down Expand Up @@ -210,6 +232,7 @@ extension TableTests {
return [
("testTableWithoutHeader", testTableWithoutHeader),
("testTableWithHeader", testTableWithHeader),
("testTableWithMultipleHeaderRows", testTableWithMultipleHeaderRows),
("testTableWithUnalignedColumns", testTableWithUnalignedColumns),
("testTableWithOnlyHeader", testTableWithOnlyHeader),
("testIncompleteTable", testIncompleteTable),
Expand Down