Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_formater_html.py
diff options
context:
space:
mode:
Diffstat (limited to 'ep_formater_html.py')
-rw-r--r--ep_formater_html.py345
1 files changed, 345 insertions, 0 deletions
diff --git a/ep_formater_html.py b/ep_formater_html.py
new file mode 100644
index 0000000..8db6de7
--- /dev/null
+++ b/ep_formater_html.py
@@ -0,0 +1,345 @@
+# coding: UTF8
+# Copyright 2009 Thomas Jourdan
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import os
+import sys
+import ka_debug
+import exon_color
+import exon_position
+import exon_direction
+
+ICON_WIDTH = ICON_HEIGHT = 48
+
+class HtmlFormater(object):
+ """HtmlFormater
+ inv: self._indent >= 0
+ """
+
+ generator = u'Minimal Kandid'
+
+ def __init__(self, base_name, unique_id, base_folder):
+ """Constructor for HTML formater."""
+ self._base_name = base_name.replace(' ', '_')
+ self.unique_id = unique_id
+ self._base_folder = base_folder.replace(' ', '_')
+ self._indent = 0
+ self._page = u''
+ self.id_count = 0
+# self.strategy = {}
+ self._header_occured = False # debugging only
+ self._footer_occured = False # debugging only
+ file_path = os.path.join(base_folder, unique_id)
+ try:
+ os.mkdir(file_path)
+ except:
+ ka_debug.err('creating directory [%s] [%s] [%s]' % \
+ (file_path, sys.exc_info()[0], sys.exc_info()[1]))
+
+
+ def _escape(self, text):
+ utext = unicode(text)
+ quoted = u''
+ for uchar in utext:
+ if uchar in [u'<']:
+ quoted += u'&lt;'
+ elif uchar in [u'&']:
+ quoted += u'&amp;'
+ else:
+ quoted += uchar
+ return quoted
+
+ def _append(self, lines):
+ indenting = u' '*(2*self._indent)
+ for line in lines:
+ self._page += indenting + line + u'\n'
+
+ def _append_escaped(self, text):
+ self._page += self._escape(text)
+
+ def _get_id(self):
+ self.id_count += 1
+ return u'id_' + unicode(self.id_count)
+
+ def get_pathname(self, extension, postfix=None):
+ return os.path.join(self._base_folder,
+ self.unique_id,
+ self.get_filename(extension, postfix))
+
+ def get_filename(self, extension, postfix=None):
+ postfix = '' if postfix is None else '_' + postfix
+ return self._base_name + postfix + '.' + extension
+
+ def header(self, title):
+ """
+ pre: not self._header_occured
+ pre: not self._footer_occured
+ """
+ self._append( ( \
+ u'<?xml version="1.0" encoding="UTF-8"?>',
+ u'<?xml-stylesheet href="treestyles.css" type="text/css"?>',
+ u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"',
+ u' "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
+ u'',
+ u'<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">',
+ u'<head>',
+ u' <meta name="generator" content="' + HtmlFormater.generator +'" />',
+ u'',
+ u' <title>' + title + u'</title>',
+ u' <link rel="stylesheet" href="' + self._base_name + u'_files/treestyles.css" type="text/css" />',
+ u' <script type="text/javascript" src="' + self._base_name + u'_files/marktree.js">',
+ u'//<![CDATA[',
+ u'',
+ u' //]]>',
+ u' </script>',
+ u'</head>',
+ u'',
+ u'<body>',
+ u' <div class="basetop">',
+ u' <a href="#" onclick="expandAll(document.getElementById(\'base\'))">Expand</a> -',
+ u' <a href="#" onclick="collapseAll(document.getElementById(\'base\'))">Collapse</a>',
+ u' </div>',
+ u'',
+ u' <div id="base" class="basetext">',
+ u'',
+ u' <ul>',
+ ) )
+ self._header_occured = True
+
+ def footer(self):
+ """
+ pre: self._indent == 0
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ self._append( ( \
+ u' </ul>',
+ u' </div>',
+ u'</body>',
+ u'</html>',
+ ) )
+ self._footer_occured = True
+
+ def _begin_list_item(self, identification):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ self._append( ( \
+ u' <li class="basic" style="" id="id_' + identification + u'" >',
+ u' <span style="">',
+ ) )
+
+ def _end_list_item(self):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ self._append( ( \
+ u' </span>',
+ u' </li>',
+ ) )
+
+ def _image_item(self, filename, width, height, description, newline=True):
+ if newline:
+ self._append( ( \
+ u' <br />',
+ ) )
+ self._append( ( \
+ u' <img src="' + filename + u'"' \
+ + u' width="' + unicode(width) + u'"' \
+ + u' height="' + unicode(height) + u'"' \
+ + u' alt="' + description + u'"'
+ + u' title="' + description + u'" border="1" />',
+ ) )
+
+ def begin_list(self, text):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ self._indent += 1
+ self._append( ( \
+ u' <li class="col" style="" id="' + self._get_id() + u'">',
+ self._escape(text),
+ u' <ul class="subexp">',
+ ) )
+
+ def end_list(self):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ self._append( ( \
+ u' </ul>',
+ u' </li>',
+ ) )
+ self._indent -= 1
+
+ def text_item(self, text):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ self._begin_list_item(self._get_id())
+ self._append_escaped(text)
+ self._end_list_item()
+
+ def text_list(self, title, text_list):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ self._begin_list_item(self._get_id())
+ self._append_escaped(title)
+ for text in text_list:
+ self._append_escaped(text + u', ')
+ self._end_list_item()
+
+ def color_item(self, color, text, alfa=True):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ identification = self._get_id()
+ pathname = self.get_pathname('png', postfix=identification)
+ filename = self.get_filename('png', postfix=identification)
+ surface = exon_color.Color.make_icon(color.rgba, alfa, ICON_WIDTH, ICON_HEIGHT)
+ surface.write_to_png(pathname)
+
+ description = color.explain(alfa)
+ self._begin_list_item(identification)
+ self._append_escaped(text)
+ self._image_item(filename, ICON_WIDTH, ICON_HEIGHT, description)
+ self._end_list_item()
+
+ def alfa_item(self, alfa, text):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ identification = self._get_id()
+ pathname = self.get_pathname('png', postfix=identification)
+ filename = self.get_filename('png', postfix=identification)
+ surface = exon_color.Color.make_icon((1.0, 1.0, 1.0, alfa), True, ICON_WIDTH, ICON_HEIGHT)
+ surface.write_to_png(pathname)
+
+ description = '%d%% opaque' % (100*alfa)
+ self._begin_list_item(self._get_id())
+ self._append_escaped(text)
+ self._image_item(filename, ICON_WIDTH, ICON_HEIGHT, description)
+ self._end_list_item()
+
+ def color_array(self, color_list, text, alfa=True):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ self._begin_list_item(self._get_id())
+ self._append_escaped(text)
+ self._append( (u' <br />',) )
+ for color in color_list:
+ identification = self._get_id()
+ pathname = self.get_pathname('png', postfix=identification)
+ filename = self.get_filename('png', postfix=identification)
+ surface = color.make_icon(color.rgba, alfa, ICON_WIDTH, ICON_HEIGHT)
+ surface.write_to_png(pathname)
+ description = color.explain(alfa)
+ self._image_item(filename, ICON_WIDTH, ICON_HEIGHT, \
+ description, newline=False)
+ self._end_list_item()
+
+ def position_item(self, position, text):
+ self.position_array([position], text)
+
+ def position_array(self, position_list, text):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ pre: len(position_list) >= 0
+ """
+ description = u'['
+ for position in position_list:
+ description += position.explain() + u'; '
+ description += u']'
+
+ self._begin_list_item(self._get_id())
+ self._append_escaped(text + u' ' + description)
+ identification = self._get_id()
+ pathname = self.get_pathname('png', postfix=identification)
+ filename = self.get_filename('png', postfix=identification)
+ surface = exon_position.Position.make_icon(position_list, ICON_WIDTH, ICON_HEIGHT)
+ surface.write_to_png(pathname)
+ self._image_item(filename, ICON_WIDTH, ICON_HEIGHT, description)
+ self._end_list_item()
+
+ def direction_item(self, direction, text):
+ self.direction_array([direction], text)
+
+ def direction_array(self, direction_list, text):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ pre: len(direction_list) >= 0
+ """
+ description = u'['
+ for direction in direction_list:
+ description += direction.explain() + u'; '
+ description += u']'
+
+ self._begin_list_item(self._get_id())
+ self._append_escaped(text + u' ' + description)
+ identification = self._get_id()
+ pathname = self.get_pathname('png', postfix=identification)
+ filename = self.get_filename('png', postfix=identification)
+ surface = exon_direction.Direction.make_icon(direction_list, ICON_WIDTH, ICON_HEIGHT)
+ surface.write_to_png(pathname)
+ self._image_item(filename, ICON_WIDTH, ICON_HEIGHT, description)
+ self._end_list_item()
+
+ def surface_item(self, surface, text, description):
+ """
+ pre: self._header_occured
+ pre: not self._footer_occured
+ """
+ identification = self._get_id()
+ pathname = self.get_pathname('png', postfix=identification)
+ filename = self.get_filename('png', postfix=identification)
+ surface.write_to_png(pathname)
+ width = surface.get_width()
+ height = surface.get_height()
+
+ self._begin_list_item(self._get_id())
+ self._append_escaped(text)
+ self._image_item(filename, width, height, description)
+ self._end_list_item()
+
+ def write_html_file(self, file_path):
+ """
+ pre: self._header_occured
+ pre: self._footer_occured
+ pre: self._indent == 0
+ """
+ out_file = None
+ try:
+ out_file = open(file_path, 'w')
+ out_file.write(self._page.encode('utf-8'))
+ except:
+ ka_debug.err('failed writing [%s] [%s] [%s]' % \
+ (file_path, sys.exc_info()[0], sys.exc_info()[1]))
+ finally:
+ if out_file:
+ out_file.close()