Sha256: 05838aed283303555345ef42a8431f179cf5b61e8c40533f33c29855141ef3e0

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module Middleman
  module Robots
    class Extension < ::Middleman::Extension
      option :rules, [], 'List of rules about sitemap.xml'
      option :sitemap, false, 'URI of sitemap.xml'

      def initialize(app, options_hash = {}, &block)
        super

        data = rules(options.rules) + sitemap(options.sitemap)
        data.gsub!(/\n+$/, "\n")

        build_dir = app.build_dir
        app.after_build do
          File.open(File.join(build_dir, "robots.txt"), "w") do |file|
            file.puts(data)
          end
          puts "   middleman-robots: robots.txt created"
        end
      end

      def rules(rules)
        return '' if rules.empty?

        data = []
        rules.each do |rule|
          row = []

          if (rule["user-agent"] || rule[:user_agent])
            user_agent = rule[:user_agent] || rule["user-agent"]
            row << "User-Agent: #{user_agent}"
          end

          if (rule[:disallow])
            rule[:disallow].each do |path|
              path = "/" + path unless /^\// =~ path
              row << "Disallow: #{path}"
            end
          end

          if (rule[:allow])
            rule[:allow].each do |path|
              path = "/" + path unless /^\// =~ path
              row << "Allow: #{path}"
            end
          end

          data << row.join("\n") + "\n\n" if row.length > 0
        end

        data.join('')
      end

      def sitemap(path)
        path ? "Sitemap: #{path}" : ""
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
middleman-robots-1.0.0 lib/middleman-robots/extension.rb