Skip to content

Instantly share code, notes, and snippets.

@bouzuya
Last active August 29, 2015 09:32
Show Gist options
  • Save bouzuya/6f9cee8d74c0684c36ba to your computer and use it in GitHub Desktop.
Save bouzuya/6f9cee8d74c0684c36ba to your computer and use it in GitHub Desktop.
mithril-b-html-plus
# mithril-b-html-plus
#
# e.g.
# ```coffee-script
# compile = require './mithril-b-html-plus'
# template = compile '''
# <p
# @b-text message
# '''
# template message: 'Hello!'
# ```
#
# ```coffee-script
# m = require 'mithril'
# MyElement = { view: -> m 'p', 'Hello' }
# compile = require './mithril-b-html-plus'
# template = compile '''
# <my-element
# '''
# template { m: m, 'my-element': MyElement }
# ```
# "b-html-plus": "0.2.0"
bHtml = require 'b-html-plus'
format = (node, options) ->
m = (options.mithril ? options.m)
return node if typeof node is 'string'
switch node.type
when 'element'
tag = node.name
attrs = node.attributes.reduce(((a, i) -> a[i.name] = i.value; a), {})
attrs = node.events.reduce(((a, i) -> a['on' + i.name] = i.value; a), attrs)
if tag.match /-/
m.component options[tag], attrs
else
children = node.children.map((i) -> format i, options).filter (i) -> i?
{ tag, children, attrs }
when 'empty element'
tag = node.name
attrs = node.attributes.reduce(((a, i) -> a[i.name] = i.value; a), {})
attrs = node.events.reduce(((a, i) -> a['on' + i.name] = i.value; a), attrs)
if tag.match /-/
m.component options[tag], attrs
else
{ tag, attrs }
else
# do nothing
null
module.exports = (source) ->
bHtml source, format: (elements, options) ->
elements.map((i) => format i, options).filter (i) -> i?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment