Changeset 4401

Show
Ignore:
Timestamp:
10/06/08 12:19:20 (2 months ago)
Author:
cboos
Message:

GraphvizPlugin: improve the generated XHTML

  • improve generated markup for image maps (#1598)
    • use cmapx map type
    • don't duplicate the id of <img> into the <map>
    • use a non-numerical id
  • improve the link extraction from URL= attributes, by using extract_link
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • graphvizplugin/0.11/graphviz/graphviz.py

    r4400 r4401  
    2525import sys 
    2626 
    27 from genshi.builder import tag 
     27from genshi.builder import Element, tag 
    2828from genshi.core import Markup 
    2929 
     
    3636from trac.web.api import IRequestHandler 
    3737from trac.wiki.api import IWikiMacroProvider 
    38 from trac.wiki.formatter import wiki_to_oneliner 
     38from trac.wiki.formatter import extract_link 
    3939 
    4040 
     
    360360                # Create the map if not in cache 
    361361                if not os.path.exists(map_path): 
    362                     errmsg = self._launch(encoded_content, proc_cmd, '-Tcmap', 
     362                    errmsg = self._launch(encoded_content, proc_cmd, '-Tcmapx', 
    363363                                          '-o%s' % map_path, 
    364364                                          *self.processor_options) 
     
    367367 
    368368        # Generate HTML output 
     369        img_url = formatter.href.graphviz(img_name) 
    369370        # for SVG(z) 
    370371        if out_format in Graphviz.Vector_Formats: 
    371372            try: # try to get SVG dimensions 
    372373                f = open(img_path, 'r') 
    373                 svg = f.readlines() 
     374                svg = f.readlines(1024) # don't read all 
    374375                f.close() 
    375376                svg = "".join(svg).replace('\n', '') 
     
    389390            # insert SVG, IE compatibility 
    390391            return tag.object( 
    391                     tag.embed(src="%s/graphviz/%s", type="image/svg+xml",  
     392                    tag.embed(src=img_url, type="image/svg+xml",  
    392393                              width=width, height=height), 
    393                     data="%s/graphviz/%s" % (req.base_url, img_name), 
    394                     type="image/svg+xml", width=width, height=height) 
     394                    data=img_url, type="image/svg+xml",  
     395                    width=width, height=height) 
    395396 
    396397        # for binary formats, add map 
     
    400401            f.close() 
    401402            map = "".join(map).replace('\n', '') 
    402             return tag(tag.map(Markup(map), id=sha_key, name=sha_key), 
    403                        tag.img(id=sha_key,  
    404                                src="%s/graphviz/%s" % (req.base_url, img_name), 
    405                                usemap="#"+sha_key, alt="GraphViz image")) 
     403            return tag(tag.map(Markup(map), id='G'+sha_key, name='G'+sha_key), 
     404                       tag.img(src=img_url, usemap="#G"+sha_key,  
     405                               alt=_("GraphViz image"))) 
    406406        else: 
    407             return tag.img(src="%s/graphviz/%s" % (req.base_url, img_name)) 
     407            return tag.img(src=img_url, alt=_("GraphViz image")) 
    408408 
    409409 
     
    412412    def _expand_wiki_links(self, formatter, out_format, content): 
    413413        """Expand TracLinks that follow all URL= patterns.""" 
    414         def _expand(match): 
    415             wiki_url = match.groups()[0] # TracLink ([1], source:file/, ...) 
    416             html_url = wiki_to_oneliner(wiki_url, self.env, req=formatter.req) 
    417             # <a href="http://someurl">...</a> 
    418  
    419             href     = re.search('href="(.*?)"', html_url)   # http://someurl 
    420             url      = href and href.groups()[0] or html_url 
     414        def expand(match): 
     415            wiki_text = match.groups()[0] # TracLink ([1], source:file/, ...) 
     416            link = extract_link(self.env, formatter.context, wiki_text) 
     417            if isinstance(link, Element): 
     418                href = link.attrib.get('href') 
     419                name = link.children 
     420                description = link.attrib.get('title', '') 
     421            else: 
     422                href = wiki_text 
     423                description = None 
    421424            if out_format == 'svg': 
    422425                format = 'URL="javascript:window.parent.location.href=\'%s\'"' 
    423426            else: 
    424427                format = 'URL="%s"' 
    425             return format % url 
    426         return re.sub(r'URL="(.*?)"', _expand, content) 
     428            url = format % href 
     429            if description: 
     430                url += '\ntooltip="%s"' % description \ 
     431                        .replace('"', '').replace('\n', '') 
     432            return url 
     433        return re.sub(r'URL="(.*?)"', expand, content) 
    427434 
    428435    def _load_config(self):