PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/markup/syntax/highlighter/PhutilPygmentsSyntaxHighlighter.php

http://github.com/facebook/libphutil
PHP | 216 lines | 203 code | 10 blank | 3 comment | 5 complexity | f690aa559980228b535a157976a61772 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. final class PhutilPygmentsSyntaxHighlighter extends Phobject {
  3. private $config = array();
  4. public function setConfig($key, $value) {
  5. $this->config[$key] = $value;
  6. return $this;
  7. }
  8. public function getHighlightFuture($source) {
  9. $language = idx($this->config, 'language');
  10. if (preg_match('/\r(?!\n)/', $source)) {
  11. // TODO: Pygments converts "\r" newlines into "\n" newlines, so we can't
  12. // use it on files with "\r" newlines. If we have "\r" not followed by
  13. // "\n" in the file, skip highlighting.
  14. $language = null;
  15. }
  16. if ($language) {
  17. $language = $this->getPygmentsLexerNameFromLanguageName($language);
  18. $future = new ExecFuture(
  19. 'pygmentize -O encoding=utf-8 -O stripnl=False -f html -l %s',
  20. $language);
  21. $scrub = false;
  22. if ($language == 'php' && strpos($source, '<?') === false) {
  23. $source = "<?php\n".$source;
  24. $scrub = true;
  25. }
  26. $future->write($source);
  27. return new PhutilDefaultSyntaxHighlighterEnginePygmentsFuture(
  28. $future,
  29. $source,
  30. $scrub);
  31. }
  32. return id(new PhutilDefaultSyntaxHighlighter())
  33. ->getHighlightFuture($source);
  34. }
  35. private function getPygmentsLexerNameFromLanguageName($language) {
  36. static $map = array(
  37. 'adb' => 'ada',
  38. 'ads' => 'ada',
  39. 'ahkl' => 'ahk',
  40. 'as' => 'as3',
  41. 'asax' => 'aspx-vb',
  42. 'ascx' => 'aspx-vb',
  43. 'ashx' => 'aspx-vb',
  44. 'ASM' => 'nasm',
  45. 'asm' => 'nasm',
  46. 'asmx' => 'aspx-vb',
  47. 'aspx' => 'aspx-vb',
  48. 'autodelegate' => 'myghty',
  49. 'autohandler' => 'mason',
  50. 'aux' => 'tex',
  51. 'axd' => 'aspx-vb',
  52. 'b' => 'brainfuck',
  53. 'bas' => 'vb.net',
  54. 'bf' => 'brainfuck',
  55. 'bmx' => 'blitzmax',
  56. 'c++' => 'cpp',
  57. 'c++-objdump' => 'cpp-objdump',
  58. 'cc' => 'cpp',
  59. 'cfc' => 'cfm',
  60. 'cfg' => 'ini',
  61. 'cfml' => 'cfm',
  62. 'cl' => 'common-lisp',
  63. 'clj' => 'clojure',
  64. 'cmd' => 'bat',
  65. 'coffee' => 'coffee-script',
  66. 'cs' => 'csharp',
  67. 'csh' => 'tcsh',
  68. 'cw' => 'redcode',
  69. 'cxx' => 'cpp',
  70. 'cxx-objdump' => 'cpp-objdump',
  71. 'darcspatch' => 'dpatch',
  72. 'def' => 'modula2',
  73. 'dhandler' => 'mason',
  74. 'di' => 'd',
  75. 'duby' => 'rb',
  76. 'dyl' => 'dylan',
  77. 'ebuild' => 'bash',
  78. 'eclass' => 'bash',
  79. 'el' => 'common-lisp',
  80. 'eps' => 'postscript',
  81. 'erl' => 'erlang',
  82. 'erl-sh' => 'erl',
  83. 'f' => 'fortran',
  84. 'f90' => 'fortran',
  85. 'feature' => 'Cucumber',
  86. 'fhtml' => 'velocity',
  87. 'flx' => 'felix',
  88. 'flxh' => 'felix',
  89. 'frag' => 'glsl',
  90. 'g' => 'antlr-ruby',
  91. 'G' => 'antlr-ruby',
  92. 'gdc' => 'gooddata-cl',
  93. 'gemspec' => 'rb',
  94. 'geo' => 'glsl',
  95. 'GNUmakefile' => 'make',
  96. 'h' => 'c',
  97. 'h++' => 'cpp',
  98. 'hh' => 'cpp',
  99. 'hpp' => 'cpp',
  100. 'hql' => 'sql',
  101. 'hrl' => 'erlang',
  102. 'hs' => 'haskell',
  103. 'htaccess' => 'apacheconf',
  104. 'htm' => 'html',
  105. 'html' => 'html+evoque',
  106. 'hxx' => 'cpp',
  107. 'hy' => 'hybris',
  108. 'hyb' => 'hybris',
  109. 'ik' => 'ioke',
  110. 'inc' => 'pov',
  111. 'j' => 'objective-j',
  112. 'jbst' => 'duel',
  113. 'kid' => 'genshi',
  114. 'ksh' => 'bash',
  115. 'less' => 'css',
  116. 'lgt' => 'logtalk',
  117. 'lisp' => 'common-lisp',
  118. 'll' => 'llvm',
  119. 'm' => 'objective-c',
  120. 'mak' => 'make',
  121. 'Makefile' => 'make',
  122. 'makefile' => 'make',
  123. 'man' => 'groff',
  124. 'mao' => 'mako',
  125. 'mc' => 'mason',
  126. 'md' => 'minid',
  127. 'mhtml' => 'mason',
  128. 'mi' => 'mason',
  129. 'ml' => 'ocaml',
  130. 'mli' => 'ocaml',
  131. 'mll' => 'ocaml',
  132. 'mly' => 'ocaml',
  133. 'mm' => 'objective-c',
  134. 'mo' => 'modelica',
  135. 'mod' => 'modula2',
  136. 'moo' => 'moocode',
  137. 'mu' => 'mupad',
  138. 'myt' => 'myghty',
  139. 'ns2' => 'newspeak',
  140. 'pas' => 'delphi',
  141. 'patch' => 'diff',
  142. 'phtml' => 'html+php',
  143. 'pl' => 'prolog',
  144. 'plot' => 'gnuplot',
  145. 'plt' => 'gnuplot',
  146. 'pm' => 'perl',
  147. 'po' => 'pot',
  148. 'pp' => 'puppet',
  149. 'pro' => 'prolog',
  150. 'proto' => 'protobuf',
  151. 'ps' => 'postscript',
  152. 'pxd' => 'cython',
  153. 'pxi' => 'cython',
  154. 'py' => 'python',
  155. 'pyw' => 'python',
  156. 'pyx' => 'cython',
  157. 'R' => 'splus',
  158. 'r' => 'rebol',
  159. 'r3' => 'rebol',
  160. 'rake' => 'rb',
  161. 'Rakefile' => 'rb',
  162. 'rbw' => 'rb',
  163. 'rbx' => 'rb',
  164. 'rest' => 'rst',
  165. 'rl' => 'ragel-em',
  166. 'robot' => 'robotframework',
  167. 'Rout' => 'rconsole',
  168. 'rss' => 'xml',
  169. 's' => 'gas',
  170. 'S' => 'splus',
  171. 'sc' => 'python',
  172. 'scm' => 'scheme',
  173. 'SConscript' => 'python',
  174. 'SConstruct' => 'python',
  175. 'scss' => 'css',
  176. 'sh' => 'bash',
  177. 'sh-session' => 'console',
  178. 'spt' => 'cheetah',
  179. 'sqlite3-console' => 'sqlite3',
  180. 'st' => 'smalltalk',
  181. 'sv' => 'v',
  182. 'tac' => 'python',
  183. 'tmpl' => 'cheetah',
  184. 'toc' => 'tex',
  185. 'tpl' => 'smarty',
  186. 'txt' => 'text',
  187. 'vapi' => 'vala',
  188. 'vb' => 'vb.net',
  189. 'vert' => 'glsl',
  190. 'vhd' => 'vhdl',
  191. 'vimrc' => 'vim',
  192. 'vm' => 'velocity',
  193. 'weechatlog' => 'irc',
  194. 'wlua' => 'lua',
  195. 'wsdl' => 'xml',
  196. 'xhtml' => 'html',
  197. 'xml' => 'xml+evoque',
  198. 'xqy' => 'xquery',
  199. 'xsd' => 'xml',
  200. 'xsl' => 'xslt',
  201. 'xslt' => 'xml',
  202. 'yml' => 'yaml',
  203. );
  204. return idx($map, $language, $language);
  205. }
  206. }