|
| 1 | +package IkiWiki::Plugin::mathjax; |
| 2 | + |
| 3 | +use warnings; |
| 4 | +use strict; |
| 5 | +use IkiWiki 3.00; |
| 6 | +use MIME::Base64; |
| 7 | + |
| 8 | +# Strategy: |
| 9 | +## - filter replaces normal TeX delimiters with imath and dmath directives |
| 10 | +## (perhaps while considering a mathconf directive); also, it adds a script |
| 11 | +## block if there is any math on the page relevant. |
| 12 | +## - preprocess handles the directives themselves. |
| 13 | +## |
| 14 | +## Later: config hooks for mathjax script tag and mathjax config block |
| 15 | +## |
| 16 | + |
| 17 | +sub import { |
| 18 | + hook(type => "filter", id => "mathjax", call => \&filter); |
| 19 | + hook(type => "format", id=>"mathjax", call=> \&format); |
| 20 | +} |
| 21 | + |
| 22 | +sub format { |
| 23 | + my %params = @_; |
| 24 | + my $content = $params{content}; |
| 25 | + return $content unless $content =~ /\!\!mathjaxbegin/; #]/{{ |
| 26 | + $content =~ s{\!\!mathjaxbegin-i!! (.*?)\s\!\!mathjaxend-i\!\!}{'\('.decode_base64($1).'\)'}ges; #{ |
| 27 | + $content =~ s{\!\!mathjaxbegin-d!! (.*?)\s\!\!mathjaxend-d\!\!}{'\['.decode_base64($1).'\]'}ges; #{ |
| 28 | + my $scripttag = _scripttag(); |
| 29 | + $content =~ s{(</head>)}{$scripttag\n$1}i; #}{ |
| 30 | + return $content; |
| 31 | +} |
| 32 | + |
| 33 | +sub filter (@) { |
| 34 | + my %params=@_; |
| 35 | + my $content = $params{content}; |
| 36 | + return $content unless $content =~ /\$[^\$]+\$|\\[\(\[][\s\S]+\\[\)\]]/; |
| 37 | + # first, handle display math... |
| 38 | + $content =~ s{(?<!\\)\\\[(.+?)(?<!\\)\\\]}{_escape_mathjax('d', $1)}ges; #};[} |
| 39 | + $content =~ s{(?<!\\)\$\$(.+?)(?<!\\)\$\$}{_escape_mathjax('d', $1)}ges; #};[} |
| 40 | + # then, the inline math -- note that it must stay on one line |
| 41 | + $content =~ s{(?<!\\)\\\((.+?)(?<!\\)\\\)}{_escape_mathjax('i', $1)}ge; #};[} |
| 42 | + # note that the 'parsing' of $..$ is extremely fragile |
| 43 | + $content =~ s{(?<!\\)\$(.+?)(?<!\\)\$}{_escape_mathjax('i', $1)}ge; #};[} |
| 44 | + return $content; |
| 45 | +} |
| 46 | + |
| 47 | +sub _escape_mathjax { |
| 48 | + my ($mode, $formula) = @_; |
| 49 | + my %modes = qw/i inline d display/; |
| 50 | + my $directive = "!!mathjaxbegin-$mode!! "; |
| 51 | + $formula =~ s/"/"/g; |
| 52 | + $formula =~ s/&/&/g; #"/}[{ |
| 53 | + $formula =~ s/</</g; |
| 54 | + $formula =~ s/>/>/g; #{" |
| 55 | + $directive .= encode_base64($formula); |
| 56 | + $directive .= " !!mathjaxend-$mode!!"; |
| 57 | + return $directive; |
| 58 | +} |
| 59 | + |
| 60 | +sub _scripttag { |
| 61 | + my $config = 'TeX-AMS_HTML'; # another possibility: TeX-AMS-MML_HTMLorMML |
| 62 | + return '<script type="text/x-mathjax-config">' |
| 63 | + . 'MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "AMS"} } });' |
| 64 | + . '</script>' |
| 65 | + . '<script type="text/javascript" ' |
| 66 | + . 'src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=' |
| 67 | + . $config |
| 68 | + . '"></script>'; |
| 69 | +} |
| 70 | + |
| 71 | +sub _scriptblock { |
| 72 | + return qq% |
| 73 | +<script type="text/javascript">//<![CDATA[ |
| 74 | +(function () { |
| 75 | + if (window.MathJax && MathJax.Hub) return; |
| 76 | + var script = document.createElement("script"); |
| 77 | + script.type = "text/javascript"; |
| 78 | + script.src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js"; |
| 79 | +
|
| 80 | + var config = 'MathJax.Hub.Config({' + |
| 81 | + 'extensions: ["tex2jax.js"],' + |
| 82 | + 'jax: ["input/TeX","output/HTML-CSS"],' + |
| 83 | + 'TeX: { equationNumbers: { autoNumber: "AMS" } }' + |
| 84 | + '});' + |
| 85 | + 'MathJax.Hub.Startup.onload();'; |
| 86 | +
|
| 87 | + if (window.opera) {script.innerHTML = config} |
| 88 | + else {script.text = config} |
| 89 | +
|
| 90 | + document.getElementsByTagName("head")[0].appendChild(script); |
| 91 | +})(); |
| 92 | +//]]></script> |
| 93 | +%; |
| 94 | +} |
| 95 | + |
| 96 | +1; |
0 commit comments