Skip to content

Instantly share code, notes, and snippets.

@gorbiz
Last active March 8, 2016 08:41
Show Gist options
  • Save gorbiz/2059e3b00bd2219c58a7 to your computer and use it in GitHub Desktop.
Save gorbiz/2059e3b00bd2219c58a7 to your computer and use it in GitHub Desktop.
HACK - atom (editor) - render Jade on change
# Your init script
#
# Atom will evaluate this file each time a new window is opened. It is run
# after packages are loaded/activated and after the previous editor state
# has been restored.
#
# An example hack to log to the console when each text editor is saved.
#
# atom.workspace.observeTextEditors (editor) ->
# editor.onDidSave ->
# console.log "Saved! #{editor.getPath()}"
renderJade = (text, filename) ->
{allowUnsafeEval, allowUnsafeNewFunction} = require '/home/gorbiz/npm/lib/node_modules/loophole'
jade = allowUnsafeNewFunction -> allowUnsafeEval -> require '/home/gorbiz/npm/lib/node_modules/jade'
options = { pretty: true, filename: filename }
fn = allowUnsafeNewFunction -> allowUnsafeEval -> jade.compile text, options
return allowUnsafeNewFunction -> allowUnsafeEval -> fn()
path = require 'fs'
path = require 'path'
delay = (t, cb) -> setTimeout cb, t
saveTimer = null
saveCooldown = 250
atom.workspace.observeTextEditors (editor) ->
subscription = editor.onDidChange (change) ->
# return if path.extname(editor.getPath()) in ['.jade', '.html', '.js']
clearTimeout saveTimer
saveTimer = delay saveCooldown, ->
editor.save()
if editor.getPath().indexOf('latin-book') != -1
return unless path.extname(editor.getPath()) == '.jade'
html = renderJade(editor.getText(), editor.getPath())
file = editor.getPath()
# .replace('/src/', '/dist/')
.replace(/jade$/, 'html')
fs.writeFile(file, html)
# always render index.html
unless editor.getPath().match(/index\.jade$/)
indexJade = path.dirname(editor.getPath()) + '/index.jade'
fs.readFile indexJade, (err, data) ->
throw err if err
html = renderJade data, indexJade
fs.writeFile(indexJade.replace().replace(/jade$/, 'html'), html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment