Skip to content

Instantly share code, notes, and snippets.

@gimenete
Created August 24, 2012 13:57
Show Gist options
  • Save gimenete/3450829 to your computer and use it in GitHub Desktop.
Save gimenete/3450829 to your computer and use it in GitHub Desktop.
SublimeText2 plugin that translates the selected code to HTML
[
{
"keys": ["command+ctrl+h"], "command": "highlight_code"
}
]
import sublime, sublime_plugin
from subprocess import Popen, PIPE, STDOUT
# Transforms the selected code to HTML
class HighlightCodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
content = self.view.substr(region)
p = Popen(["/usr/local/bin/node", "/path/to/highlighter.js"], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
out = p.communicate(input=content)[0]
self.view.replace(edit, region, out)
var hljs = require('highlight.js')
process.stdin.resume()
process.stdin.setEncoding('utf8')
var code = ''
process.stdin.on('data', function (chunk) {
code += chunk
})
process.stdin.on('end', function () {
console.log('<pre><code>'+hljs.highlightAuto(code).value+'</code></pre>')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment