class ReadmeScore::Document::Filter

Constants

SERVICES

Public Class Methods

new(noko_or_html) click to toggle source
# File lib/readme-score/document/filter.rb, line 6
def initialize(noko_or_html)
  @noko = Util.to_noko(noko_or_html, true)
end

Public Instance Methods

filtered_html!() click to toggle source
# File lib/readme-score/document/filter.rb, line 10
def filtered_html!
  remove_license!
  remove_contact!
  remove_service_images!

  @noko.to_s
end
remove_contact!() click to toggle source
# File lib/readme-score/document/filter.rb, line 24
def remove_contact!
  remove_heading_sections_named("contact")
  remove_heading_sections_named("author")
  remove_heading_sections_named("credits")
end
remove_license!() click to toggle source
# File lib/readme-score/document/filter.rb, line 18
def remove_license!
  remove_heading_sections_named("license")
  remove_heading_sections_named("licensing")
  remove_heading_sections_named("copyright")
end
remove_service_images!() click to toggle source
# File lib/readme-score/document/filter.rb, line 30
def remove_service_images!
  SERVICES.each {|service|
    remove_anchor_images_containing_url(service)
  }
end

Private Instance Methods

remove_anchor_images_containing_url(url_fragment) click to toggle source
# File lib/readme-score/document/filter.rb, line 60
def remove_anchor_images_containing_url(url_fragment)
  @noko.search('a').each {|a|
    href = a['href']
    if href && href.downcase.include?(url_fragment.downcase)
      a.remove unless a.search('img').empty?
    end
  }
end
remove_heading_sections_named(prefix) click to toggle source
# File lib/readme-score/document/filter.rb, line 37
def remove_heading_sections_named(prefix)
  any_hit = false
  selectors = (1..5).map {|i| "h#{i}"}
  selectors.each { |h|
    @noko.search(h).each { |heading|
      if heading.content.downcase == prefix
        # hit - remove everything until the next heading
        while sibling = heading.next_sibling
          if sibling.name.downcase.start_with?(heading.name)
            break
          else
            sibling.remove
          end
        end
        heading.remove
        any_hit = true
        break
      end
    }
  }
  any_hit
end