Skip to content

Instantly share code, notes, and snippets.

@splittingred
Created May 30, 2011 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save splittingred/998865 to your computer and use it in GitHub Desktop.
Save splittingred/998865 to your computer and use it in GitHub Desktop.
<?php
$eventName = $modx->event->name;
switch ($eventName) {
case 'OnLoadWebDocument' :
$output = $modx->codeContent = $modx->resource->getContent();
preg_match_all('/<pre.*?>(.*?)<\/pre>/ims', $output, $matches);
$i = 0;
if (empty($matches[1])) return;
if (empty($modx->codeMatches)) $modx->codeMatches = array();
foreach ($matches[1] as $key => $match) {
$output = str_replace($match, '~*code_' . $i . '*~', $output);
$modx->codeMatches['~*code_' . $i . '*~'] = $match;
$i++;
}
$modx->resource->setContent($output);
break;
case 'OnWebPagePrerender' :
$html = & $modx->resource->_output;
$output = $modx->resource->getContent();
/*preg_match_all('/<pre.*?>(.*?)<\/pre>/ims', $html, $matches); */
$i = 0;
$replacements = array (
'<' => '&lt;',
'>' => '&gt;',
);
if (empty($modx->codeMatches)) return;
foreach ($modx->codeMatches 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->setContent($output);
$modx->codeContent = $output;
break;
case 'OnBeforeSaveWebPageCache' :
$modx->resource->setContent($modx->codeContent);
break;
default :
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment