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> </div></li>'; #echo '</ol>'; ?> <pre class="source-code <?php echo strtolower($lang); ?> "><?php $hl->renderAndPrint($code); ?> </pre><?php }
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; }
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; }
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 }
/** * <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}>"; }