Skip to content

Instantly share code, notes, and snippets.

@Mark-H
Forked from amdbuilder/gist:3494701
Created August 28, 2012 03:49
Show Gist options
  • Save Mark-H/3494789 to your computer and use it in GitHub Desktop.
Save Mark-H/3494789 to your computer and use it in GitHub Desktop.
Busted
<?php
$eventName = $modx->event->name;
switch ($eventName) {
case 'OnLoadWebDocument' :
$output = $GLOBALS['code_content'] = $modx->resource->get('content');
preg_match_all('/<pre.*?>(.*?)<\/pre>/ims', $output, $matches);
$i = 0;
$GLOBALS['code_matches'] = array();
foreach ($matches[1] as $key => $match) {
$output = str_replace($match, '~*code_' . $i . '*~', $output);
$GLOBALS['code_matches']['~*code_' . $i . '*~'] = $match;
$i++;
}
$modx->resource->set('content', $output);
break;
case 'OnWebPagePrerender' :
$html = & $modx->resource->_output;
$output = $modx->resource->get('content');
preg_match_all('/<pre.*?>(.*?)<\/pre>/ims', $html, $matches);
$i = 0;
$replacements = array (
'<' => '&lt;',
'>' => '&gt;'
);
foreach ($GLOBALS['code_matches'] as $key => $match) {
$match = preg_replace('/&([^\s]*;)/i', '&amp;$1', $match);
foreach ($replacements as $key => $value) {
$match = str_replace($key, $value, $match);
}
$html = str_replace('~*code_' . $i . '*~', $match, $html);
$output = str_replace('~*code_' . $i . '*~', $match, $output);
$i++;
}
$modx->resource->set('content', $output);
break;
case 'OnBeforeSaveWebPageCache' :
$modx->resource->set('content', $GLOBALS['code_content']);
break;
default :
break;
}
@sepiariver
Copy link

Hmmm...output looks like this:

code_0
code_1
code_2

@sepiariver
Copy link

The above was sanitized. Those strings are wrapped pre and code html tags in the output :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment