Class: Middleman::Robots::Extension
- Inherits:
-
Extension
- Object
- Extension
- Middleman::Robots::Extension
- Defined in:
- lib/middleman-robots/extension.rb
Overview
Robots Extension Class
Create robots.txt when ‘$ middleman build`
Instance Method Summary collapse
- #allow(rule) ⇒ Object
- #disallow(rule) ⇒ Object
-
#initialize(app, options_hash = {}, &block) ⇒ Extension
constructor
A new instance of Extension.
- #rules(rules) ⇒ Object
- #sitemap(path) ⇒ Object
- #user_agent(rule) ⇒ Object
Constructor Details
#initialize(app, options_hash = {}, &block) ⇒ Extension
Returns a new instance of Extension.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/middleman-robots/extension.rb', line 10 def initialize(app, = {}, &block) super build_dir = app.config.build_dir data = rules(.rules) + sitemap(.sitemap) data.gsub!(/\n+$/, "\n") app.after_build do File.open(File.join(build_dir, 'robots.txt'), 'w') do |file| file.puts(data) end logger.info '== middleman-robots: robots.txt created ==' end end |
Instance Method Details
#allow(rule) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/middleman-robots/extension.rb', line 55 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 |
#disallow(rule) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/middleman-robots/extension.rb', line 45 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 |
#rules(rules) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/middleman-robots/extension.rb', line 25 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 |
#sitemap(path) ⇒ Object
65 66 67 |
# File 'lib/middleman-robots/extension.rb', line 65 def sitemap(path) path ? "Sitemap: #{path}" : '' end |
#user_agent(rule) ⇒ Object
39 40 41 42 43 |
# File 'lib/middleman-robots/extension.rb', line 39 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 |