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
|
'''
Task Coach - Your friendly task manager
Copyright (C) 2004-2014 Task Coach developers <developers@taskcoach.org>
Task Coach 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.
Task Coach 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, see <http://www.gnu.org/licenses/>.
'''
import wx, cgi, StringIO
from taskcoachlib.domain import task
# pylint: disable=W0142
css = '''
body {
color: #333;
background-color: white;
font: 11px verdana, arial, helvetica, sans-serif;
}
/* Styles for the title and table caption */
h1, caption {
text-align: center;
font-size: 18px;
font-weight: 900;
color: #778;
}
/* Styles for the whole table */
#table {
border-collapse: collapse;
border: 2px solid #ebedff;
margin: 10px;
padding: 0;
}
/* Styles for the header row */
.header {
font: bold 12px/14px verdana, arial, helvetica, sans-serif;
color: #07a;
background-color: #ebedff;
}
/* Mark the column that is sorted on */
#sorted {
text-decoration: underline;
}
/* Styles for a specific column */
.subject {
font-weight: bold;
}
/* Styles for regular table cells */
td {
padding: 5px;
border: 2px solid #ebedff;
}
/* Styles for table header cells */
th {
padding: 5px;
border: 2px solid #ebedff;
}
'''
def viewer2html(viewer, settings, cssFilename=None, selectionOnly=False, columns=None):
converter = Viewer2HTMLConverter(viewer, settings)
columns = columns or viewer.visibleColumns()
return converter(cssFilename, columns, selectionOnly)
class Viewer2HTMLConverter(object):
''' Class to convert the visible contents of a viewer into HTML.'''
docType = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
metaTag = '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'
cssLink = '<link href="%s" rel="stylesheet" type="text/css" media="screen">'
def __init__(self, viewer, settings):
super(Viewer2HTMLConverter, self).__init__()
self.viewer = viewer
self.settings = settings
self.count = 0
def __call__(self, cssFilename, columns, selectionOnly):
''' Create an HTML document. '''
lines = [self.docType] + self.html(cssFilename, columns, selectionOnly) + ['']
return '\n'.join(lines), self.count
def html(self, cssFilename, columns, selectionOnly, level=0):
''' Returns all HTML, consisting of header and body. '''
printing = not cssFilename
htmlContent = self.htmlHeader(cssFilename, level+1) + \
self.htmlBody(columns, selectionOnly, printing, level+1)
return self.wrap(htmlContent, 'html', level)
def htmlHeader(self, cssFilename, level):
''' Return the HTML header <head>. '''
htmlHeaderContent = self.htmlHeaderContent(cssFilename, level+1)
return self.wrap(htmlHeaderContent, 'head', level)
def htmlHeaderContent(self, cssFilename, level):
''' Returns the HTML header section, containing meta tag, title, and
optional link to a CSS stylesheet. '''
htmlHeaderContent = [self.indent(self.metaTag, level),
self.wrap(self.viewer.title(), 'title', level, oneLine=True)] + \
self.style(level, not cssFilename)
if cssFilename:
htmlHeaderContent.append(self.indent(self.cssLink%cssFilename, level))
return htmlHeaderContent
def style(self, level, includeAllCSS):
''' Add a style section that contains the alignment for the columns. If
there is no external CSS file, we include all CSS style information
in a HTML style section. '''
visibleColumns = self.viewer.visibleColumns()
columnAlignments = [{wx.LIST_FORMAT_LEFT: 'left',
wx.LIST_FORMAT_CENTRE: 'center',
wx.LIST_FORMAT_RIGHT: 'right'}[column.alignment()]
for column in visibleColumns]
styleContent = []
for column, alignment in zip(visibleColumns, columnAlignments):
columnStyle = self.indent('.%s {text-align: %s}'%(column.name(), alignment), level+1)
styleContent.append(columnStyle)
if self.viewer.isShowingTasks():
for status in task.Task.possibleStatuses():
statusColor = task.Task.fgColorForStatus(status)
statusColor = self.cssColorSyntax(statusColor)
statusStyle = '.%s {color: %s}'%(status, statusColor)
styleContent.append(self.indent(statusStyle, level+1))
if includeAllCSS:
styleContent.extend([self.indent(line, level+1) for line in css.split('\n')])
return self.wrap(styleContent, 'style', level, type='text/css')
def htmlBody(self, columns, selectionOnly, printing, level):
''' Returns the HTML body section, containing one table with all
visible data. '''
htmlBodyContent = []
if printing:
htmlBodyContent.append(self.wrap(self.viewer.title(), 'h1', level,
oneLine=True))
htmlBodyContent.extend(self.table(columns, selectionOnly, printing, level+1))
return self.wrap(htmlBodyContent, 'body', level)
def table(self, columns, selectionOnly, printing, level):
''' Returns the table, consisting of caption, table header and table
body. '''
tableContent = [] if printing else [self.tableCaption(level+1)]
tableContent.extend(self.tableHeader(columns, printing, level+1) + \
self.tableBody(columns, selectionOnly,
printing, level+1))
attributes = dict(id='table')
if printing:
attributes['border'] = '1'
return self.wrap(tableContent, 'table', level, **attributes)
def tableCaption(self, level):
''' Returns the table caption, based on the viewer title. '''
return self.wrap(self.viewer.title(), 'caption', level, oneLine=True)
def tableHeader(self, columns, printing, level):
''' Returns the table header section <thead> containing the header
row with the column headers. '''
tableHeaderContent = self.headerRow(columns, printing, level+1)
return self.wrap(tableHeaderContent, 'thead', level)
def headerRow(self, columns, printing, level):
''' Returns the header row <tr> for the table. '''
headerRowContent = []
for column in columns:
headerRowContent.append(self.headerCell(column, printing, level+1))
return self.wrap(headerRowContent, 'tr', level, **{'class': 'header'})
def headerCell(self, column, printing, level):
''' Returns a table header <th> for the specific column. '''
header = column.header() or ' '
name = column.name()
attributes = {'scope': 'col', 'class': name}
if self.viewer.isSortable() and self.viewer.isSortedBy(name):
attributes['id'] = 'sorted'
if printing:
header = self.wrap(header, 'u', level+1, oneLine=True)
return self.wrap(header, 'th', level, oneLine=True, **attributes)
def tableBody(self, columns, selectionOnly, printing, level):
''' Returns the table body <tbody>. '''
tree = self.viewer.isTreeViewer()
self.count = 0
tableBodyContent = []
for item in self.viewer.visibleItems():
if selectionOnly and not self.viewer.isselected(item):
continue
self.count += 1
tableBodyContent.extend(self.bodyRow(item, columns, tree,
printing, level+1))
return self.wrap(tableBodyContent, 'tbody', level)
def bodyRow(self, item, columns, tree, printing, level):
''' Returns a <tr> containing the values of item for the
visibleColumns. '''
bodyRowContent = []
attributes = dict()
for column in columns:
renderedItem = self.render(item, column, indent=not bodyRowContent and tree)
if printing:
itemColor = item.foregroundColor(recursive=True)
if itemColor:
itemColor = self.cssColorSyntax(itemColor)
renderedItem = self.wrap(renderedItem, 'font', level+1,
color=itemColor, oneLine=True)
bodyRowContent.append(self.bodyCell(renderedItem, column, printing, level+1))
attributes.update(self.bodyRowBgColor(item, printing))
if not printing:
attributes.update(self.bodyRowFgColor(item))
return self.wrap(bodyRowContent, 'tr', level, **attributes)
def bodyRowBgColor(self, item, printing):
''' Determine the background color for the item. Returns a CSS style
specification or a HTML style specification when printing. '''
bgColor = item.backgroundColor(recursive=True)
if bgColor and bgColor != wx.WHITE:
bgColor = self.cssColorSyntax(bgColor)
else:
return dict()
return dict(bgcolor=bgColor) if printing else \
dict(style='background: %s'%bgColor)
def bodyRowFgColor(self, item):
''' Determine the foreground color for the item. Returns a CSS style
specification. '''
if self.viewer.isShowingTasks():
return {'class': item.status().statusString}
else:
return dict()
def bodyCell(self, item, column, printing, level):
''' Return a <td> for the item/column combination. '''
attributes = {'class': column.name()}
if printing and column.alignment() == wx.LIST_FORMAT_RIGHT:
attributes['align'] = 'right'
return self.wrap(item, 'td', level, oneLine=True, **attributes)
@classmethod
def wrap(class_, lines, tagName, level, oneLine=False, **attributes):
''' Wrap one or more lines with <tagName [optional attributes]> and
</tagName>. '''
if attributes:
attributes = ' ' + ' '.join(sorted('%s="%s"'%(key, value) for key, value in attributes.iteritems()))
else:
attributes = ''
openTag = '<%s%s>'%(tagName, attributes)
closeTag = '</%s>'%tagName
if oneLine:
return class_.indent(openTag + lines + closeTag, level)
else:
return [class_.indent(openTag, level)] + \
lines + \
[class_.indent(closeTag, level)]
@staticmethod
def indent(htmlText, level=0):
''' Indent the htmlText with spaces according to the level, so that
the resulting HTML looks nicely indented. '''
return ' ' * level + htmlText
@classmethod
def cssColorSyntax(class_, wxColor):
''' Translate the wx-color, either a wx.Colour instance or a tuple,
into CSS syntax. '''
try:
return wxColor.GetAsString(wx.C2S_HTML_SYNTAX)
except AttributeError: # color is a tuple
return class_.cssColorSyntax(wx.Colour(*wxColor))
@staticmethod
def render(item, column, indent=False):
''' Render the item based on the column, escape HTML and indent
the item with non-breaking spaces, if indent == True. '''
# Escape the rendered item and then replace newlines with <br>.
if column.name() == 'notes':
def renderNotes(notes):
bf = StringIO.StringIO()
for note in sorted(notes, key=lambda note: note.subject()):
bf.write('<p>\n')
bf.write(cgi.escape(note.subject()))
bf.write(u'<br />\n')
bf.write(cgi.escape(note.description()))
bf.write('</p>\n')
if note.children():
bf.write(u'<div style="padding-left: 20px;">\n')
bf.write(renderNotes(note.children()))
bf.write(u'</div>\n')
return bf.getvalue()
return renderNotes(item.notes())
elif column.name() == 'attachments':
return u'<br />'.join(map(cgi.escape, sorted([attachment.subject() for attachment in item.attachments()])))
renderedItem = cgi.escape(column.render(item,
humanReadable=False)).replace('\n', '<br>')
if indent:
# Indent the subject with whitespace
renderedItem = ' ' * len(item.ancestors()) * 3 + renderedItem
if not renderedItem:
# Make sure the empty cell is drawn
renderedItem = ' '
return renderedItem
|