Edgewall Software

ChristopherLenz: wiki2x_in_templates.diff

File wiki2x_in_templates.diff, 7.7 KB (added by cmlenz, 6 years ago)

Foundation for moving wiki_to_xxx() into the templates

  • trac/mimeview/patch.py

     
    5555 
    5656        add_stylesheet(req, 'common/css/diff.css') 
    5757        return Chrome(self.env).render_response(req, 'diff_div.html', 
    58                                                 'text/html', data) 
     58                                                'text/html', data, 
     59                                                fragment=True) 
    5960 
    6061    # Internal methods 
    6162 
  • trac/wiki/web_ui.py

     
    396396        if page.name == 'WikiStart': 
    397397            data['title'] = '' 
    398398 
    399         page_html = comment_html = attach_href = '' 
    400         latest_page = WikiPage(self.env, page.name) 
    401  
     399        comment_html = attach_href = '' 
    402400        if page.exists: 
    403             page_html = wiki_to_html(page.text, self.env, req, db) 
     401            latest_page = WikiPage(self.env, page.name) 
    404402            if version: 
    405403                comment_html = wiki_to_oneliner(page.comment or '--', 
    406404                                                self.env, db) 
    407405        else: 
    408406            if not req.perm.has_permission('WIKI_CREATE'): 
    409407                raise HTTPNotFound('Page %s not found', page.name) 
    410             page_html = html.P('Describe "%s" here' % data['page_name']) 
    411408 
    412409        # Show attachments 
    413410        attachments = attachments_data(self.env, req, db, 'wiki', page.name) 
     
    419416                     WikiSystem(self.env).get_pages(prefix)] 
    420417 
    421418        data.update({'action': 'view', 
    422                      'page_html': page_html, 
    423419                     'comment_html': comment_html, 
    424420                     'latest_version': latest_page.version, 
    425421                     'attachments': attachments, 
  • trac/web/chrome.py

     
    3636                              format_time, http_date 
    3737from trac.web.api import IRequestHandler, HTTPNotFound 
    3838from trac.web.href import Href 
    39 from trac.wiki import IWikiSyntaxProvider 
     39from trac.wiki import wiki_to_html, wiki_to_oneliner, IWikiSyntaxProvider 
    4040 
    4141def add_link(req, rel, href, title=None, mimetype=None, classname=None): 
    4242    """Add a link to the HDF data set that will be inserted as <link> element in 
    4343    the <head> of the generated HTML 
    4444    """ 
     45    linkid = '%s:%s' % (rel, href) 
     46    linkset = req.environ.setdefault('trac.chrome.linkset', {}) 
     47    if linkid in linkset: 
     48        return # Already added that link 
     49 
    4550    link = {'href': href} 
    4651    if title: 
    4752        link['title'] = title 
     
    4954        link['type'] = mimetype 
    5055    if classname: 
    5156        link['class'] = classname 
    52     # FIXME: don't add the same link more than once 
    53     req.environ.setdefault('trac.chrome.links', {}).setdefault(rel, []).append(link) 
    5457 
     58    links = req.environ.setdefault('trac.chrome.links', {}) 
     59    links.setdefault(rel, []).append(link) 
     60    linkset[linkid] = True 
     61 
    5562def add_stylesheet(req, filename, mimetype='text/css'): 
    5663    """Add a link to a style sheet to the HDF data set so that it gets included 
    5764    in the generated HTML page. 
     
    6168        filename = filename[7:] 
    6269    else: 
    6370        href = Href(req.base_path).chrome 
    64     add_link(req, 'stylesheet', href(filename), mimetype=mimetype) 
     71    link = href(filename) 
     72    add_link(req, 'stylesheet', link, mimetype=mimetype) 
    6573 
    6674def add_script(req, filename, mimetype='text/javascript'): 
    6775    """Add a reference to an external javascript file to the template.""" 
     76    scriptset = req.environ.setdefault('trac.chrome.scriptset', {}) 
     77    if filename in scriptset: 
     78        return False # Already added that script 
     79 
    6880    if filename.startswith('common/') and 'trac.htdocs_location' in req.environ: 
    6981        href = Href(req.environ['trac.htdocs_location']) 
    7082        filename = filename[7:] 
    7183    else: 
    7284        href = Href(req.base_path).chrome 
    7385    script = {'href': href(filename), 'type': mimetype} 
    74     # FIXME: don't add the same script more than once 
     86 
    7587    req.environ.setdefault('trac.chrome.scripts', []).append(script) 
     88    scriptset[filename] = True 
    7689 
    7790def add_javascript(req, filename): 
    7891    """Deprecated: use `add_script()` instead.""" 
    79     add_script(req, filename, mimetype='text/javascript') 
     92    return add_script(req, filename, mimetype='text/javascript') 
    8093 
    8194 
    8295class INavigationContributor(Interface): 
     
    412425        data['format_time'] = partial(format_time, tzinfo=req.tz) 
    413426        data['fromtimestamp'] = partial(datetime.fromtimestamp, tz=req.tz) 
    414427 
     428        # Wiki-formatting functions 
     429        data['wiki_to_html'] = partial(wiki_to_html, env=self.env, req=req) 
     430        data['wiki_to_oneliner'] = partial(wiki_to_html, env=self.env, req=req) 
     431 
    415432    def load_template(self, filename, method=None): 
    416433        """Retrieve a Template and optionally preset the template data. 
    417434 
     
    428445 
    429446        return self.templateloader.load(filename, cls=cls) 
    430447 
    431     def render_response(self, req, template_name, content_type, data): 
     448    def render_response(self, req, template_name, content_type, data, 
     449                        fragment=False): 
    432450        """Render the `template_name` using the `data` for the context. 
    433451 
    434452        The MIME `content_type` argument is used to choose the kind of template 
     
    438456        """ 
    439457        if content_type is None: 
    440458            content_type = 'text/html' 
    441         doctype = {'text/html': DocType.XHTML_STRICT}.get(content_type) 
    442459        method = {'text/html': 'xhtml', 
    443460                  'text/plain': 'text'}.get(content_type, 'xml') 
    444461 
    445462        template = self.load_template(template_name, method=method) 
    446463        self.populate_data(req, data) 
    447  
    448464        stream = template.generate(**data) 
     465        if fragment: 
     466            return stream 
    449467 
     468        doctype = {'text/html': DocType.XHTML_STRICT}.get(content_type) 
     469        req.environ['trac.chrome.links'] = {} 
     470        req.environ['trac.chrome.scripts'] = [] 
     471        data['chrome'].update({ 
     472            'post_links': req.environ['trac.chrome.links'], 
     473            'post_scripts': req.environ['trac.chrome.scripts'], 
     474        }) 
     475 
    450476        if method == 'text': 
    451477            return stream.render('text') 
    452478        else: 
  • templates/layout.html

     
    5050    <div id="main"> 
    5151      ${select('*|text()')} 
    5252 
     53      <script type="text/javascript" py:if="chrome.post_links"> 
     54        <py:for each="link in chrome.post_links.get('stylesheet')"> 
     55          $.loadStyleSheet("${link.href}", "${link.title}"); 
     56        </py:for> 
     57      </script> 
     58      <script py:for="script in chrome.post_scripts" 
     59              type="${script.type}" src="${script.href}"></script> 
     60 
    5361      <div id="altlinks" py:if="chrome.links.alternate"> 
    5462        <h3>Download in other formats:</h3> 
    5563        <ul> 
  • templates/wiki_view.html

     
    4040        </table> 
    4141      </py:if> 
    4242 
    43       <div class="wikipage searchable"> 
    44         $page_html 
     43      <div class="wikipage searchable" py:choose=""> 
     44        <py:when test="page.exists"> 
     45          ${wiki_to_html(page.text)} 
     46        </py:when> 
     47        <py:otherwise> 
     48          Describe ${page.name} here. 
     49        </py:otherwise> 
    4550      </div> 
    4651 
    4752      ${list_of_attachments(attachments, attach_href, compact=True)}