SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def wpautop(pee, br = true)
  2.   return '' if pee.strip == ''
  3.   pee = "#{pee}\n" # just to make things a little easier, pad the end
  4.   pee = pee.gsub(/<br \/>\s*<br \/>/, "\n\n")
  5.   # pace things out a little
  6.   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|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
  7.   pee = pee.gsub(Regexp.new('(<'+allblocks+'[^>]*>)'), "\n"+'\1')
  8.   pee = pee.gsub(Regexp.new('(<\/'+allblocks+'[^>]*>)'), '\1' + "\n\n")
  9.   pee = pee.gsub(/\r\n|\r/, "\n") # cross-platform newlines
  10.   if pee.include?('<object')
  11.     pee = pee.gsub(/\s*<param([^>]*)>\s*/, '<param\1>') # no pee inside object/embed
  12.     pee = pee.gsub(/\s*<\/embed>\s*/, '</embed>')
  13.   end
  14.   pee = pee.gsub(/\n\n+/, "\n\n") # take care of duplicates
  15.   # make paragraphs, including one at the end
  16.   pees = pee.split(/\n\s*\n/)
  17.   pee = '';
  18.   pees.each do |tinkle|
  19.     next if tinkle == ""
  20.     pee += '<p>' + tinkle.gsub(/\n/, "") + "</p>\n";
  21.   end
  22.   pee = pee.gsub(/<p>\s*<\/p>/, "") # under certain strange conditions it could create a P of entirely whitespace
  23.   pee = pee.gsub(/<p>([^<]+)<\/(div|address|form)>/, '<p>\1</p></\2>')
  24.   pee = pee.gsub(Regexp.new('<p>\s*(<\/?'+allblocks+'[^>]*>)\s*<\/p>'), '\1') # don't pee all over a tag
  25.   pee = pee.gsub(/<p>(<li.+?)<\/p>/, '\1') # problem with nested lists
  26.   pee = pee.gsub(/<p><blockquote([^>]*)>/i, '<blockquote\1><p>')
  27.   pee = pee.gsub('</blockquote></p>', '</p></blockquote>')
  28.   pee = pee.gsub(Regexp.new('<p>\s*(<\/?'+ allblocks + '[^>]*>)'), '\1')
  29.   pee = pee.gsub(Regexp.new('(<\/?'+allblocks+'[^>]*>)\s*</p>'), '\1')
  30.   if br
  31.     pee = pee.scan(/<(script|style).*?<\/\\1>/) do |s|
  32.       pee = pee.gsub(s, "\n<WPPreserveNewline />")
  33.     end
  34.     pee = pee.gsub(/\(?<!<br \/>\)\s*\n/, "<br />\n") # optionally make line breaks
  35.     pee = pee.gsub('<WPPreserveNewline />', "\n")
  36.   end
  37.   pee = pee.gsub(Regexp.new('(</?'+allblocks+'[^>]*>)\s*<br />'), '\1')
  38.   pee = pee.gsub(/<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/, '\1')
  39.   if pee.include?('<pre')
  40.     pee.scan(/(<pre[^>]*>)(.*?)<\/pre>/is) do |s|
  41.       s = s.gsub('<br />', '')
  42.       s = s.gsub('<p>', "\n")
  43.       s = s.gsub('</p>', '')
  44.     end
  45.   end
  46.   return pee.gsub(/\n<\/p>$/, '</p>')
  47.  
  48. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top