chore: refactor plunker translators with a new systemjs one (#3221)

This commit is contained in:
Jesús Rodríguez 2017-02-28 23:18:04 +01:00 committed by GitHub
parent 2dd6ad1150
commit dbe7d70bbf
4 changed files with 86 additions and 54 deletions

View File

@ -9,7 +9,9 @@ var fs = require("fs");
var globby = require('globby'); var globby = require('globby');
var mkdirp = require('mkdirp'); var mkdirp = require('mkdirp');
var indexHtmlTranslator = require('./indexHtmlTranslator'); var fileTranslator = require('./translator/fileTranslator');
var indexHtmlRules = require('./translator/rules/indexHtml');
var systemjsConfigExtrasRules = require('./translator/rules/systemjsConfigExtras');
var regionExtractor = require('../doc-shredder/regionExtractor'); var regionExtractor = require('../doc-shredder/regionExtractor');
class PlunkerBuilder { class PlunkerBuilder {
@ -141,7 +143,7 @@ class PlunkerBuilder {
} }
if (relativeFileName == 'index.html') { if (relativeFileName == 'index.html') {
content = indexHtmlTranslator.translate(content); content = fileTranslator.translate(content, indexHtmlRules);
if (config.description == null) { if (config.description == null) {
// set config.description to title from index.html // set config.description to title from index.html
var matches = /<title>(.*)<\/title>/.exec(content); var matches = /<title>(.*)<\/title>/.exec(content);
@ -150,6 +152,11 @@ class PlunkerBuilder {
} }
} }
} }
if (relativeFileName == 'systemjs.config.extras.js') {
content = fileTranslator.translate(content, systemjsConfigExtrasRules);
}
content = regionExtractor.removeDocTags(content, extn.substr(1)); content = regionExtractor.removeDocTags(content, extn.substr(1));
this.options.addField(postData, relativeFileName, content); this.options.addField(postData, relativeFileName, content);

View File

@ -0,0 +1,47 @@
// var first_time = true; // DIAGNOSTIC
function translate(html, rulesFile) {
rulesFile.rulesToApply.forEach(function(rxDatum) {
var rxRule = rulesFile.rules[rxDatum.pattern];
// rxFrom is a rexexp
var rxFrom = rxRule.from;
if (rxDatum.from) {
var from = rxDatum.from.replace('/', '\/');
var rxTemp = rxFrom.toString();
rxTemp = rxTemp.replace('%tag%', from);
rxFrom = rxFromString(rxTemp);
}
// rxTo is a string
var rxTo = rxRule.to;
if (rxDatum.to) {
var to = rxDatum.to;
to = Array.isArray(to) ? to : [to];
to = to.map(function (toItem) {
return rxTo.replace("%tag%", toItem);
});
rxTo = to.join("\n ");
}
/* DIAGNOSTIC
if (first_time && rxDatum.pattern === 'zone_pkg') {
first_time = false;
console.log('zone_pkg');
console.log(' rxFrom: '+rxFrom);
console.log(' rxTo: '+rxTo);
console.log(' replace: ' + html.replace(rxFrom, rxTo ));
}
*/
html = html.replace(rxFrom, rxTo );
});
return html;
}
function rxFromString(rxString) {
var rx = /^\/(.*)\/(.*)/;
var pieces = rx.exec(rxString);
return RegExp(pieces[1], pieces[2]);
}
module.exports = {translate: translate};

View File

@ -1,8 +1,4 @@
module.exports = { var rules = {
translate: translate
};
var _rxRules = {
basehref: { basehref: {
from: /<base href=".*"[/]?>/, from: /<base href=".*"[/]?>/,
to: '<script>document.write(\'<base href="\' + document.location + \'" />\');</script>' to: '<script>document.write(\'<base href="\' + document.location + \'" />\');</script>'
@ -41,7 +37,7 @@ var _rxRules = {
}, },
}; };
var _rxData = [ var rulesToApply = [
{ {
pattern: 'basehref', pattern: 'basehref',
}, },
@ -113,49 +109,7 @@ var _rxData = [
} }
]; ];
module.exports = {
// var first_time = true; // DIAGNOSTIC rules: rules,
rulesToApply: rulesToApply
function translate(html) { };
_rxData.forEach(function(rxDatum) {
var rxRule = _rxRules[rxDatum.pattern];
// rxFrom is a rexexp
var rxFrom = rxRule.from;
if (rxDatum.from) {
var from = rxDatum.from.replace('/', '\/');
var rxTemp = rxFrom.toString();
rxTemp = rxTemp.replace('%tag%', from);
rxFrom = rxFromString(rxTemp);
}
// rxTo is a string
var rxTo = rxRule.to;
if (rxDatum.to) {
var to = rxDatum.to;
to = Array.isArray(to) ? to : [to];
to = to.map(function (toItem) {
return rxTo.replace("%tag%", toItem);
});
rxTo = to.join("\n ");
}
/* DIAGNOSTIC
if (first_time && rxDatum.pattern === 'zone_pkg') {
first_time = false;
console.log('zone_pkg');
console.log(' rxFrom: '+rxFrom);
console.log(' rxTo: '+rxTo);
console.log(' replace: ' + html.replace(rxFrom, rxTo ));
}
*/
html = html.replace(rxFrom, rxTo );
});
return html;
}
function rxFromString(rxString) {
var rx = /^\/(.*)\/(.*)/;
var pieces = rx.exec(rxString);
return RegExp(pieces[1], pieces[2]);
}

View File

@ -0,0 +1,24 @@
var rules = {
system_extra_main: {
from: /main:\s*[\'|\"]index.js[\'|\"]/g,
to: 'main: "index.ts"'
},
system_extra_defaultExtension: {
from: /defaultExtension:\s*[\'|\"]js[\'|\"]/g,
to: 'defaultExtension: "ts"'
}
};
var rulesToApply = [
{
pattern: 'system_extra_main'
},
{
pattern: 'system_extra_defaultExtension'
}
];
module.exports = {
rules: rules,
rulesToApply: rulesToApply
};