root/platform/tdiary/misc/comment_clean/tdiary-comment-clean2 @ 7648

Revision 7648, 1.7 kB (checked in by machu, 6 years ago)

platform/tdiary/misc/comment_clean: remove debug code

  • Property svn:executable set to *
Line 
1#!/usr/bin/env ruby
2#
3# Copyright (C) 2004 Satoru Takabayashi <satoru@namazu.org>
4# You can redistribute it and/or modify it under GPL2.
5#
6# Modified by machu ( http://www.machu.jp/diary/ )
7# 2008-03-07: adding last-modified filter with -a and -b option
8#
9require 'optparse'
10after_date = Time.at(0)
11before_date = Time.now
12test = false
13
14opt = OptionParser.new
15opt.on('-a AFTER_HOUR') {|v|
16  after_date = Time.at(Time.now - 60 * 60 * v.to_i)
17}
18opt.on('-b BEFORE_HOUR') {|v|
19  before_date = Time.at(Time.now - 60 * 60 * v.to_i)
20}
21opt.on('-t') {|v|
22  test = true
23}
24opt.parse!(ARGV)
25
26puts "Usage: tdiary-comment-clean [-a AFTER_HOUR] [-b BEFORE_HOUR] PATTERN FILE..." if ARGV.length == 0
27pattern = Regexp.new(ARGV.shift)
28file_names = ARGV
29
30class Comment
31  attr_accessor :body, :date
32
33  def initialize
34    @body = ""
35  end
36
37  def <<(body)
38    if body.match(/^Last-Modified: (\d+)$/)
39      @date = Time.at($1.to_i)
40    end
41    @body << body
42  end
43end
44
45deleted_comments = []
46file_names.each {|file_name|
47  i = File.open(file_name)
48  first_line = i.gets
49
50  comments = []
51  comment = Comment.new
52  while line = i.gets
53    if line == ".\n"
54      comments.push(comment)
55      comment = Comment.new
56    else
57      comment << line
58    end
59  end
60  i.close
61
62  tmp_name = "tmp.#{Process.pid}"
63  File.open(tmp_name, "w") {|o|
64    o.print first_line
65    comments.each {|comment|
66      if pattern.match(comment.body) and (before_date > comment.date) and (after_date < comment.date)
67        deleted_comments.push(comment)
68      else
69        o.print comment.body
70        o.puts "."
71      end
72    }
73  }
74  File.rename(file_name, file_name + ".bak") unless test
75  File.rename(tmp_name, file_name) unless test
76}
77
78deleted_comments.each {|comment|
79  print comment.body
80  puts "."
81}
Note: See TracBrowser for help on using the browser.