Skip to content

Commit

Permalink
feat: 支持自动识别分隔符
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed Aug 26, 2017
1 parent fc64b1c commit 8d3f668
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 73 deletions.
123 changes: 69 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,36 @@ const path = require('path');
const parser = require('node-mus/lib/compile/parser');
const quotReg = /^('|")|('|")$/g;
const spaceReg = /(>|)(?:\t| )*(?:\r?\n)+(?:\t| )*(<|)/;
const maybeQuotReg = /,\s*[^, ]+\s*=/;

function normalize(el, options) {
const list = [];
options.split = options.split || '';
if (options.split.length > 1) {
let splitKey = options.split;

// guess witch split was using
if (!splitKey) {
if (el._expr.match(maybeQuotReg)) {
splitKey = ',';
} else {
splitKey = '';
}
} else if (splitKey.length > 1) {
throw new Error('split key should has only one character');
}

parser.parseNormalAttr(
options.split || ' ',
el._expr,
(key, _, value) => {
if (key) {
key = options.attrAlias[key] || key;
if (key === '$id' && el.require) {
value = normalizeUrl(value, options);
}
list.push(`${key}=${value}`);
} else {
list.push(
el.require ? normalizeUrl(value, options) : value
);
parser.parseNormalAttr(splitKey || ' ', el._expr, (key, _, value) => {
if (key) {
key = options.attrAlias[key] || key;
if (key === '$id' && el.require) {
value = normalizeUrl(value, options);
}
});
list.push(`${key}=${value}`);
} else {
list.push(el.require ? normalizeUrl(value, options) : value);
}
});

el._expr = list.join(options.split + ' ');
el._expr = list.join(splitKey + ' ');
}

function normalizeUrl(url, options) {
Expand All @@ -40,10 +44,12 @@ function normalizeUrl(url, options) {
let quot = '';

// trim space and quot
url = url.replace(quotReg, q => {
quot = q;
return '';
}).trim();
url = url
.replace(quotReg, q => {
quot = q;
return '';
})
.trim();

// ignore url if url was variable, having operator or ext name
if (!quot || url.indexOf(quot) >= 0) {
Expand All @@ -68,7 +74,9 @@ function normalizeUrl(url, options) {
}

// replace alias
url = options._fullAlias[url] || url.replace(options._alias, (_, key) => alias[key]);
url =
options._fullAlias[url] ||
url.replace(options._alias, (_, key) => alias[key]);

if (url.charAt(0) === '.') {
url = path.resolve(path.dirname(file.realpath), url);
Expand Down Expand Up @@ -102,41 +110,48 @@ module.exports = function(content, file, options) {
blockEnd: options.blockEnd,
variableStart: options.variableStart,
variableEnd: options.variableEnd,
processor: Object.assign({
pagelet(el) { normalize(el, options); },
require(el) {
el.isUnary = true;
normalize(el, options);
},
comment(el) { el.text = ''; },
text(el) {
if (!options.compress) {
return;
}
processor: Object.assign(
{
pagelet(el) {
normalize(el, options);
},
require(el) {
el.isUnary = true;
normalize(el, options);
},
comment(el) {
el.text = '';
},
text(el) {
if (!options.compress) {
return;
}

let matches;
let newText = '';
let text = el.text;
// compress template
while ((matches = text.match(spaceReg))) {
const l = matches[1] || '';
const r = matches[2] || '';
const t = text.substring(0, matches.index);
newText += t + l;

// prevent comment in javascript
if (t.indexOf('//') >= 0) {
newText += '\n';
} else if (!l && !r) {
newText += ' ';
let matches;
let newText = '';
let text = el.text;
// compress template
while ((matches = text.match(spaceReg))) {
const l = matches[1] || '';
const r = matches[2] || '';
const t = text.substring(0, matches.index);
newText += t + l;

// prevent comment in javascript
if (t.indexOf('//') >= 0) {
newText += '\n';
} else if (!l && !r) {
newText += ' ';
}
newText += r;
text = text.substring(matches.index + matches[0].length);
}
newText += r;
text = text.substring(matches.index + matches[0].length);
}

el.text = newText + text;
el.text = newText + text;
},
},
}, options.processor),
options.processor
),
});

return utils.astTreeToString(tree);
Expand Down
57 changes: 38 additions & 19 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,55 @@ describe('parse', () => {
}, options);

assert(nstring.match(/\n/g).length === 1);
assert(nstring.indexOf('\nvar a = 123;') >= 0);
assert(nstring.indexOf('require "./" + "lib.js"') >= 0);
assert(nstring.indexOf('"component/page/test/lib.js"') >= 0);
assert(nstring.indexOf('{% require "app/lib.js" %}') >= 0);
assert(nstring.indexOf('require __pageUrl__') >= 0);
assert(nstring.indexOf('"page/test/w-game-list"') >= 0);
assert(nstring.indexOf('"p/widget/navigation"') >= 0);
assert(nstring.indexOf('$id="gameList"') >= 0);
assert(nstring.indexOf('"page/test/w-rank-list"') >= 0);
assert(nstring.indexOf('"widget/test"') >= 0);
assert(nstring.indexOf('"m/widget/foot"') >= 0);
assert(nstring2.indexOf('"page/test/w-game-list"') >= 0);
assert(nstring.includes('\nvar a = 123;'));
assert(nstring.includes('require "./" + "lib.js"'));
assert(nstring.includes('"component/page/test/lib.js"'));
assert(nstring.includes('{% require "app/lib.js" %}'));
assert(nstring.includes('require __pageUrl__'));
assert(nstring.includes('"page/test/w-game-list"'));
assert(nstring.includes('"p/widget/navigation"'));
assert(nstring.includes('$id="gameList"'));
assert(nstring.includes('"page/test/w-rank-list"'));
assert(nstring.includes('"widget/test"'));
assert(nstring.includes('"m/widget/foot"'));
assert(nstring2.includes('"page/test/w-game-list"'));
});

it('should parse with appoint split key', () => {
const nstring = pageletParser(`
{% pagelet _id="gameList", id="gameList", ref="gameList" %}
{% pagelet _id="gameList"; id="gameList"; ref="gameList" %}
{% require "./w-rank-list" %}
{% endpagelet %}
`, {
realpath: path.join(__dirname, './ref/app/component/page/test/test.tpl')
}, {
root: path.join(__dirname, './ref/'),
split: ',',
split: ';',
attrAlias: {
_id: '$id',
},
});

assert(nstring.includes('$id="gameList"; id="gameList"; ref="gameList"'));
});

it('should parse without error without split key', () => {
const nstring = pageletParser(`
{% pagelet _id="gameList" id="gameList" ref="gameList" %}{% endpagelet %}
{% pagelet _id="gameList2", id="gameList2", ref="gameList2" %}{% endpagelet %}
{% pagelet _id="gameList3" id={a: '1', b: '2'} ref="gameList3" %}{% endpagelet %}
`, {
realpath: path.join(__dirname, './ref/app/component/page/test/test.tpl')
}, {
root: path.join(__dirname, './ref/'),
attrAlias: {
_id: '$id',
},
});

assert(nstring.indexOf('$id="gameList"') >= 0);
assert(nstring.includes('$id="gameList" id="gameList" ref="gameList"'));
assert(nstring.includes('$id="gameList2", id="gameList2", ref="gameList2"'));
assert(nstring.includes(`$id="gameList3" id={a: '1', b: '2'} ref="gameList3"`));
});

it('should support resolve and full resolve', () => {
Expand All @@ -122,10 +141,10 @@ describe('parse', () => {
},
});

assert(nstring.indexOf('{% require "page/test/w-rank-list", id="123" %}') >= 0);
assert(nstring.indexOf('{% require "widget/w-rank-list" %}') >= 0);
assert(nstring.indexOf('{% require "widget/index" %}') >= 0);
assert(nstring.indexOf('{% require "index$", id="123" %}') >= 0);
assert(nstring.includes('{% require "page/test/w-rank-list", id="123" %}'));
assert(nstring.includes('{% require "widget/w-rank-list" %}'));
assert(nstring.includes('{% require "widget/index" %}'));
assert(nstring.includes('{% require "index$", id="123" %}'));
})

it('should support self defined tag', done => {
Expand Down

0 comments on commit 8d3f668

Please sign in to comment.