Rightpoint / BonMot

Beautiful, easy attributed strings in Swift
MIT License
3.54k stars 196 forks source link

Convert HTML to Attributed String #379

Open photos opened 4 years ago

photos commented 4 years ago

Hi,

I'm trying to convert an HTML snippet with styles and links to an attributed string. What parsing approach would you recommend?

Here is the HTML I am trying to convert:

"<ol><li><strong>Bee Gees</strong> - <a href=\"https://www.youtube.com/watch?v=I_izvAbhExY\" target=\"_blank\"><em>Staying Alive</em></a></li><li><strong>Daft Punk</strong> - <a href=\"https://www.youtube.com/watch?v=yca6UsllwYs\" target=\"_blank\"><strong><em>Around the World</em></strong></a></li><li><strong>Kanye West</strong> - <a href=\"https://www.youtube.com/watch?v=mWtIxc38xNE\" target=\"_blank\"><strong>Flashing Lights</strong></a></li></ol>"

This has various phrases with nested emphasis+strong+ahref tags. I would like these nested tags to be converted to custom font styles for an attributed string.

I'm sure others have attempted this before - wondering if there is a recommended approach?

zhgchgli0718 commented 1 year ago

I made a new parser that is based on pure String Regexr, feel free the check my works :) https://github.com/ZhgChgLi/ZMarkupParser