Text_Highlighter
[ class tree: Text_Highlighter ] [ index: Text_Highlighter ] [ all elements ]
Prev Next

How to use Text_Highlighter class

Creating 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().

  1. <?php
  2.   require_once 'Text/Highlighter.php';
  3.   $hl =Text_Highlighter::factory('PHP');
  4.   $code file_get_contents('somefile.php');
  5.   $html $hl->highlight($code);
  6. ?>
  7. <style>
  8.  .hl-main {font-family: monospace; font-size:13px;}
  9.  
  10.  .hl-default { color: #000000; }
  11.  .hl-code { color: #7f7f33; }
  12.  .hl-brackets { color: #009966; }
  13.  .hl-comment { color: #7F7F7F; }
  14.  .hl-quotes { color: #00007F; }
  15.  .hl-string { color: #7F0000; }
  16.  .hl-identifier { color: #000000; }
  17.  .hl-reserved { color: #7F007F; }
  18.  .hl-inlinedoc { color: #0000FF; }
  19.  .hl-var { color: #0066FF; }
  20.  .hl-url { color: #FF0000; }
  21.  .hl-special { color: #0000FF; }
  22.  .hl-number { color: #007F00; }
  23.  .hl-inlinetags { color: #FF0000; }
  24. </style>
  25. <?php
  26.   echo $html;
  27. ?>

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
  1. <?php
  2.   require_once 'Text/Highlighter.php';
  3.   $options = array('numbers' => HL_NUMBERS_TABLE);
  4.   $hl =Text_Highlighter::factory('PHP'$options);
  5.   $code file_get_contents('somefile.php');
  6.   $html $hl->highlight($code);
  7. ?>
  8. <style>
  9.  .hl-main {font-family: monospace; font-size:13px;}
  10.  .hl-gutter { background-color: #CCCCCC; padding-right: 10px; 
  11.               font-family: monospace; font-size:13px;}
  12.  .hl-table {border: solid 1px #000000; }
  13.  
  14.  .hl-default { color: #000000; }
  15.  .hl-code { color: #7f7f33; }
  16.  .hl-brackets { color: #009966; }
  17.  .hl-comment { color: #7F7F7F; }
  18.  .hl-quotes { color: #00007F; }
  19.  .hl-string { color: #7F0000; }
  20.  .hl-identifier { color: #000000; }
  21.  .hl-reserved { color: #7F007F; }
  22.  .hl-inlinedoc { color: #0000FF; }
  23.  .hl-var { color: #0066FF; }
  24.  .hl-url { color: #FF0000; }
  25.  .hl-special { color: #0000FF; }
  26.  .hl-number { color: #007F00; }
  27.  .hl-inlinetags { color: #FF0000; }
  28. </style>
  29. <?php
  30.   echo $html;
  31. ?>


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.


Prev Up Next
Creating a syntax highlighter

Documentation generated on Mon, 11 Mar 2019 13:51:46 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.