Project

General

Profile

Feature #1994 » init.rb

Save as vendor/plugins/latex_math/init.rb to support LaTeX math (note-12) - Ling Li, 2011-03-30 00:28

 
1
#
2
# Support LaTeX math equations for Redmine.
3
#
4
# Ling Li, March 2011
5
#
6
require 'redmine'
7
RAILS_DEFAULT_LOGGER.info 'Redmine LaTeX MathJax'
8

    
9
Redmine::Plugin.register :latex_math do
10
    name 'LaTeX math equations'
11
    author 'Ling Li'
12
    description 'Enable LaTeX math equation in wiki and issues via MathJax.'
13
end
14

    
15
# After this macro is called, LaTeX equations will be processed by MathJax.
16
#
17
# {{EnableLaTeXMath}}
18
# \[ x_{1,2} = \frac{-b \pm \sqrt{b^2-4ac}}{2a} \]
19
#
20
Redmine::WikiFormatting::Macros.register do
21
  desc "Enable LaTeX math equations via MathJax"
22
  macro :EnableLaTeXMath do |obj, args|
23
    args, options = extract_macro_options(args, :parent)
24
    if args.size > 0
25
      raise 'No arguments are needed.'
26
    end
27

    
28
    if defined? @IncludedMathJaxScript
29
      return ''
30
    end
31
    @IncludedMathJaxScript = 1
32

    
33
    # Dynamically load MathJax
34
    "<script type=\"text/javascript\">
35
//<![CDATA[
36
(function () {
37
  var script = document.createElement(\"script\");
38
  script.type = \"text/javascript\";
39
  script.src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\";
40

    
41
  var config = 'MathJax.Hub.Config({' +
42
                 'extensions: [\"tex2jax.js\"],' +
43
                 'jax: [\"input/TeX\",\"output/HTML-CSS\"]' +
44
               '});' +
45
               'MathJax.Hub.Startup.onload();';
46

    
47
  if (window.opera) {script.innerHTML = config}
48
               else {script.text = config}
49

    
50
  document.getElementsByTagName(\"head\")[0].appendChild(script);
51
})();
52
//]]>
53
</script>"
54

    
55
  end
56
end
(1-1/2)