PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/src/refheap/highlight.clj

http://github.com/Raynes/refheap
Clojure | 259 lines | 256 code | 3 blank | 0 comment | 6 complexity | 366254c5b299fc0b3d1310b9382c1fdf MD5 | raw file
  1. (ns refheap.highlight
  2. (:require [me.raynes.conch :refer [let-programs]]
  3. [clojure.string :as string]
  4. [refheap.markdown :refer [to-html]]))
  5. (def lexers
  6. "A map of language names to pygments lexer names."
  7. {"Clojure" {:short "clojure"
  8. :exts #{"clj" "cljs"}}
  9. "Apricot" {:short "clojure"
  10. :exts #{"apr"}}
  11. "Factor" {:short "factor"
  12. :exts #{"factor"}}
  13. "Fancy" {:short "fancy"
  14. :exts #{"fy"}}
  15. "Groovy" {:short "groovy"
  16. :exts #{"groovy"}}
  17. "Io" {:short "io"
  18. :exts #{"io"}}
  19. "Ioke" {:short "ioke"
  20. :exts #{"ioke"}}
  21. "Lua" {:short "lua"
  22. :exts #{"lua"}}
  23. "Perl" {:short "perl"
  24. :exts #{"pl"}}
  25. "Python Console" {:short "pycon"}
  26. "Python" {:short "python"
  27. :exts #{"py"}}
  28. "Python Traceback" {:short "pytb"}
  29. "Ruby Console" {:short "irb"}
  30. "Ruby" {:short "ruby"
  31. :exts #{"rb"}}
  32. "Mirah" {:short "ruby"
  33. :exts #{"mirah"}}
  34. "Tcl" {:short "tcl"
  35. :exts #{"mirah"}}
  36. "C Object Dump" {:short "c-objdump"}
  37. "C++ Object Dump" {:short "cpp-objdump"}
  38. "D Object Dump" {:short "d-objdump"}
  39. "Gas" {:short "gas"}
  40. "LLVM" {:short "llvm"}
  41. "NASM" {:short "nasm"}
  42. "Object Dump" {:short "objdump"}
  43. "Ada" {:short "ada"
  44. :exts #{"ada"}}
  45. "BlitzMax" {:short "bmax"}
  46. "C" {:short "c"
  47. :exts #{"c" "h"}}
  48. "C++" {:short "cpp"
  49. :exts #{"cpp"}}
  50. "Cython" {:short "pyx"
  51. :exts #{"pyx"}}
  52. "D" {:short "d"
  53. :exts #{"d"}}
  54. "Delphi" {:short "delphi"} ;; What the fuck *is* the right extension for this?
  55. "Dylan" {:short "dylan"
  56. :exts #{"dylan"}}
  57. "Felix" {:short "flx"
  58. :exts #{"flx"}}
  59. "Fortran" {:short "fortran"
  60. :exts #{"fortran"}}
  61. "GLSL" {:short "glsl"}
  62. "Go" {:short "go"
  63. :exts #{"go"}}
  64. "Java" {:short "java"
  65. :exts #{"java"}}
  66. "Modula-2" {:short "m2"
  67. :exts #{"def" "mod"}}
  68. "Nimrod" {:short "nim"
  69. :exts #{"nim" "nimrod"}}
  70. "Objective-C" {:short "objective-c"
  71. :exts #{"m"}}
  72. "ooc" {:short "ooc"
  73. :exts #{"ooc"}}
  74. "Prolog" {:short "prolog"
  75. :exts #{"pro"}}
  76. "Scala" {:short "scala"
  77. :exts #{"scala"}}
  78. "Vala" {:short "vala"
  79. :exts #{"vala"}}
  80. "Boo" {:short "boo"
  81. :exts #{"boo"}}
  82. "C#" {:short "csharp"
  83. :exts #{"cs"}}
  84. "F#" {:short "fsharp"
  85. :exts #{"fs"}}
  86. "Nemerle" {:short "nemerle"
  87. :exts #{"n"}}
  88. "VB.NET" {:short "vbnet"
  89. :exts #{"vb"}}
  90. "Common Lisp" {:short "cl"
  91. :exts #{"lisp"}}
  92. "Erlang" {:short "erlang"
  93. :exts #{"erl"}}
  94. "Erlang Shell" {:short "erl"}
  95. "Haskell" {:short "hs"
  96. :exts #{"hs"}}
  97. "Literate Haskell" {:short "lhs"
  98. :exts #{"lhs"}}
  99. "OCaml" {:short "ocaml"
  100. :exts #{"ml"}}
  101. "Racket" {:short "racket"
  102. :exts #{"rkt" "rktl"}}
  103. "Scheme" {:short "scm"
  104. :exts #{"scm" "ss"}}
  105. "Emacs Lisp" {:short "scm"
  106. :exts #{"el"}}
  107. "Coq" {:short "coq"
  108. :exts #{"v"}}
  109. "Verilog" {:short "v"}
  110. "Matlab" {:short "matlab"}
  111. "MuPAD" {:short "mupad"}
  112. "NumPy" {:short "numpy"}
  113. "R Console" {:short "rout"}
  114. "R" {:short "r"
  115. :exts #{"r"}}
  116. "AppleScript" {:short "applescript"
  117. :exts #{"applescript"}}
  118. "Autohotkey" {:short "ahk"
  119. :exts #{"ahk"}}
  120. "Awk" {:short "awk"
  121. :exts #{"awk"}}
  122. "Bash" {:short "sh"
  123. :exts #{"sh"}}
  124. "Bash Session" {:short "console"}
  125. "Shell Session" {:short "shell-session"
  126. :exts #{"shell-session"}}
  127. "Batch" {:short "bat"
  128. :exts #{"bat"}}
  129. "Befunge" {:short "befunge"
  130. :exts #{"befunge"}}
  131. "Brainfuck" {:short "bf"
  132. :exts #{"bf"}}
  133. "Cucumber" {:short "cucumber"}
  134. "MOOCode" {:short "moocode"}
  135. "MySQL" {:short "mysql"}
  136. "NewSpeak" {:short "newspeak"}
  137. "NewLisp" {:short "newlisp"
  138. :exts #{"lsp" "nl"}}
  139. "PostScript" {:short "postscript"
  140. :exts #{"ps"}}
  141. "Protobuf" {:short "protobuf"
  142. :exts #{"proto"}}
  143. "REBOL" {:short "rebol"
  144. :exts #{"rebol"}}
  145. "COBOL" {:short "cobol"
  146. :exts #{"cob" "COB" "cpy" "CPY"}}
  147. "Rust" {:short "rust"
  148. :exts #{"rs" "rc"}}
  149. "Smali" {:short "smali"
  150. :exts #{"smali"}}
  151. "Ceylon" {:short "ceylon"
  152. :exts #{"ceylon"}}
  153. "Julia" {:short "julia"
  154. :exts #{"jl"}}
  155. "Julia Console" {:short "jlcon"}
  156. "AutoIt" {:short "autoit"
  157. :exts #{"au3"}}
  158. "Puppet" {:short "puppet"
  159. :exts #{"pp"}}
  160. "HXML" {:short "hxml"
  161. :exts #{"hxml"}}
  162. "TypeScript" {:short "ts"
  163. :exts #{"ts"}}
  164. "Redcode" {:short "redcode"}
  165. "Smalltalk" {:short "squeak"
  166. :exts #{"st"}}
  167. "SQL" {:short "sql"
  168. :exts #{"sql"}}
  169. "TCSH" {:short "csh"}
  170. "Apache Configuration" {:short "apacheconf"}
  171. "BBCode" {:short "bbcode"}
  172. "CMake" {:short "cmake"}
  173. "Darcs Patch" {:short "dpatch"}
  174. "Diff" {:short "diff"
  175. :exts #{"diff"}}
  176. "INI" {:short "ini"
  177. :exts #{"ini"}}
  178. "IRC Logs" {:short "irc"}
  179. "Lighttpd" {:short "lighty"}
  180. "Makefile" {:short "make"}
  181. "Nginx Configuration" {:short "nginx"}
  182. "Java Properties" {:short "properties"
  183. :exts #{"properties"}}
  184. "rST" {:short "rst"
  185. :exts #{"rst"}}
  186. "LaTeX" {:short "tex"
  187. :exts #{"tex"}}
  188. "VimL" {:short "vim"
  189. :exts #{"vim"}}
  190. "YAML" {:short "yaml"
  191. :exts #{"yaml" "yml"}}
  192. "ActionScript" {:short "as"
  193. :exts #{"as"}}
  194. "CoffeeScript" {:short "coffeescript"
  195. :exts #{"coffeescript" "coffee"}}
  196. "CSS" {:short "css"
  197. :exts #{"css"}}
  198. "DTD" {:short "dtd"}
  199. "HAML" {:short "haml"
  200. :exts #{"haml"}}
  201. "haXe" {:short "hx"
  202. :exts #{"hx"}}
  203. "HTML" {:short "html"
  204. :exts #{"html"}}
  205. "Javascript" {:short "js"
  206. :exts #{"js"}}
  207. "PHP" {:short "php"
  208. :exts #{"php"}}
  209. "Plain Text" {:short "text"
  210. :ext #{"txt"}}
  211. "SASS" {:short "sass"
  212. :exts #{"scss" "sass"}}
  213. "Scaml" {:short "scaml"
  214. :exts #{"scaml"}}
  215. "XML" {:short "xml"
  216. :exts #{"xml"}}
  217. "Kotlin" {:short "kotlin"
  218. :exts #{"kt"}}
  219. "Elixir" {:short "ex"
  220. :exts #{"ex" "exs"}}
  221. "Elixir Console" {:short "iex"}
  222. "Markdown" {:short "Markdown"
  223. :exts #{"md" "markdown"}}
  224. "RPM Spec" {:short "spec"
  225. :exts #{"spec"}}})
  226. (defn lookup-lexer
  227. "Selects a language."
  228. [lang]
  229. (or
  230. (if (and lang (.startsWith lang "."))
  231. (first
  232. (filter (fn [[_ v]]
  233. (when-let [exts (:exts v)]
  234. (exts (string/join (rest lang)))))
  235. lexers))
  236. (when-let [lang-map (lexers lang)]
  237. [lang lang-map]))
  238. ["Plain Text" {:short "text"}]))
  239. (defn highlight
  240. "Syntax highlight some code. If anything other than markdown, highlight
  241. with pygments. If markdown, render with pegdown."
  242. [language text & [anchor?]]
  243. (if (= language "Markdown")
  244. (try
  245. {:success (to-html text)}
  246. (catch Exception _
  247. {:error "There was an error pasting."}))
  248. (let-programs [pygmentize "./pygmentize"]
  249. (let [output (pygmentize "-fhtml" (str "-l" language)
  250. (str "-Olinenos=table,stripnl=False,encoding=utf-8"
  251. (when anchor? ",anchorlinenos=true,lineanchors=L"))
  252. {:dir "resources/pygments"
  253. :in text})]
  254. (if (seq output)
  255. {:success output}
  256. {:error "There was an error pasting."})))))