5.2.20
04/02/20
Last Modified 06/08/07 by Walter Tasin
Horde/WickedAndTextHighlighter_OLD Reload Page

Extend Horde's Wicked to Use Text_Highlighter (with Text_Highligther-0.6.9)

For a more recent version look here

Introduction

Wicked uses the pear module Text_Wiki for parsing and rendering. There is a <code> directive to do the syntaxhighlighting of code blocks, but this is limited to php and html.
Apart from using Extend Horde's Wicked to Use GeSHi's Highlighter I searched another way to make syntax highlighting for other languages inside Horde's beta module "Wicked" possible.
After having informed the PEAR-Group about my GeSHI inclusion, they told me that the usage of PEAR's Text_Highlighter inside Text_Wiki is still a pending "enhancement".
So I decided to make this also possible for my site.
Text_Highlighter v. 0.6.9 supports the following languages:

cpp, css, diff, dtd, html, java, javascript, mysql, perl, php, python, ruby, sql, xml

If you don't find the language you need you can have a look at Extend Horde's Wicked to Use GeSHi's Highlighter or extend Text_Highlighter by yourself and send the patch to the Text_Highlighter people.

Requirements

  • a working Horde installation
  • a working Wicked installation (I use here the recent cvs version)
    • of course Text_Wiki installed (version 1.0.3 is used)
    • and Text_Highlighter has to be installed (beta version 0.6.9 is used)

Installation

Add Text_Highlighter to your PEAR installation

To add an beta staged PEAR product you have make a call like the following:

server:~ # pear install channel://pear.php.net/Text_Highlighter-0.6.9
Starting to download Text_Highlighter-0.6.9.tgz (117,767 bytes)
...............done: 117,767 bytes
install ok: channel://pear.php.net/Text_Highlighter-0.6.9
server:~ # 

For this version the attached patch Html-0.2.diff has to be applied to Text/Highlighter/Renderer/Html.php to extend its functionality.

For that you have to search the directory in which the PEAR modules are installed.

Extend Wicked (the Text_Wiki part)

To extend Wicked you need to download the attached file wickednthl-0.2.tar.gz.
Untar the package inside the Wicked directory of your Horde installation.
The tarball contains the following files:

  • lib/Text_Wiki/Render/Xhtml/Xcode.php
  • lib/Text_Wiki/Parse/Xcode.php
  • scripts/upgrades/extensions/add_texthighlighter.diff

Now you have to apply the following patch to activate the Highlighter class:

<xcode type="diff" start="1">--- lib/Page.php 2006-02-26 16:15:45.000000000 +0100

lib/Page.php 2006-03-07 22:30:26.000000000 +0100

