Class: Nanoc::Extra::LinkCollector
- Inherits:
-
Object
- Object
- Nanoc::Extra::LinkCollector
- Defined in:
- lib/nanoc/extra/link_collector.rb
Instance Method Summary (collapse)
-
- (Boolean) external_href?(href)
-
- (Object) filenames_per_href
-
- (Object) hrefs_in_file(filename)
-
- (LinkCollector) initialize(filenames, mode = nil)
constructor
A new instance of LinkCollector.
Constructor Details
- (LinkCollector) initialize(filenames, mode = nil)
Returns a new instance of LinkCollector
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/nanoc/extra/link_collector.rb', line 9 def initialize(filenames, mode = nil) Nanoc::Extra::JRubyNokogiriWarner.check_and_warn @filenames = filenames @filter = case mode when nil lambda { |h| true } when :external lambda { |h| external_href?(h) } when :internal lambda { |h| !external_href?(h) } else raise ArgumentError, 'Expected mode argument to be :internal, :external or nil' end end |
Instance Method Details
- (Boolean) external_href?(href)
38 39 40 |
# File 'lib/nanoc/extra/link_collector.rb', line 38 def external_href?(href) !!(href =~ %r{^(\/\/|[a-z\-]+:)}) end |
- (Object) filenames_per_href
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/nanoc/extra/link_collector.rb', line 26 def filenames_per_href require 'nokogiri' filenames_per_href = {} @filenames.each do |filename| hrefs_in_file(filename).each do |href| filenames_per_href[href] ||= Set.new filenames_per_href[href] << filename end end filenames_per_href end |
- (Object) hrefs_in_file(filename)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/nanoc/extra/link_collector.rb', line 42 def hrefs_in_file(filename) hrefs_in_file = Set.new doc = Nokogiri::HTML(::File.read(filename)) doc.css('a').each { |e| hrefs_in_file << e[:href] unless e[:href].nil? } doc.css('img').each { |e| hrefs_in_file << e[:src] } # Convert protocol-relative urls # e.g. //example.com => http://example.com hrefs_in_file.map! { |href| href.gsub /^\/\//, 'http://' } # Strip fragment hrefs_in_file.map! { |href| href.gsub(/#.*$/, '') } hrefs_in_file.select(&@filter) end |