latex.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Module Name: Beautiful Math
  4. * Module Description: Use LaTeX markup for complex equations and other geekery.
  5. * Sort Order: 12
  6. * First Introduced: 1.1
  7. * Requires Connection: No
  8. * Auto Activate: Yes
  9. * Module Tags: Writing
  10. * Feature: Writing
  11. * Additional Search Queries: latex, math, equation, equations, formula, code
  12. */
  13. /**
  14. * LaTeX support.
  15. *
  16. * Backward compatibility requires support for both "[latex][/latex]", and
  17. * "$latex $" shortcodes.
  18. *
  19. * $latex e^{\i \pi} + 1 = 0$ -> [latex]e^{\i \pi} + 1 = 0[/latex]
  20. * $latex [a, b]$ -> [latex][a, b][/latex]
  21. */
  22. function latex_markup( $content ) {
  23. $textarr = wp_html_split( $content );
  24. $regex = '%
  25. \$latex(?:=\s*|\s+)
  26. ((?:
  27. [^$]+ # Not a dollar
  28. |
  29. (?<=(?<!\\\\)\\\\)\$ # Dollar preceded by exactly one slash
  30. )+)
  31. (?<!\\\\)\$ # Dollar preceded by zero slashes
  32. %ix';
  33. foreach ( $textarr as &$element ) {
  34. if ( '' == $element || '<' === $element[0] ) {
  35. continue;
  36. }
  37. if ( false === stripos( $element, '$latex' ) ) {
  38. continue;
  39. }
  40. $element = preg_replace_callback( $regex, 'latex_src', $element );
  41. }
  42. return implode( '', $textarr );
  43. }
  44. function latex_src( $matches ) {
  45. $latex = $matches[1];
  46. $bg = latex_get_default_color( 'bg' );
  47. $fg = latex_get_default_color( 'text', '000' );
  48. $s = 0;
  49. $latex = latex_entity_decode( $latex );
  50. if ( preg_match( '/.+(&fg=[0-9a-f]{6}).*/i', $latex, $fg_matches ) ) {
  51. $fg = substr( $fg_matches[1], 4 );
  52. $latex = str_replace( $fg_matches[1], '', $latex );
  53. }
  54. if ( preg_match( '/.+(&bg=[0-9a-f]{6}).*/i', $latex, $bg_matches ) ) {
  55. $bg = substr( $bg_matches[1], 4 );
  56. $latex = str_replace( $bg_matches[1], '', $latex );
  57. }
  58. if ( preg_match( '/.+(&s=[0-9-]{1,2}).*/i', $latex, $s_matches ) ) {
  59. $s = (int) substr( $s_matches[1], 3 );
  60. $latex = str_replace( $s_matches[1], '', $latex );
  61. }
  62. return latex_render( $latex, $fg, $bg, $s );
  63. }
  64. function latex_get_default_color( $color, $default_color = 'ffffff' ) {
  65. global $themecolors;
  66. return isset($themecolors[$color]) ? $themecolors[$color] : $default_color;
  67. }
  68. function latex_entity_decode( $latex ) {
  69. return str_replace( array( '&lt;', '&gt;', '&quot;', '&#039;', '&#038;', '&amp;', "\n", "\r" ), array( '<', '>', '"', "'", '&', '&', ' ', ' ' ), $latex );
  70. }
  71. function latex_render( $latex, $fg, $bg, $s = 0 ) {
  72. $url = "//s0.wp.com/latex.php?latex=" . urlencode( $latex ) . "&bg=" . $bg . "&fg=" . $fg . "&s=" . $s;
  73. $url = esc_url( $url );
  74. $alt = str_replace( '\\', '&#92;', esc_attr( $latex ) );
  75. return '<img src="' . $url . '" alt="' . $alt . '" title="' . $alt . '" class="latex" />';
  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, 'latex' ) );
  89. return latex_render( latex_entity_decode( $content ), $fg, $bg, $s );
  90. }
  91. /**
  92. * LaTeX needs to be untexturized
  93. */
  94. function latex_no_texturize( $shortcodes ) {
  95. $shortcodes[] = 'latex';
  96. return $shortcodes;
  97. }
  98. add_filter( 'no_texturize_shortcodes', 'latex_no_texturize' );
  99. add_filter( 'the_content', 'latex_markup', 9 ); // before wptexturize
  100. add_filter( 'comment_text', 'latex_markup', 9 ); // before wptexturize
  101. add_shortcode( 'latex', 'latex_shortcode' );