class Juli::Command::Sitemap

generate sitemap.html and sitemap_order_by_mtime_DESC.html under $JULI_REPO

Public Class Methods

new() click to toggle source

cache File info

# File lib/juli/command/sitemap.rb, line 9
def initialize
  @file = []
  Dir.chdir(juli_repo){
    Dir.glob('**/*.txt'){|f|
      @file << FileEntry.new(f, File.stat(f).mtime)
    }
  }
end

Public Instance Methods

run(opts) click to toggle source
# File lib/juli/command/sitemap.rb, line 18
def run(opts)
  sitemap_sub('sitemap.html'){|files| files.sort}
  sitemap_sub('sitemap_order_by_mtime_DESC.html'){|files|
    files.sort{|a,b| File.stat(a).mtime <=> File.stat(b).mtime}.reverse
  }
end

Private Instance Methods

sitemap_sub(name, &block) click to toggle source

INPUTS

name

basename without extention for sitemap

block

sort condition

# File lib/juli/command/sitemap.rb, line 29
def sitemap_sub(name, &block)
  Dir.chdir(juli_repo) {
    outdir = File.join(conf['output_top'])
    FileUtils.mkdir(outdir) if !File.directory?(outdir)
    body  = ''
    count = 0
    for textfile in yield(Dir.glob('**/*.txt')) do
      count += 1
      body += sprintf("<tr><td class='num'>%d</td><td><a href='%s'>%s</a></td><td>%s</td></tr>\n",
                  count,
                  textfile.gsub(/.txt$/, conf['ext']),  # url
                  textfile.gsub(/.txt$/, ''),           # label
                  File.stat(textfile).mtime.strftime("%Y/%m/%d %H:%M:%S"))
    end

    title       = I18n.t('sitemap')
    prototype   = 'prototype.js'
    javascript  = 'juli.js'
    stylesheet  = 'juli.css'
    erb         = ERB.new(File.read(find_template(name)))
    File.open(out_filename(name), 'w') do |f|
      f.write(erb.result(binding))
    end
  }
end