Skip to content

Commit

Permalink
Update api.py
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
pkoevesdi authored Mar 3, 2022
1 parent 833ef8f commit 43c782a
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions mathjax/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# -*- coding: utf-8 -*-
#
# Copyright (C) 2011-2024 Mitar <[email protected]>
Expand All @@ -6,14 +7,21 @@
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

from trac.core import Component, implements
#from trac.core import Component, implements
from trac.mimeview.api import IHTMLPreviewRenderer
from trac.util.html import Markup, html as tag
from trac.web.chrome import ITemplateProvider, add_script
from trac.wiki.api import IWikiMacroProvider

MATHJAX_URL = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js'
from trac.core import *
from trac.wiki import IWikiSyntaxProvider

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

class MathJaxPlugin(Component):
"""Renders mathematical equations using MathJax library.
Expand All @@ -28,13 +36,14 @@ class MathJaxPlugin(Component):
}}}
}}}
"""

implements(IHTMLPreviewRenderer, ITemplateProvider, IWikiMacroProvider)
# implements(IHTMLPreviewRenderer, ITemplateProvider, IWikiMacroProvider)
implements(IHTMLPreviewRenderer, ITemplateProvider, IWikiMacroProvider, IWikiSyntaxProvider)

# IWikiMacroProvider methods

def get_macros(self):
yield 'math'
return ('latex', 'Latex', 'LaTeX', 'math')
#yield 'math'

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

# IHTMLPreviewRenderer methods

Expand All @@ -79,3 +88,21 @@ def get_templates_dirs(self):
def get_htdocs_dirs(self):
from pkg_resources import resource_filename
return [('mathjax', resource_filename(__name__, 'htdocs'))]

# IWikiSyntaxProvider methos

def get_link_resolvers(self):
return

def get_wiki_syntax(self):
yield (r"(?P<delim>\\\(|\$\$|\\\[)(?P<math>.*?)(\\\)|\$\$|\\\])", self._format_regex_math)

def _format_regex_math(self, formatter, ns, match):
self.env.log.debug('formatter: %s ns: %s' % (formatter, ns))
maths = match.group('math')
delim = match.group('delim')
if delim == "\(":
argg = { 'delim': delim }
else:
argg = None
return self.expand_macro(formatter, ns, maths, argg )

0 comments on commit 43c782a

Please sign in to comment.