Beispiel #1
0
 public function highlight()
 {
     $this->parseArgs();
     // figure out the code
     // error cases are:
     if ($this->options['code'] === null && $this->options['input-file'] === null) {
         // no input file or source code,
         $this->error('No input file or source code specified');
     } elseif ($this->options['code'] !== null && $this->options['input-file'] !== null) {
         // or both input file and source code
         $this->error('Input file (-i) and source code specified. You probably didn\'t mean this');
     }
     if ($this->options['input-file'] !== null) {
         // is there an input file? use that.
         $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'] === '-') {
         // else we're expecting code to have been given on the command line,
         // but it might be '-' which means read stdin
         $code = '';
         while (($line = fgets(STDIN)) !== false) {
             $code .= $line;
         }
         $this->options['code'] = $code;
     }
     // set the formatter
     LuminousUi::set('format', $this->options['format']);
     // lame check that the formatter is okay
     try {
         LuminousUi::formatter();
     } catch (Exception $e) {
         $this->error('Unknown formatter ' . $this->options['format']);
     }
     // set the theme
     $validThemes = LuminousUi::themes();
     $theme = $this->options['theme'];
     if (!preg_match('/\\.css$/', $theme)) {
         $theme .= '.css';
     }
     if (!LuminousUi::themeExists($theme)) {
         $this->error('No such theme: ' . $theme);
     } else {
         LuminousUi::set('theme', $theme);
     }
     // set the language
     if ($this->options['lang'] === null) {
         // guessing
         $this->options['lang'] = LuminousUi::guessLanguage($this->options['code']);
     }
     // user provided language
     $scanners = LuminousUi::scanners();
     $validScanner = false;
     foreach ($scanners as $lang => $codes) {
         if (in_array($this->options['lang'], $codes)) {
             $validScanner = true;
             break;
         }
     }
     if (!$validScanner) {
         $this->error('No such language: ' . $this->options['lang']);
     }
     // other options
     LuminousUi::set('max-height', $this->options['height']);
     LuminousUi::set('line-numbers', $this->options['line-numbers']);
     $h = LuminousUi::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
 /**
  * Sets up a formatter instance according to our current options/settings
  */
 private function setFormatterOptions(&$formatter)
 {
     $formatter->wrapLength = $this->settings->wrapWidth;
     $formatter->lineNumbers = $this->settings->lineNumbers;
     $formatter->startLine = $this->settings->startLine;
     $formatter->link = $this->settings->autoLink;
     $formatter->height = $this->settings->maxHeight;
     $formatter->strictStandards = $this->settings->htmlStrict;
     $formatter->setTheme(LuminousUi::theme($this->settings->theme));
     $formatter->highlightLines = $this->settings->highlightLines;
     $formatter->language = $this->language;
     $formatter->colorDistanceAlgorithm = $this->settings->colorDistanceAlgorithm;
 }
Beispiel #3
0
 private function setTheme($value)
 {
     if (self::checkType($value, 'string')) {
         if (!preg_match('/\\.css$/', $value)) {
             $value .= '.css';
         }
         if (!LuminousUi::themeExists($value)) {
             throw new Exception('No such theme: ' . LuminousUi::root() . '/style/' . $value);
         } else {
             $this->theme = $value;
         }
     }
 }