Skip to content

Instantly share code, notes, and snippets.

@gdotdesign
Created September 28, 2011 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdotdesign/1248030 to your computer and use it in GitHub Desktop.
Save gdotdesign/1248030 to your computer and use it in GitHub Desktop.
Minimal Syntax Highlighter
new class Highlighter
entities: (s) ->
s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
highlight: (code,lang) ->
map = code
tokens = {}
uid = 0
for a in lang
code.replace a.regexp, (str) ->
map = map.replace str, "§"+uid
match = str
tokens["§"+uid] =
class: a.class
match: match
uid++
""
m = @entities(map).split("\n")
map = (m.map (line) -> "<div>"+line+"</div>").join("\n")
map.replace /§\d+/g, (str) ->
item = tokens[str]
"<span class='sh #{item.class}'>"+@entities(item.match)+"</span>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment