Sha256: 00464f5ae153180017aa4d8314856ba3d8d8b5467808d9ee7316a0727ed4ac1c

Contents?: true

Size: 1.71 KB

Versions: 52

Compression:

Stored size: 1.71 KB

Contents

#!/usr/bin/env ruby
#
# Copyright (C) 2004 Satoru Takabayashi <satoru@namazu.org>
# You can redistribute it and/or modify it under GPL2.
#
# Modified by machu ( http://www.machu.jp/diary/ )
# 2008-03-07: adding last-modified filter with -a and -b option
#
require 'optparse'
after_date = Time.at(0)
before_date = Time.now
test = false

opt = OptionParser.new
opt.on('-a AFTER_HOUR') {|v|
  after_date = Time.at(Time.now - 60 * 60 * v.to_i)
}
opt.on('-b BEFORE_HOUR') {|v|
  before_date = Time.at(Time.now - 60 * 60 * v.to_i)
}
opt.on('-t') {|v|
  test = true
}
opt.parse!(ARGV)

puts "Usage: tdiary-comment-clean [-a AFTER_HOUR] [-b BEFORE_HOUR] PATTERN FILE..." if ARGV.length == 0
pattern = Regexp.new(ARGV.shift)
file_names = ARGV

class Comment
  attr_accessor :body, :date

  def initialize
    @body = ""
  end

  def <<(body)
    if body.match(/^Last-Modified: (\d+)$/)
      @date = Time.at($1.to_i)
    end
    @body << body
  end
end

deleted_comments = []
file_names.each {|file_name|
  i = File.open(file_name)
  first_line = i.gets

  comments = []
  comment = Comment.new
  while line = i.gets
    if line == ".\n"
      comments.push(comment)
      comment = Comment.new
    else
      comment << line
    end
  end
  i.close

  tmp_name = "tmp.#{Process.pid}"
  File.open(tmp_name, "w") {|o|
    o.print first_line
    comments.each {|comment|
      if pattern.match(comment.body) and (before_date > comment.date) and (after_date < comment.date)
        deleted_comments.push(comment)
      else
        o.print comment.body
        o.puts "."
      end
    }
  }
  File.rename(file_name, file_name + ".bak") unless test
  File.rename(tmp_name, file_name) unless test
}

deleted_comments.each {|comment|
  print comment.body
  puts "."
}

Version data entries

52 entries across 46 versions & 2 rubygems

Version Path
tdiary-contrib-5.3.0 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.2.4 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.2.3 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.2.2 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.2.1 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.2.0 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.1.7 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.1.6 util/clean-spam/tdiary-comment-clean2
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-contrib-5.1.4/util/clean-spam/tdiary-comment-clean2
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/bundler/gems/tdiary-contrib-d1e41204db13/util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.1.5 util/clean-spam/tdiary-comment-clean2
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-contrib-5.1.4/util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.1.4 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.1.3 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.1.2 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.1.1 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.1.0 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.0.13 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.0.12 util/clean-spam/tdiary-comment-clean2
tdiary-contrib-5.0.11 util/clean-spam/tdiary-comment-clean2