1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LaTeX shortcode. |
4
|
|
|
* Originally shipped as its own module, under modules/latex.php, and named "Beautiful Math". |
5
|
|
|
* |
6
|
|
|
* Backward compatibility requires support for both "[latex][/latex]", and |
7
|
|
|
* "$latex $" shortcodes. |
8
|
|
|
* |
9
|
|
|
* $latex e^{\i \pi} + 1 = 0$ -> [latex]e^{\i \pi} + 1 = 0[/latex] |
10
|
|
|
* $latex [a, b]$ -> [latex][a, b][/latex] |
11
|
|
|
* [latex s=4 bg=00f fg=ff0]\LaTeX[/latex] |
12
|
|
|
* |
13
|
|
|
* @package Jetpack |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Replace LaTeX markup in post content. |
18
|
|
|
* This covers the use of the "$latex $" format. |
19
|
|
|
* |
20
|
|
|
* @param string $content Post content. |
21
|
|
|
*/ |
22
|
|
|
function latex_markup( $content ) { |
23
|
|
|
$textarr = wp_html_split( $content ); |
24
|
|
|
|
25
|
|
|
$regex = '% |
26
|
|
|
\$latex(?:=\s*|\s+) |
27
|
|
|
((?: |
28
|
|
|
[^$]+ # Not a dollar |
29
|
|
|
| |
30
|
|
|
(?<=(?<!\\\\)\\\\)\$ # Dollar preceded by exactly one slash |
31
|
|
|
)+) |
32
|
|
|
(?<!\\\\)\$ # Dollar preceded by zero slashes |
33
|
|
|
%ix'; |
34
|
|
|
|
35
|
|
|
foreach ( $textarr as &$element ) { |
36
|
|
|
if ( '' === $element || '<' === $element[0] ) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ( false === stripos( $element, '$latex' ) ) { |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$element = preg_replace_callback( $regex, 'latex_src', $element ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return implode( '', $textarr ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Extract LaTex parameters to be used in LaTeX image. |
52
|
|
|
* |
53
|
|
|
* @param array $matches Array of matches found when parsing post content. |
54
|
|
|
*/ |
55
|
|
|
function latex_src( $matches ) { |
56
|
|
|
$latex = $matches[1]; |
57
|
|
|
$bg = latex_get_default_color( 'bg' ); |
58
|
|
|
$fg = latex_get_default_color( 'text', '000' ); |
59
|
|
|
$s = 0; |
60
|
|
|
|
61
|
|
|
$latex = latex_entity_decode( $latex ); |
62
|
|
View Code Duplication |
if ( preg_match( '/.+(&fg=[0-9a-f]{6}).*/i', $latex, $fg_matches ) ) { |
63
|
|
|
$fg = substr( $fg_matches[1], 4 ); |
64
|
|
|
$latex = str_replace( $fg_matches[1], '', $latex ); |
65
|
|
|
} |
66
|
|
View Code Duplication |
if ( preg_match( '/.+(&bg=[0-9a-f]{6}).*/i', $latex, $bg_matches ) ) { |
67
|
|
|
$bg = substr( $bg_matches[1], 4 ); |
68
|
|
|
$latex = str_replace( $bg_matches[1], '', $latex ); |
69
|
|
|
} |
70
|
|
View Code Duplication |
if ( preg_match( '/.+(&s=[0-9-]{1,2}).*/i', $latex, $s_matches ) ) { |
71
|
|
|
$s = (int) substr( $s_matches[1], 3 ); |
72
|
|
|
$latex = str_replace( $s_matches[1], '', $latex ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return latex_render( $latex, $fg, $bg, $s ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get a default color to use for the background and the foreground of LaTeX formulae. |
80
|
|
|
* Default color can be provided by themes on WordPress.com. |
81
|
|
|
* |
82
|
|
|
* @param string $color Name of the color to get (e.g. 'text' for text color, 'bg' for background color). |
83
|
|
|
* @param string $default_color Default color if no color is specified. |
84
|
|
|
*/ |
85
|
|
|
function latex_get_default_color( $color, $default_color = 'ffffff' ) { |
86
|
|
|
global $themecolors; |
87
|
|
|
|
88
|
|
|
return isset( $themecolors[ $color ] ) ? $themecolors[ $color ] : $default_color; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Clean up LaTex formula before it's sent to WordPress for the creation of a LaTex formula image. |
93
|
|
|
* |
94
|
|
|
* @param string $latex LaTeX formula. |
95
|
|
|
*/ |
96
|
|
|
function latex_entity_decode( $latex ) { |
97
|
|
|
return str_replace( |
98
|
|
|
array( |
99
|
|
|
'<', |
100
|
|
|
'>', |
101
|
|
|
'"', |
102
|
|
|
''', |
103
|
|
|
'&', |
104
|
|
|
'&', |
105
|
|
|
"\n", |
106
|
|
|
"\r", |
107
|
|
|
), |
108
|
|
|
array( '<', '>', '"', "'", '&', '&', ' ', ' ' ), |
109
|
|
|
$latex |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Render image of the LaTeX formula. |
115
|
|
|
* |
116
|
|
|
* @param string $latex LaTeX formula. |
117
|
|
|
* @param string $fg Foreground color. |
118
|
|
|
* @param string $bg Background color. |
119
|
|
|
* @param int $s Formula size. |
120
|
|
|
*/ |
121
|
|
|
function latex_render( $latex, $fg, $bg, $s = 0 ) { |
122
|
|
|
$url = add_query_arg( |
123
|
|
|
array( |
124
|
|
|
'latex' => rawurlencode( $latex ), |
125
|
|
|
'bg' => sanitize_hex_color_no_hash( $bg ), |
126
|
|
|
'fg' => sanitize_hex_color_no_hash( $fg ), |
127
|
|
|
's' => absint( $s ), |
128
|
|
|
), |
129
|
|
|
'https://s0.wp.com/latex.php' |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
return sprintf( |
133
|
|
|
'<img src="%1$s" alt="%2$s" title="%2$s" class="latex" />', |
134
|
|
|
esc_url( $url ), |
135
|
|
|
str_replace( '\\', '\', esc_attr( $latex ) ) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* The shortcode way. The attributes are the same as the old ones - 'fg' and 'bg', instead of foreground |
141
|
|
|
* and background, and 's' is for the font size. |
142
|
|
|
* |
143
|
|
|
* Example: [latex s=4 bg=00f fg=ff0]\LaTeX[/latex] |
144
|
|
|
* |
145
|
|
|
* @param array $atts Shortcode attributes. |
146
|
|
|
* @param string $content Post content. |
147
|
|
|
*/ |
148
|
|
|
function latex_shortcode( $atts, $content = '' ) { |
149
|
|
|
$attributes = shortcode_atts( |
150
|
|
|
array( |
151
|
|
|
'fg' => latex_get_default_color( 'text', '000' ), |
152
|
|
|
'bg' => latex_get_default_color( 'bg' ), |
153
|
|
|
's' => 0, |
154
|
|
|
), |
155
|
|
|
$atts, |
156
|
|
|
'latex' |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
return latex_render( |
160
|
|
|
latex_entity_decode( $content ), |
161
|
|
|
$attributes['fg'], |
162
|
|
|
$attributes['bg'], |
163
|
|
|
$attributes['s'] |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* LaTeX needs to be untexturized |
169
|
|
|
* |
170
|
|
|
* @param array $shortcodes An array of shortcodes to exempt from texturizations. |
171
|
|
|
*/ |
172
|
|
|
function latex_no_texturize( $shortcodes ) { |
173
|
|
|
$shortcodes[] = 'latex'; |
174
|
|
|
return $shortcodes; |
175
|
|
|
} |
176
|
|
|
add_filter( 'no_texturize_shortcodes', 'latex_no_texturize' ); |
177
|
|
|
|
178
|
|
|
add_filter( 'the_content', 'latex_markup', 9 ); // before wptexturize. |
179
|
|
|
add_filter( 'comment_text', 'latex_markup', 9 ); // before wptexturize. |
180
|
|
|
add_shortcode( 'latex', 'latex_shortcode' ); |
181
|
|
|
|