def __addHtmlSection(self, section, id, depth): if depth > 0: content = '' for text in section.text: content = content + text urls = self.impl.downloadHtmlImages(content) content = self.impl.remoteToLocalImageUrls(urls, content) stream = MarkupTemplate("""<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/"> <head> <title>${section.title}</title> <style type="text/css"> h1 { text-align: center; } ${section.css} </style> </head> <body> <h1>${section.title}</h1> """ + content + """ </body> </html>""").generate(section = section) html = stream.render('xhtml', doctype = 'xhtml11', drop_xml_decl = False) item = self.impl.addHtml('', '%s.html' % id, html) self.impl.addSpineItem(item) self.impl.addTocMapNode(item.destPath, section.title, depth) id += '.' if len(section.subsections) > 0: for i, subsection in enumerate(section.subsections): self.__addHtmlSection(subsection, id + str(i + 1), depth + 1)
def test_fill_option_segmented_text_no_value(self): html = MarkupTemplate("""<form> <select name="foo"> <option>foo $x bar</option> </select> </form>""").generate(x=1) | HTMLFormFiller(data={'foo': 'foo 1 bar'}) self.assertEquals("""<form> <select name="foo"> <option selected="selected">foo 1 bar</option> </select> </form>""", html.render())
def test_fill_option_segmented_text_no_value(self): html = MarkupTemplate("""<form> <select name="foo"> <option>foo $x bar</option> </select> </form>""").generate(x=1) | HTMLFormFiller(data={'foo': 'foo 1 bar'}) self.assertEquals("""<form> <select name="foo"> <option selected="selected">foo 1 bar</option> </select> </form>""", html.render())
def test_fill_option_segmented_text(self): html = MarkupTemplate(u"""<form> <select name="foo"> <option value="1">foo $x</option> </select> </form>""").generate(x=1) | HTMLFormFiller(data={'foo': '1'}) self.assertEqual( u"""<form> <select name="foo"> <option value="1" selected="selected">foo 1</option> </select> </form>""", html.render())
def expand_macro(self, formatter, name, content): template = """ <div>Hello World, args = ${args}</div> """ if genshi: from genshi.template import MarkupTemplate tmpl = MarkupTemplate(template) return tmpl.generate(args=content) else: from trac.util.text import jinja2template tmpl = jinja2template(template.strip()) return tmpl.render(args=content)
def send_html_response(self, handler, html_file, code=200, html_form_data={}, **kwargs): """ Generates and sends an HTML response. This generates headers and an HTML response either from the specified HTML source or HTML file. Both will be parsed using the Genhsi template engine and will be extended with the default template. Args: handler: References the handler of the current http request. code: Defines the response code is send within the http headers, by default, responde code 200 (success) is sent. html_file: Must reference a HTML document within the current document root or the plugin directory that will be loaded and parsed using Genshi. html_form_data: Pass additional html form data to auto-fill html forms using genshi.filters.HTMLFormFiller. **kwargs: Any additional parameter will be forwarded to the Genshi template. """ handler.send_response(code=code) handler.send_header("Content-type", 'text/html') handler.end_headers() # Add additional template parameters kwargs["plugin"] = self.__module__ template_path = os.path.dirname(__file__) + os.sep + \ "assets" + os.sep + "html" + os.sep + "index.html" fd = open(template_path) template = MarkupTemplate(fd, template_path) fd.close() filler = HTMLFormFiller(data=html_form_data) # See http://stackoverflow.com/questions/1555644/can-one-prevent-genshi-from-parsing-html-entities # because of "us-ascii" encoding. html = HTML(self.template.load(html_file).generate(**kwargs).render(encoding= 'us-ascii')) template = template.generate(Context(input=html.filter(filler), **kwargs)) handler.wfile.write(template.render('xhtml', doctype='html', encoding= 'us-ascii'))