Sha256: f894f4675da82b51758cd279375bfc48acb2bed4cee2c6a4ee7aa7bf25d5ec09

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

require 'fileutils'

module Bunto
  class PageWithoutAFile < Page
    def read_yaml(*)
      @data ||= {}
    end
  end

  class BuntoSitemap < Bunto::Generator
    safe true
    priority :lowest

    # Main plugin action, called by Bunto-core
    def generate(site)
      @site = site
      @site.config["time"]         = Time.new
      @site.config["html_files"]   = html_files.map(&:to_liquid)
      unless sitemap_exists?
        write
        @site.keep_files ||= []
        @site.keep_files << "sitemap.xml"
      end
    end

    HTML_EXTENSIONS = %W(
      .html
      .xhtml
      .htm
    ).freeze

    # Array of all non-bunto site files with an HTML extension
    def html_files
      @site.static_files.select { |file| HTML_EXTENSIONS.include? file.extname }
    end

    # Path to sitemap.xml template file
    def source_path
      File.expand_path "sitemap.xml", File.dirname(__FILE__)
    end

    # Destination for sitemap.xml file within the site source directory
    def destination_path
      if @site.respond_to?(:in_dest_dir)
        @site.in_dest_dir("sitemap.xml")
      else
        Bunto.sanitized_path(@site.dest, "sitemap.xml")
      end
    end

    # copy sitemap template from source to destination
    def write
      FileUtils.mkdir_p File.dirname(destination_path)
      File.open(destination_path, 'w') { |f| f.write(sitemap_content) }
    end

    def sitemap_content
      site_map = PageWithoutAFile.new(@site, File.dirname(__FILE__), "", "sitemap.xml")
      site_map.content = File.read(source_path)
      site_map.data["layout"] = nil
      site_map.render({}, @site.site_payload)
      site_map.output.gsub(/\s{2,}/, "\n")
    end

    # Checks if a sitemap already exists in the site source
    def sitemap_exists?
      if @site.respond_to?(:in_source_dir)
        File.exist? @site.in_source_dir("sitemap.xml")
      else
        File.exist? Bunto.sanitized_path(@site.source, "sitemap.xml")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bunto-sitemap-2.0.0 lib/bunto-sitemap.rb
bunto-sitemap-1.0.0 lib/bunto-sitemap.rb