main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.6 KB
|
Rev | Line | |
---|
[d565449] | 1 | var sourceMap = require('./sourceMap');
|
---|
| 2 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
---|
| 3 |
|
---|
| 4 | function processChildren(node, delimeter) {
|
---|
| 5 | var list = node.children;
|
---|
| 6 | var prev = null;
|
---|
| 7 |
|
---|
| 8 | if (typeof delimeter !== 'function') {
|
---|
| 9 | list.forEach(this.node, this);
|
---|
| 10 | } else {
|
---|
| 11 | list.forEach(function(node) {
|
---|
| 12 | if (prev !== null) {
|
---|
| 13 | delimeter.call(this, prev);
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | this.node(node);
|
---|
| 17 | prev = node;
|
---|
| 18 | }, this);
|
---|
| 19 | }
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | module.exports = function createGenerator(config) {
|
---|
| 23 | function processNode(node) {
|
---|
| 24 | if (hasOwnProperty.call(types, node.type)) {
|
---|
| 25 | types[node.type].call(this, node);
|
---|
| 26 | } else {
|
---|
| 27 | throw new Error('Unknown node type: ' + node.type);
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | var types = {};
|
---|
| 32 |
|
---|
| 33 | if (config.node) {
|
---|
| 34 | for (var name in config.node) {
|
---|
| 35 | types[name] = config.node[name].generate;
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | return function(node, options) {
|
---|
| 40 | var buffer = '';
|
---|
| 41 | var handlers = {
|
---|
| 42 | children: processChildren,
|
---|
| 43 | node: processNode,
|
---|
| 44 | chunk: function(chunk) {
|
---|
| 45 | buffer += chunk;
|
---|
| 46 | },
|
---|
| 47 | result: function() {
|
---|
| 48 | return buffer;
|
---|
| 49 | }
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | if (options) {
|
---|
| 53 | if (typeof options.decorator === 'function') {
|
---|
| 54 | handlers = options.decorator(handlers);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | if (options.sourceMap) {
|
---|
| 58 | handlers = sourceMap(handlers);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | handlers.node(node);
|
---|
| 63 |
|
---|
| 64 | return handlers.result();
|
---|
| 65 | };
|
---|
| 66 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.