Home Notes Web development Joomla! Code display - harder than I thought

Joomla! Code display - harder than I thought E-mail
Thursday, 23 April 2009 15:43


Finally, got one working!  It took a while but I got Highlight Code with Jumi working after modifying the helper file.  Specifically, I had to remove references to:


And replace them with the relative path of the files in question.  I also didn't like how the plugin hid the code by default and gave the user a JavaScript function to expand/collapse the codebox.  I got rid of that by disabling the JavaScript file and modifying the helper file as follows (note the commented lines near the bottom):
class highlightHelper {
 function add_tags() {
  global $mainframe;
  $document = &JFactory::getDocument();
  $plugin = &JPluginHelper::getPlugin('content', 'cdchilicode');
  $pluginParams = new JParameter($plugin->params);
  $document->addStyleSheet('plugins/content/highlight/highlight.css', 'text/css');
//  $document->addScript('plugins/content/highlight/highlight.js');
  $style = 'display: block;max-height:'.$pluginParams->get('height', "350px").
    ';width:'.$pluginParams->get('width', "650px");
  $mainframe->addCustomHeadTag('<style type="text/css">div.highlight { '.$style.' } </style>');
function highlight_replacer(&$match) {
  global $mainframe, $loaded_script, $highlight_id;
  $db = &JFactory::getDBO();
  $plugin = &JPluginHelper::getPlugin('content', 'cdchilicode');
  $pluginParams = new JParameter($plugin->params);

if ($match[2] && !file_exists("components/com_jumi/jumi.php")) {
   return '<p class="highlight_error">'.JText::_('ERROR_JUMI_COMP').'</p>';
  }($match[1] == "html") {
   $lang = "html4strict";
  else {
   $lang = $match[1];
  if ($match[1] == "html4strict") { $match[1] = "html"; }
  if (!file_exists("libraries/geshi/geshi/".$lang.".php")) {
   return '<p class="highlight_error">'.str_replace("%s", $lang,
  if (!$match[2]) {
   $code = $match[3];
   $code = str_replace("</p>", "<br />", $code);
   $code = str_replace("</div>", "<br />", $code);
   $code = str_replace("<br />", "\r\n", $code);
   $code = preg_replace("#<(.*?)>#is", "", $code);
   $code = html_entity_decode($code, ENT_QUOTES, "UTF-8");

elseif ($match[2] > 0) {
   $db->setQuery("Select custom_script from #__jumi where id='$match[2]'");
   $code = $db->loadResult();
else {
   $db->setQuery("Select custom_script from #__jumi where alias='$match[2]' limit 1");
   $code = $db->loadResult();

$geshi = new GeSHi($code, $lang);

  $out = '<div id="highlight-'.$highlight_id.'" class="highlight">'.
//  if ($pluginParams->get('hidde', 1)) {
//   $out = '<a class="highlight_mostrar" id="highlight-mostrar-'.
    $highlight_id.'" href="javascript:mostrar_ocultar('.$highlight_id.')">'.
    str_replace("%s", $match[2], JText::_('MOSTRAR_OCULTAR')).'</a>'.$out;
//  }
  return $out;

I have spent the better part of two hours trying to implement a simple code display into my Joomla! installation, with still no success.  There are 12 plugins listed here in the Extensions directory, 7 of which are v1.5 native.

I have gone through all of them.  I have read their documentation, peeked into and tweaked the PHP files, but still no go.  Either some error shows up or nothing happens at all.  I didn't realize something so simple would end up being so hard!