Skip to content

Commit

Permalink
Merge pull request #3 from KristopherGBaker/table-columns-alignments
Browse files Browse the repository at this point in the history
Add support for table columns and alignments.
  • Loading branch information
KristopherGBaker authored Dec 22, 2017
2 parents 4b4609a + 0125fd5 commit 2391751
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Maaku.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Maaku"
s.version = "0.1.3"
s.version = "0.1.4"
s.summary = "Swift cmark wrapper with a Swift friendly representation of the AST"

s.description = <<-DESC
Expand Down
33 changes: 27 additions & 6 deletions Maaku/CMark/CMParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,17 @@ public protocol CMParserDelegate: class {
///
/// - Parameters:
/// - parser: The parser.
func parserDidStartTable(parser: CMParser)
/// - columns: The number of table columns.
/// - alignemnts: The table alignments.
func parser(parser: CMParser, didStartTableWithColumns columns: UInt16, alignments: [String])

/// Sent by the parser object to the delegate when it encounters the end of a table.
///
/// - Parameters:
/// - parser: The parser.
func parserDidEndTable(parser: CMParser)
/// - columns: The number of table columns.
/// - alignemnts: The table alignments.
func parser(parser: CMParser, didEndTableWithColumns columns: UInt16, alignments: [String])

/// Sent by the parser object to the delegate when it encounters the start of a table header.
///
Expand Down Expand Up @@ -578,7 +582,8 @@ public class CMParser {
return
}

_ = handleTable(nodeName, eventType: eventType) || handleStrikethrough(nodeName, eventType: eventType)
_ = handleTable(node, nodeName: nodeName, eventType: eventType) ||
handleStrikethrough(nodeName, eventType: eventType)
}

