Class: SitemapGenerator::Tasks
- Inherits:
-
Object
- Object
- SitemapGenerator::Tasks
- Includes:
- Rake::DSL
- Defined in:
- lib/static_sitemap_tasks.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#crawl_for_links(link_path) ⇒ Object
uses Hpricot to grab links from a URI adds uri to @pages_crawled loops each link found adds link to pages array if it should be included, unless it already exists.
- #generate_sitemap ⇒ Object
-
#initialize(options = {}) ⇒ Tasks
constructor
A new instance of Tasks.
- #install ⇒ Object
-
#link_exists?(str, array) ⇒ Boolean
checks each value in a given array for the given string removes ‘/’ character before comparison.
- #resolve_file_path(path) ⇒ Object
-
#should_be_included?(str) ⇒ Boolean
returns true if any of the following are true: - link isn’t external (eg, contains ‘http://’) and doesn’t contain ‘mailto:’ - is equal to ‘/’ - link contains @base_url.
-
#strip_slashes(str) ⇒ Object
removes ‘/’ character from string.
Constructor Details
#initialize(options = {}) ⇒ Tasks
Returns a new instance of Tasks.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/static_sitemap_tasks.rb', line 16 def initialize( = {}) # Root of files to crawl @public_root = [:public_root] || Dir.pwd # Change frequency - see: http://www.sitemaps.org/protocol.php#changefreqdef @change_frequency = [:change_frequency] # Canonical domain of published site @base_url = [:base_url] # Index pages @index_files = [:index_files] || [ 'index.html', 'index.htm' ] # Compress output to sitemap.xml.gz @gzip_output = [:gzip_output] || true end |
Class Method Details
.install(options = {}) ⇒ Object
11 12 13 14 |
# File 'lib/static_sitemap_tasks.rb', line 11 def self.install( = {}) dir = .delete(:dir) || Dir.pwd self.new().install end |
Instance Method Details
#crawl_for_links(link_path) ⇒ Object
uses Hpricot to grab links from a URI adds uri to @pages_crawled loops each link found adds link to pages array if it should be included, unless it already exists
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/static_sitemap_tasks.rb', line 84 def crawl_for_links(link_path) if link_path.include?('http') return unless link_path.include?(@base_url) link_path.sub!(@base_url,'') end file_path = resolve_file_path(File.join(@public_root, link_path)) if file_path.nil? puts "Warning: Unable to resolve #{link_path} to a local file" return end puts "Inspecting #{file_path}...\n" doc = Hpricot(open(file_path)) rescue nil return unless doc @pages_crawled << link_path (doc/"a").each do |a| if a['href'] && should_be_included?(a['href']) @pages << a['href'] unless(link_exists?(a['href'],@pages)) end end end |
#generate_sitemap ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/static_sitemap_tasks.rb', line 36 def generate_sitemap # holds pages to go into map, and pages crawled @pages = [] @pages_crawled = [] # start with index pages crawl_for_links('/') # crawl each page in pages array unless it's already been crawled @pages.each {|page| crawl_for_links(page) unless @pages_crawled.include?(page) } # create xml for sitemap xml = Builder::XmlMarkup.new( :indent => 2 ) xml.instruct! xml.comment! "Generated on: " + Time.now.to_s xml.urlset("xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9") { # loop through array of pages, and build sitemap.xml @pages.sort.each {|link| xml.url { xml.loc URI.join(@base_url, link) # TODO - set changefreq dynamically per page xml.changefreq @change_frequency unless @change_frequency.nil? } } } # convert builder xml to xml string, and save xml_string = xml.to_s.gsub("<to_s/>","") filename = File.join(@public_root,'sitemap.xml') if @gzip_output require 'zlib' filename << '.gz' xml_file = Zlib::GzipWriter.open(filename) else xml_file = File.open(filename, 'w') end xml_file << xml_string xml_file.close end |
#install ⇒ Object
29 30 31 32 33 34 |
# File 'lib/static_sitemap_tasks.rb', line 29 def install desc "Generate a sitemap based on the contents of #{@public_root}" task 'generate_sitemap' do generate_sitemap end end |
#link_exists?(str, array) ⇒ Boolean
checks each value in a given array for the given string removes ‘/’ character before comparison
141 142 143 |
# File 'lib/static_sitemap_tasks.rb', line 141 def link_exists?(str, array) array.detect{|l| strip_slashes(l) == strip_slashes(str)} end |
#resolve_file_path(path) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/static_sitemap_tasks.rb', line 107 def resolve_file_path(path) file_path = nil if File.directory?(path) @index_files.each do |f| index_file = File.join(path,f) if File.exists?(index_file) file_path = index_file break end end else if File.exists?(path) file_path = path end end file_path end |
#should_be_included?(str) ⇒ Boolean
returns true if any of the following are true:
-
link isn’t external (eg, contains ‘http://’) and doesn’t contain ‘mailto:’
-
is equal to ‘/’
-
link contains @base_url
131 132 133 134 135 136 137 |
# File 'lib/static_sitemap_tasks.rb', line 131 def should_be_included?(str) if ((!str.include?('http://') && !str.include?('mailto:')) || str == '/' || str.include?(@base_url)) unless str.slice(0,1) == "#" return true end end end |
#strip_slashes(str) ⇒ Object
removes ‘/’ character from string
146 147 148 |
# File 'lib/static_sitemap_tasks.rb', line 146 def strip_slashes(str) str.gsub('/','') end |