Нема описа

latex.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Module Name: Beautiful Math
  4. * Module Description: Use the LaTeX markup language to write mathematical equations and formulas
  5. * Sort Order: 12
  6. * First Introduced: 1.1
  7. * Requires Connection: No
  8. * Auto Activate: No
  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. /**
  72. * Returns the URL for the server-side rendered image of LaTeX.
  73. *
  74. * @param string $latex LaTeX string.
  75. * @param string $fg Foreground color.
  76. * @param string $bg Background color.
  77. * @param int $s Matches.
  78. *
  79. * @return string Image URL for the rendered LaTeX.
  80. */
  81. function latex_render( $latex, $fg, $bg, $s = 0 ) {
  82. $url = add_query_arg(
  83. urlencode_deep(
  84. array(
  85. 'latex' => $latex,
  86. 'bg' => $bg,
  87. 'fg' => $fg,
  88. 's' => $s,
  89. 'c' => '20201002', // cache buster. Added 2020-10-02 after server migration caused faulty rendering.
  90. )
  91. ),
  92. ( is_ssl() ? 'https://' : 'http://' ) . 's0.wp.com/latex.php'
  93. );
  94. $alt = str_replace( '\\', '&#92;', esc_attr( $latex ) );
  95. return sprintf(
  96. '<img src="%1$s" alt="%2$s" class="latex" />',
  97. esc_url( $url ),
  98. $alt
  99. );
  100. }
  101. /**
  102. * The shortcode way. The attributes are the same as the old ones - 'fg' and 'bg', instead of foreground
  103. * and background, and 's' is for the font size.
  104. *
  105. * Example: [latex s=4 bg=00f fg=ff0]\LaTeX[/latex]
  106. */
  107. function latex_shortcode( $atts, $content = '' ) {
  108. $attr = shortcode_atts(
  109. array(
  110. 's' => 0,
  111. 'bg' => latex_get_default_color( 'bg' ),
  112. 'fg' => latex_get_default_color( 'text', '000' ),
  113. ),
  114. $atts,
  115. 'latex'
  116. );
  117. return latex_render( latex_entity_decode( $content ), $attr['fg'], $attr['bg'], $attr['s'] );
  118. }
  119. /**
  120. * LaTeX needs to be untexturized
  121. */
  122. function latex_no_texturize( $shortcodes ) {
  123. $shortcodes[] = 'latex';
  124. return $shortcodes;
  125. }
  126. add_filter( 'no_texturize_shortcodes', 'latex_no_texturize' );
  127. add_filter( 'the_content', 'latex_markup', 9 ); // before wptexturize
  128. add_filter( 'comment_text', 'latex_markup', 9 ); // before wptexturize
  129. add_shortcode( 'latex', 'latex_shortcode' );