Skip to content

Commit 43c782a

Browse files
authored
Update api.py
- made the plugin it render 'latex', 'Latex', 'LaTeX' processor as well (beside 'math') - changed the loading time to "onload" from "configured", since we had some problem rendering with trac 1.4 - for the same reason: installed MathJax locally - Implemented pairs for inline math: \( ... \) marks inline math $$ ... $$ marks block math \[ ... \] marks block math
1 parent 833ef8f commit 43c782a

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

mathjax/api.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# -*- coding: utf-8 -*-
23
#
34
# Copyright (C) 2011-2024 Mitar <mitar.trac@tnode.com>
@@ -6,14 +7,21 @@
67
# This software is licensed as described in the file COPYING, which
78
# you should have received as part of this distribution.
89

9-
from trac.core import Component, implements
10+
#from trac.core import Component, implements
1011
from trac.mimeview.api import IHTMLPreviewRenderer
1112
from trac.util.html import Markup, html as tag
1213
from trac.web.chrome import ITemplateProvider, add_script
1314
from trac.wiki.api import IWikiMacroProvider
1415

15-
MATHJAX_URL = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js'
16+
from trac.core import *
17+
from trac.wiki import IWikiSyntaxProvider
1618

19+
#MATHJAX_URL = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js'
20+
MATHJAX_URL = 'mathjax/MathJax/MathJax.js'
21+
# Install MathJax 2.7.9 locally:
22+
# wget https://github.com/mathjax/MathJax/archive/3b461438246adfcf67690795fcc0ae6dc4e335fe.zip
23+
# unpack into TracMathJax-0.1.7-py2.7.egg/mathjax/htdocs/MathJax
24+
# restart server
1725

1826
class MathJaxPlugin(Component):
1927
"""Renders mathematical equations using MathJax library.
@@ -28,13 +36,14 @@ class MathJaxPlugin(Component):
2836
}}}
2937
}}}
3038
"""
31-
32-
implements(IHTMLPreviewRenderer, ITemplateProvider, IWikiMacroProvider)
39+
# implements(IHTMLPreviewRenderer, ITemplateProvider, IWikiMacroProvider)
40+
implements(IHTMLPreviewRenderer, ITemplateProvider, IWikiMacroProvider, IWikiSyntaxProvider)
3341

3442
# IWikiMacroProvider methods
3543

3644
def get_macros(self):
37-
yield 'math'
45+
return ('latex', 'Latex', 'LaTeX', 'math')
46+
#yield 'math'
3847

3948
def get_macro_description(self, name):
4049
return self.__doc__
@@ -46,7 +55,7 @@ def expand_macro(self, formatter, name, content, args=None):
4655
# We access this internals directly because it is not possible to
4756
# use add_script with full/absolute URL (trac:#10369).
4857
req.chrome.get('scripts')[-1]['href'] = \
49-
MATHJAX_URL + '?delayStartupUntil=configured'
58+
MATHJAX_URL + '?delayStartupUntil=onload'
5059
# We load configuration afterwards, as we have delay it with
5160
# delayStartupUntil and we call MathJax.Hub.Configured here. We do
5261
# this because having text/x-mathjax-config config blocks outside
@@ -62,7 +71,7 @@ def expand_macro(self, formatter, name, content, args=None):
6271
else: # Called as processor
6372
element = tag.div
6473
return Markup(element(content, class_='trac-mathjax',
65-
style='display:none'))
74+
style='display:none'))
6675

6776
# IHTMLPreviewRenderer methods
6877

@@ -79,3 +88,21 @@ def get_templates_dirs(self):
7988
def get_htdocs_dirs(self):
8089
from pkg_resources import resource_filename
8190
return [('mathjax', resource_filename(__name__, 'htdocs'))]
91+
92+
# IWikiSyntaxProvider methos
93+
94+
def get_link_resolvers(self):
95+
return
96+
97+
def get_wiki_syntax(self):
98+
yield (r"(?P<delim>\\\(|\$\$|\\\[)(?P<math>.*?)(\\\)|\$\$|\\\])", self._format_regex_math)
99+
100+
def _format_regex_math(self, formatter, ns, match):
101+
self.env.log.debug('formatter: %s ns: %s' % (formatter, ns))
102+
maths = match.group('math')
103+
delim = match.group('delim')
104+
if delim == "\(":
105+
argg = { 'delim': delim }
106+
else:
107+
argg = None
108+
return self.expand_macro(formatter, ns, maths, argg )

0 commit comments

Comments
 (0)