How to use Text_Highlighter classCreating a highlighter object
You have generated a class and saved it in directory Text/Highlighter. To create an instance of
the class, use static method Text_Highlighter::factory().
<?php
require_once 'Text/Highlighter.php';
?>
<style>
.hl-main {font-family: monospace; font-size:13px;}
.hl-default { color: #000000; }
.hl-code { color: #7f7f33; }
.hl-brackets { color: #009966; }
.hl-comment { color: #7F7F7F; }
.hl-quotes { color: #00007F; }
.hl-string { color: #7F0000; }
.hl-identifier { color: #000000; }
.hl-reserved { color: #7F007F; }
.hl-inlinedoc { color: #0000FF; }
.hl-var { color: #0066FF; }
.hl-url { color: #FF0000; }
.hl-special { color: #0000FF; }
.hl-number { color: #007F00; }
.hl-inlinetags { color: #FF0000; }
</style>
<?php
echo $html;
?>
Note that page must have CSS stylesheet mapping color groups to CSS
styles. CSS class names start with hl-
followed by
color group name. Special class hl-main
is applied
to the whole highlighted code (i.e. surrounding
<pre>
or <code>
tag). To add line numbering and change other presentation effects, set
options.
Setting options
Optional second argument of Text_Highlighter::factory() is
$options
. It is an associative array controlling
highlighter options.
Adding line numbers
To add line nubers, 'numbers'
option is used. It
can be either HL_NUMBERS_LI
or
HL_NUMBERS_TABLE
.
With HL_NUMBERS_LI
, a numbered list will be used,
and with HL_NUMBERS_TABLE
- a table with 2
columns (line numbers on the left, code on the right). When using
HL_NUMBERS_TABLE
, additional CSS classes may be
used:
ht-table
- applies to whole
table
ht-gutter
- applies to left column
<?php
require_once 'Text/Highlighter.php';
?>
<style>
.hl-main {font-family: monospace; font-size:13px;}
.hl-gutter { background-color: #CCCCCC; padding-right: 10px;
font-family: monospace; font-size:13px;}
.hl-table {border: solid 1px #000000; }
.hl-default { color: #000000; }
.hl-code { color: #7f7f33; }
.hl-brackets { color: #009966; }
.hl-comment { color: #7F7F7F; }
.hl-quotes { color: #00007F; }
.hl-string { color: #7F0000; }
.hl-identifier { color: #000000; }
.hl-reserved { color: #7F007F; }
.hl-inlinedoc { color: #0000FF; }
.hl-var { color: #0066FF; }
.hl-url { color: #FF0000; }
.hl-special { color: #0000FF; }
.hl-number { color: #007F00; }
.hl-inlinetags { color: #FF0000; }
</style>
<?php
echo $html;
?>
Other options
'tabsize'
- number of spaces to insert instead of
tabs. Defaults to 4.
'tag'
- either HL_TAG_PRE
or
HL_TAG_CODE
. Specifies top-level tag of output.
HTML code generated using HL_TAG_CODE
is bigger
size, but it automatically wraps long lines. Note that
HL_NUMBERS_TABLE
forces
HL_TAG_PRE
. Default value is
HL_TAG_PRE
.