gollumでは自分で定義したマクロを使うことができる。
某遊園地の待ち時間をNokigiriを使って表示してみた。
マクロの作成
上記サイト通り、Gollum::Macroクラスを継承し、renderメソッドをoverrideしたら完成だ。クラス名がそのままマクロ名となる。ちなみに、Nokogiriはgollumで使っているのでrequireせずともそのまま使える。
class Gollum::Macro::WaitDisney < Gollum::Macro
URL = "http://s.tokyodisneyresort.jp/tdl/atrc_list.htm"
def render(*args)
charset = nil
html = open(URL) do |f|
charset = f.charset # 文字種別を取得
f.read # htmlを読み込んで変数htmlに渡す
end
# htmlをパース(解析)してオブジェクトを生成
doc = Nokogiri::HTML.parse(html, nil, charset)
#
result = "<table><thead><tr>"
result += "<th align=\"center\">アトラクション名</th>"
result += "<th align=\"center\">待ち時間</th>"
result += "<th align=\"center\">更新時間</th>"
result += "</tr></thead><tbody>"
list_wait_atractions( doc, args ).each do |k ,v|
result += "<tr>"
result += "<td align=\"left\">#{k}(#{v[:run]})</td>"
result += "<td align=\"left\">#{v[:wait]}</td>"
result += "<td align=\"left\">#{v[:update]}</td>"
result += "</tr>"
end
result += "</tbody></table>"
result
end
def list_wait_atractions doc, atractions
lists = Hash.new
atractions.each do |href|
doc.xpath("//a[@href=\"#{href}\"]").each do |node|
title = node.xpath('div[@class="about"]/h3').inner_text
run = node.xpath('div[@class="about"]/p[@class="run"]').inner_text
waittime = node.xpath('div[@class="time"]/p[@class="waitTime"]/strong').text
if waittime.nil? || waittime.empty?
waittime = "-"
end
updatetime = node.xpath('p[@class="update"]').text
lists[title] = { wait: waittime, update: updatetime, run: run }
end
end
lists
end
end
使い方
こんな感じでmarkdownに貼り付ける。
<<WaitDisney(
"/tdl/atrc/al_western.html",
"/tdl/atrc/al_carib.html",
"/tdl/atrc/tl_grand.html",
"/tdl/atrc/tl_jets.html",
"/tdl/atrc/tl_monster.html",
"/tdl/atrc/tt_minnie.html",
"/tdl/atrc/tt_toon.html",
"/tdl/greeting/tt_mickey.html",
"/tdl/atrc/wb_omnibus.html",
"/tdl/atrc/fl_philhar.html",
"/tdl/atrc/fl_small.html",
"/tdl/atrc/fl_castle.html",
"/tdl/atrc/fl_alice.html",
"/tdl/atrc/fl_pooh.html",
"/tdl/atrc/fl_pinocchio.html",
"/tdl/atrc/fl_snow.html",
"/tdl/atrc/wl_country.html",
)>>
公式ページが使いづらいと思って、行きたいところだけをリストアップするつもりで作ったが、作っているうちに公式ページに慣れてきてしまって結局ほとんどみなかった。