Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Remove <p> wrappers around shortcodes

Signed-off-by: Alysson Bortoli <alysson.web@gmail.com>
  • Loading branch information...
commit 801b90d8907c52d941e7d1e47353d6a4100f57a6 1 parent b22a7af
Alysson Bortoli authored October 03, 2012

Showing 1 changed file with 50 additions and 0 deletions. Show diff stats Hide diff stats

  1. 50  parsers/ShortcodeParser.php
50  parsers/ShortcodeParser.php
@@ -158,6 +158,8 @@ public function clear() {
158 158
 	 */
159 159
 	public function parse($content) {
160 160
 		if(!$this->shortcodes) return $content;
  161
+
  162
+		$content = $this->removePTag($content);
161 163
 		
162 164
 		$shortcodes = implode('|', array_map('preg_quote', array_keys($this->shortcodes)));
163 165
 		$pattern    = "/\[($shortcodes)(.*?)(\/\]|\](?(4)|(?:(.+?)\[\/\s*\\1\s*\]))|\])/s";
@@ -208,5 +210,53 @@ protected function handleShortcode($matches) {
208 210
 			$this->shortcodes[$shortcode], 
209 211
 			$attributes, isset($matches[4][0]) ? $matches[4][0] : '', $this, $shortcode);
210 212
 	}
  213
+
  214
+	/**
  215
+	 * Don't auto-p wrap shortcodes that stand alone
  216
+	 *
  217
+	 * Ensures that shortcodes are not wrapped in <<p>>...<</p>>.
  218
+	 *
  219
+	 * @param string $content The content.
  220
+	 * @return string The filtered content.
  221
+	 */
  222
+	protected function removePTag($content) {
  223
+		if(!$this->shortcodes) return $content;
  224
+
  225
+		$tagregexp = join('|', array_map('preg_quote', array_keys($this->shortcodes)));
  226
+
  227
+		$pattern =
  228
+			  '/'
  229
+			. '<p>'                              // Opening paragraph
  230
+			. '\\s*+'                            // Optional leading whitespace
  231
+			. '('                                // 1: The shortcode
  232
+			.     '\\['                          // Opening bracket
  233
+			.     "($tagregexp)"                 // 2: Shortcode name
  234
+			.     '\\b'                          // Word boundary
  235
+			                                     // Unroll the loop: Inside the opening shortcode tag
  236
+			.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
  237
+			.     '(?:'
  238
+			.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
  239
+			.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
  240
+			.     ')*?'
  241
+			.     '(?:'
  242
+			.         '\\/\\]'                   // Self closing tag and closing bracket
  243
+			.     '|'
  244
+			.         '\\]'                      // Closing bracket
  245
+			.         '(?:'                      // Unroll the loop: Optionally, anything between the opening and closing shortcode tags
  246
+			.             '[^\\[]*+'             // Not an opening bracket
  247
+			.             '(?:'
  248
+			.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
  249
+			.                 '[^\\[]*+'         // Not an opening bracket
  250
+			.             ')*+'
  251
+			.             '\\[\\/\\2\\]'         // Closing shortcode tag
  252
+			.         ')?'
  253
+			.     ')'
  254
+			. ')'
  255
+			. '\\s*+'                            // optional trailing whitespace
  256
+			. '<\\/p>'                           // closing paragraph
  257
+			. '/s';
  258
+
  259
+		return preg_replace( $pattern, '$1', $content );
  260
+	}
211 261
 	
212 262
 }

0 notes on commit 801b90d

Please sign in to comment.
Something went wrong with that request. Please try again.