Skip to content

Instantly share code, notes, and snippets.

@mcculloughsean
Created October 31, 2012 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mcculloughsean/3984584 to your computer and use it in GitHub Desktop.
Save mcculloughsean/3984584 to your computer and use it in GitHub Desktop.
node_modules/
l = require('./latte.coffee')
l.stache __dirname + '/one_level_deep', () ->
console.log('done')
fs = require 'fs'
Path = require 'path'
glob = require 'glob'
async = require 'async'
Mustache = require 'mustache'
Latte = module.exports = {}
Latte.templates = templates = {}
Latte.templates._partials = partials = {}
Latte.stache = (options, stacheCallback) ->
if isFunction(options)
baseDir = defaultDir()
stacheCallback = options
else if isString(options)
baseDir = options
else
{ baseDir, prefix } = options
baseDir = stripTrailingSlash(baseDir)
glob baseDir + '/**/*.mustache', (err, paths) ->
console.log paths
iterator = (path, callback) ->
console.log "iterate", path
cacheTemplate([path, shorten(baseDir, path)], prefix, callback)
async.forEach paths, iterator, (err) ->
console.log err
if err?
console.log('Error while iterating over files...')
throw err
stacheCallback()
null
this
cacheTemplate = (paths, prefix, callback) ->
[absolute, relative] = paths
fs.readFile absolute, 'utf8', (err, file) ->
if err?
console.log('Error Reading template: ', err)
return callback(err)
base = stripPath(relative)
if prefix?
prefix += '/' unless hasTrailingSlash(prefix)
base = prefix += base
if Path.basename(relative).match(/^_/)
partials[base] = file
else
templates[base] = (presenter) ->
Mustache.render(file, presenter, partials)
callback()
# cache path regexs
leadingRegEx = /^\//
trailingRegEx = /\/$/
stripPath = (path) ->
basename = Path.basename(path)
base = basename.split('.')[0]
base = base.split(/^_/)[1] if base.match(/^_/)
path.split(basename)[0] + base
shorten = (base, path) ->
stripLeadingSlash(path.split(base)[1])
stripTrailingSlash = (path) ->
return path unless hasTrailingSlash(path)
path.split(trailingRegEx)[0]
stripLeadingSlash = (path) ->
return path unless hasLeadingSlash(path)
path.split(leadingRegEx)[1]
hasTrailingSlash = (path) ->
path.match(trailingRegEx)?
hasLeadingSlash = (path) ->
path.match(leadingRegEx)?
defaultDir = ->
process.cwd() + '/views'
toString = Object.prototype.toString;
isString = (obj) ->
return false unless obj?
toString.call(obj) is '[object String]'
isFunction = (obj) ->
return false unless object?
toString.call(obj) is '[object Function]'
{
"name": "gist-3984511",
"version": "0.0.0",
"description": "ERROR: No README.md file found!",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"coffee-script": "*",
"async": "*",
"glob": "*",
"mustache": "*"
},
"repository": {
"type": "git",
"url": "git://gist.github.com/3984511.git"
},
"author": "",
"license": "BSD"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment