<?php /** * Module Name: Beautiful Math * Module Description: Use the LaTeX markup language to write mathematical equations and formulas * Sort Order: 12 * First Introduced: 1.1 * Requires Connection: No * Auto Activate: No * Module Tags: Writing * Feature: Writing * Additional Search Queries: latex, math, equation, equations, formula, code * * @package automattic/jetpack */ /** * LaTeX support. * * Backward compatibility requires support for both "[latex][/latex]", and * "$latex $" shortcodes. * * $latex e^{\i \pi} + 1 = 0$ -> [latex]e^{\i \pi} + 1 = 0[/latex] * $latex [a, b]$ -> [latex][a, b][/latex] */ /** * Markup LaTeX content. * * @param string $content Post or comment contents to markup. */ function latex_markup( $content ) { $textarr = wp_html_split( $content ); $regex = '% \$latex(?:=\s*|\s+) ((?: [^$]+ # Not a dollar | (?<=(?<!\\\\)\\\\)\$ # Dollar preceded by exactly one slash )+) (?<!\\\\)\$ # Dollar preceded by zero slashes %ix'; foreach ( $textarr as &$element ) { if ( '' === $element || '<' === $element[0] ) { continue; } if ( false === stripos( $element, '$latex' ) ) { continue; } $element = preg_replace_callback( $regex, 'latex_src', $element ); } return implode( '', $textarr ); } /** * Process LaTeX string to rendered image. * * @param array $matches Matched regex results. */ function latex_src( $matches ) { $latex = $matches[1]; $bg = latex_get_default_color( 'bg' ); $fg = latex_get_default_color( 'text', '000' ); $s = 0; $latex = latex_entity_decode( $latex ); if ( preg_match( '/.+(&fg=[0-9a-f]{6}).*/i', $latex, $fg_matches ) ) { $fg = substr( $fg_matches[1], 4 ); $latex = str_replace( $fg_matches[1], '', $latex ); } if ( preg_match( '/.+(&bg=[0-9a-f]{6}).*/i', $latex, $bg_matches ) ) { $bg = substr( $bg_matches[1], 4 ); $latex = str_replace( $bg_matches[1], '', $latex ); } if ( preg_match( '/.+(&s=[0-9-]{1,2}).*/i', $latex, $s_matches ) ) { $s = (int) substr( $s_matches[1], 3 ); $latex = str_replace( $s_matches[1], '', $latex ); } return latex_render( $latex, $fg, $bg, $s ); } /** * Get the default color for an attribute. * * @param string $color Attribute to color (e.g. bg). * @param string $default_color Default fallback color to use. */ function latex_get_default_color( $color, $default_color = 'ffffff' ) { global $themecolors; return isset( $themecolors[ $color ] ) ? $themecolors[ $color ] : $default_color; } /** * Decode special characters in a LaTeX string. * * @param string $latex Character encoded content. */ function latex_entity_decode( $latex ) { return str_replace( array( '<', '>', '"', ''', '&', '&', "\n", "\r" ), array( '<', '>', '"', "'", '&', '&', ' ', ' ' ), $latex ); } /** * Returns the URL for the server-side rendered image of LaTeX. * * @param string $latex LaTeX string. * @param string $fg Foreground color. * @param string $bg Background color. * @param int $s Matches. * * @return string Image URL for the rendered LaTeX. */ function latex_render( $latex, $fg, $bg, $s = 0 ) { $url = add_query_arg( urlencode_deep( array( 'latex' => $latex, 'bg' => $bg, 'fg' => $fg, 's' => $s, 'c' => '20201002', // cache buster. Added 2020-10-02 after server migration caused faulty rendering. ) ), ( is_ssl() ? 'https://' : 'http://' ) . 's0.wp.com/latex.php' ); $alt = str_replace( '\\', '\', esc_attr( $latex ) ); return sprintf( '<img src="%1$s" alt="%2$s" class="latex" />', esc_url( $url ), $alt ); } /** * The shortcode way. The attributes are the same as the old ones - 'fg' and 'bg', instead of foreground * and background, and 's' is for the font size. * * Example: [latex s=4 bg=00f fg=ff0]\LaTeX[/latex] * * @param array $atts Shortcode attributes. * @param string $content Content to format. */ function latex_shortcode( $atts, $content = '' ) { $attr = shortcode_atts( array( 's' => 0, 'bg' => latex_get_default_color( 'bg' ), 'fg' => latex_get_default_color( 'text', '000' ), ), $atts, 'latex' ); return latex_render( latex_entity_decode( $content ), $attr['fg'], $attr['bg'], $attr['s'] ); } /** * LaTeX needs to be untexturized. * * @param array $shortcodes Array of shortcodes not to texturize. */ function latex_no_texturize( $shortcodes ) { $shortcodes[] = 'latex'; return $shortcodes; } add_filter( 'no_texturize_shortcodes', 'latex_no_texturize' ); add_filter( 'the_content', 'latex_markup', 9 ); // Before wptexturize. add_filter( 'comment_text', 'latex_markup', 9 ); // Before wptexturize. add_shortcode( 'latex', 'latex_shortcode' );
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
carousel | Folder | 0755 |
|
|
cloudflare-analytics | Folder | 0755 |
|
|
comment-likes | Folder | 0755 |
|
|
comments | Folder | 0755 |
|
|
custom-post-types | Folder | 0755 |
|
|
geo-location | Folder | 0755 |
|
|
google-fonts | Folder | 0755 |
|
|
gravatar | Folder | 0755 |
|
|
infinite-scroll | Folder | 0755 |
|
|
likes | Folder | 0755 |
|
|
markdown | Folder | 0755 |
|
|
memberships | Folder | 0755 |
|
|
photon-cdn | Folder | 0755 |
|
|
plugin-search | Folder | 0755 |
|
|
post-by-email | Folder | 0755 |
|
|
related-posts | Folder | 0755 |
|
|
scan | Folder | 0755 |
|
|
seo-tools | Folder | 0755 |
|
|
sharedaddy | Folder | 0755 |
|
|
shortcodes | Folder | 0755 |
|
|
simple-payments | Folder | 0755 |
|
|
site-icon | Folder | 0755 |
|
|
sitemaps | Folder | 0755 |
|
|
stats | Folder | 0755 |
|
|
subscriptions | Folder | 0755 |
|
|
theme-tools | Folder | 0755 |
|
|
tiled-gallery | Folder | 0755 |
|
|
verification-tools | Folder | 0755 |
|
|
videopress | Folder | 0755 |
|
|
widget-visibility | Folder | 0755 |
|
|
widgets | Folder | 0755 |
|
|
woocommerce-analytics | Folder | 0755 |
|
|
wordads | Folder | 0755 |
|
|
wpcom-tos | Folder | 0755 |
|
|
blaze.php | File | 986 B | 0644 |
|
blocks.php | File | 938 B | 0644 |
|
carousel.php | File | 573 B | 0644 |
|
comment-likes.php | File | 7.89 KB | 0644 |
|
comments.php | File | 1.06 KB | 0644 |
|
contact-form.php | File | 804 B | 0644 |
|
copy-post.php | File | 12.8 KB | 0644 |
|
custom-content-types.php | File | 3.96 KB | 0644 |
|
geo-location.php | File | 2.44 KB | 0644 |
|
google-fonts.php | File | 533 B | 0644 |
|
gravatar-hovercards.php | File | 11.84 KB | 0644 |
|
infinite-scroll.php | File | 8.19 KB | 0644 |
|
json-api.php | File | 484 B | 0644 |
|
latex.php | File | 4.61 KB | 0644 |
|
likes.php | File | 19.79 KB | 0644 |
|
markdown.php | File | 1007 B | 0644 |
|
masterbar.php | File | 280 B | 0644 |
|
module-extras.php | File | 2.61 KB | 0644 |
|
module-headings.php | File | 42.24 KB | 0644 |
|
module-info.php | File | 26.1 KB | 0644 |
|
monitor.php | File | 3.51 KB | 0644 |
|
notes.php | File | 7.51 KB | 0644 |
|
photon-cdn.php | File | 12.17 KB | 0644 |
|
photon.php | File | 711 B | 0644 |
|
plugin-search.php | File | 21.58 KB | 0644 |
|
post-by-email.php | File | 596 B | 0644 |
|
post-list.php | File | 533 B | 0644 |
|
protect.php | File | 657 B | 0644 |
|
publicize.php | File | 4.09 KB | 0644 |
|
related-posts.php | File | 2.14 KB | 0644 |
|
search.php | File | 1.17 KB | 0644 |
|
seo-tools.php | File | 1.52 KB | 0644 |
|
sharedaddy.php | File | 1.59 KB | 0644 |
|
shortcodes.php | File | 6.36 KB | 0644 |
|
shortlinks.php | File | 4.4 KB | 0644 |
|
sitemaps.php | File | 1.28 KB | 0644 |
|
sso.php | File | 684 B | 0644 |
|
stats.php | File | 46.57 KB | 0644 |
|
subscriptions.php | File | 32.04 KB | 0644 |
|
theme-tools.php | File | 2.52 KB | 0644 |
|
tiled-gallery.php | File | 1.05 KB | 0644 |
|
vaultpress.php | File | 1.77 KB | 0644 |
|
verification-tools.php | File | 830 B | 0644 |
|
videopress.php | File | 1007 B | 0644 |
|
waf.php | File | 297 B | 0644 |
|
widget-visibility.php | File | 475 B | 0644 |
|
widgets.php | File | 2.72 KB | 0644 |
|
woocommerce-analytics.php | File | 861 B | 0644 |
|
wordads.php | File | 586 B | 0644 |
|
wpgroho.js | File | 1.93 KB | 0644 |
|