Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

bump version to 0.7

added convertHtml and convertDocument methods
  • Loading branch information...
commit f9dcf2d9b4779c8ffc4eac534caab35f6b9ef315 1 parent f8c1fe1
@donpark authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+attic
+node_modules
View
1  .npmignore
@@ -1 +1,2 @@
attic
+node_modules
View
15 README.md
@@ -25,6 +25,21 @@ To generate [Scalate](http://scalate.fusesource.org/) compatible output:
html2jade --scalate http://twitter.com > twitter.jade
html2jade --scalate mywebpage.html
html2jade --scalate public/*.html
+
+To convert raw HTML into Jade:
+
+ var html2jade = require('html2jade');
+ var html = "<html><body>Hello World</body></html>";
+ html2jade.convertHtml(html, {}, function (err, jade) {
+ // do your thing
+ });
+
+To convert DOM document into Jade (client-side):
+
+ // assumes html2jade.js file has been loaded
+ Html2Jade.convertDocument(document, {}, function (err, jade) {
+ // do your thing
+ });
## Status ##
View
2  bin/html2jade
@@ -66,7 +66,7 @@ argv._.forEach(function(arg) {
encoding: 'utf8',
});
//console.log('converting: ' + arg);
- convert(arg, new html2jade.Output(outstrm), options);
+ convert(arg, new html2jade.StreamOutput(outstrm), options);
}
}
}
View
105 lib/html2jade.coffee
@@ -1,3 +1,4 @@
+scope = exports ? this.Html2Jade ?= {}
class Parser
constructor: (options = {}) ->
@@ -13,7 +14,7 @@ class Parser
else
cb('null file')
-class Helper
+class Writer
constructor: (options = {}) ->
@wrapLength = options.wrapLength ? 80
@scalate = options.scalate ? false
@@ -133,7 +134,7 @@ systemIdDocTypeNames =
class Converter
constructor: (options = {}) ->
@scalate = options.scalate ? false
- @helper = options.helper ? new Helper(options)
+ @writer = options.writer ? new Writer(options)
document: (document, output) ->
if document.doctype?
@@ -154,24 +155,24 @@ class Converter
element: (node, output) ->
return if not node?.tagName
- console.log "tag: #{node.tagName}"
+ # console.log "tag: #{node.tagName}"
tagName = node.tagName.toLowerCase()
- tagHead = @helper.tagHead node
- tagAttr = @helper.tagAttr node
- tagText = @helper.tagText node
+ tagHead = @writer.tagHead node
+ tagAttr = @writer.tagAttr node
+ tagText = @writer.tagText node
if tagName is 'script' or tagName is 'style'
if not @scalate or node.hasAttribute('src')
output.writeln tagHead + tagAttr
- @helper.writeTextContent node, output, false, false, false
+ @writer.writeTextContent node, output, false, false, false
else
output.writeln if tagName is 'script' then ':javascript' else ':css'
- @helper.writeTextContent node, output, false, false, false
+ @writer.writeTextContent node, output, false, false, false
else if ['pre'].indexOf(tagName) isnt -1
# HACK: workaround jade's wonky PRE handling
output.writeln tagHead + tagAttr + '.'
output.enter()
firstline = true
- @helper.forEachChild node, (child) =>
+ @writer.forEachChild node, (child) =>
if child.nodeType is 3
data = child.data
if data? and data.length > 0
@@ -198,7 +199,7 @@ class Converter
children: (parent, output) ->
output.enter()
- @helper.forEachChild parent, (child) =>
+ @writer.forEachChild parent, (child) =>
nodeType = child.nodeType
if nodeType is 1 # element
@element child, output
@@ -211,9 +212,9 @@ class Converter
output.leave()
text: (node, output) ->
- console.log "text: #{node.data}"
+ # console.log "text: #{node.data}"
node.normalize()
- @helper.writeText node, output
+ @writer.writeText node, output
comment: (node, output) ->
condition = node.data.match /\s*\[(if\s+[^\]]+)\]/
@@ -234,45 +235,91 @@ class Converter
parser.parse data, (errors, window) =>
@children window.document.body, output
output.leave()
-
+
class Output
- constructor: (@stream) ->
+ constructor: ->
@indents = ''
+ enter: ->
+ @indents += ' '
+ leave: ->
+ @indents = @indents.substring(2)
+ write: (data, indent=true) ->
+ writeln: (data, indent=true) ->
- enter: -> @indents += ' '
- leave: -> @indents = @indents.substring(2)
-
+class StringOutput extends Output
+ constructor: ->
+ super
+ @fragments = []
+ write: (data, indent=true) ->
+ data ?= ''
+ if indent
+ @fragments.push @indents + data
+ else
+ @fragments.push data
+ writeln: (data, indent=true) ->
+ data ?= ''
+ if indent
+ @fragments.push @indents + data + '\n'
+ else
+ @fragments.push data + '\n'
+ final: ->
+ result = @fragments.join ''
+ @fragments = []
+ result
+
+class StreamOutput extends Output
+ constructor: (@stream) ->
+ super
write: (data, indent=true) ->
data ?= ''
if indent
@stream.write @indents + data
else
@stream.write data
-
writeln: (data, indent=true) ->
data ?= ''
if indent
@stream.write @indents + data + '\n'
else
@stream.write data + '\n'
+
+scope.Output = Output
+scope.StringOutput = StringOutput
+scope.Converter = Converter
+scope.Writer = Writer
+# node.js classes
+if exports?
+ scope.Parser = Parser
+ scope.StreamOutput = StreamOutput
+ scope.convert = (input, output, options) ->
+ options ?= {}
+ # specify parser and converter to override default instance
+ options.parser ?= new Parser(options)
+ options.parser.parse input, (errors, window) ->
+ if errors?.length
+ errors
+ else
+ options.output ?= new StreamOutput(process.stdout)
+ options.converter ?= new Converter(options)
+ options.converter.document window.document, options.output
-exports.Parser = Parser
-exports.Output = Output
-exports.Converter = Converter
-exports.Helper = Helper
-
-# simple conversion
-exports.convert = (input, output, options) ->
+scope.convertHtml = (html, options, cb) ->
options ?= {}
# specify parser and converter to override default instance
options.parser ?= new Parser(options)
- options.parser.parse input, (errors, window) ->
+ options.parser.parse html, (errors, window) ->
if errors?.length
errors
else
- output ?= new Output(process.stdout)
+ options.output ?= new StringOutput()
options.converter ?= new Converter(options)
- options.converter.document window.document, output
- null
+ options.converter.document window.document, options.output
+ cb(null, options.output.final()) if cb?
+scope.convertDocument = (document, options, cb) ->
+ options ?= {}
+ options.output ?= new StringOutput()
+ options.converter ?= new Converter(options)
+ options.converter.document document, options.output
+ cb(null, options.output.final()) if cb?
View
206 lib/html2jade.js
@@ -1,6 +1,14 @@
(function() {
- var Converter, Helper, Output, Parser, publicIdDocTypeNames, systemIdDocTypeNames;
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ var Converter, Output, Parser, StreamOutput, StringOutput, Writer, publicIdDocTypeNames, scope, systemIdDocTypeNames, _ref;
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
+ for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
+ function ctor() { this.constructor = child; }
+ ctor.prototype = parent.prototype;
+ child.prototype = new ctor;
+ child.__super__ = parent.prototype;
+ return child;
+ };
+ scope = typeof exports !== "undefined" && exports !== null ? exports : (_ref = this.Html2Jade) != null ? _ref : this.Html2Jade = {};
Parser = (function() {
function Parser(options) {
if (options == null) {
@@ -23,17 +31,17 @@
};
return Parser;
})();
- Helper = (function() {
- function Helper(options) {
- var _ref, _ref2;
+ Writer = (function() {
+ function Writer(options) {
+ var _ref2, _ref3;
if (options == null) {
options = {};
}
- this.wrapLength = (_ref = options.wrapLength) != null ? _ref : 80;
- this.scalate = (_ref2 = options.scalate) != null ? _ref2 : false;
+ this.wrapLength = (_ref2 = options.wrapLength) != null ? _ref2 : 80;
+ this.scalate = (_ref3 = options.scalate) != null ? _ref3 : false;
this.attrSep = this.scalate ? ' ' : ', ';
}
- Helper.prototype.tagHead = function(node) {
+ Writer.prototype.tagHead = function(node) {
var classes, result;
result = node.tagName !== 'DIV' ? node.tagName.toLowerCase() : '';
if (node.id) {
@@ -50,7 +58,7 @@
}
return result;
};
- Helper.prototype.tagAttr = function(node) {
+ Writer.prototype.tagAttr = function(node) {
var attr, attrs, nodeName, result, _i, _len;
attrs = node.attributes;
if (attrs.length === 0) {
@@ -71,9 +79,9 @@
}
}
};
- Helper.prototype.tagText = function(node) {
- var data, _ref;
- if (((_ref = node.firstChild) != null ? _ref.nodeType : void 0) !== 3) {
+ Writer.prototype.tagText = function(node) {
+ var data, _ref2;
+ if (((_ref2 = node.firstChild) != null ? _ref2.nodeType : void 0) !== 3) {
return null;
} else if (node.firstChild !== node.lastChild) {
return null;
@@ -86,7 +94,7 @@
}
}
};
- Helper.prototype.forEachChild = function(parent, cb) {
+ Writer.prototype.forEachChild = function(parent, cb) {
var child, _results;
if (parent) {
child = parent.firstChild;
@@ -98,7 +106,7 @@
return _results;
}
};
- Helper.prototype.writeTextContent = function(node, output, pipe, trim, wrap) {
+ Writer.prototype.writeTextContent = function(node, output, pipe, trim, wrap) {
if (pipe == null) {
pipe = true;
}
@@ -114,7 +122,7 @@
}, this));
return output.leave();
};
- Helper.prototype.writeText = function(node, output, pipe, trim, wrap) {
+ Writer.prototype.writeText = function(node, output, pipe, trim, wrap) {
var data, lines;
if (pipe == null) {
pipe = true;
@@ -135,7 +143,7 @@
}
}
};
- Helper.prototype.writeTextLine = function(line, output, pipe, trim, wrap) {
+ Writer.prototype.writeTextLine = function(line, output, pipe, trim, wrap) {
var lines, prefix;
if (pipe == null) {
pipe = true;
@@ -165,7 +173,7 @@
}
}
};
- Helper.prototype.breakLine = function(line) {
+ Writer.prototype.breakLine = function(line) {
var lines, word, words;
if (line && line.length > 0) {
lines = [];
@@ -190,7 +198,7 @@
return [];
}
};
- return Helper;
+ return Writer;
})();
publicIdDocTypeNames = {
"-//W3C//DTD XHTML 1.0 Transitional//EN": "transitional",
@@ -210,12 +218,12 @@
};
Converter = (function() {
function Converter(options) {
- var _ref, _ref2;
+ var _ref2, _ref3;
if (options == null) {
options = {};
}
- this.scalate = (_ref = options.scalate) != null ? _ref : false;
- this.helper = (_ref2 = options.helper) != null ? _ref2 : new Helper(options);
+ this.scalate = (_ref2 = options.scalate) != null ? _ref2 : false;
+ this.writer = (_ref3 = options.writer) != null ? _ref3 : new Writer(options);
}
Converter.prototype.document = function(document, output) {
var docTypeName, doctype, publicId, systemId;
@@ -242,24 +250,23 @@
if (!(node != null ? node.tagName : void 0)) {
return;
}
- console.log("tag: " + node.tagName);
tagName = node.tagName.toLowerCase();
- tagHead = this.helper.tagHead(node);
- tagAttr = this.helper.tagAttr(node);
- tagText = this.helper.tagText(node);
+ tagHead = this.writer.tagHead(node);
+ tagAttr = this.writer.tagAttr(node);
+ tagText = this.writer.tagText(node);
if (tagName === 'script' || tagName === 'style') {
if (!this.scalate || node.hasAttribute('src')) {
output.writeln(tagHead + tagAttr);
- return this.helper.writeTextContent(node, output, false, false, false);
+ return this.writer.writeTextContent(node, output, false, false, false);
} else {
output.writeln(tagName === 'script' ? ':javascript' : ':css');
- return this.helper.writeTextContent(node, output, false, false, false);
+ return this.writer.writeTextContent(node, output, false, false, false);
}
} else if (['pre'].indexOf(tagName) !== -1) {
output.writeln(tagHead + tagAttr + '.');
output.enter();
firstline = true;
- this.helper.forEachChild(node, __bind(function(child) {
+ this.writer.forEachChild(node, __bind(function(child) {
var data;
if (child.nodeType === 3) {
data = child.data;
@@ -291,7 +298,7 @@
};
Converter.prototype.children = function(parent, output) {
output.enter();
- this.helper.forEachChild(parent, __bind(function(child) {
+ this.writer.forEachChild(parent, __bind(function(child) {
var nodeType;
nodeType = child.nodeType;
if (nodeType === 1) {
@@ -307,9 +314,8 @@
return output.leave();
};
Converter.prototype.text = function(node, output) {
- console.log("text: " + node.data);
node.normalize();
- return this.helper.writeText(node, output);
+ return this.writer.writeText(node, output);
};
Converter.prototype.comment = function(node, output) {
var condition;
@@ -336,8 +342,7 @@
return Converter;
})();
Output = (function() {
- function Output(stream) {
- this.stream = stream;
+ function Output() {
this.indents = '';
}
Output.prototype.enter = function() {
@@ -350,6 +355,64 @@
if (indent == null) {
indent = true;
}
+ };
+ Output.prototype.writeln = function(data, indent) {
+ if (indent == null) {
+ indent = true;
+ }
+ };
+ return Output;
+ })();
+ StringOutput = (function() {
+ __extends(StringOutput, Output);
+ function StringOutput() {
+ StringOutput.__super__.constructor.apply(this, arguments);
+ this.fragments = [];
+ }
+ StringOutput.prototype.write = function(data, indent) {
+ if (indent == null) {
+ indent = true;
+ }
+ if (data == null) {
+ data = '';
+ }
+ if (indent) {
+ return this.fragments.push(this.indents + data);
+ } else {
+ return this.fragments.push(data);
+ }
+ };
+ StringOutput.prototype.writeln = function(data, indent) {
+ if (indent == null) {
+ indent = true;
+ }
+ if (data == null) {
+ data = '';
+ }
+ if (indent) {
+ return this.fragments.push(this.indents + data + '\n');
+ } else {
+ return this.fragments.push(data + '\n');
+ }
+ };
+ StringOutput.prototype.final = function() {
+ var result;
+ result = this.fragments.join('');
+ this.fragments = [];
+ return result;
+ };
+ return StringOutput;
+ })();
+ StreamOutput = (function() {
+ __extends(StreamOutput, Output);
+ function StreamOutput(stream) {
+ this.stream = stream;
+ StreamOutput.__super__.constructor.apply(this, arguments);
+ }
+ StreamOutput.prototype.write = function(data, indent) {
+ if (indent == null) {
+ indent = true;
+ }
if (data == null) {
data = '';
}
@@ -359,7 +422,7 @@
return this.stream.write(data);
}
};
- Output.prototype.writeln = function(data, indent) {
+ StreamOutput.prototype.writeln = function(data, indent) {
if (indent == null) {
indent = true;
}
@@ -372,34 +435,79 @@
return this.stream.write(data + '\n');
}
};
- return Output;
+ return StreamOutput;
})();
- exports.Parser = Parser;
- exports.Output = Output;
- exports.Converter = Converter;
- exports.Helper = Helper;
- exports.convert = function(input, output, options) {
- var _ref;
+ scope.Output = Output;
+ scope.StringOutput = StringOutput;
+ scope.Converter = Converter;
+ scope.Writer = Writer;
+ if (typeof exports !== "undefined" && exports !== null) {
+ scope.Parser = Parser;
+ scope.StreamOutput = StreamOutput;
+ scope.convert = function(input, output, options) {
+ var _ref2;
+ if (options == null) {
+ options = {};
+ }
+ if ((_ref2 = options.parser) == null) {
+ options.parser = new Parser(options);
+ }
+ return options.parser.parse(input, function(errors, window) {
+ var _ref3, _ref4;
+ if (errors != null ? errors.length : void 0) {
+ return errors;
+ } else {
+ if ((_ref3 = options.output) == null) {
+ options.output = new StreamOutput(process.stdout);
+ }
+ if ((_ref4 = options.converter) == null) {
+ options.converter = new Converter(options);
+ }
+ return options.converter.document(window.document, options.output);
+ }
+ });
+ };
+ }
+ scope.convertHtml = function(html, options, cb) {
+ var _ref2;
if (options == null) {
options = {};
}
- if ((_ref = options.parser) == null) {
+ if ((_ref2 = options.parser) == null) {
options.parser = new Parser(options);
}
- return options.parser.parse(input, function(errors, window) {
- var _ref2;
+ return options.parser.parse(html, function(errors, window) {
+ var _ref3, _ref4;
if (errors != null ? errors.length : void 0) {
return errors;
} else {
- if (output == null) {
- output = new Output(process.stdout);
+ if ((_ref3 = options.output) == null) {
+ options.output = new StringOutput();
}
- if ((_ref2 = options.converter) == null) {
+ if ((_ref4 = options.converter) == null) {
options.converter = new Converter(options);
}
- options.converter.document(window.document, output);
- return null;
+ options.converter.document(window.document, options.output);
+ if (cb != null) {
+ return cb(null, options.output.final());
+ }
}
});
};
+ scope.convertDocument = function(document, options, cb) {
+ var _ref2, _ref3;
+ if (options == null) {
+ options = {};
+ }
+ if ((_ref2 = options.output) == null) {
+ options.output = new StringOutput();
+ }
+ if ((_ref3 = options.converter) == null) {
+ options.converter = new Converter(options);
+ }
+ options.converter.document(document, options.output);
+ if (cb != null) {
+ return cb(null, options.output.final());
+ }
+ };
}).call(this);
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "html2jade",
"description": "HTML to Jade conversion tool",
- "version": "0.0.6",
+ "version": "0.0.7",
"author": "Don Park <donpark@docuverse.com>",
"repository": "git://github.com/donpark/html2jade.git",
"main": "./lib/html2jade.js",
Please sign in to comment.
Something went wrong with that request. Please try again.