Skip to content

Commit

Permalink
More flexible TagTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed Jan 17, 2018
1 parent f3bf2ed commit dca48bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Sources/String+Detection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ public struct TagTransformer {

public let tagName: String
public let tagType: TagType
public let replaceValue: String
public let transform: (Tag) -> String

public init(tagName: String, tagType: TagType, replaceValue: String) {
self.tagName = tagName
self.tagType = tagType
self.replaceValue = replaceValue
self.transform = { _ in replaceValue }
}

public init(tagName: String, tagType: TagType, transform: @escaping (Tag) -> String) {
self.tagName = tagName
self.tagType = tagType
self.transform = transform
}

public static var brTransformer: TagTransformer {
Expand Down Expand Up @@ -110,7 +116,7 @@ extension String {
if let transformer = transformers.first(where: {
$0.tagName == tag.name && $0.tagType == tagType
}) {
resultString += transformer.replaceValue
resultString += transformer.transform(tag)
}

if tagType == .start {
Expand Down
24 changes: 24 additions & 0 deletions Tests/AtributikaTests/AtributikaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,30 @@ class AtributikaTests: XCTestCase {
XCTAssertEqual(test,reference)
}


func testOL() {
var counter = 0
let transformers: [TagTransformer] = [
TagTransformer.brTransformer,
TagTransformer(tagName: "ol", tagType: .start) { _ in
counter = 0
return ""
},
TagTransformer(tagName: "li", tagType: .start) { _ in
counter += 1
return "\(counter). "
},
TagTransformer(tagName: "li", tagType: .end) { _ in
return "\n"
}
]

let test = "<div><ol type=\"\"><li>Coffee</li><li>Tea</li><li>Milk</li></ol><ol type=\"\"><li>Coffee</li><li>Tea</li><li>Milk</li></ol></div>".style(tags: [], transformers: transformers).string
let reference = "1. Coffee\n2. Tea\n3. Milk\n1. Coffee\n2. Tea\n3. Milk\n"

XCTAssertEqual(test,reference)
}

func testStyleBuilder() {

let s = Style
Expand Down

0 comments on commit dca48bd

Please sign in to comment.