Plugin Directory

Changeset 3348838


Ignore:
Timestamp:
08/22/2025 11:11:58 PM (7 months ago)
Author:
mdawaffe
Message:

WP-LaTeX: Improve inline syntax handling

  1. More efficient regular expression: unroll the loop.
  2. Use wp_html_split() to only look for LaTeX in HTML text nodes.
  3. Tell do_shortcode() to ignore shortcodes in HTML attributes when processing [latex] shortcodes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-latex/trunk/wp-latex.php

    r982453 r3348838  
    118118            return $content;
    119119
    120         return preg_replace_callback( '#(\s*)\$latex[= ](.*?[^\\\\])\$(\s*)#', array( &$this, 'inline_to_shortcode_callback' ), $content );
     120        $textarr = wp_html_split( $content );
     121
     122        $regex = '%
     123            (\s*) # 1: Leading whitespace
     124            \$latex(?:=\s*|\s+)
     125            ((?:
     126                [^$]+ # Not a dollar
     127            |
     128                (?<=(?<!\\\\)\\\\)\$ # Dollar preceded by exactly one slash
     129            )+)   # 2: Content
     130            (?<!\\\\)\$ # Dollar preceded by zero slashes
     131            (\s*) # 3: Trailing whitespace
     132        %ix';
     133
     134        foreach ( $textarr as &$element ) {
     135            if ( '' == $element || '<' === $element[0] ) {
     136                continue;
     137            }
     138
     139            if ( false === stripos( $element, '$latex' ) ) {
     140                continue;
     141            }
     142
     143            $element = preg_replace_callback( $regex, array( &$this, 'inline_to_shortcode_callback' ), $element );
     144        }
     145
     146        return implode( '', $textarr );
    121147    }
    122148
     
    148174        add_shortcode( 'latex', array( $this, 'shortcode' ) );
    149175
    150         $text = do_shortcode( $text );
     176        $text = do_shortcode( $text, true );
    151177
    152178        $GLOBALS['shortcode_tags'] = $current_shortcodes;
Note: See TracChangeset for help on using the changeset viewer.