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

[Feature] Create API for parsing to AST and NSAttributedString #132

Merged
merged 55 commits into from
Apr 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
eb84438
Create Node wrapper
johnxnguyen Apr 7, 2019
a474e49
Categorize nodes into classes
johnxnguyen Apr 8, 2019
3d53ab5
Add debug descriptions
johnxnguyen Apr 8, 2019
b0444df
Define Visitor
johnxnguyen Apr 8, 2019
e18fdb5
Add simple test for demonstration purposes
johnxnguyen Apr 8, 2019
bb58866
Move nodes into separate files
johnxnguyen Apr 9, 2019
6bd9eaf
Move visitors into separate files
johnxnguyen Apr 9, 2019
87a986c
Tidy up
johnxnguyen Apr 9, 2019
e9b9311
Expose Visitor protocol
johnxnguyen Apr 9, 2019
a774ca5
Add accept method to Document
johnxnguyen Apr 9, 2019
9f235f3
Update various nodes
johnxnguyen Apr 9, 2019
4ef819d
WIP: AttributedStringVisitor
johnxnguyen Apr 9, 2019
6f239d3
Fill out remaining visit methods
johnxnguyen Apr 13, 2019
28057c7
Move Styler protocol into its own file
johnxnguyen Apr 13, 2019
47dca50
Append blank line only for nodes with successors
johnxnguyen Apr 13, 2019
e7b10ff
Add prefixes to list items
johnxnguyen Apr 13, 2019
2169c1c
Style list prefixes
johnxnguyen Apr 13, 2019
65f07d4
Rename isLast
johnxnguyen Apr 14, 2019
c12b626
Adjust accept signature
johnxnguyen Apr 19, 2019
0fb565d
Adjust Image and Link nodes
johnxnguyen Apr 19, 2019
f1eca07
Pass url to styler methods
johnxnguyen Apr 19, 2019
ef147b5
Provide API to render to an attributed string via the AST
johnxnguyen Apr 19, 2019
23c46a9
Fix Link & Image properties
johnxnguyen Apr 19, 2019
94ddff2
Pass fenceInfo to styler method
johnxnguyen Apr 19, 2019
2b1da03
Slight refactoring of nodes
johnxnguyen Apr 19, 2019
0b5a69d
Improve DebugVisitor to print tree depth
johnxnguyen Apr 19, 2019
2ef3479
Tidy up
johnxnguyen Apr 19, 2019
4ad8787
More tidy up
johnxnguyen Apr 19, 2019
c5c42a9
Add some descriptions
johnxnguyen Apr 19, 2019
4b9fc13
Tidy up
johnxnguyen Apr 19, 2019
be92adf
Add documentation
johnxnguyen Apr 19, 2019
996e881
Make nodes public
johnxnguyen Apr 19, 2019
b022ef0
Tidy up
johnxnguyen Apr 19, 2019
765fa85
More documentation
johnxnguyen Apr 19, 2019
13728bb
Add visitor test
johnxnguyen Apr 19, 2019
23d0914
Merge branch 'master' into feature/ast-to-nsattributedstring
johnxnguyen Apr 19, 2019
e48457d
Fix compile error for Xcode 10.1
johnxnguyen Apr 19, 2019
34d6d54
Merge branch 'feature/ast-to-nsattributedstring' of github.com:iwasro…
johnxnguyen Apr 19, 2019
1b55d15
Remove unnecessary line breaks from blocks
johnxnguyen Apr 20, 2019
8f325f7
Test AttributedStringVisitor
johnxnguyen Apr 20, 2019
b7dbea9
Make lazy var private(set)
johnxnguyen Apr 21, 2019
d0389fb
Clean up TODO
johnxnguyen Apr 21, 2019
17060cf
Make all cmarkNode reference immutable
johnxnguyen Apr 21, 2019
214d0b0
Don’t fatalError on list type
johnxnguyen Apr 21, 2019
459a193
Don’t fatalError when wrapping child nodes
johnxnguyen Apr 21, 2019
e076bb7
Tidy up
johnxnguyen Apr 21, 2019
25c522e
Don’t fatalError when visiting children
johnxnguyen Apr 21, 2019
452887c
Tidy up doc comments
johnxnguyen Apr 21, 2019
930cec2
Improve documentation
johnxnguyen Apr 21, 2019
2f45bcc
Refactor Node initialization
johnxnguyen Apr 21, 2019
39eb567
Expose wrap() method
johnxnguyen Apr 21, 2019
338fb45
Tidy up
johnxnguyen Apr 21, 2019
a50037e
Fix warnings
johnxnguyen Apr 21, 2019
8a30bcb
Lazily compute children
johnxnguyen Apr 21, 2019
990e8ff
Fix typo
johnxnguyen Apr 21, 2019
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
140 changes: 140 additions & 0 deletions Down.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions Source/AST/Nodes/BaseNode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// BaseNode.swift
// Down
//
// Created by John Nguyen on 21.04.19.
//
//

import Foundation
import libcmark

public class BaseNode: Node {

public let cmarkNode: CMarkNode

public private(set) lazy var children: [Node] = {
var result: [Node] = []
var child = cmark_node_first_child(cmarkNode)

while let raw = child {

guard let node = raw.wrap() else {
assertionFailure("Couldn't wrap node of type: \(raw.type)")
continue
}

result.append(node)
child = cmark_node_next(child)
}

return result
}()

init(cmarkNode: CMarkNode) {
self.cmarkNode = cmarkNode
}
}
21 changes: 21 additions & 0 deletions Source/AST/Nodes/BlockQuote.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// BlockQuote.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class BlockQuote: BaseNode {}


