Ruby - サイトマップ作成 - 特定のディレクトリ以下は詳細を出力しない

 クラウディア
1. 概要
2. .ini ファイルの変更
3. .ini ファイル読み込みモジュール
4. メインモジュール

1. 概要

 一応、前節でモジュールはできあがったのですが・・・。  基本的には、全コンテンツを出力しています。  これを、特定のディレクトリ以下は詳細を出力しないようにしたいと思います。  具体的にいうと、本サイトの場合「FreeBSD 8.4 RELEASE」以下は、公式には更新しませんので、詳細を出力しないようにしたいと思います。

2. .ini ファイルの変更

 .ini ファイルの [sitemap] セクションに下記の変更を加えます。

[sitemap]
fullpath = html 形式のサイトマップのテンプレートファイル名を絶対パス付で指定
exturi =   詳細を出力しない URI をホワイトスペースで区切って指定

3. .ini ファイル読み込みモジュール

=begin

  コンフィグレーションファイルの読込を行う

=end
require('pry')
require('inifile')

class ReadConf
  def initialize()
  end

  def execute(path)
    begin
      parameter = Hash.new                            # ini ファイルから取得するパラメータ
      database  = nil                                 # データベースの定義
      linktag   = nil                                 # 取得すべきリンクを包括しているタグ名
      linkclass = Array.new                           # 目次でリンクを定義しているクラス名
      sitemapext = Array.new                          # サイトマップの詳細除外 URI

      inifile = IniFile.load(path+'/.rssconf')

      if (inifile[:database])                         # [database] セクションがある
        database = inifile[:database]                 # セクションの内容をそのまま取り込む
      end

      parameter[:database] = database                 # データベース定義

      if (inifile[:link])                             # [link] セクションがある
        if (inifile[:link]['tag'])                    # tag アイテムがある
          linktag = inifile[:link]['tag']             # tag = の定義を取得
        end

        if (inifile[:link]['class'])                  # class アイテムがある
          cls = inifile[:link]['class']               # class = の定義を取得
          split = cls.split(/\s/)                     # ホワイトスペースで分割

          split.each do | item |                      # 配列として取り込む
            linkclass.push(item)
          end
        end
      end

      parameter[:linktag]   = linktag                 # リンクタグ定義
      parameter[:linkclass] = linkclass               # リンククラス定義

      if (inifile[:sitemap])                          # [sitemap] セクションがある
        if (inifile[:sitemap]['fullpath'])            # 出力ファイルをフルパスで指定
          parameter[:sitemapfile] = inifile[:sitemap]['fullpath']
        end

        if (inifile[:sitemap]['exturi'])              # exturi アイテムがある
          cls = inifile[:sitemap]['exturi']           # exturi = の定義を取得
          split = cls.split(/\s/)                     # ホワイトスペースで分割

          split.each do | item |                      # 配列として取り込む
            sitemapext.push(item)
          end
        end
      end

      parameter[:sitemapext] = sitemapext             # 除外 URI 定義
    rescue => error
      exeptlog = ExceptLog.new
      exeptlog.execute(self, error)
      exit 1
    ensure
      return parameter
    end
  end
end

 追加したのは 19行(除外 URI の定義の配列宣言)、 52~58行(exturi の定義を取得して配列への入れ直し)、52行(除外 URI 配列をグローバル変数 $paramter[:sitemapext] へ設定)になります。

4. メインモジュール

#!/usr/bin/env ruby
=begin

  RSS 作成で収集した材料を元に .html 形式のサイトマップを作成する

=end

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

require('pry')
require('BaseClass')
require('ExceptLog')
require('ReadConf')

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

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

    @@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
      if (tree == nil)
        return
      end

      @@doc += '<ul>'+"\n"

      tree.each do | parentKey, parentValue |
        if (parentKey.class == String)
          if (contents.has_key?(parentKey))
            self.link=(contents[parentKey])
          end
        end

        if (parentValue == nil)
          next
        end

        $parameter[:sitemapext].each do | exturi |
          if (parentKey == exturi)
            @@doc += "</ul>\n"
            return
          end
        end

        if (parentValue.class == Hash)
          parentValue.each do | chileKey, childValue |
            write(childValue, contents)
          end
        else
          parentValue.each do | child |
            write(child, contents)
          end
        end
      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__))) # コンフィグレーションファイルの読込

  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[:sitemapfile]).open('w') do | file |
    file.puts(sitemap.doc)
  end

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

end

 こちらの変更内容は。  47~52 行を追加して除外 URI で定義しているものに関しては、その配下を出力しないようにしています。  103行は、$parameter のキーが変わっているので変更しました。
earthcar(アースカー)