aboutsummaryrefslogtreecommitdiff
path: root/lib/IkiWiki/Plugin/mathjax.pm
blob: 99b74c354c897c9a6971e062736eb0017aeaadf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package IkiWiki::Plugin::mathjax;

use warnings;
use strict;
use IkiWiki 3.00;
use MIME::Base64;

# Strategy:
## - filter replaces normal TeX delimiters with imath and dmath directives
##   (perhaps while considering a mathconf directive); also, it adds a script
##   block if there is any math on the page relevant.
## - preprocess handles the directives themselves.
##
## Later: config hooks for mathjax script tag and mathjax config block
##

sub import {
    hook(type => "filter", id => "mathjax", call => \&filter);
    hook(type => "format", id=>"mathjax", call=> \&format);
}

sub format {
    my %params = @_;
    my $content = $params{content};
    return $content unless $content =~ /\!\!mathjaxbegin/; #]/{{
    $content =~ s{\!\!mathjaxbegin-i!! (.*?)\s\!\!mathjaxend-i\!\!}{'\('.decode_base64($1).'\)'}ges; #{
    $content =~ s{\!\!mathjaxbegin-d!! (.*?)\s\!\!mathjaxend-d\!\!}{'\['.decode_base64($1).'\]'}ges; #{
    my $scripttag = _scripttag();
    $content =~ s{(</body>)}{$scripttag\n$1}i; #}{
    return $content;
}

sub filter (@) {
    my %params=@_;
    my $content = $params{content};
    return $content unless $content =~ /\$[^\$]+\$|\\[\(\[][\s\S]+\\[\)\]]/;
    # first, handle display math...
    $content =~ s{(?<!\\)\\\[(.+?)(?<!\\)\\\]}{_escape_mathjax('d', $1)}ges; #};[}
    $content =~ s{(?<!\\)\$\$(.+?)(?<!\\)\$\$}{_escape_mathjax('d', $1)}ges; #};[}
    # then, the inline math -- note that it must stay on one line
    $content =~ s{(?<!\\)\\\((.+?)(?<!\\)\\\)}{_escape_mathjax('i', $1)}ge; #};[}
    # note that the 'parsing' of $..$ is extremely fragile
    $content =~ s{(?<!\\)\$(.+?)(?<!\\)\$}{_escape_mathjax('i', $1)}ge; #};[}
    return $content;
}

sub _escape_mathjax {
    my ($mode, $formula) = @_;
    my %modes = qw/i inline d display/;
    my $directive = "!!mathjaxbegin-$mode!! ";
    $formula =~ s/"/&quot;/g;
    $formula =~ s/&/&amp;/g; #"/}[{
    $formula =~ s/</&lt;/g;
    $formula =~ s/>/&gt;/g; #{"
    $directive .= encode_base64($formula, " ");
    $directive .= "!!mathjaxend-$mode!!";
    return $directive;
}

sub _scripttag {
    my $config = 'TeX-AMS_HTML'; # another possibility: TeX-AMS-MML_HTMLorMML
    return '<script type="text/x-mathjax-config">'
      . 'MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "AMS"} } });'
      . '</script>'
      . '<script async="async" type="text/javascript" '
      # Serving MathJax script locally
      #. 'src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config='
      #. 'src="/js/MathJax.js?config='
      . 'src="/vendor/MathJax/es5/tex-chtml.js?config='
      . $config
      . '"></script>';
}

1;