Exemplo n.º 1
0
function hyperlight_test($file, $lang = null)
{
    global $tests;
    if ($lang === null) {
        $lang = $file;
    }
    if (!empty($tests) and !in_array(strtolower($lang), $tests)) {
        return;
    }
    $fname = 'tests/' . strtolower($file);
    $code = file_get_contents($fname);
    $hl = new Hyperlight($lang);
    $pretty_name = $hl->language()->name();
    $title = $file === $lang ? "<h2>Test for language {$pretty_name}</h2>" : "<h2>Test with file “{$file}” for language {$pretty_name}</h2>";
    echo "{$title}\n";
    #$lines = count(explode("\n", $code)) - 1;
    #echo '<ol class="line-numbers">';
    #for ($i = 0; $i < $lines; $i++)
    #    echo '<li><div>&nbsp;</div></li>';
    #echo '</ol>';
    ?>
<pre class="source-code <?php 
    echo strtolower($lang);
    ?>
"><?php 
    $hl->renderAndPrint($code);
    ?>
</pre><?php 
}
Exemplo n.º 2
0
function hyperlight_test($file, $lang = null)
{
    global $tests;
    if ($lang === null) {
        $lang = $file;
    }
    if (!empty($tests) and !in_array(strtolower($lang), $tests)) {
        return;
    }
    $fname = 'tests/' . strtolower($file);
    $code = file_get_contents($fname);
    $hl = new Hyperlight($lang);
    $pretty_name = $hl->language()->name();
    $title = $file === $lang ? "<h2>Test for language {$pretty_name}</h2>" : "<h2>Test with file “{$file}” for language {$pretty_name}</h2>";
    echo "{$title}\n";
    ?>
<pre class="source-code <?php 
    echo strtolower($lang);
    ?>
"><?php 
    $hl->renderAndPrint($code);
    ?>
</pre><?php 
}
Exemplo n.º 3
0
/**
 * <var>echo</var>s a highlighted code.
 *
 * For example, the following
 * <code>
 * hyperlight('<?php echo \'Hello, world\'; ?>', 'php');
 * </code>
 * results in:
 * <code>
 * <pre class="source-code php">...</pre>
 * </code>
 *
 * @param string $code The code.
 * @param string $lang The language of the code.
 * @param string $tag The surrounding tag to use. Optional.
 * @param array $attributes Attributes to decorate {@link $tag} with.
 *          If no tag is given, this argument can be passed in its place. This
 *          behaviour will be assumed if the third argument is an array.
 *          Attributes must be given as a hash of key value pairs.
 */
function hyperlight($code, $lang, $tag = 'pre', array $attributes = array())
{
    if ($code == '') {
        die("`hyperlight` needs a code to work on!");
    }
    if ($lang == '') {
        die("`hyperlight` needs to know the code's language!");
    }
    if (is_array($tag) and !empty($attributes)) {
        die("Can't pass array arguments for \$tag *and* \$attributes to `hyperlight`!");
    }
    if ($tag == '') {
        $tag = 'pre';
    }
    if (is_array($tag)) {
        $attributes = $tag;
        $tag = 'pre';
    }
    $lang = htmlspecialchars(strtolower($lang));
    $class = "source-code {$lang}";
    $attr = array();
    foreach ($attributes as $key => $value) {
        if ($key == 'class') {
            $class .= ' ' . htmlspecialchars($value);
        } else {
            $attr[] = htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
        }
    }
    $attr = empty($attr) ? '' : ' ' . implode(' ', $attr);
    $hl = new Hyperlight($lang);
    echo "<{$tag} class=\"{$class}\"{$attr}>";
    $hl->renderAndPrint(trim($code));
    echo "</{$tag}>";
}