@@ -470,6 +470,7 @@
$rules = array(
'Prefilter',
'Delimiter',

'Xcode',

'Code2',
'Function',
'Html',
@@ -525,6 +526,24 @@

$this->_proc->setRenderConf('Xhtml', 'Wikilink', $linkConf);
$this->_proc->setRenderConf('Xhtml', 'Freelink', $linkConf);

$this->_proc->setRenderConf('Xhtml', 'Xcode',

array('styles' => array(

'prepro' => 'color:#804000',

'identifier' => 'color:#0040A0',

'inlinetags' => 'color: #888822',

'brackets' => 'color:#007700',

'quotes' => 'color: #00B000',

'var' => 'color:#0000BB',

'types' => 'color:#804000',

'number' => 'color: #CC66CC',

'string' => 'color:#DD0000',

'special' => 'color:#DD0000',

'reserved' => 'color:#007700',

'inlinedoc' => 'color:#FF8000',

'comment' => 'color:#FF8000',

'mlcomment' => 'color:#FF8000',

)

));

$this->_proc->setRenderConf('Xhtml', 'Toc',
array('title' => '<strong>' . _("Table of Contents") . '</strong>',
'collapse' => false));

</xcode>
You can also find this patch inside the tarball (see scripts/upgrades/extensions/add_texthighlighter.diff).

Example of an installation

The following step by step installation assumes

  • your Wicked is installed in /srv/www/horde/wicked
  • your PEAR packages are installed at /usr/share/php
  • the tarball and the patchfile mentioned above are downloaded at /root:

server:~ # pear install channel://pear.php.net/Text_Highlighter-0.6.9
Starting to download Text_Highlighter-0.6.9.tgz (117,767 bytes)
...............done: 117,767 bytes
install ok: channel://pear.php.net/Text_Highlighter-0.6.9
server:~ # cd /usr/share/php/Text/Highlighter/Renderer/Xhtml
server:/usr/share/php/Text/Highlighter/Renderer/Xhtml # patch -p0 < /root/Html-0.2.diff
server:/usr/share/php/Text/Highlighter/Renderer/Xhtml # cd /srv/www/horde/wicked
server:/srv/www/horde/wicked # tar xvzf /root/wickednthl-0.2.tar.gz
lib/Text_Wiki/Render/Xhtml/Xcode.php
lib/Text_Wiki/Parse/Xcode.php
scripts/upgrades/extensions/add_texthighlighter.diff
server:/srv/www/horde/wicked # patch -p0 < scripts/upgrades/extensions/add_texthighlighter.diff

Advanced Configuration

Using stylesheets for the layout

The installation example above doesn't support the usage of stylesheets to have the recommended separation between layout and content.

If you want to do so you need
to apply only the first part of the add_texthighlighter.diff (line 1-10)
or remove the already added setRenderConf() call.

Instead of using <span style="..."> the default behaviour of Text_Highlighter will be activated (i.e. <span class="...">).

Now you have to add layout definitions to the css file (something like shown in the following example).
Wicked's css file is called screen.css and can be found in the themes subdirectory of your wicked installation.

Example of a css file for Text_Highlighter

(The following example creates a different look for highlighting.)
<xcode type="css">/*
Syntaxhighlighting classes
*/
div.hl-main {
margin-left: 0em;
}.hl-prepro {
color: #804000;
}.hl-identifier {
color: #804000;
}.hl-inlinetags {
color: #888822;
}.hl-brackets {
color: #007700;
}.hl-quotes {
color: #00B000;
}.hl-string, .hl-special {
color:#DD0000;
border: 1px solid #ccc;
font-weight: bold;
}.hl-var, .hl-types {
color:#0000BB;
}.hl-number {
color:#CC66CC;
}.hl-reserved {
color:#007700;
font-weight: bold;
font-style: italic;
}.hl-comment, .hl-mlcomment, .hl-inlinedoc {
color:#8888AA;
font-style: italic;
}

</xcode>
Take care: Sometimes you have to clear the local browser cache, before it takes effect.

CSS name map

The css name map allows you to change the default class name (like hl-comment) into another name.
This can be done with the following call:
<xcode type="php"><?php
...
$this->_proc->setRenderConf('Xhtml', 'Xcode',
array('css' => array(
'inlinedoc' => 'mycomment',
'comment' => 'mycomment',
'mlcomment' => 'mycomment',
)
));
...
</xcode>
and the css file would look like:
<xcode type="css">...
.mycomment {
color:#8888AA;
font-style: italic;
}

</xcode>

Usage

The <xcode> parser works like the <code> parser (which still works), except that the type attribute is mandatory.

The type attribute

With the type attribute you specify the language of the inserted code.

Don't forget: All attribute values have to be quoted. <xcode type="php">


Input:

<xcode type="diff">--- wicked/lib/Text_Wiki/Render/Xhtml/Url.php    2005-11-19 04:45:46.000000000 +0100

+++  wicked/lib/Text_Wiki/Render/Xhtml/Url.php    2005-12-16 09:14:52.000000000 +0100

@@ -38,7 +38,7 @@
         }
 
         if ($target) {
-            $target = ' target="' . htmlspecialchars($target) . '"';

+             $target = htmlspecialchars($target);

         }
 
         $text = htmlspecialchars($text);
</xcode>

Output:
<xcode type="diff">--- wicked/lib/Text_Wiki/Render/Xhtml/Url.php 2005-11-19 04:45:46.000000000 +0100

wicked/lib/Text_Wiki/Render/Xhtml/Url.php 2005-12-16 09:14:52.000000000 +0100

@@ -38,7 +38,7 @@
}

if ($target) {
- $target = ' target="' . htmlspecialchars($target) . '"';

$target = htmlspecialchars($target);

}

$text = htmlspecialchars($text);
</xcode>

The start attribute

For getting an output with line numbers you can use the start attribute.
The given number must be a positive integer value. It defines the start value of the line numbering.

Don't forget: All attribute values have to be quoted. <xcode type="mysql" start="1">

Input:

<xcode type="cpp" start="1">/* Simple example */
#include <stdio.h>

