Module: YARD::SitemapGenerator

Included in:
CLI::Yardoc
Defined in:
lib/yard-sitemap.rb

Class Method Summary collapse

Class Method Details

.generate_sitemap(basedir) ⇒ Object

Generates a sitemap at basedir

Parameters:

  • basedir (String)

    the location where the sitemap should be generated



9
10
11
12
13
14
# File 'lib/yard-sitemap.rb', line 9

def generate_sitemap(basedir)
  sitemap_file = File.join(basedir, 'sitemap.xml')
  File.open(sitemap_file, 'w') do |file|
    file.write(sitemap_contents(basedir))
  end
end

.sitemap_contents(basedir) ⇒ String

Returns the sitemap.xml contents.

Parameters:

  • basedir (String)

    the location where the sitemap should be generated

Returns:

  • (String)

    the sitemap.xml contents



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/yard-sitemap.rb', line 18

def sitemap_contents(basedir)
  data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  data << "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n"
  data << "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"

  Dir.glob(basedir + '/**/*.html').each do |file|
    next unless File.file?(file)
    mtime = File.mtime(file)
    fname = file.sub(/^#{Regexp.quote basedir}\//, '')
    fname = File.join(ENV['SITEMAP_BASEURL'] || 'http://', fname)
    data << "  <url>\n"
    data << "    <loc>#{fname}</loc>\n"
    data << "    <lastmod>#{mtime.iso8601}</lastmod>\n"
    data << "  </url>\n"
  end

  data << "</urlset>\n"
  data
end