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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
WIP: AttributedStringVisitor
  • Loading branch information
johnxnguyen committed Apr 9, 2019

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit 4ef819d8ec8ef5b8634b690fe123520d6b057ce2
18 changes: 11 additions & 7 deletions Down.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@
EEEBEE6B225D2B2200AE438D /* Link.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEEBEE6A225D2B2200AE438D /* Link.swift */; };
EEEBEE6D225D2B3200AE438D /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEEBEE6C225D2B3200AE438D /* Image.swift */; };
EEEBEE70225D2B9D00AE438D /* DebugVisitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEEBEE6F225D2B9D00AE438D /* DebugVisitor.swift */; };
EEEBEE72225D2F9200AE438D /* AttributedStringVisitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEEBEE71225D2F9200AE438D /* AttributedStringVisitor.swift */; };
EEF1376F2259E53400D7DDE0 /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEF1376E2259E53400D7DDE0 /* Node.swift */; };
EEF137712259E7E700D7DDE0 /* Vistor.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEF137702259E7E700D7DDE0 /* Vistor.swift */; };
/* End PBXBuildFile section */
@@ -239,6 +240,7 @@
EEEBEE6A225D2B2200AE438D /* Link.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Link.swift; sourceTree = "<group>"; };
EEEBEE6C225D2B3200AE438D /* Image.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = "<group>"; };
EEEBEE6F225D2B9D00AE438D /* DebugVisitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugVisitor.swift; sourceTree = "<group>"; };
EEEBEE71225D2F9200AE438D /* AttributedStringVisitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedStringVisitor.swift; sourceTree = "<group>"; };
EEF1376E2259E53400D7DDE0 /* Node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = "<group>"; };
EEF137702259E7E700D7DDE0 /* Vistor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Vistor.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -424,13 +426,6 @@
isa = PBXGroup;
children = (
EEF1376E2259E53400D7DDE0 /* Node.swift */,
EEEBEE60225D2AC000AE438D /* Code.swift */,
EEEBEE62225D2AD400AE438D /* HtmlInline.swift */,
EEEBEE64225D2AE800AE438D /* CustomInline.swift */,
EEEBEE66225D2AF900AE438D /* Emphasis.swift */,
EEEBEE68225D2B1200AE438D /* Strong.swift */,
EEEBEE6A225D2B2200AE438D /* Link.swift */,
EEEBEE6C225D2B3200AE438D /* Image.swift */,
EEEBEE46225D298D00AE438D /* Document.swift */,
EEEBEE48225D29C200AE438D /* BlockQuote.swift */,
EEEBEE4A225D29D800AE438D /* List.swift */,
@@ -444,6 +439,13 @@
EEEBEE5A225D2A7E00AE438D /* Text.swift */,
EEEBEE5C225D2A8F00AE438D /* SoftBreak.swift */,
EEEBEE5E225D2AA700AE438D /* LineBreak.swift */,
EEEBEE60225D2AC000AE438D /* Code.swift */,
EEEBEE62225D2AD400AE438D /* HtmlInline.swift */,
EEEBEE64225D2AE800AE438D /* CustomInline.swift */,
EEEBEE66225D2AF900AE438D /* Emphasis.swift */,
EEEBEE68225D2B1200AE438D /* Strong.swift */,
EEEBEE6A225D2B2200AE438D /* Link.swift */,
EEEBEE6C225D2B3200AE438D /* Image.swift */,
);
path = Nodes;
sourceTree = "<group>";
@@ -453,6 +455,7 @@
children = (
EEF137702259E7E700D7DDE0 /* Vistor.swift */,
EEEBEE6F225D2B9D00AE438D /* DebugVisitor.swift */,
EEEBEE71225D2F9200AE438D /* AttributedStringVisitor.swift */,
);
path = Visitors;
sourceTree = "<group>";
@@ -649,6 +652,7 @@
8A569F741E6B3EDE008BE2AC /* DownXMLRenderable.swift in Sources */,
8A569F6B1E6B3ED9008BE2AC /* utf8.c in Sources */,
8A569F791E6B3EE7008BE2AC /* DownOptions.swift in Sources */,
EEEBEE72225D2F9200AE438D /* AttributedStringVisitor.swift in Sources */,
EEEBEE4B225D29D800AE438D /* List.swift in Sources */,
EEEBEE57225D2A6200AE438D /* Heading.swift in Sources */,
8A569F601E6B3ED9008BE2AC /* latex.c in Sources */,
160 changes: 160 additions & 0 deletions Source/AST/Visitors/AttributedStringVisitor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
//
// AttributedStringVisitor.swift
// Down
//
// Created by John Nguyen on 09.04.19.
//

import Foundation

protocol Styler {
func style(document str: NSMutableAttributedString)
func style(blockQuote str: NSMutableAttributedString)
func style(list str: NSMutableAttributedString)
func style(item str: NSMutableAttributedString)
func style(codeBlock str: NSMutableAttributedString)
func style(htmlBlock str: NSMutableAttributedString)
func style(customBlock str: NSMutableAttributedString)
func style(paragraph str: NSMutableAttributedString)
func style(heading str: NSMutableAttributedString)
func style(thematicBreak str: NSMutableAttributedString)
func style(text str: NSMutableAttributedString)
func style(softBreak str: NSMutableAttributedString)
func style(lineBreak str: NSMutableAttributedString)
func style(code str: NSMutableAttributedString)
func style(htmlInline str: NSMutableAttributedString)
func style(customInline str: NSMutableAttributedString)
func style(emphasis str: NSMutableAttributedString)
func style(strong str: NSMutableAttributedString)
func style(link str: NSMutableAttributedString)
func style(image str: NSMutableAttributedString)
}

public class AttributedStringVisitor {

private let styler: Styler
private let options: DownOptions

init(styler: Styler, options: DownOptions = .default) {
self.styler = styler
self.options = options
}
}

extension AttributedStringVisitor: Visitor {
public typealias Result = NSMutableAttributedString

public func visit(document node: Document) -> NSMutableAttributedString {
let s = visitChildren(of: node).joined
styler.style(document: s)
return s
}

public func visit(blockQuote node: BlockQuote) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(list node: List) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(item node: Item) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(codeBlock node: CodeBlock) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(htmlBlock node: HtmlBlock) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(customBlock node: CustomBlock) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(paragraph node: Paragraph) -> NSMutableAttributedString {
let s = visitChildren(of: node).joined
styler.style(paragraph: s)
return s
}

public func visit(heading node: Heading) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(thematicBreak node: ThematicBreak) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(text node: Text) -> NSMutableAttributedString {
let s = node.literal.attributed
styler.style(text: s)
return s
}

public func visit(softBreak node: SoftBreak) -> NSMutableAttributedString {
let s = (options.contains(.hardBreaks) ? "\n" : " ").attributed
styler.style(softBreak: s)
return s
}

public func visit(lineBreak node: LineBreak) -> NSMutableAttributedString {
let s = "\n".attributed
styler.style(lineBreak: s)
return s
}

public func visit(code node: Code) -> NSMutableAttributedString {
let s = node.literal.attributed
styler.style(code: s)
return s
}

public func visit(htmlInline node: HtmlInline) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(customInline node: CustomInline) -> NSMutableAttributedString {
fatalError("unimplemented")
}

public func visit(emphasis node: Emphasis) -> NSMutableAttributedString {
let s = visitChildren(of: node).joined
styler.style(emphasis: s)
return s
}

public func visit(strong node: Strong) -> NSMutableAttributedString {
let s = visitChildren(of: node).joined
styler.style(strong: s)
return s
}

public func visit(link node: Link) -> NSMutableAttributedString {
let s = visitChildren(of: node).joined
styler.style(link: s)
return s
}

public func visit(image node: Image) -> NSMutableAttributedString {
let s = visitChildren(of: node).joined
styler.style(image: s)
return s
}
}

private extension String {
var attributed: NSMutableAttributedString {
return NSMutableAttributedString(string: self)
}
}

private extension Sequence where Iterator.Element == NSMutableAttributedString {
var joined: NSMutableAttributedString {
return reduce(into: NSMutableAttributedString()) { (acc, next) in
acc.append(next)
}
}
}