Beispiel #1
0
 function highlight()
 {
     $this->parse_args();
     // figure out the code
     // error cases are:
     // no input file or source code,
     if ($this->options['code'] === null && $this->options['input-file'] === null) {
         $this->error('No input file or source code specified');
     } elseif ($this->options['code'] !== null && $this->options['input-file'] !== null) {
         $this->error('Input file (-i) and source code specified. You probably ' . 'didn\'t mean this');
     }
     // is there an input file? use that.
     if ($this->options['input-file'] !== null) {
         $c = @file_get_contents($this->options['input-file']);
         if ($c === false) {
             $this->error('Could not read from ' . $this->options['input-file']);
         } else {
             $this->options['code'] = $c;
         }
     } elseif ($this->options['code'] === '-') {
         $code = '';
         while (($line = fgets(STDIN)) !== false) {
             $code .= $line;
         }
         $this->options['code'] = $code;
     }
     // set the formatter
     luminous::set('format', $this->options['format']);
     // lame check that the formatter is okay
     try {
         luminous::formatter();
     } catch (Exception $e) {
         $this->error('Unknown formatter ' . $this->options['format']);
     }
     // set the theme
     $valid_themes = luminous::themes();
     $theme = $this->options['theme'];
     if (!preg_match('/\\.css$/', $theme)) {
         $theme .= '.css';
     }
     if (!luminous::theme_exists($theme)) {
         $this->error('No such theme: ' . $theme);
     } else {
         luminous::set('theme', $theme);
     }
     // set the language
     if ($this->options['lang'] === null) {
         // guessing
         $this->options['lang'] = luminous::guess_language($this->options['code']);
     }
     // user provided language
     $scanners = luminous::scanners();
     $valid_scanner = false;
     foreach ($scanners as $lang => $codes) {
         if (in_array($this->options['lang'], $codes)) {
             $valid_scanner = true;
             break;
         }
     }
     if (!$valid_scanner) {
         $this->error('No such language: ' . $this->options['lang']);
     }
     // other options
     luminous::set('max-height', $this->options['height']);
     luminous::set('line-numbers', $this->options['line-numbers']);
     $h = luminous::highlight($this->options['lang'], $this->options['code']);
     if ($this->options['output-file'] !== null) {
         $r = @file_put_contents($this->options['output-file'], $h, LOCK_EX);
         if ($r === false) {
             $this->error('Could not write to ' . $this->options['output-file']);
         }
     } else {
         echo $h;
     }
     exit(0);
 }
Beispiel #2
0
  <div style='text-align:center'>
    <form method='post' action='interface.php'>
    <select name='lang'>
    <option value='guess'>Guess Language</option>
    <?php 
foreach (luminous::scanners() as $lang => $codes) {
    $def = isset($_POST['lang']) && $_POST['lang'] === $codes[0] ? ' selected' : '';
    echo "<option value='{$codes[0]}'{$def}>{$lang}</option>\n";
}
?>
    <option value='no_such_scanner'>error case</option>
    </select>
    <br/>
    <select name='theme'>
    <?php 
foreach (luminous::themes() as $t) {
    $def = isset($_POST['theme']) && $_POST['theme'] === $t ? ' selected' : '';
    echo sprintf("<option value='%s'%s>%s</option>\n", $t, $def, preg_replace('/\\.css$/i', '', $t));
}
?>
  </select>
    <br/>
    <select name='format'>
    <?php 
foreach (array('html', 'html-full', 'latex') as $f) {
    $def = isset($_POST['format']) && $_POST['format'] === $f ? ' selected' : '';
    echo sprintf("<option value='%s'%s>%s</option>\n", $f, $def, $f);
}
?>
  </select>
    <br/>
Beispiel #3
0
if (php_sapi_name() !== 'cli') {
    die('This must be run from the command line');
}
// TODO: this no longer works, rewrite it for the new testing structure.
/**
 * tests the LaTeX formatter by formatting some code as LaTeX
 * then compiling it to pdf.
 * Requires pdflatex and the ability to call programs
 */
require_once dirname(__FILE__) . '/../../luminous.php';
$testfiles = glob(__DIR__ . '/samples/output/*');
$EXIT_STATUS = 0;
luminous::set('format', 'latex');
foreach ($testfiles as $t) {
    $ts = luminous::themes();
    $theme = $ts[array_rand(luminous::themes())];
    $formatter = luminous::formatter();
    $formatter->set_theme(file_get_contents('../../style/' . $theme));
    $src = file_get_contents($t);
    $t = preg_replace('%.*/%', '', $t);
    $fmt = $formatter->format($src);
    file_put_contents(__DIR__ . "/filedump/{$t}.tex", $fmt);
    chdir('filedump');
    system("pdflatex {$t}.tex >> /dev/null", $i);
    if ($i) {
        echo "latex formatter test failed on file {$t}, pdflatex exit status: {$i}\n";
        $EXIT_STATUS = 1;
    }
    chdir(getcwd() . '/../');
}
exit($EXIT_STATUS);