// MARK: - Debug

extension BlockQuote: CustomDebugStringConvertible {

public var debugDescription: String {
return "Block Quote"
}
}
25 changes: 25 additions & 0 deletions Source/AST/Nodes/Code.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Code.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class Code: BaseNode {

/// The code content, if present.
public private(set) lazy var literal: String? = cmarkNode.literal
}


// MARK: - Debug

extension Code: CustomDebugStringConvertible {

public var debugDescription: String {
return "Code - \(literal ?? "nil")"
}
}
38 changes: 38 additions & 0 deletions Source/AST/Nodes/CodeBlock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// CodeBlock.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class CodeBlock: BaseNode {

/// The code content, if present.
public private(set) lazy var literal: String? = cmarkNode.literal

/// The fence info is an optional string that trails the opening sequence of backticks.
/// It can be used to provide some contextual information about the block, such as
/// the name of a programming language.
///
/// For example:
/// ```
/// '''<fence info>
/// <literal>
/// '''
/// ```
///
public private(set) lazy var fenceInfo: String? = cmarkNode.fenceInfo
}


// MARK: - Debug

extension CodeBlock: CustomDebugStringConvertible {

public var debugDescription: String {
return "Code Block - \(literal ?? "nil"), fenceInfo: \(fenceInfo ?? "nil")"
}
}
25 changes: 25 additions & 0 deletions Source/AST/Nodes/CustomBlock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// CustomBlock.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class CustomBlock: BaseNode {

/// The custom content, if present.
public private(set) lazy var literal: String? = cmarkNode.literal
}


// MARK: - Debug

extension CustomBlock: CustomDebugStringConvertible {

public var debugDescription: String {
return "Custom Block - \(literal ?? "nil")"
}
}
25 changes: 25 additions & 0 deletions Source/AST/Nodes/CustomInline.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// CustomInline.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class CustomInline: BaseNode {

/// The custom content, if present.
public private(set) lazy var literal: String? = cmarkNode.literal
}


// MARK: - Debug

extension CustomInline: CustomDebugStringConvertible {

public var debugDescription: String {
return "Custom Inline - \(literal ?? "nil")"
}
}
32 changes: 32 additions & 0 deletions Source/AST/Nodes/Document.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Document.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class Document: BaseNode {

deinit {
// Frees the node and all its children.
cmark_node_free(cmarkNode)
}

/// Accepts the given visitor and return its result.
public func accept<T: Visitor>(_ visitor: T) -> T.Result {
return visitor.visit(document: self)
}
}


// MARK: - Debug

extension Document: CustomDebugStringConvertible {

public var debugDescription: String {
return "Document"
}
}
21 changes: 21 additions & 0 deletions Source/AST/Nodes/Emphasis.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Emphasis.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class Emphasis: BaseNode {}


// MARK: - Debug

extension Emphasis: CustomDebugStringConvertible {

public var debugDescription: String {
return "Emphasis"
}
}
25 changes: 25 additions & 0 deletions Source/AST/Nodes/Heading.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Heading.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class Heading: BaseNode {

/// The level of the heading, a value between 1 and 6.
public private(set) lazy var headingLevel: Int = cmarkNode.headingLevel
}


// MARK: - Debug

extension Heading: CustomDebugStringConvertible {

public var debugDescription: String {
return "Heading - L\(headingLevel)"
}
}
25 changes: 25 additions & 0 deletions Source/AST/Nodes/HtmlBlock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// HtmlBlock.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class HtmlBlock: BaseNode {

/// The html content, if present.
public private(set) lazy var literal: String? = cmarkNode.literal
}


// MARK: - Debug

extension HtmlBlock: CustomDebugStringConvertible {

public var debugDescription: String {
return "Html Block - \(literal ?? "nil")"
}
}
25 changes: 25 additions & 0 deletions Source/AST/Nodes/HtmlInline.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// HtmlInline.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class HtmlInline: BaseNode {

/// The html tag, if present.
public private(set) lazy var literal: String? = cmarkNode.literal
}


// MARK: - Debug

extension HtmlInline: CustomDebugStringConvertible {

public var debugDescription: String {
return "Html Inline - \(literal ?? "nil")"
}
}
46 changes: 46 additions & 0 deletions Source/AST/Nodes/Image.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Image.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class Image: BaseNode {

/// The title of the image, if present.
///
/// In the example below, the first line is a reference link, with the reference at the
/// bottom. `<text>` is literal text belonging to children nodes. The title occurs
/// after the url and is optional.
///
/// ```
/// ![<text>][<id>]
/// ...
/// [<id>]: <url> "<title>"
/// ```
///
public private(set) lazy var title: String? = cmarkNode.title

/// The url of the image, if present.
///
/// For example:
///
/// ```
/// ![<text>](<url>)
/// ```
///
public private(set) lazy var url: String? = cmarkNode.url
}


// MARK: - Debug

extension Image: CustomDebugStringConvertible {

public var debugDescription: String {
return "Image - title: \(title ?? "nil"), url: \(url ?? "nil"))"
}
}
21 changes: 21 additions & 0 deletions Source/AST/Nodes/Item.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Item.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation
import libcmark

public class Item: BaseNode {}


// MARK: - Debug

extension Item: CustomDebugStringConvertible {

public var debugDescription: String {
return "Item"
}
}
Loading