From 54386017ceacacb2a6990a29bffc7f93982572e4 Mon Sep 17 00:00:00 2001
From: Benjamin Kellermann
Date: Thu, 26 Nov 2009 00:15:10 +0100
Subject: heavy code restructuring, moved common code to dudle.rb
---
access_control.rb | 39 +++++-----------------
charset.rb | 1 -
customize.rb | 46 ++++++++++---------------
delete_poll.rb | 52 +++++++----------------------
dudle.rb | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++
edit_columns.rb | 60 ++++++++-------------------------
help.rb | 34 ++++---------------
history.rb | 50 ++++++---------------------
html.rb | 29 ----------------
invite_participants.rb | 36 ++++----------------
participate.rb | 52 +++++++----------------------
timepollhead.rb | 9 +++++
12 files changed, 189 insertions(+), 310 deletions(-)
create mode 100644 dudle.rb
diff --git a/access_control.rb b/access_control.rb
index 6080452..a40ad43 100755
--- a/access_control.rb
+++ b/access_control.rb
@@ -19,22 +19,11 @@
# along with dudle. If not, see . #
############################################################################
-require "cgi"
-
if __FILE__ == $0
-$cgi = CGI.new
-olddir = File.expand_path(".")
-Dir.chdir("..")
-load "html.rb"
-load "config.rb"
-require "poll"
-require "yaml"
-Dir.chdir(olddir)
+load "../dudle.rb"
-POLL = YAML::load_file("data.yaml").name
-$html = HTML.new("dudle - #{POLL} - Help")
-$html.header["Cache-Control"] = "no-cache"
+$d = Dudle.new("Access Control")
acusers = {}
@@ -66,9 +55,9 @@ HTACCESS
}
VCS.commit("Access Control changed")
unless acusers.empty?
- $html.header["status"] = "REDIRECT"
- $html.header["Cache-Control"] = "no-cache"
- $html.header["Location"] = "access_control.cgi"
+ $d.html.header["status"] = "REDIRECT"
+ $d.html.header["Cache-Control"] = "no-cache"
+ $d.html.header["Location"] = "access_control.cgi"
end
end
def add_to_htdigest(user,password)
@@ -173,19 +162,10 @@ if $cgi.include?("ac_user")
end
end
-unless $html.header["status"] == "REDIRECT"
-
-load "../charset.rb"
-$html.add_css("../dudle.css")
+unless $d.html.header["status"] == "REDIRECT"
-$html << ""
-$html << Dudle::tabs("Access Control")
-$html << <
- #{POLL}
- Change Access Control Settings
-HEAD
+$d.html << "Change Access Control Settings "
if acusers.empty? && $cgi["ac_activate"] != "Activate"
@@ -216,7 +196,7 @@ else
end
-$html << <
@@ -239,8 +219,7 @@ $html << <"
end
-$html.out($cgi)
+$d.out($cgi)
end
diff --git a/charset.rb b/charset.rb
index b305eac..237a8c1 100644
--- a/charset.rb
+++ b/charset.rb
@@ -64,4 +64,3 @@ else
end
UTFCHARS = CGI.escapeHTML("✔✘↞←→↠✍⌧•▾▴")
-$html.add_cookie("utf","true","/",Time.now + (1*60*60*24*365 * (USEUTF ? 1 : -1 )))
diff --git a/customize.rb b/customize.rb
index 5300b1c..8b0aeee 100755
--- a/customize.rb
+++ b/customize.rb
@@ -19,24 +19,16 @@
# along with dudle. If not, see . #
############################################################################
-require "cgi"
if __FILE__ == $0
-$cgi = CGI.new
-load "../html.rb"
-$html = HTML.new("dudle - Customize")
-load "../charset.rb"
+load "../dudle.rb"
-$html.header["Cache-Control"] = "no-cache"
+$d = Dudle.new("Customize")
-$html.add_css("../dudle.css")
-$html << <
-#{Dudle::tabs("Customize")}
-
-
Customize Personal Settings
+$d << <Customize Personal Settings
You need cookies enabled in order to personalize your settings.
END
@@ -65,9 +57,10 @@ end
a = [["Use normal strings","ascii"],
["Use special characters (#{UTFCHARS})","utf", "Use this options if you see the characters in the parenthesis."]]
-$html << <
-Charset
+Charset
#{choosetable("Charset settings",a,USEUTF ? "utf" : "ascii")}
CHARSET
@@ -80,10 +73,10 @@ a = [["default","css=dudle.css"],
css = $cgi.cookies["css"][0]
css = $cgi["css"] if $cgi.include?("css")
css ||= "dudle.css"
-$html.add_cookie("css",css,"/",Time.now + (1*60*60*24*365 * (css == "dudle.css" ? -1 : 1 )))
-$html << <
-Stylesheet
+Stylesheet
#{choosetable("Stylesheet settings",a,"css=#{css}")}
CSS
@@ -91,15 +84,15 @@ CSS
username = $cgi.cookies["username"][0]
if $cgi.include?("delete_username")
- $html.add_cookie("username","","/",Time.now - 1*60*60*24*365)
+ $d.html.add_cookie("username","","/",Time.now - 1*60*60*24*365)
username = nil
elsif $cgi.include?("username")
username = $cgi["username"]
- $html.add_cookie("username",username,"/",Time.now + 1*60*60*24*365)
+ $d.html.add_cookie("username",username,"/",Time.now + 1*60*60*24*365)
end
-$html << <
Default Username
@@ -135,9 +128,9 @@ else
CHARSET
end
-$html << " " if username
+$d.html << " " if username
-$html << <
@@ -145,10 +138,7 @@ $html << <
CHARSET
-$html << ""
-$html << ""
-
-$html.out($cgi)
+$d.out($cgi)
end
diff --git a/delete_poll.rb b/delete_poll.rb
index d2681ed..8a7c41b 100755
--- a/delete_poll.rb
+++ b/delete_poll.rb
@@ -19,44 +19,23 @@
# along with dudle. If not, see . #
############################################################################
+if __FILE__ == $0
+
+load "../dudle.rb"
+$d = Dudle.new("Delete Poll")
+require "ftools"
+
QUESTIONS = ["Yes, I know what I am doing!",
"I hate these stupid entry fields.",
"I am aware of the consequences.",
"Please delete this poll."]
-
-require "cgi"
-require "ftools"
-
-if __FILE__ == $0
-
-$cgi = CGI.new
-
-olddir = File.expand_path(".")
-Dir.chdir("..")
-load "html.rb"
-load "config.rb"
-require "poll"
-require "yaml"
-Dir.chdir(olddir)
-
-POLLNAME = YAML::load_file("data.yaml").name
-POLL = File.basename(File.expand_path("."))
-$html = HTML.new("dudle - #{POLLNAME} - Delete")
-
-$html.header["Cache-Control"] = "no-cache"
-
-$html.add_css("../dudle.css")
-
-$html << ""
-
if $cgi.include?("confirmnumber")
CONFIRM = $cgi["confirmnumber"].to_i
if $cgi["confirm"] == QUESTIONS[CONFIRM]
Dir.chdir("..")
- File.move(POLL, "/tmp/#{POLL}.#{rand(9999999)}")
- $html << <
+ File.move(@d.table.urlsuffix, "/tmp/#{@d.table.urlsuffix}.#{rand(9999999)}")
+ $d.html << <
The poll was deleted successfully!
@@ -71,10 +50,8 @@ if $cgi.include?("confirmnumber")
Search something with Google
-
-
SUCCESS
- $html.out($cgi)
+ $d.out($cgi)
exit
else
hint = <
- #{POLLNAME}
+$d.html << <Delete this Poll
- You want to delete the poll named #{POLLNAME} .
+ You want to delete the poll named #{$d.table.name} .
This is an irreversible action!
If you are sure in what you are doing, please type into the form “#{QUESTIONS[CONFIRM]}”
#{hint}
@@ -117,10 +91,8 @@ $html << <
-
-
"
-$html << Dudle::tabs("Edit Columns")
-
-$html << <
- #{table.name}
- Add and Remove Columns
-TABLE
+$d << "Add and Remove Columns "
# ADD/REMOVE COLUMN
-$html << table.edit_column_htmlform($cgi["editcolumn"],revno)
+$d << $d.table.edit_column_htmlform($cgi["editcolumn"],revno)
h = VCS.history
urevs = h.undorevisions
@@ -120,13 +89,13 @@ if rrevs.min
hidden["Redo"] = " "
end
- $html << <
UNDOREDOREADY
["Undo","Redo"].each{|button|
- $html << <
TD
}
- $html << <
TABLE
-$html.out($cgi)
+$d.out($cgi)
end
diff --git a/dudle.rb b/dudle.rb
new file mode 100644
index 0000000..a94dfcf
--- /dev/null
+++ b/dudle.rb
@@ -0,0 +1,91 @@
+############################################################################
+# Copyright 2009 Benjamin Kellermann #
+# #
+# This file is part of dudle. #
+# #
+# Dudle is free software: you can redistribute it and/or modify it under #
+# the terms of the GNU Affero General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# Dudle is distributed in the hope that it will be useful, but WITHOUT ANY #
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or #
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
+# License for more details. #
+# #
+# You should have received a copy of the GNU Affero General Public License #
+# along with dudle. If not, see . #
+############################################################################
+
+require "yaml"
+require "cgi"
+
+$cgi = CGI.new
+
+olddir = File.expand_path(".")
+Dir.chdir("..")
+require "html"
+require "poll"
+require "config"
+require "charset"
+Dir.chdir(olddir)
+
+class Dudle
+ attr_reader :html, :table, :urlsuffix
+ def Dudle.tabs(active_tab)
+ ret = "
"
+ [["Home",".."],
+ ["",""],
+ ["Poll","."],
+ ["History","history.cgi"],
+ ["Help","help.cgi"],
+ ["",""],
+ ["Edit Columns","edit_columns.cgi"],
+ ["Invite Participants","invite_participants.cgi"],
+ ["Access Control","access_control.cgi"],
+ ["Delete Poll","delete_poll.cgi"],
+ ["",""],
+ ["Customize","customize.cgi"]
+ ].each{|tab,file|
+ case tab
+ when active_tab
+ ret += " #{tab} "
+ when ""
+ ret += " "
+ else
+ ret += " #{tab} "
+ end
+ }
+ ret += " "
+ ret
+ end
+ def initialize(htmltitle, revision=nil)
+ if revision
+ @table = YAML::load(VCS.cat(revision, "data.yaml"))
+ else
+ @table = YAML::load_file("data.yaml")
+ end
+ @urlsuffix = File.basename(File.expand_path("."))
+
+ @html = HTML.new("dudle - #{@table.name} - #{htmltitle}")
+ @html.header["Cache-Control"] = "no-cache"
+ @html.add_css("../dudle.css")
+
+ @html << <
+
+#{Dudle.tabs(htmltitle)}
+
+
#{@table.name}
+HEAD
+ end
+ def out(cgi)
+ @html << ""
+ @html.out(cgi)
+ end
+ def <<(htmlbodytext)
+ @html << htmlbodytext
+ end
+
+end
+
diff --git a/edit_columns.rb b/edit_columns.rb
index 9841bc2..474281c 100755
--- a/edit_columns.rb
+++ b/edit_columns.rb
@@ -19,62 +19,31 @@
# along with dudle. If not, see . #
############################################################################
-require "yaml"
-require "cgi"
-
-
if __FILE__ == $0
-
-$cgi = CGI.new
-
-olddir = File.expand_path(".")
-Dir.chdir("..")
-require "html"
-load "config.rb"
-require "poll"
-Dir.chdir(olddir)
-# BUGFIX for Time.parse, which handles the zone indeterministically
-class << Time
- alias_method :old_parse, :parse
- def Time.parse(date, now=self.now)
- Time.old_parse("2009-10-25 00:30")
- Time.old_parse(date)
- end
-end
+
+load "../dudle.rb"
revbeforeedit = VCS.revno
if $cgi.include?("undo_revision") && $cgi["undo_revision"].to_i < revbeforeedit
undorevision = $cgi["undo_revision"].to_i
- table = YAML::load(VCS.cat(undorevision, "data.yaml"))
+ $d = Dudle.new("Edit Columns",undorevision)
comment = "Reverted Poll"
comment = "Redo changes" if $cgi.include?("redo")
- table.store("#{comment} to version #{undorevision}")
+ $d.table.store("#{comment} to version #{undorevision}")
else
- table = YAML::load_file("data.yaml")
+ $d = Dudle.new("Edit Columns")
end
-table.edit_column($cgi["columnid"],$cgi["new_columnname"],$cgi) if $cgi.include?("new_columnname")
-table.delete_column($cgi["deletecolumn"]) if $cgi.include?("deletecolumn")
+$d.table.edit_column($cgi["columnid"],$cgi["new_columnname"],$cgi) if $cgi.include?("new_columnname")
+$d.table.delete_column($cgi["deletecolumn"]) if $cgi.include?("deletecolumn")
revno = VCS.revno
-$html = HTML.new("dudle - #{table.name} - Edit Columns")
-$html.header["Cache-Control"] = "no-cache"
-load "../charset.rb"
-$html.add_css("../dudle.css")
-
-$html << "
"
-$html.out($cgi)
+$d.out($cgi)
end
diff --git a/help.rb b/help.rb
index 923476d..7935aea 100755
--- a/help.rb
+++ b/help.rb
@@ -19,33 +19,16 @@
# along with dudle. If not, see . #
############################################################################
-require "cgi"
-
if __FILE__ == $0
-$cgi = CGI.new
-olddir = File.expand_path(".")
-Dir.chdir("..")
-load "html.rb"
-load "config.rb"
-require "poll"
-require "yaml"
-Dir.chdir(olddir)
-
-POLL = YAML::load_file("data.yaml").name
-$html = HTML.new("dudle - #{POLL} - Access Control Settings")
-$html.header["Cache-Control"] = "no-cache"
-
-$html.add_css("../dudle.css")
-
-$html << <
-#{Dudle::tabs("Help")}
-
-
#{POLL}
+load "../dudle.rb"
+
+$d = Dudle.new("Help")
+
+$d << <
#{SITEURL}
-Send this link via email...
+Send this link via email...
"
-$html.out($cgi)
+$d.out($cgi)
end
diff --git a/html.rb b/html.rb
index 6164d01..3d55b37 100644
--- a/html.rb
+++ b/html.rb
@@ -80,32 +80,3 @@ HEAD
end
end
-module Dudle
- def Dudle.tabs(active_tab)
- ret = ""
- [["Home",".."],
- ["",""],
- ["Poll","."],
- ["History","history.cgi"],
- ["Help","help.cgi"],
- ["",""],
- ["Edit Columns","edit_columns.cgi"],
- ["Invite Participants","invite_participants.cgi"],
- ["Access Control","access_control.cgi"],
- ["Delete Poll","delete_poll.cgi"],
- ["",""],
- ["Customize","customize.cgi"]
- ].each{|tab,file|
- case tab
- when active_tab
- ret += " #{tab} "
- when ""
- ret += " "
- else
- ret += " #{tab} "
- end
- }
- ret += " "
- ret
- end
-end
diff --git a/invite_participants.rb b/invite_participants.rb
index 6c9204a..abb1ddc 100755
--- a/invite_participants.rb
+++ b/invite_participants.rb
@@ -19,47 +19,23 @@
# along with dudle. If not, see . #
############################################################################
-require "yaml"
-require "cgi"
-
-
if __FILE__ == $0
-$cgi = CGI.new
-
-olddir = File.expand_path(".")
-Dir.chdir("..")
-require "html"
-load "config.rb"
-require "poll"
-Dir.chdir(olddir)
-
+load "../dudle.rb"
-table = YAML::load_file("data.yaml")
+$d = Dudle.new("Invite Participants")
if $cgi.include?("add_participant")
- table.add_participant("",$cgi["add_participant"],{})
+ $d.table.add_participant("",$cgi["add_participant"],{})
end
-$html = HTML.new("dudle - #{table.name} - Invite Participants")
-$html.header["Cache-Control"] = "no-cache"
-load "../charset.rb"
-$html.add_css("../dudle.css")
-
-$html << ""
-$html << Dudle::tabs("Invite Participants")
-
-$html << <
- #{table.name}
+$d << <Invite Participants
TABLE
-$html << "
"
-
-$html.out($cgi)
+$d.out($cgi)
end
diff --git a/participate.rb b/participate.rb
index 6efca03..f085e8f 100755
--- a/participate.rb
+++ b/participate.rb
@@ -19,26 +19,14 @@
# along with dudle. If not, see . #
############################################################################
-require "yaml"
if __FILE__ == $0
-
-require "cgi"
-
-$cgi = CGI.new
-
-olddir = File.expand_path(".")
-Dir.chdir("..")
-require "html"
-require "poll"
-load "config.rb"
-Dir.chdir(olddir)
-
-table = YAML::load_file("data.yaml")
+load "../dudle.rb"
+$d = Dudle.new("Poll")
if $cgi.include?("add_participant")
if $cgi.include?("delete_participant")
- table.delete($cgi["olduser"])
+ $d.table.delete($cgi["olduser"])
else
agreed = {}
$cgi.params.each{|k,v|
@@ -47,43 +35,27 @@ if $cgi.include?("add_participant")
end
}
- table.add_participant($cgi["olduser"],$cgi["add_participant"],agreed)
+ $d.table.add_participant($cgi["olduser"],$cgi["add_participant"],agreed)
end
end
-table.add_comment($cgi["commentname"],$cgi["comment"]) if $cgi["comment"] != ""
-table.delete_comment($cgi["delete_comment"].to_i) if $cgi.include?("delete_comment")
-
-$html = HTML.new("dudle - #{table.name}")
-$html.header["Cache-Control"] = "no-cache"
-load "../charset.rb"
-$html.add_css("../dudle.css")
-$html.add_css("../print.css","print")
+$d.table.add_comment($cgi["commentname"],$cgi["comment"]) if $cgi["comment"] != ""
+$d.table.delete_comment($cgi["delete_comment"].to_i) if $cgi.include?("delete_comment")
-$html.add_atom("atom.cgi") if File.exists?("../atom.rb")
+$d.html.add_css("../print.css","print")
-
-$html << ""
-
-$html << Dudle::tabs("Poll")
-
-$html << <
-HEAD
+$d.html.add_atom("atom.cgi") if File.exists?("../atom.rb")
# TABLE
-$html << <#{table.name}
+$d << <
TABLE
-$html << table.comment_to_html
-
-$html << ""
+$d << $d.table.comment_to_html
-$html.out($cgi)
+$d.out($cgi)
end
diff --git a/timepollhead.rb b/timepollhead.rb
index 4521f26..24958a0 100644
--- a/timepollhead.rb
+++ b/timepollhead.rb
@@ -17,6 +17,15 @@
# along with dudle. If not, see . #
############################################################################
+# BUGFIX for Time.parse, which handles the zone indeterministically
+class << Time
+ alias_method :old_parse, :parse
+ def Time.parse(date, now=self.now)
+ Time.old_parse("2009-10-25 00:30")
+ Time.old_parse(date)
+ end
+end
+
class TimePollHead
class TimeString
attr_reader :date, :time
--
cgit v1.2.3