Created
April 26, 2012 10:43
-
-
Save tiye/2498711 to your computer and use it in GitHub Desktop.
coffee_page, tiny template functions in CoffeeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pair_elems = 'html head title body script ' | |
pair_elems+= 'div nav header footer section article ' | |
pair_elems+= 'p span textarea br pre code ' | |
pair_elems+= 'ul li ol table tr td' | |
pair_elems = pair_elems.split ' ' | |
single_elems = 'img meta input link' | |
single_elems = single_elems.split ' ' | |
write = (attrs) -> | |
attr_str = '' | |
if typeof attrs is 'object' | |
for key, value of attrs | |
attr_str+= " #{key}=\"#{value}\"" | |
return attr_str | |
for elem in pair_elems | |
do (elem) -> | |
global[elem] = (attrs, elems...) -> | |
"<#{elem}#{write attrs}>#{elems.join ''}</#{elem}>" | |
for elem in single_elems | |
do (elem) -> global[elem] = (attrs) -> "<#{elem}#{write attrs}/>" | |
global.text = (str) -> str.replace(/\s/g, ' ') | |
.replace(/</g, '<') | |
.replace(/>/g, '>') | |
global.style = (attrs) -> | |
str = '' | |
for key, value of attrs | |
st = '' | |
for k, v of value | |
while (k.match /\d$/)? | |
k = k[...-1] | |
st+= "#{k}: #{v};" | |
str += "#{key}{#{st}}" | |
return "<style>#{str}</style>" | |
global.hsla = (h, s, l, a) -> "hsla(#{h},#{s}%,#{l}%,#{a/100})" | |
global._ = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './index' | |
page = html _, | |
head _, | |
title _, | |
text 'html' | |
style | |
body: | |
color: 'red' | |
div: | |
color: 'blue' | |
color2: 'bue' | |
color4: 'ue' | |
body _, | |
div id: 'main', | |
div id: 'left_bar', | |
nav id: 'home_button', class: 'nav' | |
nav id: 'search_button', class: 'nav' | |
nav id: 'msg_button', class: 'nav' | |
nav id: 'back_button', class: 'nav' | |
div id: 'right_bar', | |
article class: 'post', | |
p _, | |
text 'need to clarify sth' | |
text 'more things need to be examined' | |
article class: 'post' | |
article class: 'post' | |
article class: 'post' | |
console.log page |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
不再需要{}和text