int main()
{
    printf("Hello world!\ 
 This is only a test\n");
    return 0;
}
</xcode>

Output:
<xcode type="cpp" start="1">/* Simple example */
#include <stdio.h>

int main()
{ printf("Hello world!\
This is only a test\n");
return 0;
}

</xcode>

Further examples

Input:

<xcode type="cpp" start="161">...
int Screen::GetScreenHeight()
{
        bool bOk;
        int nValue;
        if((bOk=GetScreenInfo()))
          nValue=m_InfoConsole.dwMaximumWindowSize.Y;

        return (bOk) ? nValue : -1;
}

WINDOW Screen::GetVisibleWindow()
{
        WINDOW wValue={-1, -1, -1, -1};
        if((GetScreenInfo()))
          wValue=m_InfoConsole.srWindow;

        return wValue ;
}
...
</xcode>

Output:
<xcode type="cpp" start="161">...
int Screen::GetScreenHeight()
{

bool bOk;
int nValue;
if((bOk=GetScreenInfo()))
nValue=m_InfoConsole.dwMaximumWindowSize.Y;

return (bOk) ? nValue : -1;
}

WINDOW Screen::GetVisibleWindow()
{

WINDOW wValue={-1, -1, -1, -1};
if((GetScreenInfo()))
wValue=m_InfoConsole.srWindow;

return wValue ;
}

...
</xcode>


Input:

<xcode type="mysql">-- $Horde: wicked/scripts/sql/wicked.sql,v 1.4 2005/03/09 18:03:47 eraserhd Exp $

...

CREATE TABLE wicked_attachment_history (
    page_id                 INT NOT NULL,
    attachment_name         VARCHAR(100) NOT NULL,
    attachment_majorversion SMALLINT NOT NULL,
    attachment_minorversion INT NOT NULL,
    attachment_created      INT NOT NULL,
    change_author           VARCHAR(255),
    change_log              TEXT,
-- 
    PRIMARY KEY (page_id, attachment_name, attachment_majorversion,
                 attachment_minorversion)
);

CREATE INDEX wicked_attachment_history_name_idx ON wicked_attachment_history 
  (page_id, attachment_name);
CREATE INDEX wicked_attachment_history_version_idx ON wicked_attachment_history
  (attachment_majorversion, attachment_minorversion);

GRANT SELECT, INSERT, UPDATE, DELETE ON wicked_pages TO horde;
GRANT SELECT, INSERT, UPDATE, DELETE ON wicked_history TO horde;
GRANT SELECT, INSERT, UPDATE, DELETE ON wicked_attachments TO horde;
GRANT SELECT, INSERT, UPDATE, DELETE ON wicked_attachment_history TO horde;
</xcode>

Output:
<xcode type="mysql">-- $Horde: wicked/scripts/sql/wicked.sql,v 1.4 2005/03/09 18:03:47 eraserhd Exp $

...

CREATE TABLE wicked_attachment_history (
page_id INT NOT NULL,
attachment_name VARCHAR(100) NOT NULL,
attachment_majorversion SMALLINT NOT NULL,
attachment_minorversion INT NOT NULL,
attachment_created INT NOT NULL,
change_author VARCHAR(255),
change_log TEXT,
--
PRIMARY KEY (page_id, attachment_name, attachment_majorversion,
attachment_minorversion)
);

CREATE INDEX wicked_attachment_history_name_idx ON wicked_attachment_history
(page_id, attachment_name);
CREATE INDEX wicked_attachment_history_version_idx ON wicked_attachment_history
(attachment_majorversion, attachment_minorversion);

GRANT SELECT, INSERT, UPDATE, DELETE ON wicked_pages TO horde;
GRANT SELECT, INSERT, UPDATE, DELETE ON wicked_history TO horde;
GRANT SELECT, INSERT, UPDATE, DELETE ON wicked_attachments TO horde;
GRANT SELECT, INSERT, UPDATE, DELETE ON wicked_attachment_history TO horde;
</xcode>


Input:

<xcode type="java" start="1">/*
 * RMIDateCalc.java
 *
 * Created on 20. Januar 2006, 22:28
 *
 */
import java.rmi.*;
 
/**
 * Like DateCalc (only for Remote Method Invocation)
 * @author Tasin
 */
public interface RMIDateCalc extends Remote
{
       public String RMICalculate(int a) throws RemoteException;    
       public String RMICalculate(String algo, int a) throws RemoteException;    
       public String RMICalculate(String codebase, String algo, int a) 
          throws RemoteException;    
}
</xcode>

Output:
<xcode type="java" start="1">/*
* RMIDateCalc.java
*
* Created on 20. Januar 2006, 22:28
*
*/
import java.rmi.*;

/**
* Like DateCalc (only for Remote Method Invocation)
* @author Tasin
*/
public interface RMIDateCalc extends Remote
{ public String RMICalculate(int a) throws RemoteException;
public String RMICalculate(String algo, int a) throws RemoteException;
public String RMICalculate(String codebase, String algo, int a)
throws RemoteException;
}

</xcode>