|
|
@ -6,6 +6,9 @@ import java.util.regex.Pattern |
|
|
|
|
|
|
|
|
|
|
|
@Suppress("RegExpRedundantEscape") |
|
|
|
@Suppress("RegExpRedundantEscape") |
|
|
|
object HtmlFormatter { |
|
|
|
object HtmlFormatter { |
|
|
|
|
|
|
|
private val nbspRegex = "( )+".toRegex() |
|
|
|
|
|
|
|
private val espRegex = "( | )".toRegex() |
|
|
|
|
|
|
|
private val noPrintRegex = "( |‌|‍)".toRegex() |
|
|
|
private val wrapHtmlRegex = "</?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex() |
|
|
|
private val wrapHtmlRegex = "</?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex() |
|
|
|
private val commentRegex = "<!--[^>]*-->".toRegex() //注释 |
|
|
|
private val commentRegex = "<!--[^>]*-->".toRegex() //注释 |
|
|
|
private val notImgHtmlRegex = "</?(?!img)[a-zA-Z]+(?=[ >])[^<>]*>".toRegex() |
|
|
|
private val notImgHtmlRegex = "</?(?!img)[a-zA-Z]+(?=[ >])[^<>]*>".toRegex() |
|
|
@ -17,7 +20,10 @@ object HtmlFormatter { |
|
|
|
|
|
|
|
|
|
|
|
fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String { |
|
|
|
fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String { |
|
|
|
html ?: return "" |
|
|
|
html ?: return "" |
|
|
|
return html.replace(wrapHtmlRegex, "\n") |
|
|
|
return html.replace(nbspRegex, " ") |
|
|
|
|
|
|
|
.replace(espRegex, " ") |
|
|
|
|
|
|
|
.replace(noPrintRegex, "") |
|
|
|
|
|
|
|
.replace(wrapHtmlRegex, "\n") |
|
|
|
.replace(commentRegex, "") |
|
|
|
.replace(commentRegex, "") |
|
|
|
.replace(otherRegex, "") |
|
|
|
.replace(otherRegex, "") |
|
|
|
.replace("\\s*\\n+\\s*".toRegex(), "\n ") |
|
|
|
.replace("\\s*\\n+\\s*".toRegex(), "\n ") |
|
|
|