Changeset 3463

Show
Ignore:
Timestamp:
04/04/08 03:58:46 (8 months ago)
Author:
rlotun
Message:

Applied patch by guyer@nist.gov

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tracmathplugin/tracmath/tracmath.py

    r3462 r3463  
    11""" TracMath - A trac plugin that renders latex formulas within a wiki page. 
    22 
    3 This has currently been tested only on trac 0.11. 
     3This has currently been tested only on trac 0.10.4 and 0.11. 
    44""" 
    55 
     
    4242             re.compile(r'.+dvi$'), 
    4343            ] 
     44reLABEL = re.compile(r'\\label\{(.*?)\}') 
    4445 
    4546class TracMathPlugin(Component): 
     
    6667        self.max_png = self.config.get('tracmath', 'max_png') or max_png 
    6768        self.max_png = int(self.max_png) 
     69        self.use_dollars = self.config.get('tracmath', 'use_dollars') or "False" 
     70        self.use_dollars = self.use_dollars.lower() in ("true", "on", "enabled") 
    6871 
    6972        if not os.path.exists(self.cacheDirectory): 
     
    9396        return [] 
    9497 
     98    # IWikiSyntaxProvider methods 
     99    #   stolen from http://trac-hacks.org/ticket/248 
     100 
     101    def get_wiki_syntax(self): 
     102        if self.use_dollars: 
     103            yield (r"\$\$(?P<displaymath>.*?)\$\$", 
     104                   lambda formatter, ns, match: "<blockquote>" + self.expand_macro(formatter, 'latex', ns) + "</blockquote>") 
     105            yield (r"\$(?P<latex>.*?)\$", 
     106                   lambda formatter, ns, match: self.expand_macro(formatter, 'latex', ns)) 
     107 
     108    def get_link_resolvers(self): 
     109        return [] 
     110 
    95111    # IWikiMacroProvider methods 
    96112    def get_macros(self): 
     
    99115    def get_macro_description(self, name): 
    100116        if name == 'latex': 
    101             return 'LaTeX' 
     117            return """ 
     118            This plugin allows embedded equations in Trac markup. 
     119            Basically a port of http://www.amk.ca/python/code/mt-math to Trac. 
     120 
     121            Simply use 
     122            {{{ 
     123              {{{ 
     124              #!latex 
     125              [latex code] 
     126              }}} 
     127            }}} 
     128            for a block of LaTeX code. 
     129 
     130            If `use_dollars` is enabled in `trac.ini`, then you can also use 
     131            `$[latex formula]$` for inline math or `$$[latex formula]$$` for 
     132            display math. 
     133            """ 
    102134 
    103135    def internal_render(self, req, name, content): 
    104136        if not name == 'latex': 
    105137            return 'Unknown macro %s' % (name) 
     138 
     139        label = None 
     140        for line in content.split("\n"): 
     141            m = reLABEL.match(content) 
     142            if m: 
     143                label = m.group(1) 
    106144 
    107145        key = sha.new(content.encode('utf-8')).hexdigest() 
     
    147185            self.manage_cache() 
    148186 
    149         return '<img src="%s/tracmath/%s" />' % (req.base_url, imgname) 
     187        result = '<img src="%s/tracmath/%s" alt="%s" />' % (req.base_url, imgname, content) 
     188        if label: 
     189            result = '<a name="%s">(%s)<a/>&nbsp;%s' % (label, label, result) 
     190        return result 
    150191 
    151192    def manage_cache(self): 
     
    176217    def expand_macro(self, formatter, name, content): 
    177218        return self.internal_render(formatter.req, name, content) 
     219 
     220    # needed for Trac 0.10.4 
     221    def render_macro(self, req, name, content): 
     222        return self.internal_render(req, name, content) 
    178223 
    179224    # IHTMLPreviewRenderer methods