/// Handles strikethrough extensions for the current node.
Expand Down Expand Up @@ -607,19 +612,35 @@ public class CMParser {
/// Handles table extensions for the current node.
///
/// - Parameters:
/// - node: The current node.
/// - nodeName: The human readable node name.
/// - eventType: The event type.
/// - Returns:
/// true if the node was handled as a table, table header, table row, or table cell, false otherwise.
@discardableResult
private func handleTable(_ nodeName: String, eventType: CMEventType) -> Bool {
private func handleTable(_ node: CMNode, nodeName: String, eventType: CMEventType) -> Bool {
switch nodeName {
case CMExtensionName.table.rawValue:
let columns = cmarkextensions_get_table_columns(node.cmarkNode)
var alignments = cmarkextensions_get_table_alignments(node.cmarkNode)

var align: [String] = []

for i in 0..<columns {
if let val = alignments?.pointee, let str = String(bytes: [val], encoding: .utf8) {
align.append(str)
}

if i < (columns - 1) {
alignments = alignments?.successor()
}
}

if eventType == .enter {
delegate?.parserDidStartTable(parser: self)
delegate?.parser(parser: self, didStartTableWithColumns: columns, alignments: align)
}
else {
delegate?.parserDidEndTable(parser: self)
delegate?.parser(parser: self, didEndTableWithColumns: columns, alignments: align)
}
case CMExtensionName.tableHeader.rawValue:
if eventType == .enter {
Expand Down
6 changes: 3 additions & 3 deletions Maaku/Core/DocumentConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ extension DocumentConverter: CMParserDelegate {
nodes.append(FootnoteReference(reference: reference))
}

public func parserDidStartTable(parser: CMParser) {
public func parser(parser: CMParser, didStartTableWithColumns columns: UInt16, alignments: [String]) {
nodes.append(Table())
}

public func parserDidEndTable(parser: CMParser) {
public func parser(parser: CMParser, didEndTableWithColumns columns: UInt16, alignments: [String]) {
var rows: [TableRow] = []
var header = TableHeader()

Expand All @@ -360,7 +360,7 @@ extension DocumentConverter: CMParserDelegate {

if let _ = nodes.last as? Table {
nodes.removeLast()
nodes.append(Table(header: header, rows: rows))
nodes.append(Table(header: header, rows: rows, columns: Int(columns), alignments: alignments))
}
}

Expand Down
65 changes: 55 additions & 10 deletions Maaku/Core/Extensions/Table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,42 @@ public protocol TableLine: Node {

}

/// Represents a table alignment.
public enum TableAlignment: String {

/// Center alignment.
case center = "c"

/// Left alignment.
case left = "l"

/// No alignment.
case none = ""

/// Right alignment.
case right = "r"

/// Creates a TableAlignment matching the raw value.
///
/// - Parameters:
/// - rawValue: The raw alignment value.
/// - Returns:
/// - The table alignment matching the raw value,
/// TableAlignment.none if there is no match.
public init(rawValue: String) {
switch rawValue {
case TableAlignment.center.rawValue:
self = .center
case TableAlignment.left.rawValue:
self = .left
case TableAlignment.right.rawValue:
self = .right
default:
self = .none
}
}
}

/// Represents a markdown table.
public struct Table: LeafBlock {

Expand All @@ -26,36 +62,45 @@ public struct Table: LeafBlock {
/// The table rows.
public let rows: [TableRow]

/// The number of columns.
public let columns: Int

/// The table alignments.
public let alignments: [TableAlignment]

/// Creates a Table.
///
/// - Returns:
/// The initialized Table.
public init() {
self.header = TableHeader()
rows = []
self.init(header: TableHeader(), rows: [], columns: 0, alignments: [])
}

/// Creates a Table.
/// Creates a Paragraph with the specified values.
///
/// - Parameters:
/// - header: The table header.
/// - columns: The number of columns in the table.
/// - alignments: The table alignments.
/// - Returns:
/// The initialized Table.
public init(header: TableHeader) {
self.header = header
rows = []
public init(columns: Int, alignments: [String]) {
self.init(header: TableHeader(), rows: [], columns: columns, alignments: alignments)
}

/// Creates a Paragraph with the specified items.
/// Creates a Paragraph with the specified values.
///
/// - Parameters:
/// - header: The table header.
/// - rows: The table rows.
/// - columns: The number of columns in the table.
/// - alignments: The table alignments.
/// - Returns:
/// The initialized Paragraph.
public init(header: TableHeader, rows: [TableRow]) {
/// The initialized Table.
public init(header: TableHeader, rows: [TableRow], columns: Int, alignments: [String]) {
self.header = header
self.rows = rows
self.columns = columns
self.alignments = alignments.map { TableAlignment(rawValue: $0) }
}
}

Expand Down
14 changes: 11 additions & 3 deletions MaakuTests/Core/Extensions/TableSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,27 @@ class TableSpec: QuickSpec {
let table = document[0] as! Table

it("parses the header") {
expect(table.header.cells.count).to(equal(2))
expect(table.header.cells.count).to(equal(3))
}

it("parses the rows") {
expect(table.rows.count).to(equal(2))
}

it("parses the first row") {
expect(table.rows[0].cells.count).to(equal(2))
expect(table.rows[0].cells.count).to(equal(3))
}

it("parses the second row") {
expect(table.rows[1].cells.count).to(equal(2))
expect(table.rows[1].cells.count).to(equal(3))
}

it("parses the number of columns") {
expect(table.columns).to(equal(3))
}

it("parses the alignments") {
expect(table.alignments).to(equal([.left, .center, .right]))
}
}
catch let error {
Expand Down
8 changes: 4 additions & 4 deletions MaakuTests/Markdown/table.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
| First Header | Second Header |
| ------------- | ------------- |
| Content Cell | Content Cell |
| Content Cell | Content Cell |
| Left-aligned | Center-aligned | Right-aligned |
| :--- | :---: | ---: |
| git status | git status | git status |
| git diff | git diff | git diff |

0 comments on commit 2391751

Please sign in to comment.