psharanda / Atributika

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
MIT License
1.45k stars 156 forks source link

Support Ordered Lists? #20

Closed onato closed 6 years ago

onato commented 6 years ago

Any idea how I could render ordered lists <ol> with Atributika?

psharanda commented 6 years ago

Thanks for your idea. Please check new version of Atributika. I made TagTransformer more flexible, so it can be used now to support such things as ordered lists. General idea can be found here: https://github.com/psharanda/Atributika/blob/master/Tests/AtributikaTests/AtributikaTests.swift#L343

onato commented 6 years ago

Awesome, that was super helpful. I got what I wanted with the following…

var counter = 0
var isOrdered = false
let transformers: [TagTransformer] = [
    TagTransformer.brTransformer,
    TagTransformer(tagName: "ul", tagType: .start) { _ in
        isOrdered = false
        return ""
    },
    TagTransformer(tagName: "ol", tagType: .start) { _ in
        counter = 0
        isOrdered = true
        return ""
    },
    TagTransformer(tagName: "li", tagType: .start) { _ in
        counter += 1
        return isOrdered ? "\(counter). " : "• "
    },
    TagTransformer(tagName: "li", tagType: .end) { _ in
        return "\n"
    },
…