diff options
Diffstat (limited to 'plugins/jetpack/modules/latex.php')
-rw-r--r-- | plugins/jetpack/modules/latex.php | 48 |
1 files changed, 37 insertions, 11 deletions
diff --git a/plugins/jetpack/modules/latex.php b/plugins/jetpack/modules/latex.php index 3257fdf3..3658e4df 100644 --- a/plugins/jetpack/modules/latex.php +++ b/plugins/jetpack/modules/latex.php @@ -9,6 +9,8 @@ * Module Tags: Writing * Feature: Writing * Additional Search Queries: latex, math, equation, equations, formula, code + * + * @package automattic/jetpack */ /** @@ -21,6 +23,11 @@ * $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 ); @@ -35,7 +42,7 @@ function latex_markup( $content ) { %ix'; foreach ( $textarr as &$element ) { - if ( '' == $element || '<' === $element[0] ) { + if ( '' === $element || '<' === $element[0] ) { continue; } @@ -49,36 +56,51 @@ function latex_markup( $content ) { 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; - + $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 ); + $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 ); + $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 ); + $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; + 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 ); } @@ -121,6 +143,9 @@ function latex_render( $latex, $fg, $bg, $s = 0 ) { * 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( @@ -137,15 +162,16 @@ function latex_shortcode( $atts, $content = '' ) { } /** - * LaTeX needs to be untexturized + * 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_filter( 'the_content', 'latex_markup', 9 ); // Before wptexturize. +add_filter( 'comment_text', 'latex_markup', 9 ); // Before wptexturize. add_shortcode( 'latex', 'latex_shortcode' ); |