public function __construct($id) { $this->dir = LuminousUi::root() . '/cache/'; $this->subdir = substr($id, 0, 2); $this->filename = substr($id, 2); $this->path = rtrim($this->dir, '/') . '/' . $this->subdir . '/' . $this->filename; parent::__construct($id); }
function generate($path) { global $output_ext; $out = highlight_luminous($path); if ($output_ext === '._html.luminous') { $out = Luminous::headHtml() . $out; $out = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . $out; } file_put_contents($path . $output_ext, $out); }
function doAjax() { global $useCache; $language = $_POST['language']; $code = $_POST['code']; // Arbitrarily sized security check if (strlen($code) < 500) { echo Luminous::highlight($language, $code, $useCache); } die(0); // we're done now. }
</style> </head> <body> <table style='margin-left:auto; margin-right: auto;'> <thead> <tr> <th style='border: 0px'></th> <th>Language</th> <th>Valid Codes</th> </tr> </thead> <tbody> <?php $i = 0; foreach (Luminous::scanners() as $l => $codes) { ?> <tr> <td><?php echo ++$i; ?> </td> <td><?php echo $l; ?> </td> <td><?php echo join(', ', $codes); ?> </td> </tr>
<!DOCTYPE html> <html> <head> <title>Inline code highlighting with AJAX example</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <?php echo Luminous::headHtml(); ?> </head> <body> Lorem ipsum dolor sit amet, <?php echo Luminous::highlight('c', '#include <stdio.h>'); ?> consectetur adipiscing elit. Pellentesque <?php echo Luminous::highlight('c', 'int main()'); ?> orci eros, pellentesque sed elementum eu, mattis nec neque. Vestibulum hendrerit leo vel mi tristique mollis. Mauris magna odio, porta ut fringilla iaculis, <?php echo Luminous::highlight('c', 'printf("hello, world!\\n");'); ?> placerat eu urna. Vivamus non nisi nec <?php echo Luminous::highlight('c', 'return 0;'); ?> ante euismod vehicula. Curabitur nec enim tortor. Proin viverra ligula nec quam pulvinar vehicula. Vivamus turpis diam </body> </html>
<?php require __DIR__ . '/helper.php'; $files = array('Standard example' => 'example.php', 'AJAX interface' => 'ajax.php', 'Full page output' => 'fullpage.php', 'Inline code' => 'inline.php', 'Theme switcher' => 'themeswitcher.php', 'Setting options' => 'options.php'); if (isset($_GET['file']) && in_array($_GET['file'], $files)) { Luminous::set('includeJquery', true); $source = Luminous::highlight('php', file_get_contents(__DIR__ . '/' . $_GET['file'])); Luminous::set('theme', 'github'); $head = Luminous::headHtml(); echo <<<EOF <!DOCTYPE html> <html> <head> <title></title> <style> body { font-size: smaller; margin: 0;} </style> {$head} </head> <body> {$source} </body> </html> EOF; exit(0); } ?> <!DOCTYPE html> <html> <head> <title>Luminous examples</title> <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<?php if (php_sapi_name() !== 'cli') { die('This must be run from the command line'); } /* * Very simple check to ensure that each scanner is okay, and does not * contain any syntax errors which prevent PHP compilation, or any errors * which are trivially possible to reproduce. * * Scanners are included lazily, so it's possible that a syntax error could * go undetected for some time */ require 'helper.inc'; error_reporting(E_ALL | E_STRICT); foreach (Luminous::scanners() as $codes) { Luminous::highlight($codes[0], ' '); }
$defaultTheme = Luminous::setting('theme'); if (!preg_match('/\\.css$/', $defaultTheme)) { $defaultTheme .= '.css'; } foreach (Luminous::themes() as $theme) { ?> <option id='<?php echo $theme; ?> ' <?php echo $theme == $defaultTheme ? ' selected' : ''; ?> > <?php echo $theme; ?> </option> <?php } ?> </select> <noscript><input type='submit' value='Switch'></noscript> </form> <p> <?php echo Luminous::highlightFile('php', __FILE__, $useCache); ?> </body> </html>
<?php // very easy, set the format to html-full require_once 'helper.php'; Luminous::set('format', 'html-full'); echo luminous::highlightFile('php', 'themeswitcher.php', $useCache);
echo Luminous::highlight('php', <<<EOF <?php /** * @ingroup LuminousUtils * @internal * @brief Decodes a PCRE error code into a string * @param errcode The error code to decode (integer) * @return A string which is simply the name of the constant which matches the * error code (e.g. 'PREG_BACKTRACK_LIMIT_ERROR') * * @todo this should all be namespaced */ function pcre_error_decode(\$errcode) { switch (\$errcode) { case PREG_NO_ERROR: return 'PREG_NO_ERROR'; case PREG_INTERNAL_ERROR: return 'PREG_INTERNAL_ERROR'; case PREG_BACKTRACK_LIMIT_ERROR: return 'PREG_BACKTRACK_LIMIT_ERROR'; case PREG_RECURSION_LIMIT_ERROR: return 'PREG_RECURSION_LIMIT_ERROR'; case PREG_BAD_UTF8_ERROR: return 'PREG_BAD_UTF8_ERROR'; case PREG_BAD_UTF8_OFFSET_ERROR: return 'PREG_BAD_UTF8_OFFSET_ERROR'; default: return 'Unknown error code'; } } EOF , $useCache);
<?php $luminousRoot = dirname(__DIR__); if (file_exists($luminousRoot . '/vendor/autoload.php')) { // standalone install require_once $luminousRoot . '/vendor/autoload.php'; } elseif (file_exists($luminousRoot . '/../../autoload.php')) { // dep install require_once $luminousRoot . '/../../autoload.php'; } else { die('Please install the Composer autoloader by running `composer install` from within ' . $luminousRoot . PHP_EOL); } set_time_limit(2); // This var is because on my dev machine I symlink some directories and // from that, PHP/Luminous cannot figure out where it is relative to the // document root. $httpPath = '../'; $useCache = false; Luminous::set('relative-root', $httpPath); Luminous::set('include-jquery', true);
function testCache() { global $sqlExecuted; $sqlExecuted = false; Luminous::set('sql_function', 'sql'); // this will throw a cache not creatable warning which we don't really care // about @Luminous::highlight('plain', '123', true); assert($sqlExecuted); }
/** * Handles the parsing process. It's recommended to use Easy_Parse instead, * which wraps this function (and others). * \return a string which is formatted with an internal tagging spec. * But the caller should not worry about that, it's only for recursion. * * \throw Exception in the event that the PCRE module fails somehow. The * error is given in the exception message, but this is likely down to an * exceptionally large string which is triggering the PCRE backtrack limit. * The parser should survive, but the string will not by syntax highlighted * */ public function Parse_Full() { $this->DoStartEnds(); $pos = -1; $parse_nothing = false; $parse_everything = false; // no legal start/end if (count($this->grammar->ignore_outside) && (!count($this->starts) || !count($this->ends))) { // Strict mode - don't parse anything if ($this->grammar->ignore_outside_strict) { $parse_nothing = true; } } if (!count($this->grammar->ignore_outside)) { $parse_everything = true; } if ($parse_nothing) { $this->output = $this->input_src; } else { if ($parse_everything) { $this->parse_all = true; } $this->ParseDelimiters(); if ($parse_everything) { $this->output = str_replace("<&START>", "", $this->output); $this->output = str_replace("<&END>", "", $this->output); $this->output = $this->Parse_Regex_Wrapper_Callback(array(0, $this->output)); } else { $this->SplitStartEnds(); } } if ($this->output === null && preg_last_error() !== PREG_NO_ERROR) { throw new Exception("PCRE error: " . pcre_error_decode(preg_last_error())); $this->output = $this->input; return $this->input; } if ($this->grammar->child_grammar !== null) { $highlighter = new Luminous(); $highlighter->verbosity = $this->verbosity; $highlighter->SetSource($this->output); $highlighter->SetGrammar($this->grammar->child_grammar); $highlighter->SetExtractionsOffset($this->num_extractions + $this->extractions_offset); $highlighter->FinaliseSetup(); $this->output = $highlighter->Parse_Full(); } $this->DoReplacements(); return $this->output; }
<title>Setting options</title> <?php echo Luminous::headHtml(); ?> </head> <body> <p> There are two ways you can set options: globally via the set() method, and per-call in the highlight() method. Let's disable auto-linking and make all highlights begin at line 17 via the global call. <?php // NOTE: this is equivalent to calling luminous::set(array('auto-link' => false, 'start-line' => 17)); Luminous::set('autoLink', false); Luminous::set('startLine', 17); echo Luminous::highlight($language, $code); ?> <p> Now let's override both of those for the duration of the next call <?php echo Luminous::highlight($language, $code, array('autoLink' => true, 'startLine' => 1)); ?> <p> When we next call highlight(), the options will be back to their global states: <?php echo Luminous::highlight($language, $code); ?> <p> We can get the current value for an option by calling setting(): auto-link is: <?php echo var_dump(Luminous::setting('autoLink')); ?> . </body> </html>
/** * \ingroup LuminousEasyAPI * \brief Simple call to luminous with language grammar * * \param grammar The grammar to use to parse the string, as LuminousGrammar * \param src the source string to be formatted (string) * \param use_cache determines whether to use the caching system, * default is true. (true|false) * \return an HTML formatted piece of text representing the input string * \throw Exception if luminous encounters a fatal error (a more descriptive * string will be set as its message) * \see Luminous * \see LuminousEasyAPI::$luminous_grammars */ function luminous_grammar(LuminousGrammar $grammar, $src, $use_cache = true) { global $LUMINOUS_PERFORMANCE_LOG; $start = microtime(true); $perf_log = array(); $perf_log['language'] = $grammar->info['language']; $perf_log['input_size'] = strlen($src); $perf_log['cache_time'] = 0.0; $o = false; $cache = null; if ($use_cache) { $c_t = microtime(true); // the cache's unique ID needs to be an alagamation of the input source, // grammar, and highlighting settings. $md5 = md5($src); $id = md5($md5 . serialize($grammar->info)); $id = md5($id . serialize(luminous_create_settings())); $cache = new LuminousCache($id, $md5); $cache->version = $GLOBALS['LUMINOUS_VERSION']; $cache->cache_max_age = $GLOBALS['LUMINOUS_MAX_AGE']; $cache->purge_time = $GLOBALS['LUMINOUS_PURGE_TIME']; $cache->Purge(); $o = $cache->ReadCache(); $GLOBALS['LUMINOUS_WAS_CACHED'] = true; $perf_log['cached'] = true; $perf_log['parse_time'] = 0.0; $perf_log['format_time'] = 0.0; $perf_log['cache_time'] = microtime(true) - $c_t; } if ($o === false) { $perf_log['cached'] = false; $GLOBALS['LUMINOUS_WAS_CACHED'] = false; $p_start = microtime(true); $l = new Luminous(); $l->verbosity = $GLOBALS['LUMINOUS_HIGHLIGHTING_LEVEL']; $l->pre_escaped = !$GLOBALS['LUMINOUS_ESCAPE_INPUT']; $l->separate_lines = $GLOBALS['LUMINOUS_LINE_NUMBERS']; $o = $l->Easy_Parse($src, $grammar); $p_end = microtime(true); $f_start = microtime(true); $f = luminous_get_formatter(); // $f = new LuminousFormatterHTML(); $f->SetTheme(luminous_get_theme()); $f->wrap_length = $GLOBALS['LUMINOUS_WRAP_WIDTH']; $f->line_numbers = $GLOBALS['LUMINOUS_LINE_NUMBERS']; $f->link = $GLOBALS['LUMINOUS_LINK_URIS']; $f->height = $GLOBALS['LUMINOUS_WIDGET_HEIGHT']; // this really shouldn't be here. if ($grammar instanceof LuminousGrammarWhitespace) { $f->tab_width = -1; } $o = $f->Format($o); $f_end = microtime(true); if ($use_cache) { $cache->WriteCache($o); } $perf_log['format_time'] = $f_end - $f_start; $perf_log['parse_time'] = $p_end - $p_start; } $end = microtime(true); $perf_log['time'] = $end - $start; $perf_log['output_size'] = strlen($o); $LUMINOUS_PERFORMANCE_LOG[] = $perf_log; return $o; }
$line_numbers = false; } $line_numbers_start = false; if (!empty($_POST) && isset($_POST['line-numbers-start'])) { $line_numbers_start = (int) $_POST['line-numbers-start']; if ($line_numbers_start > 0) { Luminous::set('start-line', $line_numbers_start); } } Luminous::set('line-numbers', $line_numbers); ?> <!DOCTYPE html> <html> <head> <?php echo Luminous::headHtml(); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <?php if (count($_POST)) { $lang = $_POST['lang']; if ($lang === 'guess') { $s = microtime(true); $guesses = luminous::guessLanguageFull($_POST['src']); $e = microtime(true); $lang = luminous::guessLanguage($_POST['src']); $printable_guesses = array(); foreach ($guesses as $g) {
protected function getLayout() { // this path info shouldn't really be here $path = LuminousUi::root() . '/style/luminous.css'; $this->css = file_get_contents($path); }