Skip to content

Instantly share code, notes, and snippets.

@ganxtamac
Created August 30, 2013 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganxtamac/6393486 to your computer and use it in GitHub Desktop.
Save ganxtamac/6393486 to your computer and use it in GitHub Desktop.
Lil' pain
# lil' pain
class LilPain
tags: ['html', 'head', 'title', 'base', 'link', 'meta', 'style', 'script', 'noscript', 'body', 'body', 'section', 'nav',
'article', 'aside', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h1', 'h6', 'header', 'footer', 'address', 'main', 'main', 'p',
'hr', 'pre', 'blockquote', 'ol', 'ul', 'li', 'dl', 'dt', 'dd', 'dd', 'figure', 'figcaption', 'div', 'a', 'em', 'strong',
'small', 's', 'cite', 'q', 'dfn', 'abbr', 'data', 'time', 'code', 'var', 'samp', 'kbd', 'sub', 'sup', 'i', 'b', 'u',
'mark', 'ruby', 'rt', 'rp', 'bdi', 'bdo', 'span', 'br', 'wbr', 'ins', 'del', 'img', 'iframe', 'embed', 'object', 'param',
'object', 'video', 'audio', 'source', 'video', 'audio', 'track', 'video', 'audio', 'canvas', 'map', 'area', 'area', 'map',
'svg', 'math', 'table', 'caption', 'colgroup', 'col', 'tbody', 'thead', 'tfoot', 'tr', 'td', 'th', 'form', 'fieldset',
'legend', 'fieldset', 'label', 'input', 'button', 'select', 'datalist', 'optgroup', 'option', 'select', 'datalist',
'textarea', 'keygen', 'output', 'progress', 'meter', 'details', 'summary', 'details', 'menuitem', 'menu']
stack: []
render: (block) ->
@_doWithTags(block)
_doWithTags: (block) ->
tmp = {}
for tag in @tags
tmp[tag] = window[tag]
do (tag) =>
window[tag] = () =>
@_renderElement.apply(@, _.union([tag], arguments))
result = block()
for tag in @tags
window[tag] = tmp[tag]
tmp = {}
result
_renderElement: () ->
elTag = _.first(arguments)
el = $('<' + elTag + '>')
if @stack.length
@stack[@stack.length - 1].append(el)
@stack.push(el)
otherArguments = _.last(arguments, arguments.length - 1)
if otherArguments.length > 1 and typeof otherArguments[0] == "object"
for name, value of otherArguments[0]
value = if typeof value == "function" then value() else value
el.attr(name, value)
content = if otherArguments.length > 1 then otherArguments[1] else otherArguments[0]
if typeof content == "function"
content()
else
el.append(content)
@stack.pop()
el
window.lp = new LilPain
myP = (text) ->
p class: "my-p", text
$(container).append lp.render ->
div class: "well", ->
myP "asd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment