HTMLファイルを集めてindex.htmlを作る
カレントディレクトリにあるHTMLファイルを集めてindex.htmlを作るスクリプトです。
HTMLであれこれ実験した後、ちらばったHTMLをとりまとめるために作りました。
- 出力するindex.htmlは手抜き
- index.htmlは毎回上書き
- ファイル操作といえばPathnameを使う…といいつつ使っているのはPathname.globだけですが
require 'pathname' INDEX = "index.html" open(INDEX, "w") do |out| out.puts "<html><body><ul>" Pathname.glob("*.html").sort.each do |file| next if file.to_s == INDEX out.puts %Q(<li><a href="#{file}">#{file}</a></li>) end out.puts "</ul></body></html>" end
- 失敗したこと: fileに入っているのがPathnameオブジェクトだということを忘れてStringオブジェクトである"index.html"と==で比較しようとした。
追記:id:znzさんのアドバイスにより、INDEXをやめて、最初からPathnameオブジェクトとしてのindex.htmlを作るようにしました。ありがとうございます♪
require 'pathname' index = Pathname.new("index.html") index.open("w") do |out| out.puts "<html><body><ul>" Pathname.glob("*.html").sort.each do |file| next if file == index out.puts %Q(<li><a href="#{file}">#{file}</a></li>) end out.puts "</ul></body></html>" end