def styledtext_to_html( styledtext: StyledText, space_format: int, contains_html: bool = False, link_format: Optional[str] = None, ): """Return the note in HTML format. Adapted from DynamicWeb. """ backend = HtmlBackend() if link_format is not None: backend.build_link = build_link_factory(link_format) text = str(styledtext) if not text: return "" s_tags = styledtext.get_tags() html_list = Html("div", class_="grampsstylednote") if contains_html: markuptext = backend.add_markup_from_styled(text, s_tags, split="\n", escape=False) html_list += markuptext else: markuptext = backend.add_markup_from_styled(text, s_tags, split="\n") linelist = [] linenb = 1 sigcount = 0 for line in markuptext.split("\n"): [line, sigcount] = process_spaces(line, format=space_format) if sigcount == 0: # The rendering of an empty paragraph '<p></p>' # is undefined so we use a non-breaking space if linenb == 1: linelist.append(" ") html_list.extend(Html("p") + linelist) linelist = [] linenb = 1 else: if linenb > 1: linelist[-1] += "<br />" linelist.append(line) linenb += 1 if linenb > 1: html_list.extend(Html("p") + linelist) # 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 if sigcount == 0: linelist = [" "] html_list.extend(Html("p") + linelist) return "\n".join(html_list)
def styled_note(styledtext, format, contains_html=False): """Return the note in HTML format. Adapted from DynamicWeb. """ _backend = HtmlBackend() text = str(styledtext) if (not text): return ('') s_tags = styledtext.get_tags() htmllist = Html("div", class_="grampsstylednote") if contains_html: markuptext = _backend.add_markup_from_styled(text, s_tags, split='\n', escape=False) htmllist += markuptext else: markuptext = _backend.add_markup_from_styled(text, s_tags, split='\n') linelist = [] linenb = 1 sigcount = 0 for line in markuptext.split('\n'): [line, sigcount] = process_spaces(line, format) if sigcount == 0: # The rendering of an empty paragraph '<p></p>' # is undefined so we use a non-breaking space if linenb == 1: linelist.append(' ') htmllist.extend(Html('p') + linelist) linelist = [] linenb = 1 else: if linenb > 1: linelist[-1] += '<br />' linelist.append(line) linenb += 1 if linenb > 1: htmllist.extend(Html('p') + linelist) # 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 if sigcount == 0: linelist = [" "] htmllist.extend(Html('p') + linelist) return '\n'.join(htmllist)
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 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 == 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 == 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 == 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()