def build_header(self): """ Build up the header of the html file over the defaults of Html() """ # add additional meta tags and stylesheet links to head section # create additional meta tags _meta1 = 'name="generator" content="%s %s %s"' % ( PROGRAM_NAME, VERSION, URL_HOMEPAGE) meta = Html('meta', attr=_meta1) #set styles of the report as inline css self.build_style_declaration() # Gramps favicon en css fname1 = '/'.join([self._backend.datadir(), 'favicon.ico']) fname2 = '/'.join([self._backend.datadir(), _TEXTDOCSCREEN]) fname3 = '/'.join([self._backend.datadir(), _HTMLSCREEN]) # links for GRAMPS favicon and stylesheets links = Html( 'link', rel='shortcut icon', href=fname1, type='image/x-icon') + (Html('link', rel='stylesheet', href=fname2, type='text/css', media='screen', indent=False), ) if self.css_filename: links += (Html('link', rel='stylesheet', href=fname3, type='text/css', media='screen', indent=False), ) self._backend.html_header += (meta, links)
def open(self): """ overwrite method, htmlbackend creates a html object that is written on close """ try: DocBackend.open(self) except IOError as msg: errmsg = "%s\n%s" % (_("Could not create %s") % self._filename, msg) raise ReportError(errmsg) except: raise ReportError(_("Could not create %s") % self._filename) if not os.path.isdir(self.datadirfull()): try: os.mkdir(self.datadirfull()) except IOError as msg: errmsg = "%s\n%s" % (_("Could not create %s") % self.datadirfull(), msg) raise ReportError(errmsg) except: raise ReportError( _("Could not create %s") % self.datadirfull()) self.html_page, self.html_header, self.html_body = Html.page( lang=xml_lang(), title=self.title)
def add_media(self, name, pos, w_cm, h_cm, alt='', style_name=None, crop=None): """ Overwrite base method """ self._empty = 0 size = int(max(w_cm, h_cm) * float(150.0 / 2.54)) refname = "is%s" % os.path.basename(name) imdir = self._backend.datadirfull() try: resize_to_jpeg(name, imdir + os.sep + refname, size, size, crop=crop) except: LOG.warning(_("Could not create jpeg version of image %(name)s"), name) return if len(alt): alt = '<br />'.join(alt) if pos not in ["right", "left"]: if len(alt): self.htmllist[-1] += Html('div') + (Html( 'img', src=imdir + os.sep + refname, border='0', alt=alt), Html('p', class_="DDR-Caption") + alt) else: self.htmllist[-1] += Html('img', src=imdir + os.sep + refname, border='0', alt=alt) else: if len(alt): self.htmllist[-1] += Html( 'div', style_="float: %s; padding: 5px; margin: 0;" % pos ) + (Html( 'img', src=imdir + os.sep + refname, border='0', alt=alt), Html('p', class_="DDR-Caption") + alt) else: self.htmllist[-1] += Html('img', src=imdir + os.sep + refname, border='0', alt=alt, align=pos)
def start_cell(self, style_name, span=1): """ Overwrite base method """ if self.use_table_headers and self.first_row: tag = "th" else: tag = "td" self._empty = 1 if span > 1: self.htmllist += (Html(tag, colspan=str(span), class_=style_name), ) self._col += span else: self.htmllist += (Html( tag, colspan=str(span), width=str(self._tbl.get_column_width(self._col)) + '%', class_=style_name), ) self._col += 1
def open(self, filename): """ Overwrite base method """ self._backend = HtmlBackend(filename) self._backend.open() self.htmllist += [self._backend.html_body] #start a gramps report self.htmllist += [Html('div', id="grampstextdoc")] self.build_header()
def start_table(self, name, style): """ Overwrite base method """ self.first_row = True styles = self.get_style_sheet() self._tbl = styles.get_table_style(style) self.htmllist += [ Html('table', width=str(self._tbl.get_width()) + '%', cellspacing='0') ]
def start_paragraph(self, style_name, leader=None): """ Overwrite base method """ style_sheet = self.get_style_sheet() style = style_sheet.get_paragraph_style(style_name) level = style.get_header_level() if level == 0: #a normal paragraph self.htmllist += (Html('p', class_=style_name, inline=True), ) elif level == 1: if self.__title_written == -1 and \ style_name.upper().find('TITLE') != -1: self.__title_written = 0 self.htmllist += (Html('div', id="header"), ) self.htmllist += (Html('h1', class_=style_name, id='SiteTitle', inline=True), ) else: self.htmllist += (Html('h1', class_=style_name, inline=True), ) elif 2 <= level <= 5: tag = 'h' + str(level + 1) self.htmllist += (Html(tag, class_=style_name, inline=True), ) else: # a low level header self.htmllist += (Html('div', id='grampsheading', class_=style_name), ) if leader is not None: self.write_text(leader + ' ')
def start_underline(self): """ Starts a section of underlining. """ self.htmllist += [Html('u')]
def start_link(self, link): """ Starts a section to add a link. Link is a URI. """ self.htmllist += [Html('a', href=link, **self.__link_attrs)]
def write_styled_note(self, styledtext, format, style_name, contains_html=False, links=False): """ Convenience function to write a styledtext to the html doc. styledtext : assumed a StyledText object to write format : = 0 : Flowed, = 1 : Preformatted style_name : name of the style to use for default presentation contains_html: bool, the backend should not check if html is present. If contains_html=True, then the textdoc is free to handle that in some way. Eg, a textdoc could remove all tags, or could make sure a link is clickable. HtmlDoc will show the html as pure text, so no escaping will happen. links: bool, make URLs clickable if True """ text = str(styledtext) self.htmllist += [Html('div', id='grampsstylednote')] if contains_html: #just dump the note out as it is. Adding markup would be dangerous # as it could destroy the html. If html code, one can do the self.start_paragraph(style_name) self.__write_text(text, markup=True, links=links) self.end_paragraph() else: s_tags = styledtext.get_tags() markuptext = self._backend.add_markup_from_styled(text, s_tags, split='\n') self.start_paragraph(style_name) inpara = True self._empty = 1 # para is empty # we explicitly set _empty because start and end para do not seem # to do a very good job at setting them linenb = 1 # The code is tricky here, because we don't want to start a new para # at the end of the last line if there is no newline there. # Instead, we want to just end the current para. for line in markuptext.split('\n'): [line, sigcount] = process_spaces(line, format) if sigcount == 0: if inpara is False: # needed for runs of three or more newlines self.start_paragraph(style_name) inpara = True self._empty = 1 # para is empty self.end_paragraph() inpara = False linenb = 1 else: if inpara is False: self.start_paragraph(style_name) inpara = True self._empty = 1 # para is empty if linenb > 1: self.htmllist[-1] += Html('br') self.__write_text(line, markup=True, links=links) self._empty = 0 # para is not empty linenb += 1 if inpara is True: self.end_paragraph() if sigcount == 0: # if the last line was blank, then as well as outputting the # previous para, which we have just done, we also output a new # blank para self.start_paragraph(style_name) self._empty = 1 # para is empty self.end_paragraph() #end div element self.__reduce_list()
def start_superscript(self): """ Overwrite base method """ self.htmllist += [Html('sup')]
def start_bold(self): """ Overwrite base method """ self.htmllist += [Html('strong')]
def start_row(self): """ Overwrite base method """ self.htmllist += [Html('tr')] self._col = 0
def write_title(self): """ Add title field to header """ self._backend.html_header += Html('title', self.title, inline=True)
def get_html(self, action, style=None, tab_height=200): style = style if style else self.style ## Hack of levels of nbsp html = Html( 'div', class_="content", id=self.id, style=("overflow: auto; height:%spx; background-color: #f4f0ec;" % tab_height) if not style else style) table = Html("table", width="95%", cellspacing="0") rowhtml = Html("tr") for (name, width) in self.columns: cell = Html("th", class_="TableHeaderCell", width=("%s%%" % width), colspan="1") cell += self.form._(name) rowhtml += cell table += rowhtml row_count = 1 for row in self.rows: rowhtml = Html("tr") cell = Html("td", class_="TableDataCell", width=("%s%%" % self.columns[0][1]), colspan="1") div = Html( "div", style="background-color: lightgray; padding: 2px 0px 0px 2px") if action != "edit": disabled = "disabled" else: disabled = "" url = self.edit_url(row_count - 1) img_remove = self.form.handler.app.make_url( "/images/gtk-remove.png") img_up = self.form.handler.app.make_url("/images/up.png") img_down = self.form.handler.app.make_url("/images/down.png") div += """<input type="submit" name="update_json" value="%s/remove" onclick="set_has_data_to_save(null)" style="background:url(%s) no-repeat; font-size:0; width: 30px; height: 30px;" %s/>""" % ( url, img_remove, disabled) div += """<input type="submit" name="update_json" value="%s/up" onclick="set_has_data_to_save(null)" style="background:url(%s) no-repeat; font-size:0; width: 30px; height: 30px;" %s/>""" % ( url, img_up, disabled) div += """<input type="submit" name="update_json" value="%s/down" onclick="set_has_data_to_save(null)" style="background:url(%s) no-repeat; font-size:0; width: 30px; height: 30px;" %s/>""" % ( url, img_down, disabled) cell += div rowhtml += cell for count in range(1, len(self.columns)): cell = Html("td", class_="TableDataCell", width=("%s%%" % self.columns[count][1]), colspan="1", style="vertical-align: middle") cell += """<a href="%s" style="display: block;">%s</a>""" % ( self.goto_url(row_count - 1), row[count - 1]) rowhtml += cell table += rowhtml row_count += 1 html += table return str(html) #.replace("&nbsp;", " ")
def get_html(self, action, style=None, tab_height=200): style = style if style else self.style ## Hack of levels of nbsp html = Html( 'div', class_="content", id=self.id, style=("overflow: auto; height:%spx; background-color: #f4f0ec;" % tab_height) if not style else style) table = Html("table", width="95%", cellspacing="0") rowhtml = Html("tr") for (name, width) in self.columns: cell = Html("th", class_="TableHeaderCell", width=("%s%%" % width), colspan="1") cell += self.form._(name) rowhtml += cell table += rowhtml row_count = 1 for row in self.rows: #TODO: Properly implement the remove/up/down buttons rowhtml = Html("tr") cell = Html("td", class_="TableDataCell", width=("%s%%" % self.columns[0][1]), colspan="1") div = Html( "div", style="background-color: lightgray; padding: 2px 0px 0px 2px") div += Html( "img", height="22", width="22", alt="Delete row", title="Delete row", src="/images/gtk-remove.png", onmouseover="buttonOver(this)" if action == "edit" else None, onmouseout="buttonOut(this)" if action == "edit" else None, onclick="document.location.href='/%s/%s/remove/%s/%s'" % (self.obj_type, self.handle, self.ttype, row_count) if action == "edit" else None, style= "background-color: lightgray; border: 1px solid lightgray; border-radius:5px; margin: 0px 1px; padding: 1px;" ) div += Html( "img", height="22", width="22", alt="Move row up", title="Move row up", src="/images/up.png", onmouseover="buttonOver(this)" if action == "edit" and row_count > 1 else None, onmouseout="buttonOut(this)" if action == "edit" and row_count > 1 else None, onclick="document.location.href='/%s/%s/up/%s/%s'" % (self.obj_type, self.handle, self.ttype, row_count) if action == "edit" and row_count > 1 else None, style= "background-color: lightgray; border: 1px solid lightgray; border-radius:5px; margin: 0px 1px; padding: 1px;" ) div += Html( "img", height="22", width="22", alt="Move row down", title="Move row down", src="/images/down.png", onmouseover="buttonOver(this)" if action == "edit" and row_count < len(self.rows) else None, onmouseout="buttonOut(this)" if action == "edit" and row_count < len(self.rows) else None, onclick="document.location.href='/%s/%s/down/%s/%s'" % (self.obj_type, self.handle, self.ttype, row_count) if action == "edit" and row_count < len(self.rows) else None, style= "background-color: lightgray; border: 1px solid lightgray; border-radius:5px; margin: 0px 1px; padding: 1px;" ) cell += div rowhtml += cell for count in range(1, len(self.columns)): cell = Html("td", class_="TableDataCell", width=("%s%%" % self.columns[count][1]), colspan="1") url = self.links[row_count - 1] try: cell += """<a href="%s" style="display: block;">%s</a>""" % ( url, row[count - 1]) except: tornado.log.logging.info("improper rows: %s", row) rowhtml += cell table += rowhtml row_count += 1 html += table return str(html) #.replace("&nbsp;", " ")