Module: SitemapGen

Defined in:
lib/sitemap_gen.rb,
lib/sitemap_gen/version.rb

Constant Summary collapse

IGNORE_DIRS_REGEX =
/img|cgi-bin|images|css|js/i
VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.csv_data(dir_path, base_url) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sitemap_gen.rb', line 23

def csv_data(dir_path, base_url)
  # If there is a foward slash at the end of dir path then remove it
  #dir_path = dir_path[0..-2] if dir_path[-1] =~ /\//

  # Exit if there is no html files
  html_files = Dir.glob("#{dir_path}/**/*.html")
  exit if html_files.empty?

  data = []
  html_files.each_with_index do |file_path, i|
    next if file_path =~ IGNORE_DIRS_REGEX
    server_pathname = file_path.sub(dir_path, '')
    base_path = File.dirname(server_pathname)
    last_slash = base_path == '/' ? '' : '/'
    data.push({
      id: i + 1,
      #page_title: page_title(file_path),
      url: base_url + base_path + last_slash
    }.merge(dir_levels(server_pathname)))
  end
  data
end

.dir_levels(server_pathname) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/sitemap_gen.rb', line 51

def dir_levels(server_pathname)
  levels = {}
  dirs = server_pathname.split('/')

  # Drop first and last element of dirs array, because they are a empty string and a filename
  dirs[1..-2].each_with_index do |dir, i|
    levels.merge!({"level_#{i + 1}": dir})
  end
  levels
end

.generate_csv(data, save_path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/sitemap_gen.rb', line 12

def generate_csv(data, save_path)
  header = data.inject([]) { |max, row| max.size < row.keys.size ? row.keys : max }
  save_path ||= Dir.pwd
  CSV.open("#{save_path}/sitemap.csv", 'wb') do |csv|
    csv << header
    data.each do |row|
      csv << row.values
    end
  end
end

.page_title(file_path) ⇒ Object



46
47
48
49
# File 'lib/sitemap_gen.rb', line 46

def page_title(file_path)
  html_doc = Nokogiri::HTML(File.read(file_path))
  html_doc.css('head title').first.content
end

.run(dir_path, base_url, save_path = nil) ⇒ Object



8
9
10
# File 'lib/sitemap_gen.rb', line 8

def run(dir_path, base_url, save_path = nil)
  generate_csv(csv_data(dir_path, base_url), save_path)
end