PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.7.5
Jetpack – WP Security, Backup, Speed, & Growth v2.7.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / latex.php

latex.php in Jetpack – WP Security, Backup, Speed, & Growth 2.7.5, at modules/latex.php

107 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module Name: Beautiful Math
4 * Module Description: Mark up your posts with the <img src="//s0.wp.com/latex.php?latex=%5CLaTeX&amp;bg=transparent&amp;fg=000&amp;s=-2" alt="LaTeX logo" title="LaTeX" style="vertical-align: -25%" /> markup language, perfect for complex mathematical equations and other &#252;ber-geekery.
5 * Sort Order: 12
6 * First Introduced: 1.1
7 * Requires Connection: No
8 * Auto Activate: Yes
9 * Module Tags: Writing
10 */
11
12 /**
13 * LaTeX support.
14 *
15 * Backward compatibility requires support for both "[latex][/latex]", and
16 * "$latex $" shortcodes.
17 *
18 * $latex e^{\i \pi} + 1 = 0$ -> [latex]e^{\i \pi} + 1 = 0[/latex]
19 * $latex [a, b]$ -> [latex][a, b][/latex]
20 */
21
22 function latex_markup( $content ) {
23 $regex = '%
24 \$latex(?:=\s*|\s+)
25 ((?:
26 [^$]+ # Not a dollar
27 |
28 (?<=(?<!\\\\)\\\\)\$ # Dollar preceded by exactly one slash
29 )+)
30 (?<!\\\\)\$ # Dollar preceded by zero slashes
31 %ix';
32 return preg_replace_callback( $regex, 'latex_src', $content );
33 }
34
35 function latex_src( $matches ) {
36 $latex = $matches[1];
37
38 $bg = latex_get_default_color( 'bg' );
39 $fg = latex_get_default_color( 'text', '000' );
40 $s = 0;
41
42
43 $latex = latex_entity_decode( $latex );
44 if ( preg_match( '/.+(&fg=[0-9a-f]{6}).*/i', $latex, $fg_matches ) ) {
45 $fg = substr( $fg_matches[1], 4 );
46 $latex = str_replace( $fg_matches[1], '', $latex );
47 }
48 if ( preg_match( '/.+(&bg=[0-9a-f]{6}).*/i', $latex, $bg_matches ) ) {
49 $bg = substr( $bg_matches[1], 4 );
50 $latex = str_replace( $bg_matches[1], '', $latex );
51 }
52 if ( preg_match( '/.+(&s=[0-9-]{1,2}).*/i', $latex, $s_matches ) ) {
53 $s = (int) substr( $s_matches[1], 3 );
54 $latex = str_replace( $s_matches[1], '', $latex );
55 }
56
57 return latex_render( $latex, $fg, $bg, $s );
58 }
59
60 function latex_get_default_color( $color, $default_color = 'ffffff' ) {
61 global $themecolors;
62 return isset($themecolors[$color]) ? $themecolors[$color] : $default_color;
63 }
64
65 function latex_entity_decode( $latex ) {
66 return str_replace( array( '&lt;', '&gt;', '&quot;', '&#039;', '&#038;', '&amp;', "\n", "\r" ), array( '<', '>', '"', "'", '&', '&', ' ', ' ' ), $latex );
67 }
68
69 function latex_render( $latex, $fg, $bg, $s = 0 ) {
70 $url = "//s0.wp.com/latex.php?latex=" . urlencode( $latex ) . "&bg=" . $bg . "&fg=" . $fg . "&s=" . $s;
71 $url = esc_url( $url );
72 $alt = str_replace( '\\', '&#92;', esc_attr( $latex ) );
73
74 return '<img src="' . $url . '" alt="' . $alt . '" title="' . $alt . '" class="latex" />';
75 }
76
77 /**
78 * The shortcode way. The attributes are the same as the old ones - 'fg' and 'bg', instead of foreground
79 * and background, and 's' is for the font size.
80 *
81 * Example: [latex s=4 bg=00f fg=ff0]\LaTeX[/latex]
82 */
83 function latex_shortcode( $atts, $content = '' ) {
84 extract( shortcode_atts( array(
85 's' => 0,
86 'bg' => latex_get_default_color( 'bg' ),
87 'fg' => latex_get_default_color( 'text', '000' )
88 ), $atts ) );
89
90 return latex_render( latex_entity_decode( $content ), $fg, $bg, $s );
91 }
92
93 /**
94 * LaTeX needs to be untexturized
95 */
96 function latex_no_texturize( $shortcodes ) {
97 $shortcodes[] = 'latex';
98 return $shortcodes;
99 }
100
101 add_filter( 'no_texturize_shortcodes', 'latex_no_texturize' );
102
103 add_filter( 'the_content', 'latex_markup', 9 ); // before wptexturize
104 add_filter( 'comment_text', 'latex_markup', 9 ); // before wptexturize
105 add_shortcode( 'latex', 'latex_shortcode' );
106
107