Added compiler/util/adjustIndent.js

This commit is contained in:
Patrick Steele-Idem 2015-12-29 19:44:41 -07:00
parent 8263c6aad4
commit 73f383e96e
28 changed files with 187 additions and 0 deletions

View File

@ -0,0 +1,69 @@
var splitLinesRegExp = /\r?\n/;
var initialIndentationRegExp = /^\s+/;
// var initialSpaceMatches = /^\s+/.exec(text);
// if (initialSpaceMatches) {
// var indentMatches = /\n[^\n]*$/.exec(initialSpaceMatches[0]);
// if (indentMatches) {
// var indentRegExp = new RegExp(indentMatches[0].replace(/\n/g, '\\n'), 'g');
// text = text.replace(indentRegExp, '\n');
// }
// text = text.replace(/^\s*/, '').replace(/\s*$/, '');
// this.setText(text);
// }
function adjustIndent(str, newIndentation) {
if (!str) {
return str;
}
var lines = str.split(splitLinesRegExp);
var initialIndentationMatches = initialIndentationRegExp.exec(lines[0]);
var indentation = initialIndentationMatches ? initialIndentationMatches[0] : '';
if (!indentation && !newIndentation) {
return str;
}
lines.forEach((line, i) => {
if (line.startsWith(indentation)) {
line = line.substring(indentation.length);
}
lines[i] = line;
});
return newIndentation ?
newIndentation + lines.join('\n' + newIndentation) :
lines.join('\n');
}
// function adjustIndent(str, newIndentation) {
// if (!str) {
// return str;
// }
//
// var lines = str.split(splitLinesRegExp);
// var initialIndentationMatches = initialIndentationRegExp.exec(lines[0]);
//
// var indentation = initialIndentationMatches ? initialIndentationMatches[0] : '';
// if (!indentation && !newIndentation) {
// return str;
// }
//
// var result = '';
//
// lines.forEach((line, i) => {
// if (line.startsWith(indentation)) {
// line = line.substring(indentation.length);
// }
//
// if (newIndentation) {
// line = newIndentation + line;
// }
//
// result += line + '\n';
// });
//
// return result;
// }
module.exports = adjustIndent;

25
test/adjustIndent-test.js Normal file
View File

@ -0,0 +1,25 @@
'use strict';
var chai = require('chai');
chai.config.includeStack = true;
var path = require('path');
var adjustIndent = require('../compiler/util/adjustIndent');
var autotest = require('./autotest');
var fs = require('fs');
describe('compiler/util/adjustIndent', function() {
var autoTestDir = path.join(__dirname, 'fixtures/adjustIndent/autotest');
autotest.scanDir(
autoTestDir,
function run(dir) {
var inputPath = path.join(dir, 'input.txt');
var testSettings = require(path.join(dir, 'test.js'));
var input = fs.readFileSync(inputPath, { encoding: 'utf8' });
var newIndentation = testSettings.newIndentation;
var output = adjustIndent(input, newIndentation);
return output;
},
{
compareExtension: '.txt'
});
});

View File

@ -0,0 +1,3 @@
var foo = require('foo');
var bar = require('bar');
var baz = require('baz');

View File

@ -0,0 +1,3 @@
var foo = require('foo');
var bar = require('bar');
var baz = require('baz');

View File

@ -0,0 +1,3 @@
var foo = require('foo');
var bar = require('bar');
var baz = require('baz');

View File

@ -0,0 +1 @@
exports.newIndentation = ' ';

View File

View File

View File

View File

@ -0,0 +1 @@
exports.newIndentation = ' ';

View File

@ -0,0 +1,2 @@
var foo = require('foo');
var bar = require('bar');

View File

@ -0,0 +1,2 @@
var foo = require('foo');
var bar = require('bar');

View File

@ -0,0 +1,2 @@
var foo = require('foo');
var bar = require('bar');

View File

@ -0,0 +1 @@
exports.newIndentation = ' ';

View File

@ -0,0 +1,3 @@
var foo = require('foo');
var bar = require('bar');
var baz = require('baz');

View File

@ -0,0 +1,3 @@
var foo = require('foo');
var bar = require('bar');
var baz = require('baz');

View File

@ -0,0 +1,3 @@
var foo = require('foo');
var bar = require('bar');
var baz = require('baz');

View File

@ -0,0 +1 @@
exports.newIndentation = ' ';

View File

@ -0,0 +1,2 @@
var foo = require('foo');
var bar = require('bar');

View File

@ -0,0 +1,2 @@
var foo = require('foo');
var bar = require('bar');

View File

@ -0,0 +1,2 @@
var foo = require('foo');
var bar = require('bar');

View File

@ -0,0 +1 @@
exports.newIndentation = '';

View File

@ -0,0 +1,3 @@
var foo = require('foo');
var bar = require('bar');
var baz = require('baz');

View File

@ -0,0 +1,3 @@
var foo = require('foo');
var bar = require('bar');
var baz = require('baz');

View File

@ -0,0 +1,3 @@
var foo = require('foo');
var bar = require('bar');
var baz = require('baz');

View File

@ -0,0 +1 @@
exports.newIndentation = ' ';

View File

@ -0,0 +1,43 @@
function create(__helpers) {
var str = __helpers.s,
empty = __helpers.e,
notEmpty = __helpers.ne,
escapeXml = __helpers.x,
forEach = __helpers.f;
return function render(data, out) {
out.w("Hello " +
escapeXml(data.name) +
"! ");
if (notEmpty(data.colors)) {
out.w("<ul>");
forEach(data.colors, function(color) {
out.w("<li>" +
escapeXml(color) +
"</li>");
});
out.w("</ul>");
} else {
out.w("<div>No colors!</div>");
}
if (notEmpty(data.colors)) {
out.w("<ul>");
forEach(data.colors, function(color) {
out.w("<li>" +
escapeXml(color) +
"</li>");
});
out.w("</ul>");
} else {
out.w("<div>No colors!</div>");
}
};
}
(module.exports = require("marko").c(__filename)).c(create);

View File

@ -0,0 +1,5 @@
<template-init>
var name = 'Frank';
</template-init>
Hello ${name}!