This repository has been archived by the owner on Feb 17, 2018. It is now read-only.
forked from realityking/Joomla-GeSHi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geshi.php
78 lines (63 loc) · 2.06 KB
/
geshi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* @package Joomla.Plugin
* @subpackage Content.geshi
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* GeSHi Content Plugin
*
* @package Joomla.Plugin
* @subpackage Content.geshi
*/
class plgContentGeshi extends JPlugin
{
/**
* @param string The context of the content being passed to the plugin.
* @param object The article object. Note $article->text is also available
* @param object The article params
* @param int The 'page' number
*/
public function onContentPrepare($context, &$article, &$params, $page = 0)
{
// Simple performance check to determine whether bot should process further.
if (JString::strpos($article->text, 'pre>') === false) {
return true;
}
// Define the regular expression for the bot.
$regex = "#<pre xml:\s*(.*?)>(.*?)</pre>#s";
// Perform the replacement.
$article->text = preg_replace_callback($regex, array(&$this, '_replace'), $article->text);
return true;
}
/**
* Replaces the matched tags.
*
* @param array An array of matches (see preg_match_all)
* @return string
*/
protected function _replace(&$matches)
{
jimport('joomla.utilities.utility');
require_once __DIR__ . '/geshi/geshi.php';
$args = JUtility::parseAttributes($matches[1]);
$text = $matches[2];
$lang = JArrayHelper::getValue($args, 'lang', 'php');
$lines = JArrayHelper::getValue($args, 'lines', 'false');
$html_entities_match = array("|\<br \/\>|", "#<#", "#>#", "|'|", '#"#', '# #');
$html_entities_replace = array("\n", '<', '>', "'", '"', ' ');
$text = preg_replace($html_entities_match, $html_entities_replace, $text);
$text = str_replace('<', '<', $text);
$text = str_replace('>', '>', $text);
$text = str_replace("\t", ' ', $text);
$geshi = new GeSHi($text, $lang);
if ($lines == 'true') {
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
}
$text = $geshi->parse_code();
return $text;
}
}