Skip to content
This repository has been archived by the owner on Jan 19, 2018. It is now read-only.

Commit

Permalink
coffee_page 初步
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Apr 26, 2012
1 parent 69964e5 commit 4e6f9a8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
42 changes: 42 additions & 0 deletions coffee_page/index.coffee
@@ -0,0 +1,42 @@

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, '&nbsp')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')

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._ = ''
31 changes: 31 additions & 0 deletions coffee_page/test.coffee
@@ -0,0 +1,31 @@

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

0 comments on commit 4e6f9a8

Please sign in to comment.