Skip to content

Instantly share code, notes, and snippets.

@halferty
Last active February 28, 2017 06:36
Show Gist options
  • Select an option

  • Save halferty/a168fc677672f9f28cd2 to your computer and use it in GitHub Desktop.

Select an option

Save halferty/a168fc677672f9f28cd2 to your computer and use it in GitHub Desktop.
wpautop ported to Ruby (including an incidental "fix" for an obscure bug in the PHP version - which will be lost to the world because no one will ever look at this gist lol)
def autop(px, br = true)
pre_tags = []
return '' if px.strip == ""
px = px + "\n"
if px =~ /<pre/
px_parts = px.split "</pre>"
last_px = px_parts.pop
px = ""
i = 0
px_parts.each do |px_part|
start = px_part.index("<pre")
#malformed html?
if !start
px += px_part
else
name = "<pre wp-pre-tag-#{i}></pre>"
pre_tags[name] = px_part[start..-1] + "</pre>"
px += px_part[0..start] + name
i += 1
end
end
px += last_px
end
px = px.gsub(/<br \/>\s*<br \/>/, "\n\n")
# Space things out a little
allblocks = "(?: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|p|h[1-6]|hr|fieldset|noscript|samp|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)"
px = px.gsub(/(<#{allblocks}[^>]*>)/, "\n" + '\1')
px = px.gsub(/(<\/#{allblocks}>)/, '\1' + "\n\n")
px = px.gsub(/\r\n|\r/, "\n")
if px.index("<object")
px = px.gsub(/\s*<param([^>]*)>\s*/, "<param" + '\1' + ">")
px = px.gsub(/\s*<\/embed>\s*/, "</embed>")
end
px = px.gsub(/\n\n+/, "\n\n")
pxs = px.split(/\n\s*\n/).reject { |a| a.empty? }
px = ""
pxs.each do |t|
px += "<p>" + t.gsub(/\A\n+/, "").gsub(/\n+\Z/, "") + "</p>\n"
end
px = px.gsub(/<p>\s*<\/p>/, "")
px = px.gsub(/<p>([^<]+)<\/(div|address|form)>/, "<p>" + '\1' + "</p></" + '\2' + ">")
px = px.gsub(/<p>\s*(<\/?#{allblocks}[^>]*>)\s*<\/p>/, '\1')
px = px.gsub(/<p>(<li.+?)<\/p>/, '\1')
px = px.gsub(/<p><blockquote([^>]*)>/, "<blockquote" + '\1' + "><p>")
px = px.gsub(/<\/blockquote><\/p>/, "</p></blockquote>")
px = px.gsub(/<p>\s*(<\/?#{allblocks}[^>]*>)/, '\1')
px = px.gsub(/(<\/?#{allblocks}[^>]*>)\s*<\/p>/, '\1')
if br
px.scan(/<(script|style).*?<\/\\1>/) { |s| px = px.gsub(s, "\n<WPPreserveNewline />") }
px = px.gsub(/(?<!<br \/>)\s*\n/, "<br />\n")
px = px.gsub(/<WPPreserveNewline \/>/, "\n")
end
px = px.gsub(/(<\/?#{allblocks}[^>]*>)\s*<br \/>/, '\1')
px = px.gsub(/<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/, '\1')
px = px.gsub(/\n<\/p>\Z/, "</p>")
unless pre_tags.empty?
pre_tags.each { |k, v| px = px.gsub(/#{k}/, v) }
end
px
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment