Jump to main content
University Computer Centre
Syntax highlighting module

Syntax highlighting module

  1. Usage
  2. Examples
    1. Multiline strings
    2. Within a line
    3. Reading a file with line numbering

With the help of this module, web authors can easily highlight code blocks for a better overview. The following languages and configuration files are supported:

  • PHP
  • HTML
  • CSS
  • JavaScript
  • htaccess ‐ Apache web server configuration file

To use the functions, the file php/syn.inc must be loaded at the top of the page:

<?php
    …
    # Use of the syntax highlighting module
    require_once('php/syn.inc');
    seite(__FILE__);
?>

Usage

Initialisation with $myVar = new TucalSyn($source$language, [$lineNumbers, [$lineBegin, [$lineEnd]]])

  • $source string, code to be output
  • $language string, language of the code. Possibilities are:
    • php, for PHP and JS
    • html, for HTML and PHP within HTML
    • css
    • htaccess
  • $lineNumbers boolean, determines whether line numbers should be displayed [optional]
  • $lineBegin integer, line number where the string should start [optional]
  • $lineEnd integer, line number where the string should end [optional]

Functions of the TucalSyn class

  • $myVar->parseCode(), returns the output as string
  • $myVar->setWithinLine(true), formats the output to be used within a line or a sentence
  • Modification methods:
    • $myVar->setSource($source) string
    • $myVar->setLang($lang) string
    • $myVar->setLineNumbers($lineNumbers) boolean
    • $myVar->setLineBegin($lineBegin) integer
    • $myVar->setLineEnd($lineEnd) integer

Examples

Multiline strings

<?php
    …
    # Use of the syntax highlighting module
    require_once('php/syn.inc');
    seite(__FILE__);
?>
<?php
$temp_string "<?php
…
# Use of the syntax highlighting module
require_once('php/syn.inc');
seite(__FILE__);
?>";

$tsyn = new tucalsyn($temp_string'php');
echo $tsyn->parseCode();
?>

Within a line

$myVar = new TucalSyn($source$language, [$lineNumbers, [$lineBegin, [$lineEnd]]])
<?php
$temp_string '$myVar = new TucalSyn($source, $language, $lineNumbers, $lineBegin, $lineEnd)';
$tsyn = new TucalSyn($temp_string'php');
$tsyn->setWithinLine(true);
echo $tsyn->parseCode();
?>

Reading a file with line numbering

            <li><a href="#syn_exam_inner">Within a line</a></li>
            <li><a href="#syn_exam_ausl">Reading a file with line numbering</a></li>
        </ol>
    </li>
</ol>

<p style="margin-bottom:0">
With the help of this module, web authors can easily highlight code blocks for a better overview.
The following languages and configuration files are supported:
</p>
<ul>
<?php
$tsyn = new tucalsyn(file_get_contents("highlight.html.en"), 'html'true1525);
echo $tsyn->parseCode();
?>

Start line numbering at line 15

To have the line numbering start at a certain number, the CSS property counter-reset must be assigned.

            <li><a href="#syn_exam_inner">Within a line</a></li>
            <li><a href="#syn_exam_ausl">Reading a file with line numbering</a></li>
        </ol>
    </li>
</ol>

<p style="margin-bottom:0">
With the help of this module, web authors can easily highlight code blocks for a better overview.
The following languages and configuration files are supported:
</p>
<ul>
<div class="tucbox-example counterreset">
<?php
$tsyn = new tucalsyn(file_get_contents("highlight.html.en"), 'html'true1525);
echo $tsyn->parseCode();
?>
</div>
/* CSS: */
.counterreset span.ln {
    /*  "linenb" must not be changed;
        the number given must be one lower than the desired starting value. */
    counter-reset: linenb 14;
}