diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..281df39 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +src +test diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e062248 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) 2012, Alexander Solovyov + +Permission to use, copy, modify, and/or distribute this software for any purpose +with or without fee is hereby granted, provided that the above copyright notice +and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF +THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d9e45e3 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# Leco: Le Embedded CoffeeScript templates + +Leco is reimplementation of [Eco](https://github.com/sstephenson/eco), partly +because of NIH, partly because I wanted to play with writing template compiler, +partly because I was not satisfied with how original implementation was +structured and in here I was responsible for everything. + +It works largely as Eco, but supports custom tag types, so you can add your own +tags, i.e. `<%_ ... %>` for i18n and whatnot. + +Proper description will follow. I guess. diff --git a/bin/leco b/bin/leco new file mode 100755 index 0000000..c5f9bed --- /dev/null +++ b/bin/leco @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +console.log(require('../lib/command').run()); diff --git a/package.json b/package.json index 12b0e4c..3fcffae 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,18 @@ "description": "Le Embedded CoffeeScript templates", "author": "Alexander Solovyov", "version": "1.0.0", + "licenses": { + "type": "ISC", + "url": "http://github.com/piranha/leco/raw/master/LICENSE" + }, "dependencies": { "argumentum": "0.6.0", "coffee-script": "1.6.2", "underscore": "1.4.4" - } + }, + "scripts": { + "prepublish": "coffee -o lib -c src/*.coffee" + }, + "bin": "./bin/leco", + "main": "./lib/leco.js" } diff --git a/src/command.coffee b/src/command.coffee new file mode 100644 index 0000000..9ead7f1 --- /dev/null +++ b/src/command.coffee @@ -0,0 +1,75 @@ +fs = require 'fs' +argumentum = require 'argumentum' +helpers = require './helpers' +compiler = require './compiler' + + +optConfig = + script: 'leco' + commandRequired: no + options: + 'no-include-helpers': + abbr: 'n' + flag: true + help: 'do not include helpers in template' + 'wrap': + abbr: 'w' + default: 'amd' + help: 'wrapper type (AMD, CommonJS, global)' + 'helpers-name': + help: 'name of object containing helper functions' + default: 'helpers' + 'helpers': + flag: true + help: 'only output helpers' + + +wrappers = + amd: + begin: 'define(function() { return ' + end: '});' + commonjs: + begin: 'module.exports = ' + end: '' + global: + begin: '''(function() { + (this.templates || (this.templates = {}))["%name%"] = ''' + end: '}).call(this);' + + +wrap = (name, template, path) -> + wrapper = wrappers[name.toLowerCase()] + if not wrapper + return "Unknown wrapper: #{wrapper}" + + begin = wrapper.begin + if begin.indexOf('%name%') != -1 + tname = path.slice(path.lastIndexOf('/') + 1, path.lastIndexOf('.')) + begin = begin.replace('%name%', name) + + return begin + template.trim() + wrapper.end + + +run = -> + parser = argumentum.load(optConfig) + options = parser.parse() + + if options.helpers + return compiler.printHelpers(helpers, options['helpers-name']) + + if not options[0] + return parser.getUsage() + + replaceName = (s) -> + + source = fs.readFileSync(options[0]).toString() + + if options['no-include-helpers'] + template = compiler.compile(source) + else + template = compiler.compile(source, helpers, options['helpers-name']) + + return wrap(options.wrap, template, options[0]) + + +exports.run = run diff --git a/src/leco.coffee b/src/leco.coffee index 01b61a0..2c4410a 100644 --- a/src/leco.coffee +++ b/src/leco.coffee @@ -1,75 +1,5 @@ -fs = require 'fs' -argumentum = require 'argumentum' -helpers = require './helpers' -compiler = require './compiler' +{compile, printHelpers} = require './compiler' +{tokenize} = require './tokenizer' +{transform} = require './transformer' - -optConfig = - script: 'leco' - commandRequired: no - options: - 'no-include-helpers': - abbr: 'n' - flag: true - help: 'do not include helpers in template' - 'wrap': - abbr: 'w' - default: 'amd' - help: 'wrapper type (AMD, CommonJS, global)' - 'helpers-name': - help: 'name of object containing helper functions' - default: 'helpers' - 'helpers': - flag: true - help: 'only output helpers' - - -wrappers = - amd: - begin: 'define(function() { return ' - end: '});' - commonjs: - begin: 'module.exports = ' - end: '' - global: - begin: '''(function() { - (this.templates || (this.templates = {}))["%name%"] = ''' - end: '}).call(this);' - - -wrap = (name, template, path) -> - wrapper = wrappers[name.toLowerCase()] - if not wrapper - return "Unknown wrapper: #{wrapper}" - - begin = wrapper.begin - if begin.indexOf('%name%') != -1 - tname = path.slice(path.lastIndexOf('/') + 1, path.lastIndexOf('.')) - begin = begin.replace('%name%', name) - - return begin + template.trim() + wrapper.end - - -run = -> - parser = argumentum.load(optConfig) - options = parser.parse() - - if options.helpers - return compiler.printHelpers(helpers, options['helpers-name']) - - if not options[0] - return parser.getUsage() - - replaceName = (s) -> - - source = fs.readFileSync(options[0]).toString() - - if options['no-include-helpers'] - template = compiler.compile(source) - else - template = compiler.compile(source, helpers, options['helpers-name']) - - return wrap(options.wrap, template, options[0]) - - -console.log(run()) +module.exports = {compile, printHelpers, tokenize, transform}