Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricemach committed Sep 3, 2010
0 parents commit 75ee63a
Show file tree
Hide file tree
Showing 16 changed files with 857 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Cakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
exec = require('child_process').exec
coffeekup = require 'coffeekup'
render = coffeekup.render

task 'build', ->
exec 'coffee -c lib/coffeekup.coffee', (err) ->
puts err if err
exec 'cp lib/coffeekup.js examples/browser', (err) ->
puts err if err

task 'test', ->
[tests, passed, failed, errors] = [[], [], [], []]

test = (name, code) ->
tests.push name
print "Testing \"#{name}\"... "
try
if code()
passed.push name
puts "[OK]"
else
failed.push name
puts "[Failed]"
catch ex
errors.push name
puts "[Error] (#{ex.message})"

test 'Literal text', ->
'Just text' is render ->
text 'Just text'

test 'Default DOCTYPE', ->
'<!DOCTYPE html>' is render ->
doctype()

test 'DOCTYPE', ->
'<?xml version="1.0" encoding="utf-8" ?>' is render ->
doctype 'xml'

test 'Self-closing tags', ->
'<br />' is (render -> br()) and
'<img src="icon.png" alt="Icon" />' is render -> img src: 'icon.png', alt: 'Icon'

test 'Normal tags', ->
'<h1>hi</h1>' is render ->
h1 'hi'

test 'Attributes', ->
'<a href="/" title="Home"></a>' is render ->
a href: '/', title: 'Home'

test 'HereDocs', ->
"<script>$(document).ready(function(){\n alert('test');\n});</script>" is render ->
script """
$(document).ready(function(){
alert('test');
});
"""

test 'CoffeeScript', ->
"<script>$(document).ready(function() {\n return alert('hi!');\n });</script>" is render ->
coffeescript ->
$(document).ready ->
alert 'hi!'

test 'Comments', ->
'<!--Comment-->' is render ->
comment 'Comment'

puts "\nTests: #{tests.length} | Passed: #{passed.length} | Failed: #{failed.length} | Errors: #{errors.length}"
80 changes: 80 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# CoffeeKup
Caffeinated Templates

In (shamelessly late) celebration of [whyday](http://whyday.org/), here goes a little experiment in revisiting Markaby's concept, this time with the fine flavour of fresh [CoffeeScript](http://coffeescript.org):

doctype 5
html ->
head ->
meta charset: 'utf-8'
title "#{@title} | My awesome website"
meta(name: 'description', content: @description) if @description?
link rel: 'stylesheet', href: '/stylesheets/app.css'
style '''
body {font-family: sans-serif}
header, nav, section, footer {display: block}
'''
script src: "/javascripts/jquery.js"
coffeescript ->
$(document).ready ->
alert 'Alerts are so annoying...'
body ->
header ->
h1 @title
nav ->
ul ->
(li -> a href: '/', -> 'Home') unless @path is '/'
li -> a href: '/chunky', -> 'Chunky'
li -> a href: '/bacon', -> 'Bacon!'
section ->
h2 "Let's count to 10:"
p i for i in [1..10]
footer ->
p 'Bye!'

## _Why?

* Profit from a hell of a terse and expressive language in your templates.
* Keep the dignity of templates when embedding them in your app.
* Feels like an extensible language, as there's no syntactic distiction between your "helpers" and the original "vocabulary" of elements.
* Use it from coffeescript or javascript apps, in node.js or in the browser.
* It's just coffeescript! It doesn't need separate syntax highlighting, syntax checking, etc.

## Installing

Just grab [node.js](http://nodejs.org/#download) and [npm](http://github.com/isaacs/npm) and you're set:

[sudo] npm install coffeekup

## Using

coffeekup = require 'coffeekup'
coffeekup.render "h1 'You can feed me raw strings!'"
coffeekup.render -> h1 "Or live code. I'm not too picky."

With [express](http://expressjs.com):

app.register '.coffee', require('coffeekup')
app.set 'view engine', 'coffee'
app.get '/', (req, res) ->
# Will render views/index.coffee:
res.render 'index', context: {foo: 'bar'}

In the browser (see /examples dir):

<script src="/coffee-script.js"></script>
<script src="/coffeekup.js"></script>
<script type="text/coffeescript">
template = -> h1 "Hello #{@world}"
alert(CoffeeKup.render template, context: {world: 'mars'})
</script>

Command-line:

coffeekup FILE [> OUTPUT]

Please note that even though all examples were written in coffeescript, their javascript counterparts will also work just fine.

## Caveats

* Like Markaby, not the fastest horse in the stable. Run benchmark.coffee for details. In the context of node's screaming performance though, maybe it won't matter as much as it did for Markaby in the MRI. Your feedback is appreciated.
3 changes: 3 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- IE conditionals
- Decent error reporting
- Improve test coverage
77 changes: 77 additions & 0 deletions benchmark.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env coffee

jade = require 'jade'
coffeekup = require 'coffeekup'

jade_template = '''
!!! 5
html(lang="en")
head
title= pageTitle
:javascript
| if (foo) {
| bar()
| }
body
h1 Jade - node template engine
#container
- if (youAreUsingJade)
p You are amazing
- else
p Get on it!
'''

coffeekup_template = ->
doctype 5
html lang: 'en', ->
head ->
title @title
script '''
if (foo) {
bar()
}
'''
body ->
h1 'Jade - node template engine'
div id: 'container', ->
if @you_are_using_coffeekup
p 'You are amazing'
else
p 'Get on it!'

#coffeekup_template = """
# doctype 5
# html lang: 'en', ->
# head ->
# title @title
# script '''
# if (foo) {
# bar()
# }
# '''
# body ->
# h1 'Jade - node template engine'
# div id: 'container', ->
# if @you_are_using_coffeekup
# p 'You are amazing'
# else
# p 'Get on it!'
#"""

benchmark = (title, code) ->
start = new Date
for i in [1..5000]
code()
puts "#{title}: #{new Date - start} ms"

benchmark 'Jade', ->
jade.render jade_template, {locals: {pageTitle: 'pageTitle', youAreUsingJade: yes}}

benchmark 'CoffeeKup', ->
coffeekup.render coffeekup_template, {context: {title: 'title', you_are_using_coffeekup: yes}}

benchmark 'Jade (cached)', ->
jade.render jade_template, {locals: {pageTitle: 'pageTitle', youAreUsingJade: yes}, cache: yes, filename: 'aaa'}

benchmark 'CoffeeKup (cached)', ->
coffeekup.render coffeekup_template, {context: {title: 'title', you_are_using_coffeekup: yes}, cache: yes}
27 changes: 27 additions & 0 deletions bin/coffeekup.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env coffee

coffeekup = require 'coffeekup'
fs = require 'fs'

usage = '''
Usage:
coffeekup INPUT_FILE
Options:
-v, --version
-h, --help
'''

args = process.argv

if args.length is 0
puts usage
else
input = args[0]
if input in ['-v', '--version']
puts coffeekup.version
else if input in ['-h', '--help']
puts usage
else
code = fs.readFileSync input, 'utf8'
puts coffeekup.render(code)
8 changes: 8 additions & 0 deletions examples/browser/coffee-script.js

Large diffs are not rendered by default.

Loading

0 comments on commit 75ee63a

Please sign in to comment.