diff --git a/codefilter.module b/codefilter.module index 65e1634..0959dce 100644 --- a/codefilter.module +++ b/codefilter.module @@ -112,41 +112,13 @@ function codefilter_fix_spaces($text) { function codefilter_escape($text, $type = 'code', $attributes = NULL) { // Note, pay attention to odd preg_replace-with-/e behaviour on slashes. $text = check_plain(str_replace('\"', '"', $text)); - // Protect newlines from line break converter. $text = str_replace(array("\r", "\n"), array('', ' '), $text); - // Prepare the class HTML attribute for the codefilter tag. - if (isset($attributes)) { - $attributes = ' ' . $attributes; - } - - // Add codefilter escape tags. - $text = "[codefilter_{$type}{$attributes}]{$text}[/codefilter_$type]"; - return $text; } /** - * Callback to escape content of elements. - */ -function _codefilter_escape_code_tag_callback($matches) { - // Extract a potentially existing class HTML attribute. - $attributes = NULL; - if (!empty($matches[1]) && preg_match('@class="[^"]+"@', $matches[1], $class_matches)) { - $attributes = $class_matches[0]; - } - return codefilter_escape($matches[2], 'code', $attributes); -} - -/** - * Callback to escape content of , [?php ?], <% %>, and [% %] elements. - */ -function _codefilter_escape_php_tag_callback($matches) { - return codefilter_escape($matches[2], 'php'); -} - -/** * Implements hook_filter_info(). */ function codefilter_filter_info() { @@ -169,12 +141,12 @@ function codefilter_filter_info() { * Implements hook_filter_FILTER_prepare(). * * Escape code tags to prevent other filters from acting on them. - * , , [?php ?], <% %>, and [% %] tags are escaped. + * , are escaped. */ function _codefilter_prepare($text, $format) { - $callback_prefix = $format->settings['codefilter_prism'] ? '_codefilter_prism' : '_codefilter'; - $text = preg_replace_callback('@]*)>(.+?)@s', "{$callback_prefix}_escape_code_tag_callback", $text); - $text = preg_replace_callback('@[\[<](\?php)(.+?)(\?)[\]>]@s', "{$callback_prefix}_escape_php_tag_callback", $text); + $tag_prefix = $format->settings['codefilter_prism'] ? 'codefilter_prism' : 'codefilter'; + $text = replace_tags_with_nesting($text, '', "[{$tag_prefix}_php]", "[/{$tag_prefix}_php]"); + $text = replace_tags_with_nesting($text, '', '', "[{$tag_prefix}_code]", "[/{$tag_prefix}_code]"); return $text; } @@ -238,3 +210,94 @@ function _codefilter_settings($form, &$form_state, $filter, $format, $defaults) } return $elements; } + + +/** + * Replace tags with placeholders, without replacing nested tags. + * + * @param string $string + * String to process. + * @param string $opening_tag + * Opening tag to replace. + * @param string $closing_tag + * Closing tag to replace. + * @param string $replace_opening + * Replacement opening tag. + * @param string $replace_closing + * Replacement closing tag. + * @param bool $preserve_attributes + * If set to TRUE, attributes like "class='myclass'" will be preserved. + * + * @return string + * The processed string. + */ +function replace_tags_with_nesting($string, $opening_tag = '', $replace_opening = '[codefilter_php]', $replace_closing = '[/codefilter_php]', $preserve_attributes = TRUE) { + $string_length = strlen($string); + $return = ''; + $open_count = 0; + + for ($i = 0; $i < $string_length; $i++) { + + if ($string[$i] === substr($opening_tag, 0, 1)) { + // Hacky special case for code tags with attributes. + if ($preserve_attributes && $opening_tag == '') { + if (substr($string, $i, 5) == ' declaration. + * + * @param string $string + * String to check. + * @param int $start_position + * Offset in string to begin checking from. + * + * @return string + * The attribute string, e.g. 'attribute="value"'. + */ +function _get_attributes($string, $start_position) { + $attributes = ''; + $string = substr($string, $start_position); + // Find attributes if any. + if (preg_match('/]*)>/', $string, $matches) !== FALSE) { + $attributes = $matches[1]; + } + return $attributes; +} diff --git a/modules/codefilter_prism/codefilter_prism.module b/modules/codefilter_prism/codefilter_prism.module index d9328e2..169015b 100644 --- a/modules/codefilter_prism/codefilter_prism.module +++ b/modules/codefilter_prism/codefilter_prism.module @@ -148,26 +148,6 @@ function _codefilter_prism_process_code_callback($matches) { } /** - * Callback to escape content of elements. - */ -function _codefilter_prism_escape_code_tag_callback($matches) { - // Extract a potentially existing class HTML attribute. - $attributes = ''; - if (!empty($matches[1]) && preg_match('@class="[^"]+"@', $matches[1], $class_matches)) { - $attributes = $class_matches[0]; - } - - return codefilter_prism_escape($matches[2], 'code', $attributes); -} - -/** - * Callback to escape content of , [?php ?], <% %>, and [% %] elements. - */ -function _codefilter_prism_escape_php_tag_callback($matches) { - return codefilter_prism_escape($matches[2], 'php'); -} - -/** * Escape code blocks during input filter 'prepare'. * * @param string $text