module JekyllImport
module Util
# Ruby translation of wordpress wpautop (see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php)
#
# A group of regex replaces used to identify text formatted with newlines and
# replace double line-breaks with HTML paragraph tags. The remaining
# line-breaks after conversion become <
> tags, unless $br is set to false
#
# @param string pee The text which has to be formatted.
# @param bool br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
# @return string Text which has been converted into correct paragraph tags.
#
def self.wpautop(pee, br = true)
return '' if pee.strip == ''
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|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)'
pre_tags = {}
pee = pee + "\n"
if pee.include?('
') last_pee = pee_parts.pop pee = '' pee_parts.each_with_index do |pee_part, i| start = pee_part.index('' pee += pee_part[0, start] + name end pee += last_pee end pee = pee.gsub(Regexp.new('
\s*
'), "\n\n") pee = pee.gsub(Regexp.new("(<" + allblocks + "[^>]*>)"), "\n\\1") pee = pee.gsub(Regexp.new("(" + allblocks + ">)"), "\\1\n\n") pee = pee.gsub("\r\n", "\n").gsub("\r", "\n") if pee.include? '