Class: Sitemap

Inherits:
Middleman::Extension
  • Object
show all
Defined in:
lib/middleman-sitemap.rb

Overview

Extension namespace

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ Sitemap

Returns a new instance of Sitemap.



9
10
11
12
13
14
15
16
17
18
# File 'lib/middleman-sitemap.rb', line 9

def initialize(app, options_hash={}, &block)
  # Call super to build options from the options_hash
  super

  # Require libraries only when activated
  # require 'necessary/library'

  # set up your extension
  # puts options.my_option
end

Instance Method Details

#after_buildObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/middleman-sitemap.rb', line 20

def after_build
  require 'erb'
  @pages = app.sitemap.resources.find_all{ |p| p.source_file.match(/\.html/) }
  @hostname = options.hostname
  template = Tilt::ERBTemplate.new(File.expand_path(File.join("#{File.dirname(__FILE__)}", "../templates/sitemap.xml.erb")))
  sitemap = template.render(self)
  outfile = File.join(app.build_dir, "sitemap.xml")
  File.open(outfile, 'w') {|f| f.write(sitemap) }
  if options.gzip
    gzip_file(File.read(outfile))
  end
end

#gzip_file(sitemap) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/middleman-sitemap.rb', line 33

def gzip_file(sitemap)
  require 'zlib'
  File.open(File.join(@app.build_dir, "sitemap.xml.gz"), 'wb') do |f|
    gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION)
    gz.write sitemap
    gz.close
  end
end