# coding: utf-8
############################################################################
# Copyright 2009,2010 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 . #
############################################################################
class HTML
attr_accessor :body, :header
attr_reader :relative_dir
def initialize(title, relative_dir = "")
@title = title
@relative_dir = relative_dir
@header = {}
@header["type"] = "text/html"
# @header["type"] = "application/xhtml+xml"
@header["charset"] = "utf-8"
@body = ""
@htmlheader = ''
@css = []
@atom = []
end
def head
ret = <
#{@title}
HEAD
@css = [@css[0]] + @css[1..-1].sort unless @css.empty?
@css.each{|title,href|
titleattr = "title='#{title}'" if title != ""
ret += "\n"
ret += "\n" if title == "print"
}
@atom.each{|href|
ret += "\n"
}
ret += @htmlheader
ret += ""
ret
end
def add_css(href, title = "", default = false)
if default
@css.unshift([title,href])
else
@css << [title,href]
end
end
def add_atom(href)
@atom << href
end
def add_cookie(key,value,path,expiretime)
c = CGI::Cookie.new(key, value)
c.path = path
c.expires = expiretime
@header["cookie"] ||= []
@header["cookie"] << c
end
def add_head_script(file)
add_html_head("")
end
def add_script_file(file)
self << ""
end
def add_script(script)
self << <
SCRIPT
end
def << (bodycontent)
@body += bodycontent.chomp + "\n"
end
def add_html_head(headercontent)
@htmlheader += headercontent.chomp + "\n"
end
def out(cgi)
#FIXME: quick and dirty fix for encoding problem
{
"ö" => "ö",
"ü" => "ü",
"ä" => "ä",
"Ö" => "Ö",
"Ü" => "Ü",
"Ä" => "Ä",
"ß" => "ß",
"–" => "–",
"„" => "„",
"“" => "“",
"”" => "”",
"✔" => "✔",
"✘" => "✘",
"◀" => "◀",
"▶" => "▶",
"✍" => "✍",
"✖" => "✖",
"•" => "•",
"▾" => "▾",
"▴" => "▴"
}.each{|from,to|
@body.gsub!(from,to)
}
# @body.gsub!(/./){|char|
# code = char[0]
# code > 127 ? "#{code};" : char
# }
xmllang = _("xml:lang='en' dir='ltr'")
cgi.out(@header){
<
#{head}
#{@body}
HEAD
}
end
end