lib/middleman-robots/extension.rb in middleman-robots-1.0.0 vs lib/middleman-robots/extension.rb in middleman-robots-1.0.1
- old
+ new
@@ -1,60 +1,70 @@
module Middleman
module Robots
+ # Robots Extension Class
+ #
+ # Create robots.txt when `$ middleman build`
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
+ build_dir = app.build_dir
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.open(File.join(build_dir, 'robots.txt'), 'w') do |file|
file.puts(data)
end
- puts " middleman-robots: robots.txt created"
+ logger.info '== middleman-robots: robots.txt created =='
end
end
def rules(rules)
return '' if rules.empty?
-
data = []
rules.each do |rule|
row = []
+ row << user_agent(rule)
+ row << disallow(rule)
+ row << allow(rule)
+ row.compact!
+ data << row.join("\n") + "\n\n" if row.length > 0
+ end
+ data.join('')
+ end
- if (rule["user-agent"] || rule[:user_agent])
- user_agent = rule[:user_agent] || rule["user-agent"]
- row << "User-Agent: #{user_agent}"
- end
+ def user_agent(rule)
+ return unless rule.key?('user-agent') || rule.key?(:user_agent)
+ user_agent = rule[:user_agent] || rule['user-agent']
+ "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
+ def disallow(rule)
+ return unless rule.key?(:disallow)
+ lines = []
+ rule[:disallow].each do |path|
+ path = File.join('/', path) unless /^\// =~ path
+ lines << "Disallow: #{path}"
end
+ lines
+ end
- data.join('')
+ def allow(rule)
+ return unless rule.key?(:allow)
+ lines = []
+ rule[:allow].each do |path|
+ path = File.join('/' + path) unless /^\// =~ path
+ lines << "Allow: #{path}"
+ end
+ lines
end
def sitemap(path)
- path ? "Sitemap: #{path}" : ""
+ path ? "Sitemap: #{path}" : ''
end
end
end
end
-