Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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