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

[AST] Replace line separator with paragraph separator. #149

Merged
merged 2 commits into from
Jun 3, 2019
Merged
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
24 changes: 15 additions & 9 deletions Source/AST/Visitors/AttributedStringVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension AttributedStringVisitor: Visitor {

public func visit(blockQuote node: BlockQuote) -> NSMutableAttributedString {
let s = visitChildren(of: node).joined
if node.hasSuccessor { s.append(.blankLine) }
if node.hasSuccessor { s.append(.paragraphSeparator) }
styler.style(blockQuote: s)
return s
}
Expand All @@ -59,14 +59,14 @@ extension AttributedStringVisitor: Visitor {
}

let s = items.joined
if node.hasSuccessor { s.append(.blankLine) }
if node.hasSuccessor { s.append(.paragraphSeparator) }
styler.style(list: s)
return s
}

public func visit(item node: Item) -> NSMutableAttributedString {
let s = visitChildren(of: node).joined
if node.hasSuccessor { s.append(.blankLine) }
if node.hasSuccessor { s.append(.paragraphSeparator) }
styler.style(item: s)
return s
}
Expand All @@ -91,20 +91,20 @@ extension AttributedStringVisitor: Visitor {

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

public func visit(heading node: Heading) -> NSMutableAttributedString {
let s = visitChildren(of: node).joined
if node.hasSuccessor { s.append(.blankLine) }
if node.hasSuccessor { s.append(.paragraphSeparator) }
styler.style(heading: s, level: node.headingLevel)
return s
}

public func visit(thematicBreak node: ThematicBreak) -> NSMutableAttributedString {
let s = "\n".attributed
let s = String.lineSeparator.attributed
styler.style(thematicBreak: s)
return s
}
Expand All @@ -116,13 +116,13 @@ extension AttributedStringVisitor: Visitor {
}

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

public func visit(lineBreak node: LineBreak) -> NSMutableAttributedString {
let s = "\n".attributed
let s = String.lineSeparator.attributed
styler.style(lineBreak: s)
return s
}
Expand Down Expand Up @@ -185,7 +185,7 @@ private extension String {
}

private extension NSAttributedString {
static var blankLine: NSAttributedString {
static var paragraphSeparator: NSAttributedString {
return "\n".attributed
}
}
Expand All @@ -195,3 +195,9 @@ private extension NSMutableAttributedString {
return "".attributed
}
}
private extension String {
// https://lists.apple.com/archives/Cocoa-dev/2010/Dec/msg00347.html
static var lineSeparator: String {
return "\u{2028}"
}
}
6 changes: 2 additions & 4 deletions Tests/VisitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ class VisitorTests: XCTestCase {
let expected = """
Heading
This is a paragraph with inline elements <p></p>
This is followed by a hard linebreak
This is after the linebreak

this is a link this is an image
This is followed by a hard linebreak\u{2028}This is after the linebreak
\u{2028}this is a link this is an image
this is a quote
code block
code block
Expand Down