#! """DECnet/Python HTML building classes """ import time from datetime import timedelta def setstarttime (): global start_time start_time = time.time () class wraphtml (object): open = "" close = "" sep = "\n" def __init__ (self, *contents): self.contents = contents def __str__ (self): return self.open + \ self.sep.join (str (i) for i in self.contents) + \ self.close def wrap1 (content, cls): if isinstance (content, wraphtml): return content return cls (content) def wrap (content, cls): if isinstance (content, (str, wraphtml)): return content return cls (*content) def makelink (mobile, path, title, qs = ""): return '{}'.format ("m/" if mobile else "", path, qs, title) class cell (wraphtml): tag = "td" align = ' valign="top" ' markup = "" def __init__ (self, contents, markup = None, valign = None): super ().__init__ (contents) if markup: self.markup = markup if valign: self.align = ' valign="{}"'.format (valign) def __str__ (self): return '<{0.tag}{0.align}{0.markup}>{0.contents[0]}'.format (self) class hcell (cell): tag = "th" class trow (wraphtml): open = "" close = "" sep = "" cclass = cell def __init__ (self, *contents): super ().__init__ (*[ wrap1 (c, self.cclass) for c in contents ]) class thdr (trow): cclass = hcell class drow (trow): def __init__ (self, col1, col2): super ().__init__ (cell (col1, 'class="td-col1"'), cell (col2, 'class="td-col2"')) class table (wraphtml): open = "" close = "
" rclass = trow def __init__ (self, header, data): """Create an HTML table. Arguments are the header and data. Header is a row of items, the column headers. Data is a sequence of data rows. """ super ().__init__ (wrap (header, thdr), *[ wrap (i, self.rclass) for i in data]) class dtable (table): open = '' rclass = drow def __init__ (self, data): super ().__init__ ("", data) class detailrow (trow): detailtable = dtable detailclass = "details" def __init__ (self, *contents): *row1, extra = contents if extra: row1[0] = cell (row1[0], 'rowspan="2"') self.extra = self.detailtable (extra) else: self.extra = None super ().__init__ (*row1) def __str__ (self): line1 = super ().__str__ () if self.extra: return '{}\n' \ .format (line1, len (self.contents), self.detailclass, self.extra) return line1 class detail_table (table): rclass = detailrow class lines (wraphtml): open = "

" close = "

" sep = "
\n" class div (wraphtml): open = "
" close = "
" class pre (wraphtml): open = "
"
    close = "
" sep = "\n" # Subclasses of div to change the class attribute (CSS style) class middle (div): open = '
' class sidebar (div): open = '
{}