Permalink
Browse files

`convert` now accepts a buffer and async callback.

  • Loading branch information...
brandonbloom committed Sep 20, 2011
1 parent 5dc7587 commit fb2cfd9426d97f16b072567ff769610f1c7eafd9
Showing with 17 additions and 9 deletions.
  1. +2 −1 bin/html2coffeekup
  2. +15 −8 lib/convert.coffee
View
@@ -13,4 +13,5 @@ lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib')
{convert} = require "#{lib}/convert"
html = fs.readFileSync process.argv[2], 'utf8'
-convert(html)
+convert html, process.stdout, (err) ->
+ console.error err if err
View
@@ -1,17 +1,15 @@
{inspect} = require 'util'
htmlparser = require 'htmlparser'
-
stringLiteral = (html) ->
inspect html.trim()
-
-exports.convert = (html) ->
+exports.convert = (html, buffer, done) ->
depth = 0
emit = (code) ->
- console.log Array(depth + 1).join(' ') + code
+ buffer.write Array(depth + 1).join(' ') + code + '\n'
nest = (callback) ->
depth++
@@ -82,9 +80,18 @@ exports.convert = (html) ->
style: (style) ->
visit.tag style #TODO: Something better
+ throw 'omg'
handler = new htmlparser.DefaultHandler (err, dom) =>
- throw err if err
- visit.array dom
- parser = new htmlparser.Parser(handler)
- parser.parseComplete(html)
+ return done err if err
+ try
+ visit.array dom
+ catch exception
+ err = exception
+ done err
+
+ try
+ parser = new htmlparser.Parser(handler)
+ parser.parseComplete(html)
+ catch exception
+ done exception

0 comments on commit fb2cfd9

Please sign in to comment.