forked from phillord/mathjax-latex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathjax-latex.php
195 lines (157 loc) · 6.13 KB
/
mathjax-latex.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
Plugin Name: MathJax-LaTeX
Description: Transform latex equations in javascript using mathjax
Version: 1.2.1
Author: Phillip Lord, Simon Cockell
Author URI: http://knowledgeblog.org
Copyright 2010. Phillip Lord ([email protected])
Simon Cockell ([email protected])
Newcastle University.
*/
/*
* The contents of this file are subject to the LGPL License, Version 3.0.
*
* Copyright (C) 2010-2013, Phillip Lord, Newcastle University
* Copyright (C) 2010-2011, Simon Cockell, Newcastle University
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
require_once( dirname( __FILE__ ) . "/mathjax-latex-admin.php" );
class MathJax{
static $add_script;
static $block_script;
function init(){
register_activation_hook(__FILE__, array(__CLASS__, 'mathjax_install'));
register_deactivation_hook(__FILE__, array(__CLASS__, 'mathjax_uninstall'));
if (get_option('kblog_mathjax_force_load')) {
self::$add_script = true;
}
add_shortcode('mathjax',
array(__CLASS__, 'mathjax_shortcode' ));
add_shortcode('nomathjax',
array(__CLASS__, 'nomathjax_shortcode' ));
add_shortcode('latex',
array(__CLASS__, 'latex_shortcode' ));
add_action('wp_footer',
array(__CLASS__, 'add_script'));
add_action('wp_footer',
array(__CLASS__, 'unconditional'));
if (get_option('kblog_mathjax_use_wplatex_syntax')) {
add_filter( 'the_content', array(__CLASS__, 'inline_to_shortcode' ) );
}
add_filter('plugin_action_links', array(__CLASS__, 'mathjax_settings_link'), 9, 2 );
}
function mathjax_install() {
//registers default options
add_option('kblog_mathjax_force_load', false);
add_option('kblog_mathjax_latex_inline', 'inline');
add_option('kblog_mathjax_use_wplatex_syntax', false );
add_option('kblog_mathjax_use_cdn', true);
add_option('kblog_mathjax_custom_location',false);
add_option('kblog_mathjax_config',"default");
}
function mathjax_uninstall() {
delete_option('kblog_mathjax_force_load');
delete_option('kblog_mathjax_latex_inline');
delete_option('kblog_mathjax_use_wplatex_syntax');
delete_option('kblog_mathjax_use_cdn');
delete_option('kblog_mathjax_custom_location');
delete_option('kblog_mathjax_config');
}
function unconditional(){
echo '<!-- MathJax Latex Plugin installed';
if( !self::$add_script )
echo ': Disabled as no shortcodes on this page';
if( self::$block_script )
echo ': Disabled by nomathjax shortcode';
echo ' -->';
}
function mathjax_shortcode($atts,$content){
self::$add_script = true;
}
function nomathjax_shortcode($atts,$content){
self::$block_script = true;
}
function latex_shortcode($atts,$content)
{
self::$add_script = true;
//this gives us an optional "syntax" attribute, which defaults to "inline", but can also be "display"
extract(shortcode_atts(array(
'syntax' => get_option('kblog_mathjax_latex_inline'),
), $atts));
if ($syntax == 'inline') {
return "\(" . $content . "\)";
}
else if ($syntax == 'display') {
return "\[" . $content . "\]";
}
}
function add_script(){
if( !self::$add_script )
return;
if( self::$block_script )
return;
//initialise option for existing MathJax-LaTeX users
if (get_option('kblog_mathjax_use_cdn')
|| !get_option('kblog_mathjax_custom_location')){
$mathjax_location = "http://cdn.mathjax.org/mathjax/latest/MathJax.js";
}
else{
$mathjax_location = get_option('kblog_mathjax_custom_location');
}
$mathjax_url = $mathjax_location . "?config=" .get_option( "kblog_mathjax_config" );
wp_register_script( 'mathjax',
$mathjax_url,
false, null, true );
wp_print_scripts( 'mathjax' );
}
function inline_to_shortcode( $content ) {
if ( false === strpos( $content, '$latex' ) )
return $content;
self::$add_script = true;
return preg_replace_callback( '#\$latex[= ](.*?[^\\\\])\$#',
array(__CLASS__,'inline_to_shortcode_callback'),
$content );
}
function inline_to_shortcode_callback( $matches ) {
// ##
// ## Also support wp-latex syntax. This includes the ability to set background and foreground
// ## colour, which we can ignore.
// ##
if ( preg_match( '/.+((?:&|&)s=(-?[0-4])).*/i',
$matches[1], $s_matches ) ) {
$matches[1] = str_replace( $s_matches[1], '', $matches[1] );
}
if ( preg_match( '/.+((?:&|&)fg=([0-9a-f]{6})).*/i',
$matches[1], $fg_matches ) ) {
$matches[1] = str_replace( $fg_matches[1], '', $matches[1] );
}
if ( preg_match( '/.+((?:&|&)bg=([0-9a-f]{6})).*/i',
$matches[1], $bg_matches ) ) {
$matches[1] = str_replace( $bg_matches[1], '', $matches[1] );
}
return "[latex]{$matches[1]}[/latex]";
}
//add a link to settings on the plugin management page
function mathjax_settings_link( $links, $file ) {
if ($file == 'mathjax-latex/mathjax-latex.php' && function_exists('admin_url')) {
$settings_link = '<a href="' .admin_url('options-general.php?page=kblog-mathjax-latex').'">'. __('Settings') . '</a>';
array_unshift($links, $settings_link);
}
return $links;
}
}
MathJax::init();
?>