Skip to content

Instantly share code, notes, and snippets.

@wanabe
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save wanabe/9482090 to your computer and use it in GitHub Desktop.

Select an option

Save wanabe/9482090 to your computer and use it in GitHub Desktop.
ななめ作り
mtime = File.mtime($0) - 1
Dir.chdir(ARGV[0] || File.dirname($0))
loop do
sleep 1
if File.exist?($0) && File.mtime($0) > mtime
mtime = File.mtime($0)
print "starting... "
open($0) do |f|
nil until f.gets =~ /^__END__$/
open("result.log", "w") do |w|
begin
$stdout = $stderr = w
eval f.read, TOPLEVEL_BINDING, $0, f.lineno + 1
mtime = File.mtime($0)
rescue Exception => e
puts e.class, e.message, *e.backtrace
ensure
$stderr = STDERR
$stdout = STDOUT
puts "#{Time.now} done."
end
end
end
end
end
__END__
require 'open-uri'
class Ticket
@table = {}
class << self
def [](num, title, opening = false)
@table[num.to_i] ||= new(num, title, opening)
end
def each
@table.keys.sort.each do |key|
yield @table[key]
end
end
def mails
@table.values.map(&:mails).group_by(&:type)
end
def read
open(url) do |f|
while f.gets =~ /^#(\d+) (.*)/
self[$1, $2]
end
end
end
def url
"https://gist.githubusercontent.com/wanabe/9481537/raw"
#"./ticket.txt" # for debug
end
end
attr_reader :num, :title, :mails, :opening
def initialize(num, title, opening = false)
@num = num
@title = title
@opening = opening
@mails = []
end
def <<(mail)
@mails << mail
end
def to_s
@num
end
end
class Mail
class Index
attr_reader :type, :start, :last, :mails
def initialize(type, n, last = nil)
@start = n
@last = last
@type = type
@mails = []
set_range n
end
def set_range(n)
@n = n
@min = (@n - 1) / 200 * 200 + 1
@max = @min + 199
read
end
def url
"http://blade.nagaokaut.ac.jp/ruby/ruby-#{@type}/#{@min}-#{@max}.shtml"
#"./61201-61400.shtml" # for debug
end
def read
open(url, "r:euc-jp") do |f|
@content = f.read.encode("utf-8", invalid: :replace)
@main = @content[/<pre>(.*)<\/pre>/im, 1]
end
end
def each
loop do
if @last
last = [@last, @max].min
else
last = @main.scan(/<a name="(\d+)">/i).last.first.to_i
end
(@n..last).each do |i|
mail = Mail.new(@type, i)
@mails << mail
yield mail
end
if @max > last || @max == @last
@last = last
break
end
set_range @max + 1
end
end
end
attr_reader :id, :main, :title, :num, :type, :ticket, :opening
def initialize(type, n)
@type = type
@num = n
read
end
def read
open(url, "r:euc-jp") do |f|
@content = f.read.encode("utf-8", undef: :replace, invalid: :replace)
@header, @main = @content.match(/<div id="header">(.*)<pre>(.*)<\/pre>/im)[1, 2]
@subject = @header[/Subject: (.*)<br>/, 1].gsub(/<\/?strong>/, '')
@id, @ticket, @title = @subject.match(/(\[[^\]]*\]) (?:[Rr][Ee]: ?)*(?:\[[^\] ]* - [^\] ]* #(\d+)\])? ?(.*)/)[1, 3]
@from = @header[/From: <strong>(.*)<\/strong>/, 1]
if @ticket
@by = @main[/^.*?( reported)? by (.+)\.$[ \n]*/, 2]
if $1
@opening = true
@main.sub!(/^\n*-- $.*/m, "\n\n")
else
@main.sub!(/----------------------------------------.*/m, '')
end
else
@main << "\n"
end
@main.gsub!(/^/, " ")
end
end
def sender
@by || @from
end
def url
"http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-#{@type}/#{@num}"
#"61279" # for debug
end
def <=>(other)
id <=> other.id
end
end
Ticket.read
indexes = $indexes ||= [
Mail::Index.new(:dev, stamp = 48077),
Mail::Index.new(:core, stamp = 61753),
]
open($0, "r+") do |f|
scr = f.read
indexes.each do |ml|
ml.each do |mail|
ticket = Ticket[mail.ticket, mail.title, mail.opening]
ticket << mail
end
scr.sub!(/(#{ml.type}, stamp = )\d+/) { "#{$1}#{ml.last ? ml.last + 1 : ml.start}" }
end
f.rewind
f.truncate(0)
f.write scr
end
print "ruby-ML ななめ読む ", indexes.map {|idx|
range = case idx.last
when nil, idx.start - 1
"無し"
when idx.start
idx.last.to_s
else
"%d-%d" % [idx.start, idx.last]
end
"[ruby-%s:%s]" % [idx.type, range ]
}.join(" / "), "\n\n"
indexes.each do |ml|
ml.mails.each do |mail|
print "<a href=\"##{mail.type}-#{mail.num}\">#{mail.id}</a> by #{mail.sender}"
print ":<a href=\"##{stamp}-#{mail.ticket}\">##{mail.ticket}</a>" if mail.ticket
puts ""
end
end
Ticket.each do |ticket|
next if ticket.mails.empty?
if ticket.num
puts "", "<h2 id=\"#{stamp}-#{ticket.num}\">",
"<a href=\"https://bugs.ruby-lang.org/issues/#{ticket.num}\">",
" ##{ticket.num}</a>#{ticket.opening ? "(新規)" : ""}", " #{ticket.title}</h2>", ""
else
puts "", "<h2>", " チケット無し</h2>", ""
end
ticket.mails.each do |mail|
id, num, sender, main = mail.id, mail.num, mail.sender, mail.main.chomp
puts "<h5 id=\"#{mail.type}-#{num}\"><a href=\"#{mail.url}\">", "#{id}</a> by #{sender}</h5>", "", main
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment