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("()"), "\\1\n\n") pee = pee.gsub("\r\n", "\n").gsub("\r", "\n") if pee.include? ']*)>\s*'), "") pee = pee.gsub(Regexp.new('\s*\s*'), '') end pees = pee.split(/\n\s*\n/).compact pee = '' pees.each { |tinkle| pee += '

' + tinkle.chomp("\n") + "

\n" } pee = pee.gsub(Regexp.new('

\s*

'), '') pee = pee.gsub(Regexp.new('

([^<]+)'), "

\\1

") pee = pee.gsub(Regexp.new('

\s*(]*>)\s*

'), "\\1") pee = pee.gsub(Regexp.new('

('), "\\1") pee = pee.gsub(Regexp.new('

]*)>', 'i'), "

") pee = pee.gsub('

', '

') pee = pee.gsub(Regexp.new('

\s*(]*>)'), "\\1") pee = pee.gsub(Regexp.new('(]*>)\s*

'), "\\1") if br pee = pee.gsub(Regexp.new('<(script|style).*?')) { |match| match.gsub("\n", "") } pee = pee.gsub(Regexp.new('(?)\s*\n'), "
\n") pee = pee.gsub('', "\n") end pee = pee.gsub(Regexp.new('(]*>)\s*
'), "\\1") pee = pee.gsub(Regexp.new('
(\s*]*>)'), "\\1") pee = pee.gsub(Regexp.new('\n

$'), '

') pre_tags.each do |name, value| pee.gsub!(name, value) end pee end end end