mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Added compiler/util/adjustIndent.js
This commit is contained in:
parent
8263c6aad4
commit
73f383e96e
69
compiler/util/adjustIndent.js
Normal file
69
compiler/util/adjustIndent.js
Normal 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
25
test/adjustIndent-test.js
Normal 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'
|
||||
});
|
||||
});
|
||||
3
test/fixtures/adjustIndent/autotest/bad-indent/actual.txt
vendored
Normal file
3
test/fixtures/adjustIndent/autotest/bad-indent/actual.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
var baz = require('baz');
|
||||
3
test/fixtures/adjustIndent/autotest/bad-indent/expected.txt
vendored
Normal file
3
test/fixtures/adjustIndent/autotest/bad-indent/expected.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
var baz = require('baz');
|
||||
3
test/fixtures/adjustIndent/autotest/bad-indent/input.txt
vendored
Normal file
3
test/fixtures/adjustIndent/autotest/bad-indent/input.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
var baz = require('baz');
|
||||
1
test/fixtures/adjustIndent/autotest/bad-indent/test.js
vendored
Normal file
1
test/fixtures/adjustIndent/autotest/bad-indent/test.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
exports.newIndentation = ' ';
|
||||
0
test/fixtures/adjustIndent/autotest/empty/actual.txt
vendored
Normal file
0
test/fixtures/adjustIndent/autotest/empty/actual.txt
vendored
Normal file
0
test/fixtures/adjustIndent/autotest/empty/expected.txt
vendored
Normal file
0
test/fixtures/adjustIndent/autotest/empty/expected.txt
vendored
Normal file
0
test/fixtures/adjustIndent/autotest/empty/input.txt
vendored
Normal file
0
test/fixtures/adjustIndent/autotest/empty/input.txt
vendored
Normal file
1
test/fixtures/adjustIndent/autotest/empty/test.js
vendored
Normal file
1
test/fixtures/adjustIndent/autotest/empty/test.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
exports.newIndentation = ' ';
|
||||
2
test/fixtures/adjustIndent/autotest/increase-indent/actual.txt
vendored
Normal file
2
test/fixtures/adjustIndent/autotest/increase-indent/actual.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
2
test/fixtures/adjustIndent/autotest/increase-indent/expected.txt
vendored
Normal file
2
test/fixtures/adjustIndent/autotest/increase-indent/expected.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
2
test/fixtures/adjustIndent/autotest/increase-indent/input.txt
vendored
Normal file
2
test/fixtures/adjustIndent/autotest/increase-indent/input.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
1
test/fixtures/adjustIndent/autotest/increase-indent/test.js
vendored
Normal file
1
test/fixtures/adjustIndent/autotest/increase-indent/test.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
exports.newIndentation = ' ';
|
||||
3
test/fixtures/adjustIndent/autotest/no-indent/actual.txt
vendored
Normal file
3
test/fixtures/adjustIndent/autotest/no-indent/actual.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
var baz = require('baz');
|
||||
3
test/fixtures/adjustIndent/autotest/no-indent/expected.txt
vendored
Normal file
3
test/fixtures/adjustIndent/autotest/no-indent/expected.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
var baz = require('baz');
|
||||
3
test/fixtures/adjustIndent/autotest/no-indent/input.txt
vendored
Normal file
3
test/fixtures/adjustIndent/autotest/no-indent/input.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
var baz = require('baz');
|
||||
1
test/fixtures/adjustIndent/autotest/no-indent/test.js
vendored
Normal file
1
test/fixtures/adjustIndent/autotest/no-indent/test.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
exports.newIndentation = ' ';
|
||||
2
test/fixtures/adjustIndent/autotest/remove-indent/actual.txt
vendored
Normal file
2
test/fixtures/adjustIndent/autotest/remove-indent/actual.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
2
test/fixtures/adjustIndent/autotest/remove-indent/expected.txt
vendored
Normal file
2
test/fixtures/adjustIndent/autotest/remove-indent/expected.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
2
test/fixtures/adjustIndent/autotest/remove-indent/input.txt
vendored
Normal file
2
test/fixtures/adjustIndent/autotest/remove-indent/input.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
1
test/fixtures/adjustIndent/autotest/remove-indent/test.js
vendored
Normal file
1
test/fixtures/adjustIndent/autotest/remove-indent/test.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
exports.newIndentation = '';
|
||||
3
test/fixtures/adjustIndent/autotest/tab-indent/actual.txt
vendored
Normal file
3
test/fixtures/adjustIndent/autotest/tab-indent/actual.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
var baz = require('baz');
|
||||
3
test/fixtures/adjustIndent/autotest/tab-indent/expected.txt
vendored
Normal file
3
test/fixtures/adjustIndent/autotest/tab-indent/expected.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
var baz = require('baz');
|
||||
3
test/fixtures/adjustIndent/autotest/tab-indent/input.txt
vendored
Normal file
3
test/fixtures/adjustIndent/autotest/tab-indent/input.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = require('foo');
|
||||
var bar = require('bar');
|
||||
var baz = require('baz');
|
||||
1
test/fixtures/adjustIndent/autotest/tab-indent/test.js
vendored
Normal file
1
test/fixtures/adjustIndent/autotest/tab-indent/test.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
exports.newIndentation = ' ';
|
||||
43
test/fixtures/compiler/autotest/template-init/expected.js
vendored
Normal file
43
test/fixtures/compiler/autotest/template-init/expected.js
vendored
Normal 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);
|
||||
5
test/fixtures/compiler/autotest/template-init/template.marko
vendored
Normal file
5
test/fixtures/compiler/autotest/template-init/template.marko
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
<template-init>
|
||||
var name = 'Frank';
|
||||
</template-init>
|
||||
|
||||
Hello ${name}!
|
||||
Loading…
x
Reference in New Issue
Block a user