Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_formater_html.py
blob: 8db6de721a16fe37a972e4ff931ad9c05594e587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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()