Edgewall Software

Ticket #47: trac-ticket_notify_mail-r3697.diff

File trac-ticket_notify_mail-r3697.diff, 4.7 KB (added by cboos, 9 years ago)

Usage example, for Trac:source:sandbox/markup@3698: conversion of the ticket_notify_email.txt template, plus some adaptations to the Chrome component

  • trac/web/chrome.py

     
    2121from markup import Markup
    2222from markup.builder import tag
    2323from markup.output import DocType
    24 from markup.template import TemplateLoader
     24from markup.template import TemplateLoader, Template, TextTemplate
    2525
    2626from trac import mimeview
    2727from trac.config import *
     
    413413        if req:
    414414            data['MARKUP_DEBUG'] = req.args.get('markup_debug')
    415415
    416     def load_template(self, filename, req=None, data=None):
     416    def load_template(self, filename, req=None, data=None, method=None):
    417417        """Retrieve a Template and optionally preset the template data"""
    418418        if req and data:
    419419            self.populate_data(req, data)
    420420        if not self.templateloader:
    421421            self.templateloader = TemplateLoader(self.get_all_templates_dirs(),
    422422                                                 auto_reload=self.auto_reload)
    423         return self.templateloader.load(filename)
     423        cls = method == 'text' and TextTemplate or Template
     424        return self.templateloader.load(filename, cls=cls)
    424425
    425426    def render_response(self, req, template_name, content_type, data):
    426         self.populate_data(req, data)
    427 
    428         stream = self.load_template(template_name).generate(**data)
    429 
    430427        if content_type is None:
    431428            content_type = 'text/html'
     429        doctype = {'text/html': DocType.XHTML_STRICT}.get(content_type)
    432430        method = {'text/html': 'xhtml',
    433431                  'text/plain': 'text'}.get(content_type, 'xml')
    434         doctype = {'text/html': DocType.XHTML_STRICT}.get(content_type)
     432       
     433        template = self.load_template(template_name, req, data, method=method)
     434        stream = template.generate(**data)
    435435
    436436        if method == 'text':
    437437            return stream.render('text')
    438         return stream.render(method, doctype=doctype)
     438        else:
     439            return stream.render(method, doctype=doctype)
  • trac/notification.py

     
    9696        self.config = env.config
    9797        self.db = env.get_db_cnx()
    9898
    99         self.template = Chrome(self.env).load_template(self.template_name)
     99        self.template = Chrome(self.env).load_template(self.template_name,
     100                                                       method='text')
    100101        self.data = {'CRLF': CRLF}
    101102        Chrome(self.env).populate_data(None, self.data)
    102103
  • templates/ticket_notify_email.txt

     
    1 <mail xmlns:py="http://markup.edgewall.org/" py:strip=""><!--!
    2 
    3   Some notes about how to handle white-spaces in text templates:
    4 
    5   - every structure tag should be followed by a comment start: < ! - - !
    6 
    7   - every tag but the first should be preceeded by a comment end: - - >
    8 
    9   - text data following a new line will be inserted either directly,
    10     by first ending the comment then placing the content on the same
    11     line, or inserted after a newline, by first closing the comment,
    12     then placing the data on a line of its own.
    13 
    14   - after text data, a comment start should be started on a newline,
    15     which will effectively insert a newline.
    16     Sometimes though this is not wanted, for example when the data
    17     itself ends with a newline... (see $changes_body below)
    18 
    19 -->
    201$ticket_body_hdr
    212$ticket_props
    22 <!--!
    23   --><py:choose test="ticket.new"><!--!
    24     --><py:when test="True"><!--!
    25 -->
     3#choose ticket.new
     4  #when True
    265$ticket.description
    27 <!--!
    28     --></py:when><!--!
    29     --><py:otherwise><!--!
    30       --><py:if test="changes_body"><!--!
    31 -->
     6  #endwhen
     7  #otherwise
     8    #if changes_body
    329Changes (by $change.author):
    3310
    34 $changes_body<!--!
    35       --></py:if><!--!
    36       --><py:if test="changes_descr"><!--!
    37 -->
     11$changes_body
     12    #endif
     13    #if changes_descr
    3814$changes_descr
    3915--
    40 <!--!
    41       --></py:if><!--!
    42       --><py:if test="change.comment"><!--!
    43 -->
    44 Comment<py:if test="not changes_body"> (by $change.author)</py:if>:
     16    #endif
     17    #if change.comment
    4518
     19Comment${not changes_body and '(by %s)' % change.author or ''}:
     20
    4621$change.comment
    47 <!--!
    48       --></py:if><!--!
    49     --></py:otherwise><!--!
    50   --></py:choose><!--!
    51 -->
     22    #endif
     23  #endotherwise
     24#endchoose
     25
    5226--
    5327Ticket URL: &lt;$ticket.link&gt;
    5428$project.name &lt;${abs_href}&gt;
    5529$project.descr
    56 </mail>
    57  No newline at end of file