Skip to content

Commit

Permalink
Banana render syntax refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsopacifer committed Aug 26, 2016
1 parent 5b66dab commit 819a0d5
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ config.compress = true; // Default: false
const Banana = require('banana')(config);

// Output the css
const output = Banana.render("./fake_path.bnn", inputBananaCode);
const output = Banana.render(inputBananaCode);

console.log(output); // .a {width: 50px; height: 50px;}
```
Expand Down
2 changes: 1 addition & 1 deletion bin/fsRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const fsRender = (inputPath, outputPath, log = 'Your file has been' +
}

const bnnStylesheet = data;
const cssStylesheet = Banana.render(inputPath , bnnStylesheet);
const cssStylesheet = Banana.render(bnnStylesheet, inputPath);

fs.writeFile(outputPath, cssStylesheet);
console.log(log);
Expand Down
13 changes: 5 additions & 8 deletions src/banana.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,26 @@ const Banana = (config) => {
/**
* Iteration in AST and run all modules
* @method render
* @param {string} inputPath - input file path
* @param {object} stylesheet - AST stylesheet
* @param {string} inputPath - input file path
*/

return {
render: (inputPath, stylesheet) => {
render: (stylesheet, inputPath = 'fake_path') => {

const ast = css.parse(stylesheet);

// Add all methods for itarate on AST (before import modules)
// Add all methods for itarate on AST
addIterations(ast);

// Search for all the @import and generate a single AST
ast.findAllRulesByType('import', (rule, index) => {
ast.findAllImport((urlForImport, index) => {
if (config.bnnImport) {
const bnnImport = require('../src/core/bnnImport.js');
bnnImport(inputPath, rule.import, ast, index);
bnnImport(urlForImport, ast, index, inputPath);
}
});

// Add all methods for itarate on AST (after import modules)
addIterations(ast);

// Search for all global variables and compile
ast.findAllRulesBySelectors(':root', (rule, index) => {
if (config.bnnVariable) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/bnnImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const path = require('path');
/**
* Get a module (@import) and add to mains bnn file (AST).
* @module src/core/bnnImport
* @param {string} inputPath - Main file path
* @param {string} importPath - Module file path
* @param {object} ast - Rules list for a CSS (AST)
* @param {number} index - @import position in main AST array
* @param {string} inputPath - Main file path
*/

const bnnImport = (inputPath, importPath, ast, index) => {
const bnnImport = (importPath, ast, index, inputPath) => {

// Delete the import rule
ast.removeRule(index);
Expand Down
4 changes: 2 additions & 2 deletions test/bnnImport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('bnnImport()', () => {
addIterations(ast);

ast.stylesheet.rules.forEach((rule, index) => {
if (rule.import) bnnImport('test/main.bnn', rule.import, ast, index);
if (rule.import) bnnImport(rule.import, ast, index, 'test/main.bnn');
});

const result = css.stringify(ast);
Expand All @@ -32,7 +32,7 @@ describe('bnnImport()', () => {
addIterations(ast);

ast.stylesheet.rules.forEach((rule, index) => {
if (rule.import) bnnImport('test/main.bnn', rule.import, ast, index);
if (rule.import) bnnImport(rule.import, ast, index, 'test/main.bnn');
});

const result = css.stringify(ast);
Expand Down
2 changes: 1 addition & 1 deletion test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('render()', () => {
const stylesheet = '.a {color:#000;bnn-size: 50px 100px;}' +
'.b {color:#000;bnn-position: 10px 5px 8px 90px;margin: 10px;}';

const result = banana.render('teste.bnn', stylesheet);
const result = banana.render(stylesheet, 'teste.bnn');

const expect = '.a {\n color: #000;\n width: 50px;\n' +
' height: 100px;\n}\n\n.b {\n color: #000;\n' +
Expand Down

0 comments on commit 819a0d5

Please sign in to comment.