#! /usr/bin/env ruby # coding: utf-8 # # 設定は ~/.diary.yaml に書かれる。 # 一部コマンドラインオプションで与えることができるが、 # その場合はコマンドラインオプションで指定した値を優先する。 # # options # -n date : the last day in latest.html. e.g., -n 2011/10/1 . # Default value is the present day. # -d days : the number of date included in latest.html. e.g., -d 30 . # Default value is 60. SOURCE = "/home/ippei/doc/website/ur/diary" TARGET = "/home/ippei/doc/website/ur/diary" DAYS = 60 SITE_NAME = "水上都市ウル" DIARY_TITLE = "きづき(日記):最新" DIARY_PREFACE = "
きづき、またかんがえたことをしるす (いっぺい)
\n

[トップページへ]\n" FOOTER = "


\n

\n 最新日記の各日付が当該月の日記へのリンクになっています。\n さらに過去のものはトップページから辿れます。\n

\n | トップページ |\n" require "optparse" require "yaml" require "pp" require "date" require "time" require "array/selectindices.rb" class DiaryLatest def initialize(options) @source_directory = SOURCE @source_files = source_files if @source_files.size == 0 raise "No month diary files in #{options['source']}. Abort." end @site_name = SITE_NAME @diary_title = DIARY_TITLE @diary_preface = DIARY_PREFACE @footer = FOOTER end # Make latest.html. # An argument 'newest_date' indicates the last day in latest.html. # An argument 'days' indicates the number of date included in latest.html. # Scanning all files named /^\d{4}/\d{2}.html$/ like '2011/09.html', def generate(io, days, newest_date = Date.now, now = Time.now) year = now.year month = now.month # head strs = [ "", "", "", " ", " ", " ", " ", " #{@site_name}/#{@diary_title}\n", " ", " ", " ", "", "", "

", #"\n", #"\n", #"\n", #" #{@site_name}/#{@diary_title}\n", #" \n", #" \n", #"\n", #"\n", #"
\n", #"
\n", #"

#{@diary_title}

\n", #"#{@diary_preface}\n", #"\n", #"
Last modified : #{now.to_s}
\n", #"\n" ] # articles articles = {} @source_files.reverse.each do |filename| #pp filename; exit month_diary = MonthDiary.new(filename) month_diary.articles.each do |date, article| next if (date > newest_date) articles[date] = article end break if ( articles.size >= days ) end #pp articles; exit articles.keys.sort.reverse[0..(days-1)].each do |date| yyyy = sprintf("%04d", date.year) mm = sprintf("%02d", date.month) articles[date].each do |line| line.gsub!("' io.puts '
' io.puts '' io.puts '' end private # 2011/10.html のように、年/月.html 形式のファイルをソート済状態で返す。 def source_files html_files = Dir.glob( "#{@source_directory}/????/??.html") html_files.select! do |file| file =~ /#{@source_directory}\/(\d\d\d\d)\/(\d\d)\.html/ yyyy = $1.to_i mm = $2.to_i if (yyyy == 2015 && 10 <= mm ) true elsif 2016 <= yyyy true else false end end return html_files.sort end end class MonthDiary attr_reader :articles # 月別日記を読んで、日付の Date クラスインスタンスを鍵、 # 日付の記事全体を値とするハッシュを返す。 # 日付行がなければ空ハッシュ def initialize(filename) #pp filename; exit @articles = Hash.new lines = File.open(filename, "r").readlines indices = lines.select_indices { |line| line =~ /^\

\n" } #pp indices; exit (indices.size - 1).times do |i| i_start = indices[i] i_end = indices[i+1]-1 #pp lines[i_start] /(\d{4})年(\d{2})月(\d{2})日/ =~ lines[i_start] @articles[Date.new($1.to_i, $2.to_i, $3.to_i)] = lines[i_start..i_end] end #pp @articles; exit end end Options = Hash.new ## option analysis opt = OptionParser.new opt.on( "-n date", "--newest_date=date", "Newest date on latest.html"){ |v| Options['newest_date'] = DateTime.parse(v).to_date } opt.on( "-d days", "--days=num", "Number of days on latest.html"){ |v| Options['days'] = v.to_i } opt.parse!(ARGV) Options['newest_date'] ||= Date.today ld = DiaryLatest.new( Options ) io = File.open("#{TARGET}/latest.html", "w") ld.generate( io, DAYS, Options['newest_date'] ) puts "#{TARGET}/latest.html was generated from #{DAYS} days upto #{Options['newest_date']}."