|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pygments; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class Pygmentize |
| 7 | + * @package Pygments |
| 8 | + */ |
| 9 | +class Pygmentize |
| 10 | +{ |
| 11 | + |
| 12 | + const EXIT_SUCCESS = 0; |
| 13 | + |
| 14 | + private static $bin = '/usr/bin/pygmentize'; |
| 15 | + |
| 16 | + public static function version() |
| 17 | + { |
| 18 | + $return = self::exec('-V'); |
| 19 | + $res = preg_match( |
| 20 | + '/Pygments version ([^,]*), \(c\) (?:.*)/i', |
| 21 | + $return, |
| 22 | + $matches |
| 23 | + ); |
| 24 | + if($res) { |
| 25 | + return $matches[1]; |
| 26 | + } else { |
| 27 | + return $return; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public static function lexers() |
| 32 | + { |
| 33 | + $return = self::exec('-L lexers'); |
| 34 | + preg_match_all('/\* (.*):(?:\s*)([^\(\*\n]*)/', $return, $matches); |
| 35 | + $lexers = array(); |
| 36 | + foreach($matches[1] as $k=>$match) { |
| 37 | + $list = explode(', ', $match); |
| 38 | + foreach($list as $e) { |
| 39 | + $lexers[$e] = $matches[2][$k]; |
| 40 | + } |
| 41 | + } |
| 42 | + return $lexers; |
| 43 | + } |
| 44 | + |
| 45 | + public static function styles() |
| 46 | + { |
| 47 | + $return = self::exec('-L styles'); |
| 48 | + preg_match_all('/\* (.*):(?:\s*)([^\n]*)/', $return, $matches); |
| 49 | + $styles = array(); |
| 50 | + foreach($matches[1] as $k=>$match) { |
| 51 | + $styles[$match] = $matches[2][$k]; |
| 52 | + } |
| 53 | + return $styles; |
| 54 | + } |
| 55 | + |
| 56 | + public static function getStyle($style) |
| 57 | + { |
| 58 | + if(!array_key_exists($style, self::styles())) { |
| 59 | + throw new StyleException('Style not supported: ' . $style); |
| 60 | + } |
| 61 | + return self::exec('-f html -S ' . $style); |
| 62 | + } |
| 63 | + |
| 64 | + public static function format($source, $lexer) |
| 65 | + { |
| 66 | + if(!array_key_exists($lexer, self::lexers())) { |
| 67 | + throw new LexerException('Lexer not supported: ' . $lexer); |
| 68 | + } |
| 69 | + $format = self::exec('-f html -l ' . $lexer, $source); |
| 70 | + preg_match('#<div class="highlight"><pre>(.*)\s</pre></div>#s', $format, $matches); |
| 71 | + return $matches[1]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param $options |
| 76 | + * @param string $pipe |
| 77 | + * @throws FormatException |
| 78 | + * @throws \Exception |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + private static function exec($options, $pipe = null) |
| 82 | + { |
| 83 | + $cmd = $pipe ? 'echo ' . escapeshellarg($pipe) . ' | ' : ''; |
| 84 | + exec($cmd . self::$bin . ' ' . $options . ' 2>&1', $output, $returnVar); |
| 85 | + $output = implode(PHP_EOL, $output); |
| 86 | + if ($returnVar == self::EXIT_SUCCESS) { |
| 87 | + return $output; |
| 88 | + } |
| 89 | + $error = self::parseError($output); |
| 90 | + if($error == 'format') { |
| 91 | + throw new FormatException($output); |
| 92 | + } else { |
| 93 | + throw new \Exception($output); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static function parseError($output) |
| 98 | + { |
| 99 | + if(preg_match("/Error: No formatter found for name '(.*)'/", $output)) { |
| 100 | + return 'format'; |
| 101 | + } else { |
| 102 | + return 'error'; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | + |
0 commit comments