def test_CustomElementInsidePara(self): # It's just too hard to write a standard test with a custom renderer. doc, section, styles = RTFTestCase.initializeDoc() p = Paragraph() p.append('This is a standard paragraph with the default style.') class CustomClass(object): pass p.append(CustomClass()) section.append(p) # Define renderer with custom element support. specialString = "ABC I'm unique" def customElementWriter(renderer, element): renderer._write(specialString) r = Renderer(write_custom_element_callback=customElementWriter) # Render with custom element. result = StringIO() r.Write(doc, result) testData = result.getvalue() result.close() # Confirm generate result has custom rendering. assert specialString in testData
def render(self, output): document = Document() section = Section() for o in output: section.append(o) document.Sections.append(section) renderer = Renderer() renderer.Write(document, StringIO()) # Setup instance variables renderer._doc = document renderer._fout = StringIO() renderer._CurrentStyle = "" renderer._WriteSection(section, True, False) return renderer._fout.getvalue()
def main(): """ Main processing for command line invocation """ cmdline = process_command_line() cfg = json.load(file(cmdline.configfile)) if cmdline.lang: langs = [cmdline.lang] else: langs = LANGS if cmdline.wiki: for lang in sorted(langs): conv = SubdimensionsToWikiConverter(lang,cfg[lang]) conv.create_cms_schemas() conv.create_lms() ofname = lang + '_' + cmdline.outfile lines = conv.get_wiki_pages() with codecs.open(ofname,"w",encoding="utf-8") as of: for line in lines: print >> of, line else: plog = {} if cmdline.prevlog: plog = json.load(file(cmdline.prevlog),encoding='utf-8') doc = Document() section = Section() doc.Sections.append(section) ss = doc.StyleSheet logdoc = {} for lang in sorted(langs): conv = SubdimensionsConverter(lang,cfg[lang],plog) conv.set_ss(ss) conv.add_tables(section) conv.add_to_json(logdoc) DR = Renderer() ofile = codecs.open(cmdline.outfile, "w", encoding="utf-8") DR.Write( doc, ofile ) lfile = codecs.open(cmdline.outfile+'.log', "w", encoding="utf-8") json.dump(logdoc, lfile, indent=2, encoding='utf-8') return 0
def render(self): """ Mostly a copy of the render method in OSHAActionPlanReportDownload, but with some changes to handle the special reqs of Italy """ document = report.createDocument(self.session) ss = document.StyleSheet # Define some more custom styles ss.ParagraphStyles.append( ParagraphStyle( "RiskPriority", TextStyle( TextPropertySet(font=ss.Fonts.Arial, size=22, italic=True, colour=ss.Colours.Blue)), ParagraphPropertySet(left_indent=300, right_indent=300))) ss.ParagraphStyles.append( ParagraphStyle( "MeasureField", TextStyle( TextPropertySet(font=ss.Fonts.Arial, size=18, underline=True)), ParagraphPropertySet(left_indent=300, right_indent=300))) ss.ParagraphStyles.append( ParagraphStyle( "ITTitle", TextStyle( TextPropertySet(font=ss.Fonts.Arial, size=36, italic=True, bold=True)), ParagraphPropertySet(left_indent=300, right_indent=300))) ss.ParagraphStyles.append( ParagraphStyle( "ITSubtitle", TextStyle( TextPropertySet(font=ss.Fonts.Arial, size=32, italic=True, bold=True)), ParagraphPropertySet(left_indent=300, right_indent=300))) ss.ParagraphStyles.append( ParagraphStyle( "ITSubSubtitle", TextStyle( TextPropertySet(font=ss.Fonts.Arial, size=28, italic=True, bold=True)), ParagraphPropertySet(left_indent=300, right_indent=300))) ss.ParagraphStyles.append( ParagraphStyle( "ITNormalBold", TextStyle( TextPropertySet(font=ss.Fonts.Arial, size=24, bold=True)), ParagraphPropertySet(left_indent=50, right_indent=50))) # XXX: This part is removed # self.addActionPlan(document) # XXX: and replaced with this part: t = lambda txt: "".join([ "\u%s?" % str(ord(e)) for e in translate(txt, context=self.request) ]) intro = createItalianIntro(document, self.context, self.request) toc = createSection(document, self.context, self.request, first_page_number=2) body = Section() heading = t( _("header_oira_report_download", default=u"OiRA Report: \"${title}\"", mapping=dict(title=self.session.title))) toc.append( Paragraph( ss.ParagraphStyles.Heading1, ParagraphPropertySet(alignment=ParagraphPropertySet.CENTER), heading, )) if self.session.report_comment: # Add comment. #5985 normal_style = document.StyleSheet.ParagraphStyles.Normal toc.append(Paragraph(normal_style, self.session.report_comment)) toc_props = ParagraphPropertySet() toc_props.SetLeftIndent(TabPropertySet.DEFAULT_WIDTH * 1) toc_props.SetRightIndent(TabPropertySet.DEFAULT_WIDTH * 1) p = Paragraph(ss.ParagraphStyles.Heading6, toc_props) txt = t(_("toc_header", default=u"Contents")) p.append(character.Text(txt)) toc.append(p) headings = [ t(u"Adempimenti/rischi identificati, valutati e gestiti con misure " "obbligatorie adottate ed eventuali misure di miglioramento"), t(u"Adempimenti/rischi non pertinenti"), ] nodes = [ self.actioned_nodes, self.risk_not_present_nodes, ] for nodes, heading in zip(nodes, headings): if not nodes: continue self.addReportNodes(document, nodes, heading, toc, body) toc.append(Paragraph(LINE)) body.append(Paragraph(LINE)) document.Sections.append(body) # Until here... renderer = Renderer() output = StringIO() renderer.Write(document, output) # Custom filename filename = u"Documento di valutazione dei rischi {}".format( self.session.title) self.request.response.setHeader( "Content-Disposition", "attachment; filename=\"%s.rtf\"" % filename.encode("utf-8")) self.request.response.setHeader("Content-Type", "application/rtf") return output.getvalue()
def render(self): """ Mostly a copy of the render method in euphorie.client, but with some changes to also show unanswered risks and non-present risks. #1517 and #1518 """ document = report.createDocument(self.session) ss = document.StyleSheet # Define some more custom styles ss.ParagraphStyles.append( ParagraphStyle( "RiskPriority", TextStyle( TextPropertySet(font=ss.Fonts.Arial, size=22, italic=True, colour=ss.Colours.Blue)), ParagraphPropertySet(left_indent=300, right_indent=300))) ss.ParagraphStyles.append( ParagraphStyle( "MeasureField", TextStyle( TextPropertySet(font=ss.Fonts.Arial, size=18, underline=True)), ParagraphPropertySet(left_indent=300, right_indent=300))) # XXX: This part is removed # self.addActionPlan(document) # XXX: and replaced with this part: t = lambda txt: "".join([ "\u%s?" % str(ord(e)) for e in translate(txt, context=self.request) ]) toc = createSection(document, self.context, self.request) body = Section() heading = t( _("header_oira_report_download", default=u"OiRA Report: \"${title}\"", mapping=dict(title=self.session.title))) toc.append( Paragraph( ss.ParagraphStyles.Heading1, ParagraphPropertySet(alignment=ParagraphPropertySet.CENTER), heading, )) if self.session.report_comment: # Add comment. #5985 normal_style = document.StyleSheet.ParagraphStyles.Normal toc.append(Paragraph(normal_style, self.session.report_comment)) toc_props = ParagraphPropertySet() toc_props.SetLeftIndent(TabPropertySet.DEFAULT_WIDTH * 1) toc_props.SetRightIndent(TabPropertySet.DEFAULT_WIDTH * 1) p = Paragraph(ss.ParagraphStyles.Heading6, toc_props) txt = t(_("toc_header", default=u"Contents")) p.append(character.Text(txt)) toc.append(p) headings = [ t( _("header_present_risks", default=u"Risks that have been identified, " u"evaluated and have an Action Plan")), t( _("header_unevaluated_risks", default=u"Risks that have been identified but " u"do NOT have an Action Plan")), t( _("header_unanswered_risks", default=u'Hazards/problems that have been "parked" ' u'and are still to be dealt with')), t( _("header_risks_not_present", default=u"Hazards/problems that have been managed " u"or are not present in your organisation")) ] nodes = [ self.actioned_nodes, self.unactioned_nodes, self.unanswered_nodes, self.risk_not_present_nodes, ] for nodes, heading in zip(nodes, headings): if not nodes: continue self.addReportNodes(document, nodes, heading, toc, body) toc.append(Paragraph(LINE)) body.append(Paragraph(LINE)) self.addConsultationBox(body, document) document.Sections.append(body) # Until here... renderer = Renderer() output = StringIO() renderer.Write(document, output) filename = translate( _("filename_report_actionplan", default=u"Action plan ${title}", mapping=dict(title=self.session.title)), context=self.request, ) self.request.response.setHeader( "Content-Disposition", "attachment; filename=\"%s.rtf\"" % filename.encode("utf-8")) self.request.response.setHeader("Content-Type", "application/rtf") return output.getvalue()