# encoding: UTF-8 module Publishr class HtmlProcessor def initialize(markup='',inpath='',metadata={},rails_resources_url='') @markup = markup @lines = markup.split("\n") @line = '' @inpath = inpath @metadata = metadata @rails_resources_url = rails_resources_url @custom_fixes = File.open(File.join(@inpath,'html_postprocessing.rb'), 'r').read if File.exists?(File.join(@inpath,'html_postprocessing.rb')) @depth = 0 @quotetype = nil @add_footnote = false @process_footnotes = false @footnote_number = 0 @footnote_reference = '' end def self.sanitize(html) Sanitize.clean(html, :elements => ['b','i','em','strong','code','br','var','p','blockquote','img','span'], :attributes => { 'img' => ['src', 'alt'] }) end def degrade processed_lines = [] @lines.each do |l| @line = l process_line processed_lines << @line end processed_lines.join("\n") end def process_line @process_footnotes = true if @line.include?('
') @process_footnotes = false if @process_footnotes == true and @line.include?('
') @add_footnote = true and @footnote_number += 1 if @line.include?('
  • /.match(@line) if @depth == 1 end if @line.include?('(.*?)<\/var>/){ "#{ $1.upcase }" } @line.gsub!(/(.*?)<\/h1>/){ "#{ $2.upcase }
    " } end def change_footnote_references @line.gsub! //, '' @line.gsub! '', '' @line.gsub! /rel="footnote">(.*?)<\/a>/, '> [\1]' @line.gsub!(/(
    )/){ "
    #{ $1 }

    #{ @metadata['footnote_heading'] }

    " } end def process_footnotes @line.gsub!(//){ "

    " } @footnote_reference = /

  • /, '' @line.gsub! /<\/li>/, '' @line.gsub! /
      /, '' @line.gsub! /<\/ol>/, '' @line.gsub! /

      [#{ @footnote_number }]: " @add_footnote = false end def make_footnote_paragraph @line.gsub! /

      ' @line.gsub! '=======', '

      ' @line.gsub! /»»».*$/, '

      ' end end end