Skip to content

Commit 4e2fef5

Browse files
pkoevesdip.koevesdi
authored andcommitted
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 4e2fef5

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

mathjax/api.py

Lines changed: 31 additions & 8 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,18 @@
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 *
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
15+
from trac.wiki import IWikiSyntaxProvider
1416

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

1823
class MathJaxPlugin(Component):
1924
"""Renders mathematical equations using MathJax library.
@@ -28,13 +33,13 @@ class MathJaxPlugin(Component):
2833
}}}
2934
}}}
3035
"""
31-
32-
implements(IHTMLPreviewRenderer, ITemplateProvider, IWikiMacroProvider)
36+
implements(IHTMLPreviewRenderer, ITemplateProvider, IWikiMacroProvider, IWikiSyntaxProvider)
3337

3438
# IWikiMacroProvider methods
3539

3640
def get_macros(self):
37-
yield 'math'
41+
return ('latex', 'Latex', 'LaTeX', 'math')
42+
#yield 'math'
3843

3944
def get_macro_description(self, name):
4045
return self.__doc__
@@ -46,7 +51,7 @@ def expand_macro(self, formatter, name, content, args=None):
4651
# We access this internals directly because it is not possible to
4752
# use add_script with full/absolute URL (trac:#10369).
4853
req.chrome.get('scripts')[-1]['href'] = \
49-
MATHJAX_URL + '?delayStartupUntil=configured'
54+
MATHJAX_URL + '?delayStartupUntil=onload'
5055
# We load configuration afterwards, as we have delay it with
5156
# delayStartupUntil and we call MathJax.Hub.Configured here. We do
5257
# this because having text/x-mathjax-config config blocks outside
@@ -62,7 +67,7 @@ def expand_macro(self, formatter, name, content, args=None):
6267
else: # Called as processor
6368
element = tag.div
6469
return Markup(element(content, class_='trac-mathjax',
65-
style='display:none'))
70+
style='display:none'))
6671

6772
# IHTMLPreviewRenderer methods
6873

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

0 commit comments

Comments
 (0)