Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
publishable form
Browse files Browse the repository at this point in the history
  • Loading branch information
piranha committed Mar 28, 2013
1 parent 3d70734 commit 77f4693
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src
test
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2012, Alexander Solovyov <[email protected]>

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.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions bin/leco
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

console.log(require('../lib/command').run());
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
75 changes: 75 additions & 0 deletions src/command.coffee
Original file line number Diff line number Diff line change
@@ -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
78 changes: 4 additions & 74 deletions src/leco.coffee
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 77f4693

Please sign in to comment.