Changeset 2172174
- Timestamp:
- 10/12/2019 01:08:51 PM (6 years ago)
- Location:
- simple-mathjax
- Files:
-
- 4 edited
- 1 copied
-
tags/2.0 (copied) (copied from simple-mathjax/trunk)
-
tags/2.0/readme.txt (modified) (2 diffs)
-
tags/2.0/simple-mathjax.php (modified) (6 diffs)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/simple-mathjax.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
simple-mathjax/tags/2.0/readme.txt
r2093199 r2172174 5 5 Tags: mathjax, latex 6 6 Requires at least: 3.0 7 Tested up to: 4.9.48 Stable tag: 1.17 Tested up to: 5.2.3 8 Stable tag: 2.0 9 9 10 10 Yet another plugin to add MathJax support to your wordpress blog. 11 Just wrap your equations inside $ signs and MathJax will render 12 them visually. 11 Just wrap your equations inside $ signs and MathJax will render them visually. 13 12 14 13 == Description == 15 14 16 This wordpress plugin is yet another simple plugin to load the 17 [MathJax](http://www.mathjax.org) scripts at the bottom of all of your 18 pages. It uses a very all-inclusive mathjax configuration by default, 19 with $'s and $$'s the default delimeters for in-line and displayed 20 equations. 15 This wordpress plugin is yet another simple plugin to load the [MathJax](http://www.mathjax.org) scripts at the bottom of all of your pages. It uses a very all-inclusive mathjax configuration by default, with $'s and $$'s the default delimeters for in-line and displayed equations. 21 16 22 A preference pane is added to the "Settings" group where you can 23 modify the MathJax server location (CDN) and the MathJax configuration 24 settings. (See [this 25 page](https://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options) 26 for details on the options available.) You can also specify a LaTeX 27 "preamble" of newcommands which will be loaded in a hidden element 28 near the top of each page. 17 A preference pane is added to the "Settings" group where you can choose whether to use MathJax version 2 or 3, change the MathJax server location (CDN) and the MathJax configuration settings. (See [this page](https://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options) for details on the options available.) 18 You can also specify a LaTeX "preamble" of newcommands which will be loaded in a hidden element near the top of each page. 29 19 30 Fork this plugin on 31 [GitHub](https://github.com/scoskey/Simple-Mathjax-wordpress-plugin) 20 Fork this plugin on [GitHub](https://github.com/boolesrings/Simple-Mathjax-wordpress-plugin). 32 21 33 22 == Installation == … … 37 26 == Changelog == 38 27 39 `1.1` update latest mathjax version, load mathjax in footer instead of header 28 = 2.0 = 29 MathJax 3 support, and the default config makes MathJax ignore the new block editor. 40 30 41 `1.0` send default url to new cdn 31 = 1.1 = 32 update latest mathjax version, load mathjax in footer instead of header 42 33 43 `0.5` minor code cleanup, allow mathjax in admin screens 34 = 1.0 = 35 send default url to new cdn 44 36 45 `0.4` use safe mode (prevents evil scripts) by default 37 = 0.5 = 38 minor code cleanup, allow mathjax in admin screens 46 39 47 `0.3` use wp_enqueue_script to allow others to use mathjax as a dependency 48 (christianp). removed disqus compatibility due to reports of it no longer 49 working. 40 = 0.4 = 41 use safe mode (prevents evil scripts) by default 50 42 51 `0.2` added disqus compatibility. enclosed the preamble in a hidden span to 43 = 0.3 = 44 use wp_enqueue_script to allow others to use mathjax as a dependency (christianp). removed disqus compatibility due to reports of it no longer working. 45 46 = 0.2 = 47 added disqus compatibility. enclosed the preamble in a hidden span to 52 48 cover a small space created by mathjax v.2 53 49 54 `0.1` improved loading of the LaTeX preamble so that it appears just 50 = 0.1 = 51 improved loading of the LaTeX preamble so that it appears just 55 52 below the body tag, rather than in the header 56 53 57 `0.0` initial release 54 = 0.0 = 55 initial release -
simple-mathjax/tags/2.0/simple-mathjax.php
r1840306 r2172174 3 3 * Plugin Name: Simple Mathjax 4 4 * Description: Load the mathjax scripts across your wordpress blog 5 * Version: 1.16 * Author: Samuel Coskey, Peter Krautzberger 5 * Version: 2.0 6 * Author: Samuel Coskey, Peter Krautzberger, Christian Lawson-Perfect 7 7 * Author URI: https://boolesrings.org 8 8 */ 9 9 10 10 11 $default_options = array( 12 'major_version' => 3, 13 'mathjax_in_admin' => false, 14 'custom_mathjax_cdn' => '', 15 'custom_mathjax_config' => '', 16 'latex_preamble' => '' 17 ); 18 $default_configs = array( 19 2 => "MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']], processEscapes: true}});\n", 20 3 => "MathJax = {tex: {inlineMath: [['$','$'],['\\\\(','\\\\)']], processEscapes: true}};\n" 21 ); 22 $default_cdns = array( 23 2 => "//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js", 24 3 => "//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js" 25 ); 26 27 function load_options() { 28 global $default_options; 29 $options = array_merge($default_options,array()); 30 31 // restore options from old versions of this plugin 32 $old_keys = array('custom_mathjax_cdn', 'custom_mathjax_config', 'latex_preamble', 'mathjax_in_admin'); 33 $has_old_values = false; 34 foreach($old_keys as $key) { 35 if(($value = get_option($key)) !== false) { 36 $options[$key] = $value; 37 $has_old_values = true; 38 } 39 } 40 41 42 // apply options set locally 43 $set_options = get_option('simple_mathjax_options'); 44 if(is_array($set_options)) { 45 foreach($set_options as $key=>$value) { 46 if(array_key_exists($key,$options)) { 47 $options[$key] = $value; 48 } 49 } 50 } 51 52 if($has_old_values) { 53 $options['major_version'] = 2; 54 foreach($old_keys as $key) { 55 delete_option($key); 56 } 57 add_option('simple_mathjax_options',$options); 58 } 59 60 return $options; 61 } 62 63 11 64 /* 12 65 * inserts the mathjax configuration 13 66 */ 67 14 68 add_action('wp_head','configure_mathjax',1); 15 69 function configure_mathjax() { 16 $custom_config = wp_kses( get_option('custom_mathjax_config'), array() ); 17 $config = $custom_config ? $custom_config : "MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']], processEscapes: true}});\n"; 18 echo "\n<script type='text/x-mathjax-config'>\n" . $config . "</script>\n"; 70 global $default_configs; 71 $options = load_options(); 72 $version = $options['major_version']; 73 $custom_config = wp_kses( $options['custom_mathjax_config'], array() ); 74 $config = $custom_config ? $custom_config : $default_configs[$version]; 75 if($version==2) { 76 echo "\n<script type='text/x-mathjax-config'>\n{$config}\n</script>\n"; 77 } else { 78 echo "\n<script>\n{$config}\n</script>\n"; 79 } 19 80 } 20 81 … … 24 85 add_action('wp_enqueue_scripts', 'add_mathjax'); 25 86 function add_mathjax() { 26 $custom_cdn = esc_url( get_option('custom_mathjax_cdn') ); 27 $cdn = $custom_cdn ? $custom_cdn : "//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js"; 87 global $default_cdns; 88 $options = load_options(); 89 $version = $options['major_version']; 90 $custom_cdn = esc_url( $options['custom_mathjax_cdn'] ); 91 $cdn = $custom_cdn ? $custom_cdn : $default_cdns[$version]; 28 92 wp_enqueue_script('mathjax', $cdn, array(), false, true); 29 93 } … … 34 98 add_action('wp_footer', 'add_preamble_adder'); 35 99 function add_preamble_adder() { 36 $preamble = get_option('latex_preamble'); 100 $options = load_options(); 101 $version = $options['major_version']; 102 $preamble = $options['latex_preamble']; 37 103 if ( $preamble ) { 38 $preamble = preg_replace('/\\\\/','\\\\\\\\',$preamble); 39 ?> 40 <script type='text/javascript'> 41 newContainer = document.createElement('span'); 42 newContainer.style.setProperty("display","none",""); 43 newNode = document.createElement('script'); 44 newNode.type = "math/tex"; 45 newNode.innerHTML = '<?php echo esc_js($preamble); ?>'; 46 newContainer.appendChild(newNode); 47 document.body.insertBefore(newContainer,document.body.firstChild); 104 if($version==2) { 105 $preamble = preg_replace('/\\\\/','\\\\\\\\',$preamble); 106 107 ?> 108 <script> 109 (function() { 110 var newContainer = document.createElement('span'); 111 newContainer.style.setProperty("display","none",""); 112 var newNode = document.createElement('script'); 113 newNode.type = "math/tex"; 114 var preamble = '<?php echo esc_js($preamble); ?>'; 115 newNode.innerHTML = preamble; 116 newContainer.appendChild(newNode); 117 document.body.insertBefore(newContainer,document.body.firstChild); 118 })(); 48 119 </script> 49 120 <?php 121 122 } else if($version==3) { 123 124 ?> 125 <script> 126 (function() { 127 var newNode = document.createElement('span'); 128 newNode.style.setProperty("display","none",""); 129 var preamble = '\\( <?= esc_js(addslashes($preamble)); ?> \\)'; 130 newNode.innerHTML = preamble; 131 document.body.insertBefore(newNode,document.body.firstChild); 132 })(); 133 </script> 134 <?php 135 136 } 50 137 } 51 138 } … … 53 140 /* 54 141 * Perform all three actions in admin pages too, if the option is set (CP) 55 */ 56 if ( get_option( 'mathjax_in_admin' ) ) { 57 add_action('admin_head', 'configure_mathjax', 1); 58 add_action('admin_enqueue_scripts', 'add_mathjax'); 59 add_action('admin_footer', 'add_preamble_adder'); 142 */ 143 $options = load_options(); 144 if ( $options['mathjax_in_admin'] ) { 145 add_action('admin_head', 'configure_mathjax', 1); 146 add_action('admin_enqueue_scripts', 'add_mathjax'); 147 add_action('admin_footer', 'add_preamble_adder'); 60 148 } 61 149 … … 70 158 'Simple Mathjax', // Label in menu 71 159 'manage_options', // Capability required 72 'simple_mathjax_ identifier', // Menu slug, used to uniquely identify the page73 'simple_mathjax_options ' // Function that renders the options page160 'simple_mathjax_options', // Menu slug, used to uniquely identify the page 161 'simple_mathjax_options_page' // Function that renders the options page 74 162 ); 75 163 … … 80 168 add_action( 'admin_init', 'register_simple_mathjax_settings' ); 81 169 } 82 function register_simple_mathjax_settings() { 83 register_setting( 'simple_mathjax_group', 'custom_mathjax_cdn' ); 84 register_setting( 'simple_mathjax_group', 'custom_mathjax_config' ); 85 register_setting( 'simple_mathjax_group', 'latex_preamble' ); 86 register_setting( 'simple_mathjax_group', 'mathjax_in_admin' ); 87 } 88 function simple_mathjax_options() { 89 ?> 90 <div class="wrap"> 91 <h2>Simple Mathjax options</h2> 92 <p> 93 (Please note that this still needs to be tested. There may be a problem with string-escaping. For instance, I'm not sure if special characters such as < will be properly processed.) 94 </p> 170 171 function simple_mathjax_options_page() { 172 ?> 173 <div> 174 <h1>Simple Mathjax options</h1> 95 175 <form method="post" action="options.php"> 96 <?php settings_fields( 'simple_mathjax_group' ); ?> 97 <table class="form-table"> 98 <tr valign="top"> 99 <th scope="row">Custom mathjax CDN</th> 100 <td><input type="text" name="custom_mathjax_cdn" size="50" value="<?php echo esc_url( get_option('custom_mathjax_cdn') ); ?>" /></td> 101 <td><p>If you leave this blank, the default will be used: <code>//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js</code></p></td> 102 </tr> 103 <tr valign="top"> 104 <th scope="row">Custom mathjax config</th> 105 <td><textarea name="custom_mathjax_config" cols="50" rows="10"/><?php echo esc_textarea(get_option('custom_mathjax_config')); ?></textarea></td> 106 <td><p>This text will be placed inside the <code><script x-mathjax-config></code> tag</p><p>If you leave this blank, the default will be used: <code>MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});</code></p></td> 107 </tr> 108 <tr valign="top"> 109 <th scope="row">Custom LaTeX preamble</th> 110 <td><textarea name="latex_preamble" cols="50" rows="10"/><?php echo esc_textarea(get_option('latex_preamble')); ?></textarea></td> 111 <td><p>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.<br/><code>\newcommand{\NN}{\mathbb N}<br/>\newcommand{\abs}[1]{\left|#1\right|}</code></p></td> 112 </tr> 113 <tr valign="top"> 114 <th scope="row">Load MathJax on admin pages</th> 115 <td><input type="checkbox" name="mathjax_in_admin" value="yes" <?php if( get_option('mathjax_in_admin') ) { echo "checked"; } ?> /></td> 116 <td><p>If you tick this box, MathJax will be loaded on admin pages as well as the actual site.</p></td> 117 </tr> 118 </table> 119 <p class="submit"> 120 <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> 121 </p> 176 <?php settings_fields( 'simple_mathjax_options' ); ?> 177 <?php do_settings_sections('simple_mathjax'); ?> 178 179 <button type="submit"><?php _e('Save Changes'); ?></button> 122 180 </form> 123 181 </div> 124 182 <?php } 125 183 126 127 ?> 184 function register_simple_mathjax_settings() { 185 global $default_options; 186 register_setting( 187 'simple_mathjax_options', 188 'simple_mathjax_options', 189 array( 190 'sanitize_callback' => 'simple_mathjax_options_validate' 191 ) 192 ); 193 194 add_settings_section( 195 'simple_mathjax_main', 196 'Main Settings', 197 'simple_mathjax_main_text', 198 'simple_mathjax' 199 ); 200 add_settings_section('simple_mathjax_config', 201 'Configuration', 202 'simple_mathjax_config_text', 203 'simple_mathjax' 204 ); 205 206 add_settings_field( 207 'major_version', 208 'MathJax major version', 209 'simple_mathjax_major_version_input', 210 'simple_mathjax', 211 'simple_mathjax_main', 212 array( 213 'label_for' => 'major_version' 214 ) 215 ); 216 add_settings_field( 217 'mathjax_in_admin', 218 'Load MathJax on admin pages?', 219 'simple_mathjax_in_admin_input', 220 'simple_mathjax', 221 'simple_mathjax_main', 222 array( 223 'label_for' => 'mathjax_in_admin' 224 ) 225 ); 226 227 add_settings_field( 228 'custom_mathjax_cdn', 229 'Custom MathJax CDN', 230 'simple_mathjax_cdn_input', 231 'simple_mathjax', 232 'simple_mathjax_config', 233 array( 234 'label_for' => 'custom_mathjax_cdn' 235 ) 236 ); 237 add_settings_field( 238 'custom_mathjax_config', 239 'Custom MathJax config', 240 'simple_mathjax_config_input', 241 'simple_mathjax', 242 'simple_mathjax_config', 243 array( 244 'label_for' => 'custom_mathjax_config' 245 ) 246 ); 247 add_settings_field( 248 'latex_preamble', 249 'Custom LaTeX preamble', 250 'simple_mathjax_latex_preamble_input', 251 'simple_mathjax', 252 'simple_mathjax_config', 253 array( 254 'label_for' => 'latex_preamble' 255 ) 256 ); 257 258 } 259 260 function simple_mathjax_options_validate($options) { 261 global $default_options; 262 $cleaned = array(); 263 foreach($default_options as $key => $value) { 264 $cleaned[$key] = $options[$key]; 265 } 266 $cleaned['mathjax_in_admin'] = $cleaned['mathjax_in_admin'] ? true : false; 267 return $cleaned; 268 } 269 270 function simple_mathjax_main_text() { 271 } 272 273 function simple_mathjax_config_text() { 274 } 275 276 function simple_mathjax_major_version_input() { 277 $options = load_options(); 278 ?> 279 <select id="major_version" name="simple_mathjax_options[major_version]"> 280 <option value="2" <?= $options['major_version']==2 ? 'selected' : '' ?>>2</option> 281 <option value="3" <?= $options['major_version']==3 ? 'selected' : '' ?>>3</option> 282 </select> 283 <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> 284 <?php 285 } 286 287 function simple_mathjax_in_admin_input() { 288 $options = load_options(); 289 ?> 290 <input type="checkbox" id="mathjax_in_admin" name="simple_mathjax_options[mathjax_in_admin]" <?= $options['mathjax_in_admin'] ? 'checked' : '' ?>> 291 <p>If you tick this box, MathJax will be loaded on admin pages as well as the actual site.</p> 292 <?php 293 } 294 295 function simple_mathjax_cdn_input() { 296 global $default_cdns; 297 $options = load_options(); 298 ?> 299 <input type="text" id="custom_mathjax_cdn" size="50" name="simple_mathjax_options[custom_mathjax_cdn]" value="<?= $options['custom_mathjax_cdn'] ?>"> 300 <p>If you leave this blank, the default will be used, depending on the major version of MathJax:</p> 301 <dl> 302 <dt>Version 2</dt> 303 <dd><code><?= $default_cdns[2] ?></code></dd> 304 <dt>Version 3</dt> 305 <dd><code><?= $default_cdns[3] ?></code></dd> 306 </dl> 307 <?php 308 } 309 310 function simple_mathjax_config_input() { 311 global $default_configs; 312 $options = load_options(); 313 ?> 314 <textarea id="custom_mathjax_config" cols="50" rows="10" name="simple_mathjax_options[custom_mathjax_config]"><?= $options['custom_mathjax_config'] ?></textarea> 315 <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> 316 <p>If you leave this blank, the default will be used, according to the major version of MathJax:</p> 317 <dl> 318 <dt>Version 2</dt> 319 <dd><pre><?= $default_configs[2] ?></pre></dd> 320 <dt>Version 3</dt> 321 <dd><pre><?= $default_configs[3] ?></pre></dd> 322 </dl> 323 <?php 324 } 325 326 function simple_mathjax_latex_preamble_input() { 327 $options = load_options(); 328 ?> 329 <textarea id="latex_preamble" cols="50" rows="10" name="simple_mathjax_options[latex_preamble]"><?= $options['latex_preamble'] ?></textarea> 330 <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> 331 <p><strong>Do not us $ signs</strong>, they will be added for you</p> 332 <p>E.g.</p> 333 <pre>\newcommand{\NN}{\mathbb N} 334 \newcommand{\abs}[1]{\left|#1\right|}</pre> 335 <?php 336 } -
simple-mathjax/trunk/readme.txt
r2093199 r2172174 5 5 Tags: mathjax, latex 6 6 Requires at least: 3.0 7 Tested up to: 4.9.48 Stable tag: 1.17 Tested up to: 5.2.3 8 Stable tag: 2.0 9 9 10 10 Yet another plugin to add MathJax support to your wordpress blog. 11 Just wrap your equations inside $ signs and MathJax will render 12 them visually. 11 Just wrap your equations inside $ signs and MathJax will render them visually. 13 12 14 13 == Description == 15 14 16 This wordpress plugin is yet another simple plugin to load the 17 [MathJax](http://www.mathjax.org) scripts at the bottom of all of your 18 pages. It uses a very all-inclusive mathjax configuration by default, 19 with $'s and $$'s the default delimeters for in-line and displayed 20 equations. 15 This wordpress plugin is yet another simple plugin to load the [MathJax](http://www.mathjax.org) scripts at the bottom of all of your pages. It uses a very all-inclusive mathjax configuration by default, with $'s and $$'s the default delimeters for in-line and displayed equations. 21 16 22 A preference pane is added to the "Settings" group where you can 23 modify the MathJax server location (CDN) and the MathJax configuration 24 settings. (See [this 25 page](https://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options) 26 for details on the options available.) You can also specify a LaTeX 27 "preamble" of newcommands which will be loaded in a hidden element 28 near the top of each page. 17 A preference pane is added to the "Settings" group where you can choose whether to use MathJax version 2 or 3, change the MathJax server location (CDN) and the MathJax configuration settings. (See [this page](https://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options) for details on the options available.) 18 You can also specify a LaTeX "preamble" of newcommands which will be loaded in a hidden element near the top of each page. 29 19 30 Fork this plugin on 31 [GitHub](https://github.com/scoskey/Simple-Mathjax-wordpress-plugin) 20 Fork this plugin on [GitHub](https://github.com/boolesrings/Simple-Mathjax-wordpress-plugin). 32 21 33 22 == Installation == … … 37 26 == Changelog == 38 27 39 `1.1` update latest mathjax version, load mathjax in footer instead of header 28 = 2.0 = 29 MathJax 3 support, and the default config makes MathJax ignore the new block editor. 40 30 41 `1.0` send default url to new cdn 31 = 1.1 = 32 update latest mathjax version, load mathjax in footer instead of header 42 33 43 `0.5` minor code cleanup, allow mathjax in admin screens 34 = 1.0 = 35 send default url to new cdn 44 36 45 `0.4` use safe mode (prevents evil scripts) by default 37 = 0.5 = 38 minor code cleanup, allow mathjax in admin screens 46 39 47 `0.3` use wp_enqueue_script to allow others to use mathjax as a dependency 48 (christianp). removed disqus compatibility due to reports of it no longer 49 working. 40 = 0.4 = 41 use safe mode (prevents evil scripts) by default 50 42 51 `0.2` added disqus compatibility. enclosed the preamble in a hidden span to 43 = 0.3 = 44 use wp_enqueue_script to allow others to use mathjax as a dependency (christianp). removed disqus compatibility due to reports of it no longer working. 45 46 = 0.2 = 47 added disqus compatibility. enclosed the preamble in a hidden span to 52 48 cover a small space created by mathjax v.2 53 49 54 `0.1` improved loading of the LaTeX preamble so that it appears just 50 = 0.1 = 51 improved loading of the LaTeX preamble so that it appears just 55 52 below the body tag, rather than in the header 56 53 57 `0.0` initial release 54 = 0.0 = 55 initial release -
simple-mathjax/trunk/simple-mathjax.php
r1840306 r2172174 3 3 * Plugin Name: Simple Mathjax 4 4 * Description: Load the mathjax scripts across your wordpress blog 5 * Version: 1.16 * Author: Samuel Coskey, Peter Krautzberger 5 * Version: 2.0 6 * Author: Samuel Coskey, Peter Krautzberger, Christian Lawson-Perfect 7 7 * Author URI: https://boolesrings.org 8 8 */ 9 9 10 10 11 $default_options = array( 12 'major_version' => 3, 13 'mathjax_in_admin' => false, 14 'custom_mathjax_cdn' => '', 15 'custom_mathjax_config' => '', 16 'latex_preamble' => '' 17 ); 18 $default_configs = array( 19 2 => "MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']], processEscapes: true}});\n", 20 3 => "MathJax = {tex: {inlineMath: [['$','$'],['\\\\(','\\\\)']], processEscapes: true}};\n" 21 ); 22 $default_cdns = array( 23 2 => "//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js", 24 3 => "//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js" 25 ); 26 27 function load_options() { 28 global $default_options; 29 $options = array_merge($default_options,array()); 30 31 // restore options from old versions of this plugin 32 $old_keys = array('custom_mathjax_cdn', 'custom_mathjax_config', 'latex_preamble', 'mathjax_in_admin'); 33 $has_old_values = false; 34 foreach($old_keys as $key) { 35 if(($value = get_option($key)) !== false) { 36 $options[$key] = $value; 37 $has_old_values = true; 38 } 39 } 40 41 42 // apply options set locally 43 $set_options = get_option('simple_mathjax_options'); 44 if(is_array($set_options)) { 45 foreach($set_options as $key=>$value) { 46 if(array_key_exists($key,$options)) { 47 $options[$key] = $value; 48 } 49 } 50 } 51 52 if($has_old_values) { 53 $options['major_version'] = 2; 54 foreach($old_keys as $key) { 55 delete_option($key); 56 } 57 add_option('simple_mathjax_options',$options); 58 } 59 60 return $options; 61 } 62 63 11 64 /* 12 65 * inserts the mathjax configuration 13 66 */ 67 14 68 add_action('wp_head','configure_mathjax',1); 15 69 function configure_mathjax() { 16 $custom_config = wp_kses( get_option('custom_mathjax_config'), array() ); 17 $config = $custom_config ? $custom_config : "MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']], processEscapes: true}});\n"; 18 echo "\n<script type='text/x-mathjax-config'>\n" . $config . "</script>\n"; 70 global $default_configs; 71 $options = load_options(); 72 $version = $options['major_version']; 73 $custom_config = wp_kses( $options['custom_mathjax_config'], array() ); 74 $config = $custom_config ? $custom_config : $default_configs[$version]; 75 if($version==2) { 76 echo "\n<script type='text/x-mathjax-config'>\n{$config}\n</script>\n"; 77 } else { 78 echo "\n<script>\n{$config}\n</script>\n"; 79 } 19 80 } 20 81 … … 24 85 add_action('wp_enqueue_scripts', 'add_mathjax'); 25 86 function add_mathjax() { 26 $custom_cdn = esc_url( get_option('custom_mathjax_cdn') ); 27 $cdn = $custom_cdn ? $custom_cdn : "//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js"; 87 global $default_cdns; 88 $options = load_options(); 89 $version = $options['major_version']; 90 $custom_cdn = esc_url( $options['custom_mathjax_cdn'] ); 91 $cdn = $custom_cdn ? $custom_cdn : $default_cdns[$version]; 28 92 wp_enqueue_script('mathjax', $cdn, array(), false, true); 29 93 } … … 34 98 add_action('wp_footer', 'add_preamble_adder'); 35 99 function add_preamble_adder() { 36 $preamble = get_option('latex_preamble'); 100 $options = load_options(); 101 $version = $options['major_version']; 102 $preamble = $options['latex_preamble']; 37 103 if ( $preamble ) { 38 $preamble = preg_replace('/\\\\/','\\\\\\\\',$preamble); 39 ?> 40 <script type='text/javascript'> 41 newContainer = document.createElement('span'); 42 newContainer.style.setProperty("display","none",""); 43 newNode = document.createElement('script'); 44 newNode.type = "math/tex"; 45 newNode.innerHTML = '<?php echo esc_js($preamble); ?>'; 46 newContainer.appendChild(newNode); 47 document.body.insertBefore(newContainer,document.body.firstChild); 104 if($version==2) { 105 $preamble = preg_replace('/\\\\/','\\\\\\\\',$preamble); 106 107 ?> 108 <script> 109 (function() { 110 var newContainer = document.createElement('span'); 111 newContainer.style.setProperty("display","none",""); 112 var newNode = document.createElement('script'); 113 newNode.type = "math/tex"; 114 var preamble = '<?php echo esc_js($preamble); ?>'; 115 newNode.innerHTML = preamble; 116 newContainer.appendChild(newNode); 117 document.body.insertBefore(newContainer,document.body.firstChild); 118 })(); 48 119 </script> 49 120 <?php 121 122 } else if($version==3) { 123 124 ?> 125 <script> 126 (function() { 127 var newNode = document.createElement('span'); 128 newNode.style.setProperty("display","none",""); 129 var preamble = '\\( <?= esc_js(addslashes($preamble)); ?> \\)'; 130 newNode.innerHTML = preamble; 131 document.body.insertBefore(newNode,document.body.firstChild); 132 })(); 133 </script> 134 <?php 135 136 } 50 137 } 51 138 } … … 53 140 /* 54 141 * Perform all three actions in admin pages too, if the option is set (CP) 55 */ 56 if ( get_option( 'mathjax_in_admin' ) ) { 57 add_action('admin_head', 'configure_mathjax', 1); 58 add_action('admin_enqueue_scripts', 'add_mathjax'); 59 add_action('admin_footer', 'add_preamble_adder'); 142 */ 143 $options = load_options(); 144 if ( $options['mathjax_in_admin'] ) { 145 add_action('admin_head', 'configure_mathjax', 1); 146 add_action('admin_enqueue_scripts', 'add_mathjax'); 147 add_action('admin_footer', 'add_preamble_adder'); 60 148 } 61 149 … … 70 158 'Simple Mathjax', // Label in menu 71 159 'manage_options', // Capability required 72 'simple_mathjax_ identifier', // Menu slug, used to uniquely identify the page73 'simple_mathjax_options ' // Function that renders the options page160 'simple_mathjax_options', // Menu slug, used to uniquely identify the page 161 'simple_mathjax_options_page' // Function that renders the options page 74 162 ); 75 163 … … 80 168 add_action( 'admin_init', 'register_simple_mathjax_settings' ); 81 169 } 82 function register_simple_mathjax_settings() { 83 register_setting( 'simple_mathjax_group', 'custom_mathjax_cdn' ); 84 register_setting( 'simple_mathjax_group', 'custom_mathjax_config' ); 85 register_setting( 'simple_mathjax_group', 'latex_preamble' ); 86 register_setting( 'simple_mathjax_group', 'mathjax_in_admin' ); 87 } 88 function simple_mathjax_options() { 89 ?> 90 <div class="wrap"> 91 <h2>Simple Mathjax options</h2> 92 <p> 93 (Please note that this still needs to be tested. There may be a problem with string-escaping. For instance, I'm not sure if special characters such as < will be properly processed.) 94 </p> 170 171 function simple_mathjax_options_page() { 172 ?> 173 <div> 174 <h1>Simple Mathjax options</h1> 95 175 <form method="post" action="options.php"> 96 <?php settings_fields( 'simple_mathjax_group' ); ?> 97 <table class="form-table"> 98 <tr valign="top"> 99 <th scope="row">Custom mathjax CDN</th> 100 <td><input type="text" name="custom_mathjax_cdn" size="50" value="<?php echo esc_url( get_option('custom_mathjax_cdn') ); ?>" /></td> 101 <td><p>If you leave this blank, the default will be used: <code>//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js</code></p></td> 102 </tr> 103 <tr valign="top"> 104 <th scope="row">Custom mathjax config</th> 105 <td><textarea name="custom_mathjax_config" cols="50" rows="10"/><?php echo esc_textarea(get_option('custom_mathjax_config')); ?></textarea></td> 106 <td><p>This text will be placed inside the <code><script x-mathjax-config></code> tag</p><p>If you leave this blank, the default will be used: <code>MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});</code></p></td> 107 </tr> 108 <tr valign="top"> 109 <th scope="row">Custom LaTeX preamble</th> 110 <td><textarea name="latex_preamble" cols="50" rows="10"/><?php echo esc_textarea(get_option('latex_preamble')); ?></textarea></td> 111 <td><p>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.<br/><code>\newcommand{\NN}{\mathbb N}<br/>\newcommand{\abs}[1]{\left|#1\right|}</code></p></td> 112 </tr> 113 <tr valign="top"> 114 <th scope="row">Load MathJax on admin pages</th> 115 <td><input type="checkbox" name="mathjax_in_admin" value="yes" <?php if( get_option('mathjax_in_admin') ) { echo "checked"; } ?> /></td> 116 <td><p>If you tick this box, MathJax will be loaded on admin pages as well as the actual site.</p></td> 117 </tr> 118 </table> 119 <p class="submit"> 120 <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> 121 </p> 176 <?php settings_fields( 'simple_mathjax_options' ); ?> 177 <?php do_settings_sections('simple_mathjax'); ?> 178 179 <button type="submit"><?php _e('Save Changes'); ?></button> 122 180 </form> 123 181 </div> 124 182 <?php } 125 183 126 127 ?> 184 function register_simple_mathjax_settings() { 185 global $default_options; 186 register_setting( 187 'simple_mathjax_options', 188 'simple_mathjax_options', 189 array( 190 'sanitize_callback' => 'simple_mathjax_options_validate' 191 ) 192 ); 193 194 add_settings_section( 195 'simple_mathjax_main', 196 'Main Settings', 197 'simple_mathjax_main_text', 198 'simple_mathjax' 199 ); 200 add_settings_section('simple_mathjax_config', 201 'Configuration', 202 'simple_mathjax_config_text', 203 'simple_mathjax' 204 ); 205 206 add_settings_field( 207 'major_version', 208 'MathJax major version', 209 'simple_mathjax_major_version_input', 210 'simple_mathjax', 211 'simple_mathjax_main', 212 array( 213 'label_for' => 'major_version' 214 ) 215 ); 216 add_settings_field( 217 'mathjax_in_admin', 218 'Load MathJax on admin pages?', 219 'simple_mathjax_in_admin_input', 220 'simple_mathjax', 221 'simple_mathjax_main', 222 array( 223 'label_for' => 'mathjax_in_admin' 224 ) 225 ); 226 227 add_settings_field( 228 'custom_mathjax_cdn', 229 'Custom MathJax CDN', 230 'simple_mathjax_cdn_input', 231 'simple_mathjax', 232 'simple_mathjax_config', 233 array( 234 'label_for' => 'custom_mathjax_cdn' 235 ) 236 ); 237 add_settings_field( 238 'custom_mathjax_config', 239 'Custom MathJax config', 240 'simple_mathjax_config_input', 241 'simple_mathjax', 242 'simple_mathjax_config', 243 array( 244 'label_for' => 'custom_mathjax_config' 245 ) 246 ); 247 add_settings_field( 248 'latex_preamble', 249 'Custom LaTeX preamble', 250 'simple_mathjax_latex_preamble_input', 251 'simple_mathjax', 252 'simple_mathjax_config', 253 array( 254 'label_for' => 'latex_preamble' 255 ) 256 ); 257 258 } 259 260 function simple_mathjax_options_validate($options) { 261 global $default_options; 262 $cleaned = array(); 263 foreach($default_options as $key => $value) { 264 $cleaned[$key] = $options[$key]; 265 } 266 $cleaned['mathjax_in_admin'] = $cleaned['mathjax_in_admin'] ? true : false; 267 return $cleaned; 268 } 269 270 function simple_mathjax_main_text() { 271 } 272 273 function simple_mathjax_config_text() { 274 } 275 276 function simple_mathjax_major_version_input() { 277 $options = load_options(); 278 ?> 279 <select id="major_version" name="simple_mathjax_options[major_version]"> 280 <option value="2" <?= $options['major_version']==2 ? 'selected' : '' ?>>2</option> 281 <option value="3" <?= $options['major_version']==3 ? 'selected' : '' ?>>3</option> 282 </select> 283 <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> 284 <?php 285 } 286 287 function simple_mathjax_in_admin_input() { 288 $options = load_options(); 289 ?> 290 <input type="checkbox" id="mathjax_in_admin" name="simple_mathjax_options[mathjax_in_admin]" <?= $options['mathjax_in_admin'] ? 'checked' : '' ?>> 291 <p>If you tick this box, MathJax will be loaded on admin pages as well as the actual site.</p> 292 <?php 293 } 294 295 function simple_mathjax_cdn_input() { 296 global $default_cdns; 297 $options = load_options(); 298 ?> 299 <input type="text" id="custom_mathjax_cdn" size="50" name="simple_mathjax_options[custom_mathjax_cdn]" value="<?= $options['custom_mathjax_cdn'] ?>"> 300 <p>If you leave this blank, the default will be used, depending on the major version of MathJax:</p> 301 <dl> 302 <dt>Version 2</dt> 303 <dd><code><?= $default_cdns[2] ?></code></dd> 304 <dt>Version 3</dt> 305 <dd><code><?= $default_cdns[3] ?></code></dd> 306 </dl> 307 <?php 308 } 309 310 function simple_mathjax_config_input() { 311 global $default_configs; 312 $options = load_options(); 313 ?> 314 <textarea id="custom_mathjax_config" cols="50" rows="10" name="simple_mathjax_options[custom_mathjax_config]"><?= $options['custom_mathjax_config'] ?></textarea> 315 <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> 316 <p>If you leave this blank, the default will be used, according to the major version of MathJax:</p> 317 <dl> 318 <dt>Version 2</dt> 319 <dd><pre><?= $default_configs[2] ?></pre></dd> 320 <dt>Version 3</dt> 321 <dd><pre><?= $default_configs[3] ?></pre></dd> 322 </dl> 323 <?php 324 } 325 326 function simple_mathjax_latex_preamble_input() { 327 $options = load_options(); 328 ?> 329 <textarea id="latex_preamble" cols="50" rows="10" name="simple_mathjax_options[latex_preamble]"><?= $options['latex_preamble'] ?></textarea> 330 <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> 331 <p><strong>Do not us $ signs</strong>, they will be added for you</p> 332 <p>E.g.</p> 333 <pre>\newcommand{\NN}{\mathbb N} 334 \newcommand{\abs}[1]{\left|#1\right|}</pre> 335 <?php 336 }
Note: See TracChangeset
for help on using the changeset viewer.