Skip to content

Instantly share code, notes, and snippets.

@schleumer
Created June 14, 2015 19:25
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 schleumer/9302fe1547a897124938 to your computer and use it in GitHub Desktop.
Save schleumer/9302fe1547a897124938 to your computer and use it in GitHub Desktop.
highlight = require 'highlight.js'
cheerio = require 'cheerio'
color-it = (code, lang) ->
html = (highlight.highlight lang, code).value
$ = cheerio.load("<div id=\"the-wrapper\">#{html}</div>")
elements = []
($ '#the-wrapper *').each ->
let el = ($ this)
elements.push [el.children!length, el]
wtf-tag = new Date!get-time!to-string!
# never ever do this at home:
elements
.sort (x, y) ->
if x.0 < y.0
-1
else if x.0 > y.0
1
else 0
.reverse!for-each ([..._, el]) ->
if el.children!length < 1
el.before("__open_#{wtf-tag}__\x1b[38;5;25m")
el.after("\x1b[0m__close_#{wtf-tag}__")
($ '#the-wrapper').text!
.trim!
.replace (new RegExp "__close_#{wtf-tag}__([\\s\\S]*?)__open_#{wtf-tag}__", "mg"), "\x1b[38;5;15m$1\x1b[0m"
.replace (new RegExp "^([\\s\\S]*?)__open_#{wtf-tag}__", "mg"), "\x1b[38;5;15m$1\x1b[0m"
.replace (new RegExp "__close_#{wtf-tag}__([\\s\\S]*)", "mg"), "\x1b[38;5;15m$1\x1b[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment