Exemple #1
0
function hyperlight_highlight_block($match)
{
    global $hyperlight_codes, $hyperlight_replace_token, $hyperlight_code_index;
    // Notice: a key and its value must NOT be separated by space!
    $attributes = preg_split('/\\s+/s', trim($match[1]));
    $code = $match[2];
    if (count($attributes) > 0) {
        $new_attr = array();
        foreach ($attributes as $attr) {
            list($name, $value) = explode('=', $attr);
            $new_attr[$name] = $value;
        }
        $attributes = $new_attr;
    }
    if (array_key_exists('lang', $attributes)) {
        $lang = trim($attributes['lang'], '"\'');
        $attributes = array_diff_key($attributes, array('lang' => ''));
    }
    if (!isset($lang)) {
        return $match[0];
    }
    // No language given: don't highlight.
    $quote = '"';
    $class = "source-code {$lang}";
    if (array_key_exists('class', $attributes)) {
        $oldclass = $attributes['class'];
        if (substr($oldclass, 0, 1) === "'") {
            $quote = "'";
        }
        $class .= ' ' . trim($oldclass, '"\'');
    }
    $attributes['class'] = "{$quote}{$class}{$quote}";
    $new_attr = array();
    foreach ($attributes as $key => $value) {
        $new_attr[] = "{$key}={$value}";
    }
    $attributes = ' ' . implode(' ', $new_attr);
    $hyperlight = new Hyperlight($lang);
    $code = $hyperlight->render($code);
    $index = "{$hyperlight_replace_token}{$hyperlight_code_index}";
    ++$hyperlight_code_index;
    $hyperlight_codes[$index] = "<pre{$attributes}>{$code}</pre>";
    return $index;
}
Exemple #2
0
function hyperlight_highlight_block($match)
{
    global $hyperlight_codes, $hyperlight_replace_token, $hyperlight_code_index;
    list(, $lang, $code) = $match;
    $code = html_entity_decode($code, ENT_QUOTES);
    if (!isset($lang)) {
        return $match[0];
    }
    // No language given: don't highlight.
    // Auto-detect implicit PHP code
    if ('php' == $lang && !preg_match('/(<\\?php|\\?>)/', $code)) {
        $lang = 'iphp';
    }
    $hyperlight = new Hyperlight($lang);
    $code = $hyperlight->render($code);
    // $code =  htmlspecialchars_decode($code, ENT_NOQUOTES);
    $index = "{$hyperlight_replace_token}{$hyperlight_code_index}";
    ++$hyperlight_code_index;
    $hyperlight_codes[$index] = '<code class="' . esc_attr($lang) . '">' . $code . '</code>';
    return $index;
}