Browse files

Replace Converter class with `convert` function

  • Loading branch information...
1 parent 60d381c commit 5dc7587d66d824ca74691a3fe698aa2f731daf32 @brandonbloom committed Sep 20, 2011
Showing with 92 additions and 93 deletions.
  1. +2 −2 bin/html2coffeekup
  2. +90 −0 lib/convert.coffee
  3. +0 −91 lib/converter.coffee
View
4 bin/html2coffeekup
@@ -10,7 +10,7 @@ if process.argv.length < 3
lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib')
-{Converter} = require "#{lib}/converter"
+{convert} = require "#{lib}/convert"
html = fs.readFileSync process.argv[2], 'utf8'
-new Converter().convert(html)
+convert(html)
View
90 lib/convert.coffee
@@ -0,0 +1,90 @@
+{inspect} = require 'util'
+htmlparser = require 'htmlparser'
+
+
+stringLiteral = (html) ->
+ inspect html.trim()
+
+
+exports.convert = (html) ->
+
+ depth = 0
+
+ emit = (code) ->
+ console.log Array(depth + 1).join(' ') + code
+
+ nest = (callback) ->
+ depth++
+ result = callback()
+ depth--
+ result
+
+ visit =
+
+ node: (node) ->
+ visit[node.type](node)
+
+ array: (array) ->
+ for node in array
+ visit.node node
+
+ tag: (tag) ->
+ code = tag.name
+
+ # Force attribute ordering of `id`, `class`, then others.
+ attribs = []
+ if tag.attribs?
+ extractAttrib = (key) ->
+ value = tag.attribs[key]
+ if value
+ attribs.push [key, value]
+ delete tag.attribs[key]
+ extractAttrib 'id'
+ extractAttrib 'class'
+ for key, value of tag.attribs
+ attribs.push [key, value]
+
+ # Render attributes
+ attribs = for [key, value] in attribs
+ " #{key}: #{stringLiteral value}"
+ code += attribs.join(',')
+
+ # Render content
+ endTag = (suffix) =>
+ if suffix
+ code += ',' if attribs.length > 0
+ code += suffix
+ emit code
+ if (children = tag.children)?
+ if children.length == 1 and children[0].type == 'text'
+ endTag " #{stringLiteral children[0].data}"
+ else
+ endTag ' ->'
+ nest -> visit.array children
+ else
+ endTag()
+
+ text: (text) ->
+ return if text.data.match /^\s*$/
+ emit "text #{stringLiteral text.data}"
+
+ directive: (directive) ->
+ if directive.name.toLowerCase() == '!doctype'
+ emit "doctype TODO" #TODO: Extract doctype
+ else
+ console.error "Unknown directive: #{inspect directive.name}"
+
+ comment: (comment) ->
+ emit "comment #{stringLiteral comment.data}"
+
+ script: (script) ->
+ visit.tag script #TODO: Something better
+
+ style: (style) ->
+ visit.tag style #TODO: Something better
+
+ handler = new htmlparser.DefaultHandler (err, dom) =>
+ throw err if err
+ visit.array dom
+ parser = new htmlparser.Parser(handler)
+ parser.parseComplete(html)
View
91 lib/converter.coffee
@@ -1,91 +0,0 @@
-{inspect} = require 'util'
-htmlparser = require 'htmlparser'
-
-
-stringLiteral = (html) ->
- inspect html.trim()
-
-
-class exports.Converter
-
- constructor: ->
- @depth = 0
-
- convert: (html) ->
- handler = new htmlparser.DefaultHandler (err, dom) =>
- throw err if err
- @visitArray dom
- parser = new htmlparser.Parser(handler)
- parser.parseComplete(html)
-
- emit: (code) ->
- console.log Array(@depth + 1).join(' ') + code
-
- visitNode: (node) ->
- type = node.type.charAt(0).toUpperCase() + node.type.substring(1)
- @['visit' + type](node)
-
- visitArray: (array) ->
- for node in array
- @visitNode node
-
- nest: (callback) ->
- @depth++
- result = callback()
- @depth--
- result
-
- visitTag: (tag) ->
- code = tag.name
-
- # Force attribute ordering of `id`, `class`, then others.
- attribs = []
- if tag.attribs?
- extractAttrib = (key) ->
- value = tag.attribs[key]
- if value
- attribs.push [key, value]
- delete tag.attribs[key]
- extractAttrib 'id'
- extractAttrib 'class'
- for key, value of tag.attribs
- attribs.push [key, value]
-
- # Render attributes
- attribs = for [key, value] in attribs
- " #{key}: #{stringLiteral value}"
- code += attribs.join(',')
-
- # Render content
- endTag = (suffix) =>
- if suffix
- code += ',' if attribs.length > 0
- code += suffix
- @emit code
- if (children = tag.children)?
- if children.length == 1 and children[0].type == 'text'
- endTag " #{stringLiteral children[0].data}"
- else
- endTag ' ->'
- @nest => @visitArray children
- else
- endTag()
-
- visitText: (text) ->
- return if text.data.match /^\s*$/
- @emit "text #{stringLiteral text.data}"
-
- visitDirective: (directive) ->
- if directive.name.toLowerCase() == '!doctype'
- @emit "doctype TODO" #TODO: Extract doctype
- else
- console.error "Unknown directive: #{inspect directive.name}"
-
- visitComment: (comment) ->
- @emit "comment #{stringLiteral comment.data}"
-
- visitScript: (script) ->
- @visitTag script #TODO: Something better
-
- visitStyle: (style) ->
- @visitTag style #TODO: Something better

0 comments on commit 5dc7587

Please sign in to comment.