Class: Middleman::Robots::Extension

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-robots/extension.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Extension.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/middleman-robots/extension.rb', line 7

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

Instance Method Details

#rules(rules) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/middleman-robots/extension.rb', line 22

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

#sitemap(path) ⇒ Object



54
55
56
# File 'lib/middleman-robots/extension.rb', line 54

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