-
Notifications
You must be signed in to change notification settings - Fork 9
/
simple-mathjax.php
369 lines (333 loc) · 11.2 KB
/
simple-mathjax.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/*
* Plugin Name: Simple MathJax
* Description: Load the mathjax scripts across your wordpress blog
* Version: 2.0.3
* Author: Samuel Coskey, Peter Krautzberger, Christian Lawson-Perfect
* Author URI: https://boolesrings.org
*/
class SimpleMathjax {
/*
* Default options for the plugin
*/
public static $default_options = array(
'major_version' => 3,
'mathjax_in_admin' => false,
'custom_mathjax_cdn' => '',
'custom_mathjax_config' => '',
'latex_preamble' => ''
);
/*
* Default MathJax configuration scripts, for each major version.
*/
public static $default_configs = array(
2 => "MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [['$','$'], ['\\\\(','\\\\)']],\n processEscapes: true,\n ignoreHtmlClass: 'tex2jax_ignore|editor-rich-text'\n }\n});\n",
3 => "MathJax = {\n tex: {\n inlineMath: [['$','$'],['\\\\(','\\\\)']], \n processEscapes: true\n },\n options: {\n ignoreHtmlClass: 'tex2jax_ignore|editor-rich-text'\n }\n};\n"
);
/*
* Default CDN URLs, for each major version.
*/
public static $default_cdns = array(
2 => "//cdn.jsdelivr.net/npm/mathjax@2.7.8/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js",
3 => "//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"
);
/*
* Load the plugin's options, beginning with the default options
*/
public static function load_options() {
$options = array_merge(self::$default_options,array());
// restore options from old versions of this plugin
$old_keys = array('custom_mathjax_cdn', 'custom_mathjax_config', 'latex_preamble', 'mathjax_in_admin');
$has_old_values = false;
foreach($old_keys as $key) {
if(($value = get_option($key)) !== false) {
$options[$key] = $value;
$has_old_values = true;
}
}
// apply options set locally
$set_options = get_option('simple_mathjax_options');
if(is_array($set_options)) {
foreach($set_options as $key=>$value) {
if(array_key_exists($key,$options)) {
$options[$key] = $value;
}
}
}
if($has_old_values) {
$options['major_version'] = 2;
foreach($old_keys as $key) {
delete_option($key);
}
add_option('simple_mathjax_options',$options);
}
return $options;
}
/*
* Insert the mathjax configuration into the page.
*/
public static function configure_mathjax() {
$options = self::load_options();
$version = $options['major_version'];
$custom_config = wp_kses( $options['custom_mathjax_config'], array() );
$config = $custom_config ? $custom_config : self::$default_configs[$version];
if($version==2) {
echo "\n<script type='text/x-mathjax-config'>\n{$config}\n</script>\n";
} else {
echo "\n<script>\n{$config}\n</script>\n";
}
}
/*
* Load the MathJax scripts.
*/
public static function add_mathjax() {
$options = self::load_options();
$version = $options['major_version'];
$custom_cdn = esc_url( $options['custom_mathjax_cdn'] );
$cdn = $custom_cdn ? $custom_cdn : self::$default_cdns[$version];
wp_enqueue_script('mathjax', $cdn, array(), false, true);
}
/*
* Insert the MathJax preamble inside the body and above the content.
*/
public static function add_preamble_adder() {
$options = self::load_options();
$version = $options['major_version'];
$preamble = $options['latex_preamble'];
if ( $preamble ) {
if($version==2) {
$preamble = preg_replace('/\\\\/','\\\\\\\\',$preamble);
?>
<script>
(function() {
var newContainer = document.createElement('span');
newContainer.style.setProperty("display","none","");
var newNode = document.createElement('script');
newNode.type = "math/tex";
var preamble = '<?php echo esc_js($preamble); ?>';
newNode.innerHTML = preamble;
newContainer.appendChild(newNode);
document.body.insertBefore(newContainer,document.body.firstChild);
})();
</script>
<?php
} else if($version==3) {
?>
<script>
(function() {
var newNode = document.createElement('span');
newNode.style.setProperty("display","none","");
var preamble = '\\( <?= esc_js(addslashes($preamble)); ?> \\)';
newNode.innerHTML = preamble;
document.body.insertBefore(newNode,document.body.firstChild);
})();
</script>
<?php
}
}
}
/*
* Validate an array of options: only keep keys corresponding to options, and set 'mathjax_in_admin' to a boolean.
*/
public static function options_validate($options) {
$cleaned = array();
foreach(self::$default_options as $key => $value) {
$cleaned[$key] = $options[$key];
}
$cleaned['mathjax_in_admin'] = $cleaned['mathjax_in_admin'] ? true : false;
return $cleaned;
}
/*
* Text at the top of the 'Main settings' section.
*/
public static function main_text() {
}
/*
* Text at the top of the 'Configuration' section.
*/
public static function config_text() {
}
/*
* Select box for the 'MathJax major version' option.
*/
public static function major_version_input() {
$options = self::load_options();
?>
<select id="major_version" name="simple_mathjax_options[major_version]">
<option value="2" <?= $options['major_version']==2 ? 'selected' : '' ?>>2</option>
<option value="3" <?= $options['major_version']==3 ? 'selected' : '' ?>>3</option>
</select>
<p>MathJax versions 2 and 3 work very differently. See the <a href="http://docs.mathjax.org/en/latest/upgrading/v2.html">MathJax documentation</a>.</p>
<?php
}
/*
* Tickbox for the 'Load MathJax on admin pages?' option.
*/
public static function in_admin_input() {
$options = self::load_options();
?>
<input type="checkbox" id="mathjax_in_admin" name="simple_mathjax_options[mathjax_in_admin]" <?= $options['mathjax_in_admin'] ? 'checked' : '' ?>>
<p>If you tick this box, MathJax will be loaded on admin pages as well as the actual site.</p>
<?php
}
/*
* Input box for the 'Custom MathJax CDN' option.
*/
public static function cdn_input() {
$options = self::load_options();
?>
<input type="text" id="custom_mathjax_cdn" size="50" name="simple_mathjax_options[custom_mathjax_cdn]" value="<?= $options['custom_mathjax_cdn'] ?>">
<p>If you leave this blank, the default will be used, depending on the major version of MathJax:</p>
<dl>
<dt>Version 2</dt>
<dd><code><?= self::$default_cdns[2] ?></code></dd>
<dt>Version 3</dt>
<dd><code><?= self::$default_cdns[3] ?></code></dd>
</dl>
<?php
}
/*
* Textarea for the 'Custom MathJax config' option.
*/
public static function config_input() {
$options = self::load_options();
?>
<textarea id="custom_mathjax_config" cols="50" rows="10" name="simple_mathjax_options[custom_mathjax_config]"><?= $options['custom_mathjax_config'] ?></textarea>
<p>This text will be used to configure MathJax. See <a href="https://docs.mathjax.org/en/latest/options/index.html">the documentation on configuring MathJax</a>.</p>
<p>If you leave this blank, the default will be used, according to the major version of MathJax:</p>
<dl>
<dt>Version 2</dt>
<dd><pre><?= self::$default_configs[2] ?></pre></dd>
<dt>Version 3</dt>
<dd><pre><?= self::$default_configs[3] ?></pre></dd>
</dl>
<?php
}
/*
* Textarea for the 'Custom LaTeX preamble' option.
*/
public static function latex_preamble_input() {
$options = self::load_options();
?>
<textarea id="latex_preamble" cols="50" rows="10" name="simple_mathjax_options[latex_preamble]"><?= $options['latex_preamble'] ?></textarea>
<p>This LaTeX will be run invisibly before any other LaTeX on the page. A good place to put \newcommand's and \renewcommand's</p>
<p><strong>Do not us $ signs</strong>, they will be added for you</p>
<p>E.g.</p>
<pre>\newcommand{\NN}{\mathbb N}
\newcommand{\abs}[1]{\left|#1\right|}</pre>
<?php
}
/*
* The options pane in the settings section
*/
public static function create_menu() {
$simple_mathjax_page = add_options_page(
'Simple MathJax options', // Name of page
'Simple MathJax', // Label in menu
'manage_options', // Capability required
'simple_mathjax_options', // Menu slug, used to uniquely identify the page
'SimpleMathJax::options_page' // Function that renders the options page
);
if ( ! $simple_mathjax_page )
return;
//call register settings function
add_action( 'admin_init', 'SimpleMathJax::register_settings' );
}
/*
* Render the options page.
*/
public static function options_page() {
?>
<div>
<h1>Simple MathJax options</h1>
<form method="post" action="options.php">
<?php settings_fields( 'simple_mathjax_options' ); ?>
<?php do_settings_sections('simple_mathjax'); ?>
<button type="submit"><?php _e('Save Changes'); ?></button>
</form>
</div>
<?php }
public static function register_settings() {
register_setting(
'simple_mathjax_options',
'simple_mathjax_options',
array(
'sanitize_callback' => 'SimpleMathJax::options_validate'
)
);
add_settings_section(
'simple_mathjax_main',
'Main Settings',
'SimpleMathJax::main_text',
'simple_mathjax'
);
add_settings_section('simple_mathjax_config',
'Configuration',
'SimpleMathJax::config_text',
'simple_mathjax'
);
add_settings_field(
'major_version',
'MathJax major version',
'SimpleMathJax::major_version_input',
'simple_mathjax',
'simple_mathjax_main',
array(
'label_for' => 'major_version'
)
);
add_settings_field(
'mathjax_in_admin',
'Load MathJax on admin pages?',
'SimpleMathJax::in_admin_input',
'simple_mathjax',
'simple_mathjax_main',
array(
'label_for' => 'mathjax_in_admin'
)
);
add_settings_field(
'custom_mathjax_cdn',
'Custom MathJax CDN',
'SimpleMathJax::cdn_input',
'simple_mathjax',
'simple_mathjax_config',
array(
'label_for' => 'custom_mathjax_cdn'
)
);
add_settings_field(
'custom_mathjax_config',
'Custom MathJax config',
'SimpleMathJax::config_input',
'simple_mathjax',
'simple_mathjax_config',
array(
'label_for' => 'custom_mathjax_config'
)
);
add_settings_field(
'latex_preamble',
'Custom LaTeX preamble',
'SimpleMathJax::latex_preamble_input',
'simple_mathjax',
'simple_mathjax_config',
array(
'label_for' => 'latex_preamble'
)
);
}
}
add_action('wp_head','SimpleMathJax::configure_mathjax',1);
add_action('wp_enqueue_scripts', 'SimpleMathJax::add_mathjax');
add_action('wp_footer', 'SimpleMathJax::add_preamble_adder');
/*
* Perform all three actions in admin pages too, if the option is set (CP)
*/
$options = SimpleMathJax::load_options();
if ( $options['mathjax_in_admin'] ) {
add_action('admin_head', 'SimpleMathJax::configure_mathjax', 1);
add_action('admin_enqueue_scripts', 'SimpleMathJax::add_mathjax');
add_action('admin_footer', 'SimpleMathJax::add_preamble_adder');
}
add_action('admin_menu', 'SimpleMathJax::create_menu');