Last active
August 29, 2015 13:56
-
-
Save danielschonfeld/9046289 to your computer and use it in GitHub Desktop.
Porting Wordpress wpautop Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Ported from https://gist.github.com/remino/3036680 (thank you for that!) | |
def wpautop(String content, boolean br) { | |
def allhtmlblocks = "(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)" | |
if (content.trim().length() == 0) return "" | |
content += "\n" | |
content = content.replaceAll(/<br \/>\s*<br \/>/, "\n\n") | |
content = content.replaceAll(/(<${allhtmlblocks}[^>]*>)/,{ | |
log.error("${it}") | |
"\n${it[1]}" | |
}) | |
content = content.replaceAll(/(<\/${allhtmlblocks}[^>]*>)/,{ | |
"${it[1]}\n" | |
}) | |
content = content.replaceAll(/\r\n|\r/,"\n") | |
if (content.indexOf('<object')) { | |
content = content.replaceAll(/\s*<param([^>]*)>\s*/, { | |
"<param${it[1]}>" | |
}) | |
content = content.replaceAll(/\s*<\/embed>\s*/, "</embed>") | |
} | |
content = content.replaceAll(/\n\n+/, "\n\n") | |
def crap = content.split(/\n\s*\n/) | |
content = crap.inject("", { acc, ln -> | |
if (ln != '') { | |
acc += '<p>' + ln.replaceAll(/\n/, "") + "</p>\n" | |
} | |
acc | |
}) | |
content = content.replaceAll(/<p>\s*<\/p>/, "") | |
content = content.replaceAll(/<p>([^<]+)<\/(div|address|form)>/, { it -> "<p>${it[1]}</p></${it[2]}>" }) | |
content = content.replaceAll(/<p>\s*(<\/?${allhtmlblocks}[^>]*>)\s*<\/p>/, { it[1] }) | |
content = content.replaceAll(/<p>(<li.+?)<\/p>/, { it[1] }) | |
content = content.replaceAll(/<p><blockquote([^>]*)>/, { "<blockquote${it[1]}><p>" }) | |
content = content.replaceAll("</blockquote></p>", "</p></blockquote>") | |
content = content.replaceAll(/<p>\s*(<\/?'+ allhtmlblocks + '[^>]*>)/, { it[1] }) | |
content = content.replaceAll(/(<\/?${allhtmlblocks}[^>]*>)\s*<\/p>/, { it[1] }) | |
if (br) { | |
content.findAll(/<(script|style).*?<\/\\1>/,{ | |
content.replaceAll(it, "\n<WPPreserveNewline />") | |
}) | |
content = content.replaceAll(/\(?<!<br \/>\)\s*\n/, "<br />\n") | |
content = content.replaceAll("<WPPreserveNewline />", "\n") | |
} | |
content = content.replaceAll(/'(<\/?${allhtmlblocks}[^>]*>)\s*<br \/>/, { it[1] }) | |
content = content.replaceAll(/<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/, { it[1] }) | |
if (content.indexOf("<pre")) { | |
content.findAll(/(<pre[^>]*>)(.*?)<\/pre>/, { | |
it.replaceAll('<br />', '') | |
it.replaceAll('<p>', "\n") | |
it.replaceAll('</p>', '') | |
}) | |
} | |
content = content.replaceAll(/\n<\/p>$/, '</p>') | |
return content | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment