Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate entry points for most modules and related refactoring (#47)
- standalone entry points for parser, walker, generator and convertor (i.e. `require('css-tree/lib/parser')`) - replace `syntax#syntax` -> `syntax#grammar`
- Loading branch information
Showing
25 changed files
with
1,024 additions
and
815 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var createConvertor = require('./create'); | ||
|
||
module.exports = createConvertor(require('../walker')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
'use strict'; | ||
|
||
var sourceMapGenerator = require('./sourceMap'); | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
var noop = function() {}; | ||
|
||
function each(processChunk, node) { | ||
var list = node.children; | ||
var cursor = list.head; | ||
|
||
while (cursor !== null) { | ||
this.generate(processChunk, cursor.data, cursor, list); | ||
cursor = cursor.next; | ||
} | ||
} | ||
|
||
function eachComma(processChunk, node) { | ||
var list = node.children; | ||
var cursor = list.head; | ||
|
||
while (cursor !== null) { | ||
if (cursor.prev) { | ||
processChunk(','); | ||
} | ||
|
||
this.generate(processChunk, cursor.data, cursor, list); | ||
cursor = cursor.next; | ||
} | ||
} | ||
|
||
function createGenerator(types) { | ||
var context = { | ||
generate: function(processChunk, node, item, list) { | ||
if (hasOwnProperty.call(types, node.type)) { | ||
types[node.type].call(this, processChunk, node, item, list); | ||
} else { | ||
throw new Error('Unknown node type: ' + node.type); | ||
} | ||
}, | ||
each: each, | ||
eachComma: eachComma | ||
}; | ||
|
||
return function(node, fn) { | ||
if (typeof fn !== 'function') { | ||
// default generator concats all chunks in a single string | ||
var buffer = []; | ||
context.generate(function(chunk) { | ||
buffer.push(chunk); | ||
}, node); | ||
return buffer.join(''); | ||
} | ||
context.generate(fn, node); | ||
}; | ||
} | ||
|
||
function createMarkupGenerator(types) { | ||
var context = { | ||
generate: function(processChunk, node, item, list) { | ||
if (hasOwnProperty.call(types, node.type)) { | ||
var nodeBuffer = []; | ||
types[node.type].call(this, function(chunk) { | ||
nodeBuffer.push(chunk); | ||
}, node, item, list); | ||
processChunk({ | ||
node: node, | ||
value: nodeBuffer | ||
}); | ||
} else { | ||
throw new Error('Unknown node type: ' + node.type); | ||
} | ||
}, | ||
each: each, | ||
eachComma: eachComma | ||
}; | ||
|
||
return function(node, enter, leave) { | ||
function walk(node, buffer) { | ||
var value = node.value; | ||
|
||
enter(node.node, buffer, value); | ||
|
||
if (typeof value === 'string') { | ||
buffer += value; | ||
} else { | ||
for (var i = 0; i < value.length; i++) { | ||
if (typeof value[i] === 'string') { | ||
buffer += value[i]; | ||
} else { | ||
buffer = walk(value[i], buffer); | ||
} | ||
} | ||
} | ||
|
||
leave(node.node, buffer, value); | ||
|
||
return buffer; | ||
} | ||
|
||
if (typeof enter !== 'function') { | ||
enter = noop; | ||
} | ||
if (typeof leave !== 'function') { | ||
leave = noop; | ||
} | ||
|
||
var buffer = []; | ||
context.generate(function() { | ||
buffer.push.apply(buffer, arguments); | ||
}, node); | ||
return walk(buffer[0], ''); | ||
}; | ||
} | ||
|
||
function getTypesFromConfig(config) { | ||
var types = {}; | ||
|
||
if (config.node) { | ||
for (var name in config.node) { | ||
var nodeType = config.node[name]; | ||
|
||
types[name] = nodeType.generate; | ||
} | ||
} | ||
|
||
return types; | ||
} | ||
|
||
module.exports = function(config) { | ||
var types = getTypesFromConfig(config); | ||
var markupGenerator = createMarkupGenerator(types); | ||
|
||
return { | ||
translate: createGenerator(types), | ||
translateWithSourceMap: function(node) { | ||
return sourceMapGenerator(markupGenerator, node); | ||
}, | ||
translateMarkup: markupGenerator | ||
}; | ||
}; | ||
|
||
module.exports.createGenerator = createGenerator; | ||
module.exports.createMarkupGenerator = createMarkupGenerator; | ||
module.exports.sourceMap = require('./sourceMap'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,4 @@ | ||
'use strict'; | ||
var createGenerator = require('./create'); | ||
var config = require('../syntax/config/parser'); | ||
|
||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
var noop = function() {}; | ||
|
||
function each(processChunk, node) { | ||
var list = node.children; | ||
var cursor = list.head; | ||
|
||
while (cursor !== null) { | ||
this.generate(processChunk, cursor.data, cursor, list); | ||
cursor = cursor.next; | ||
} | ||
} | ||
|
||
function eachComma(processChunk, node) { | ||
var list = node.children; | ||
var cursor = list.head; | ||
|
||
while (cursor !== null) { | ||
if (cursor.prev) { | ||
processChunk(','); | ||
} | ||
|
||
this.generate(processChunk, cursor.data, cursor, list); | ||
cursor = cursor.next; | ||
} | ||
} | ||
|
||
function createGenerator(types) { | ||
var context = { | ||
generate: function(processChunk, node, item, list) { | ||
if (hasOwnProperty.call(types, node.type)) { | ||
types[node.type].call(this, processChunk, node, item, list); | ||
} else { | ||
throw new Error('Unknown node type: ' + node.type); | ||
} | ||
}, | ||
each: each, | ||
eachComma: eachComma | ||
}; | ||
|
||
return function(node, fn) { | ||
if (typeof fn !== 'function') { | ||
// default generator concats all chunks in a single string | ||
var buffer = []; | ||
context.generate(function(chunk) { | ||
buffer.push(chunk); | ||
}, node); | ||
return buffer.join(''); | ||
} | ||
context.generate(fn, node); | ||
}; | ||
} | ||
|
||
function createMarkupGenerator(types) { | ||
var context = { | ||
generate: function(processChunk, node, item, list) { | ||
if (hasOwnProperty.call(types, node.type)) { | ||
var nodeBuffer = []; | ||
types[node.type].call(this, function(chunk) { | ||
nodeBuffer.push(chunk); | ||
}, node, item, list); | ||
processChunk({ | ||
node: node, | ||
value: nodeBuffer | ||
}); | ||
} else { | ||
throw new Error('Unknown node type: ' + node.type); | ||
} | ||
}, | ||
each: each, | ||
eachComma: eachComma | ||
}; | ||
|
||
return function(node, enter, leave) { | ||
function walk(node, buffer) { | ||
var value = node.value; | ||
|
||
enter(node.node, buffer, value); | ||
|
||
if (typeof value === 'string') { | ||
buffer += value; | ||
} else { | ||
for (var i = 0; i < value.length; i++) { | ||
if (typeof value[i] === 'string') { | ||
buffer += value[i]; | ||
} else { | ||
buffer = walk(value[i], buffer); | ||
} | ||
} | ||
} | ||
|
||
leave(node.node, buffer, value); | ||
|
||
return buffer; | ||
} | ||
|
||
if (typeof enter !== 'function') { | ||
enter = noop; | ||
} | ||
if (typeof leave !== 'function') { | ||
leave = noop; | ||
} | ||
|
||
var buffer = []; | ||
context.generate(function() { | ||
buffer.push.apply(buffer, arguments); | ||
}, node); | ||
return walk(buffer[0], ''); | ||
}; | ||
} | ||
|
||
module.exports = { | ||
createGenerator: createGenerator, | ||
createMarkupGenerator: createMarkupGenerator, | ||
sourceMap: require('./sourceMap') | ||
}; | ||
module.exports = createGenerator(config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./syntax/default'); | ||
module.exports = require('./syntax'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.