Ruby - サイトマップ作成 - モジュール

クラウディア 
1. 概要
2. モジュール

1. 概要

 簡易的なものを作成したので、案外簡単にできました(2018年11月09日)。  小さかったので、ソースファイルは1本です。  なかで require しているソースは、前節のものをそのまま使用しています。

2. モジュール

#!/usr/bin/env ruby
=begin

=end

$LOAD_PATH << File.dirname(File.expand_path(__FILE__))

require('pry')
#require('openssl')
#require('Contents')
require('BaseClass')
require('ExceptLog')
#require('DBaccess')
require('ReadConf')
#require('CheckParent')

class Sitemap < BaseClass
  def initialize
    @@doc = ""
  end

  def link=(contents)
    clone = contents[:document].clone
    clone.slice!("index.html")
    mylink = './' + clone
    #binding.pry

    @@doc += '<li>'
    @@doc += '<a href="'+mylink+'">'
    @@doc += contents[:title]+"("+contents[:modified].strftime("%Y年%m月%d日")+" 更新)"
    @@doc += '</a>'
    @@doc += "</li>\n"
  end

  def write(tree, contents)
    begin
      @@doc += '<ul style="list-style: none;">'+"\n"

      tree.each do | parentKey, parentValue |
        #puts("[#{self.class.name}.#{__method__}][#{sprintf("%3d", __LINE__)}] parentKey=[#{parentKey}]")
        #binding.pry

        if (parentKey.class == String)
          if (contents.has_key?(parentKey))
            self.link=(contents[parentKey])
          end
        end

        if (parentValue == nil)
          next
        end

        if (parentValue.class == Hash)
          parentValue.each do | chileKey, childValue |
            write(childValue, contents)
          end
        else
          parentValue.each do | child |
            write(child, contents)
          end
        end

        #puts("[#{self.class.name}.#{__method__}][#{sprintf("%3d", __LINE__)}] parentKey.class=[#{parentKey.class}]")
      end

      @@doc += "</ul>\n"
    rescue => error
      exeptlog = ExceptLog.new
      exeptlog.execute(self, error)
      exit 1
    end
  end

  def doc
    return @@doc
  end
end

begin
  readconf = ReadConf.new
  $parameter = readconf.execute(File.dirname(File.expand_path(__FILE__))) # コンフィグレーションファイルの読込
  #binding.pry

  tree     = Hash.new
  conArray = Array.new

  Pathname.new(File.dirname(File.expand_path(__FILE__))+'/tree.dmp').open('rb') do | file |
    tree = Marshal.load(file)
  end

  Pathname.new(File.dirname(File.expand_path(__FILE__))+'/contents.dmp').open('rb') do | file |
    conArray = Marshal.load(file)
  end

  contents = Hash.new

  conArray.each do | item |
    contents[item[:uri]] = item
  end

  sitemap = Sitemap.new
  sitemap.write(tree, contents)

  Pathname.new($parameter[:sitemap]['fullpath']).open('w') do | file |
    file.puts(sitemap.doc)
  end

rescue => error
  exeptlog = ExceptLog.new
  exeptlog.execute(self, error)
  exit 1
ensure

end

 全コンテンツを出力してしまいましたが、FreeBSD 10.4 以前の古いコンテンツは最上位のみ出力して、配下を出力しないようにしようと考えています。  もしくはアコーディオンで表示させるべきか?(2018年11月09日)