Class: Homeostasis::Sitemap

Inherits:
Stasis::Plugin
  • Object
show all
Includes:
Helpers
Defined in:
lib/homeostasis.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stasis) ⇒ Sitemap

Returns a new instance of Sitemap.



395
396
397
398
# File 'lib/homeostasis.rb', line 395

def initialize(stasis)
  @stasis = stasis
  @@url = nil
end

Class Method Details

.config(options) ⇒ Object



400
401
402
403
# File 'lib/homeostasis.rb', line 400

def self.config(options)
  @@url = options[:url]
  @@lastmod = options[:lastmod] || false
end

Instance Method Details

#after_allObject



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/homeostasis.rb', line 405

def after_all
  return if @@url.nil?
  xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  xml += "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"
  front_site = Homeostasis::Front._front_site
  pages = front_site.keys.sort_by do |page|
    depth = page.scan('/').length
    depth -= 1 if page =~ /index\.html/
    "#{depth}-#{page}"
  end
  pages.each do |page|
    front = front_site[page]
    filename = File.join(@stasis.root, page)
    next if page !~ /\.html/ || !front || front[:private] || front[:path].nil?

    log = `git log -n1 #{filename} 2> /dev/null | grep "Date:"`
    lastmod = log.length > 0 ?
      Date.parse(log.split("\n")[0].split(":",2)[1].strip).strftime('%Y-%m-%d') :
      nil
    xml += "  <url>\n"
    xml += "    <loc>#{h(@@url + front[:path])}</loc>\n" if front[:path]
    xml += "    <lastmod>#{h lastmod}</lastmod>\n" if @@lastmod && lastmod
    xml += "    <changefreq>#{h front[:changefreq]}</changefreq>\n" if front[:changefreq]
    xml += "    <priority>#{h front[:priority]}</priority>\n" if front[:priority]
    xml += "  </url>\n"
  end
  xml += '</urlset>'
  File.open(File.join(@stasis.destination, 'sitemap.xml'), 'w') do |f|
    f.puts(xml)
  end
end