Permalink
Browse files

initial commit

  • Loading branch information...
dodo committed Feb 26, 2012
0 parents commit 6cb28a613b9793981aeced99d23b11f41fdaadca
Showing with 232 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +2 −0 .npmignore
  3. +41 −0 Cakefile
  4. +7 −0 LICENSE
  5. +2 −0 dt-stream.js
  6. +20 −0 package.json
  7. +154 −0 src/stream.coffee
@@ -0,0 +1,6 @@
+*~
+*.swp
+lib/*
+*.browser.*
+.lock-wscript
+node_modules/
@@ -0,0 +1,2 @@
+Cakefile
+src/
@@ -0,0 +1,41 @@
+path = require 'path'
+{ run, compileScript, minifyScript, writeFile, notify } = require 'muffin'
+
+task 'compile', 'compile coffeescript → javascript', (options) ->
+ run
+ options:options
+ after:options.after
+ files:[
+ "./src/**/*.coffee"
+ ]
+ map:
+ 'src/(.+).coffee': (m) ->
+ compileScript m[0], path.join("lib" ,"#{m[1]}.js"), options
+
+task 'bundle', 'build a browser bundle', (options) ->
+ browserify = require 'browserify'
+ { createScope } = require 'scopify'
+ run
+ options:options
+ files:[
+ "./lib/*.js"
+ ]
+ map:
+ 'lib/(dt-stream).js': (m) ->
+ bundle = browserify({
+ require: path.join(__dirname, m[0])
+ cache: on
+ }).use(createScope require:'./'+m[1]).bundle()
+ notify m[0], "successful browserify!"
+ filename = "#{m[1]}.browser.js"
+ writeFile(filename, bundle, options).then ->
+ minifyScript filename, options
+
+task 'build', 'compile && bundle', (options) ->
+ timeout = 0
+ options.after = ->
+ clearTimeout(timeout) if timeout
+ timeout = setTimeout( ->
+ invoke 'bundle', options
+ , 250)
+ invoke 'compile'
@@ -0,0 +1,7 @@
+Copyright (c) 2011 ▟ ▖▟ ▖(dodo)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/stream')
@@ -0,0 +1,20 @@
+{ "name": "dt-stream"
+, "description": "Δt stream adapter - async & dynamic templating engine"
+, "version": "0.0.1"
+, "homepage": "https://github.com/dodo/node-dt-stream"
+, "author": "dodo (https://github.com/dodo)"
+, "repository": {"type": "git", "url": "git://github.com/dodo/node-dt-stream.git"}
+, "main": "dt-stream.js"
+, "engines": {"node": ">= 0.4.x"}
+, "keywords": ["dt", "async", "dynamic", "event", "template", "generation", "string", "stream", "browser"]
+, "scripts": {
+ "prepublish": "cake build"}
+, "devDependencies": {
+ "browserify": "1.6.1",
+ "scopify": ">= 0.2.1",
+ "muffin": ">= 0.2.6",
+ "coffee-script": ">= 1.1.3"}
+, "licenses" : [
+ { "type": "MIT" ,
+ "url": "http://github.com/dodo/node-dt-stream/raw/master/LICENSE"} ]
+}
@@ -0,0 +1,154 @@
+{ Stream } = require 'stream'
+
+# TODO drainage
+
+indent = ({level, pretty}) ->
+ pretty = " " if pretty is on
+ return new Array(level + 1).join(pretty)
+
+
+breakline = ({level, pretty}, data) ->
+ return data unless pretty
+ if data?[data?.length-1] is "\n"
+ return data
+ else
+ return "#{data}\n"
+
+
+prettify = (el, data) ->
+ unless el?.pretty
+ return data
+ else
+ return "#{indent el}#{breakline el, data}"
+
+
+attrStr = (attrs = {}) ->
+ strattrs = for k, v of attrs
+ if v?
+ v = "\"#{v}\"" unless typeof v is 'number'
+ "#{k}=#{v}"
+ else "#{k}"
+ strattrs.unshift '' if strattrs.length
+ strattrs.join ' '
+
+
+
+removed = (el) ->
+ el.closed is "removed"
+
+
+#return true if @hidden # dont emit data when this tag is hidden # FIXME
+streamify = (tpl) ->
+ stream = new Stream
+ stream.readable = on
+
+ emit = (data) ->
+ stream.emit('data', data)
+
+ _write = (data) ->
+ return if @hidden
+ if @_stream.before is null
+# console.log "write", @name, attrStr(@attrs), data
+ emit data
+ else
+# console.log "buffer", @name, attrStr(@attrs), data
+ @_stream.buffer.push data
+
+ write_head = ->
+ if @isempty and not @closed
+# console.log "head", @name, attrStr(@attrs)
+ _write.call this, prettify this, "<#{@name}#{attrStr @attrs}>"
+
+ write = (data) ->
+ write_head.call this
+ _write.call this, data
+
+ builder = tpl.xml ? tpl
+ tpl.stream = stream
+ builder._stream =
+ buffer:[]
+ before:null
+ after:null
+ first:null
+ last:null
+
+ tpl.on 'add', (parent, el) ->
+ console.log "add", el.name, attrStr(el.attrs)
+ write_head.call parent
+ parstream = parent._stream
+ el._stream =
+ buffer:[]
+ before:parstream.last
+ after:null
+ first:null
+ last:null
+ parstream.last?.after = el
+ parstream.first ?= el
+ parstream.last = el
+
+ tpl.on 'close', (el) ->
+
+ console.log "close", el.name, attrStr(el.attrs), el.closed, el.isempty
+ if el._stream.before
+ cur = el._stream
+ after = cur.after?._stream
+ before = cur.before._stream
+ before.buffer = before.buffer?.concat(cur.buffer)
+ after?.before = cur.before
+ before.after = cur.after
+ if el is el.parent._stream.last
+ el.parent._stream.last = cur.before
+ el._stream = "done"
+ return
+ while el?.closed
+ break unless el.parent._stream.first is el
+ # empty buffer
+ emit data for data in el._stream.buffer
+ if el.closed is 'self'
+ data = "<#{el.name}#{attrStr el.attrs}/>"
+ else
+ data = "</#{el.name}>"
+# console.log el.name, attrStr(el.attrs), el.isempty, el.closed, data
+ write.call el, prettify el, data
+ # update linked list
+ el._stream.after?.before = null
+ if el is el.parent._stream.first
+ el.parent._stream.first = el._stream.after
+ if el is el.parent._stream.last
+ el.parent._stream.last = null
+ el._stream = "done"
+ el = el._stream.after
+ 0
+
+ tpl.on 'data', (el, data) ->
+ write.call el, data
+
+ tpl.on 'text', (el, text) ->
+ write.call el, text
+
+ tpl.on 'raw', (el, html) ->
+ write.call el, html
+
+ tpl.on 'data', (el, data) ->
+ write.call el, data
+
+ tpl.on 'attr', (el, key, value) ->
+ return unless el.isempty
+ console.warn "attributes of #{el.toString()} don't change anymore"
+
+ tpl.on 'end', ->
+ console.log "tpl end"
+ stream.emit 'end'
+
+# exports
+
+module.exports = streamify
+
+# browser support
+
+( ->
+ if @dynamictemplate?
+ @dynamictemplate.streamify = streamify
+ else
+ @dynamictemplate = {streamify}
+).call window if process.title is 'browser'

0 comments on commit 6cb28a6

Please sign in to comment.