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

Moving bold and italic to children #3

Merged
merged 1 commit into from
Nov 22, 2020
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
12 changes: 12 additions & 0 deletions Sources/Evergreen/Evergreen+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,16 @@ extension String {
func fullRange() -> NSRange {
return NSRange(location: 0, length: self.count)
}

/// Insert a substring into a string, based on a regular expression match
mutating func replaceRange(matching: NSRegularExpression, with: String, options: NSRegularExpression.MatchingOptions = [], range _range: NSRange? = nil) {
let range = _range ?? self.fullRange()
let _match = matching.firstMatch(in: self, options: options, range: range)

guard let match = _match?.range else { return }
let startIndex = self.index(self.startIndex, offsetBy: match.location)
let endIndex = self.index(startIndex, offsetBy: match.length)

self.replaceSubrange(startIndex..<endIndex, with: with)
}
}
71 changes: 37 additions & 34 deletions Sources/Evergreen/EvergreenProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,56 +147,59 @@ public class EvergreenProcessor {
element.text = lineCopy
}

func splitItalicMatch(line: String, in range: NSRange? = nil) -> String {
let matchRange = range ?? line.fullRange()

let italic = italicMatch.firstMatch(in: line, options: [], range: matchRange)
func splitItalicMatch(element: EvergreenElement, in range: NSRange? = nil) -> EvergreenElement {
let matchRange = range ?? element.text.fullRange()

let text = element.text.stringFromMatch(italicMatch, in: matchRange).replaceSubstrings(["*"]).trim()
let italicElement = EvergreenElement(elementType: "i", text: text)
let identifier = UUID().uuidString

guard italic != nil else { return line }
italicElement.identifier = identifier

let identifier = line.stringFromMatch(italicMatch, in: matchRange).replaceSubstrings(["*"]).trim()
element.text.replaceRange(matching: italicMatch, with: identifier, range: matchRange)

return line.replaceFirst(matching: italicMatch, with: "<i!>\(identifier)<!i>", in: matchRange)
return italicElement
}

func splitBoldMatch(line: String, in range: NSRange? = nil) -> String {
let matchRange = range ?? line.fullRange()

let bold = boldMatch.firstMatch(in: line, options: [], range: matchRange)
func splitBoldMatch(element: EvergreenElement, in range: NSRange? = nil) -> EvergreenElement {
let matchRange = range ?? element.text.fullRange()

guard bold != nil else { return line }
let match = element.text.stringFromMatch(boldMatch, in: matchRange)
let text = match.replaceSubstrings(["**"]).trim()
let boldElement = EvergreenElement(elementType: "b", text: text)
let identifier = UUID().uuidString

let identifier = line.stringFromMatch(boldMatch, in: matchRange).replaceSubstrings(["**"]).trim()
boldElement.identifier = identifier

return line.replaceFirst(matching: boldMatch, with: "<b!>\(identifier)<!b>", in: matchRange)
element.text.replaceRange(matching: boldMatch, with: identifier, range: matchRange)
return boldElement
}

func splitDoubleMatch(line: String) -> String {
let match = boldItalicMatch.firstMatch(in: line, options: [], range: line.fullRange())
var subbedRange = match?.range

if subbedRange != nil {
subbedRange!.length += 6
}
func splitDoubleMatch(element: EvergreenElement) -> EvergreenElement {
let text = element.text.stringFromMatch(boldItalicMatch, in: element.text.fullRange()).replaceSubstrings(["***"]).trim()
let identifier = UUID().uuidString

let italicElement = EvergreenElement(elementType: "i", text: text)
let boldElement = EvergreenElement(elementType: "b", text: "")
boldElement.children = [italicElement]
boldElement.identifier = identifier

return splitBoldMatch(line: splitItalicMatch(line: line, in: match?.range), in: subbedRange)
element.text.replaceRange(matching: boldItalicMatch, with: identifier)

return boldElement
}

func parseModifiers(element: EvergreenElement) {
var lineCopy = element.text

if lineCopy.isMatching(italicMatch) {
while lineCopy.isMatching(italicMatch) {
if lineCopy.isMatching(boldItalicMatch) {
lineCopy = splitDoubleMatch(line: lineCopy)
} else if lineCopy.isMatching(boldMatch) {
lineCopy = splitBoldMatch(line: lineCopy)
} else if lineCopy.isMatching(italicMatch) {
lineCopy = splitItalicMatch(line: lineCopy)
if element.text.isMatching(italicMatch) {
while element.text.isMatching(italicMatch) {
if element.text.isMatching(boldItalicMatch) {
element.children.append(splitDoubleMatch(element: element))
} else if element.text.isMatching(boldMatch) {
element.children.append(splitBoldMatch(element: element))
} else if element.text.isMatching(italicMatch) {
element.children.append(splitItalicMatch(element: element))
}
}

element.text = lineCopy
}
}

Expand Down
40 changes: 35 additions & 5 deletions Tests/EvergreenTests/EvergreenProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ final class EvergreenProcessorTests: XCTestCase {

let element = elements.first!

XCTAssertEqual(element.text, "A <b!>bolded<!b> statement")
XCTAssertEqual(element.children.count, 1)

let child = element.children.first!
XCTAssertEqual(element.text, "A \(child.identifier!) statement")
XCTAssertEqual(element.elementType, "h2")
}

Expand Down Expand Up @@ -140,7 +143,11 @@ final class EvergreenProcessorTests: XCTestCase {

let element = elements.first!

XCTAssertEqual(element.text, "A <b!>bold<!b> statement")
XCTAssertEqual(element.children.count, 1)

let child = element.children.first!

XCTAssertEqual(element.text, "A \(child.identifier!) statement")
}

func testItalicProcessed() {
Expand All @@ -151,7 +158,11 @@ final class EvergreenProcessorTests: XCTestCase {

let element = elements.first!

XCTAssertEqual(element.text, "A <i!>slanted<!i> statement")
XCTAssertEqual(element.children.count, 1)

let child = element.children.first!

XCTAssertEqual(element.text, "A \(child.identifier!) statement")
}

func testBoldItalicProcessed() {
Expand All @@ -162,7 +173,16 @@ final class EvergreenProcessorTests: XCTestCase {

let element = elements.first!

XCTAssertEqual(element.text, "A <b!><i!>boldly italic<!i><!b> statement")
XCTAssertEqual(element.children.count, 1)

let bold = element.children.first!

XCTAssertEqual(bold.elementType, "b")
XCTAssertEqual(bold.children.count, 1)
XCTAssertEqual(element.text, "A \(bold.identifier!) statement")

let italic = bold.children.first!
XCTAssertEqual(italic.text, "boldly italic")
}

func testBoldItalicMixProcessed() {
Expand All @@ -173,7 +193,17 @@ final class EvergreenProcessorTests: XCTestCase {

let element = elements.first!

XCTAssertEqual(element.text, "A <b!>bold<!b> statement <i!>slanted<!i> <b!><i!>words<!i><!b>")
XCTAssertEqual(element.children.count, 3)
let boldItalic = element.children[0]
let bold = element.children[1]
let italic = element.children[2]

XCTAssertEqual(boldItalic.elementType, "b")
XCTAssertEqual(boldItalic.children.count, 1)
XCTAssertEqual(bold.elementType, "b")
XCTAssertEqual(italic.elementType, "i")

XCTAssertEqual(element.text, "A \(bold.identifier!) statement \(italic.identifier!) \(boldItalic.identifier!)")
}

func testImageProcessed() {
Expand Down