EOS__
##########################################################
EDIT_FORM_TMPL = <
好きにコメントを編集してください。ただし、あまり他の人のコメントを書き換えることは感心しません。
EOS__
##########################################################
PREV_FORM_TMPL = <
ハムー (check if your post is not spam)
Do not touch this field:
EOS__
##########################################################
module KoDiary
class CommentEditor
def initialize
@cgi = cgi = CGI.new(tag_maker: 'html4', accept_charset: 'Windows-31J')
@mode ,= cgi['mode']
@nospam ,= cgi['nospam']
if @mode == 'after_preview'
load_passed cgi['passed']
else
@yy, = cgi['year']
@mm, = cgi['month']
@dd, = cgi['day']
@body ,= cgi['body']
@name ,= cgi['name']
@presp,= cgi['presp']
@stmp ,= cgi['stmp']
@yy = @yy.to_i
@mm = @mm.to_i
@dd = @dd.to_i
# @body &&= Kconv.tosjis(@body)
# @name &&= Kconv.tosjis(@name)
end
@hm = HtmlManager.new @yy,@mm,@dd
@yy, @mm, @dd = @hm.target_day
end
def decode64 str
str.unpack("m")
end
def encode64 str
[str].pack("m")
end
def load_passed passed
str = Base64.decode64 passed
@yy, @mm, @dd, @body, @name,
@presp, @stmp, @prev_mode = Marshal.load str
end
def make_passed
str = Marshal.dump(
[@yy, @mm, @dd, @body, @name,
@presp, @stmp, @mode]
)
Base64.encode64(str)
end
def exec
case @mode
when 'longadd'
longadd
when 'add'
add
when 'long'
longedit
when 'edit'
edit
when 'set'
set
when 'after_preview'
after_preview
else
show 'sorry,unsupport operation.'
end
end
def show_page file, b
@cgi.out{
ERB.new(File.read(file).force_encoding('Windows-31J')).result(b)
}
end
def fill_tmpl str, b
ERB.new(str).result(b)
end
def longedit
msg = 'please add long comment :).'
prev = @hm.get_diary_page
file = "#{'%04d%02d.html' % [@yy,@mm]}#d#{@dd}"
link = "back"
form = fill_tmpl LONG_FORM_TMPL, binding
show_page 'longedit.html', binding
end
def edit
msg = 'please set comment :).'
prev = @hm.get_diary_page
file = "#{'%04d%02d.html' % [@yy,@mm]}#d#{@dd}"
link = "back"
stmp = @hm.get_comment_date.to_i
form = fill_tmpl EDIT_FORM_TMPL, binding
show_page 'longedit.html', binding
end
def show_preview
msg = 'This is preview of your post. OK?'
prev = @cgi.pre('class' => 'code'){
@body
}
file = "#{'%04d%02d.html' % [@yy,@mm]}#d#{@dd}"
link = "back"
passed = make_passed
form = fill_tmpl PREV_FORM_TMPL, binding
show_page 'longedit.html', binding
end
def check!(flag = {})
unless flag[:ignore_name]
show('name is empty') if @name.length == 0
end
show('body is empty') if @body.length == 0
show("don't write!") if @yy < 2005
end
def longadd
check!
add
end
def add
check!
show_preview
end
def after_preview
unless @nospam
show 'spam :(' #)
end
begin
case @prev_mode
when 'add', 'longadd'
@body = ' ' + @body if @presp != 'no'
@hm.add_comment(@name, @body)
when 'set'
@hm.set_comment(@body, @stmp)
else
raise "unknow prev mode"
end
rescue => e
return show(e.to_s + e.backtrace.inspect)
end
show
end
def set
check! :ignore_name => true
show_preview
end
def show msg = nil
file = "#{'%04d%02d.html' % [@yy,@mm]}#cs-#{@dd}"
if !msg
msg = 'writing comment succeed.'
meta = ""
else
meta = ''
end
msg = CGI::escapeHTML(msg)
link = "back"
body = open('edit.html','r'){|f| f.read}.force_encoding('Windows-31J')
body = body.sub(/\<\\>/,msg).
sub(/\<\\>/,link).
sub(/\<\\>/,meta)
#
@cgi.out{body}
raise ShowException
end
def show_exception e
@cgi.out{
@cgi.html{
@cgi.body{
@cgi.pre{
CGI.escapeHTML(
e.to_s +
"\n-- \n" +
e.backtrace.join("\n")
)
}
}
}
}
end
end
end
class ShowException
end
begin
ce = KoDiary::CommentEditor.new
ce.exec
rescue ShowException
#
rescue Exception => e
raise
# ce.show_exception e
end