File size: 5,297 Bytes
7f88cfb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | /*
* Visualizacao
*/
var fs = require("fs");
var View = function (contentType, inputView) {
if (contentType.match(/html/))
this.filePath = __dirname+"/../app/view/";
else
this.filePath = __dirname+"/../app";
this.contentType = contentType;
this.fileName = inputView["file"];
this.inputVars = inputView["vars"];
this.auxPath; // TODO refatorar logica de replaceExtend e replaceInclude para essa variavel nao ser mais necessaria
this.finalyPage;
this.readFile(function(){
});
};
View.prototype.readFile = function (callback) {
if (this.contentType.match(/text/)) {
if (this.fileName) {
this.finalyPage = fs.readFileSync(this.filePath + this.fileName, function () {});
if (this.contentType.match(/html/)) {
try { this.replaceExtend(function(that) {
try { that.replaceInclude(function(that) {
try { that.replacePrint(function() {
}); } catch (e) { throw `Print > ${e}`; }
}); } catch (e) { throw `Include > ${e}`; }
}); } catch (e) { throw `Extend > ${e}`; }
}
} else this.finalyPage = this.inputVars.output.toString();
} else if (this.contentType.match(/image/)) {
if (fs.existsSync(this.filePath + this.fileName)) {
this.finalyPage = fs.readFileSync(this.filePath + this.fileName, function () {});
} else throw `Arquivo ".${this.fileName}" não foi encontrado.`;
} else throw "Não há suporte para o conteúdo solicitado";
callback();
};
View.prototype.replaceExtend = function (callback) {
var caminho = this.filePath + this.fileName.replace(/\/\w*\.html$/,'') + '/';
var fileAux1 = this.finalyPage.toString();
var fileAux2;
var erExtend = /<!--\[\[extends (\"(.*)\"|\'(.*)\')\]\]-->/;
var erBlocks = /<!--\[\[block \w*\]\]-->/g; //para encontrar todo os blocos
var erKeyBlock = /<!--\[\[block (\w*)\]\]-->/; //para encontrar a chave do primeiro bloco
var result = fileAux1.match(erExtend);
if (result) {
result = result[1].toString().replace(/\'|\"/g,'');
if (fs.existsSync(caminho + result)) {
fileAux2 = fs.readFileSync(caminho + result, function () {});
fileAux2 = fileAux2.toString();
var qtd = 0;
if (qtd = fileAux2.match(erBlocks))
qtd = qtd.length;
for (i = 0; i < qtd; i++)
fileAux2 = fileAux2.replace(fileAux2.match(erKeyBlock)[0], this.getBlock(fileAux2.match(erKeyBlock)[1]));
this.finalyPage = fileAux2;
this.auxPath = caminho + result;
} else throw `Erro durante processo de extensão de arquivo:\nNão foi possivel abrir "${caminho}${result}".`;
}
callback(this);
};
View.prototype.getBlock = function (nameBlock) {
var erResult;
var erBlock = new RegExp("<!--\\[\\[block "+ nameBlock +"\\]\\]-->\(.\|\\s\)*?<!--\\[\\[endblock\\]\\]-->", "gm");
if (erResult = this.finalyPage.toString().match(erBlock))
return erResult[0].slice(17+nameBlock.length, erResult.length-20);
return '';
};
View.prototype.replaceInclude = function (callback) {
var caminho
if (this.auxPath)
caminho = this.auxPath.replace(/\/\w*\.html$/,'') + '/';
else
caminho = __dirname + "/../app/view/";
var fileAux1 = this.finalyPage.toString();
var fileAux2;
var erIncludes = /<!--\[\[\include (\"(.*)\"|\'(.*)\')\]\]-->/g; //para encontrar todos os includes que existirem
var erKeyInclude = /<!--\[\[\include (\"(.*)\"|\'(.*)\')\]\]-->/; //para pegar o arquivo da key include
var result = fileAux1.match(erKeyInclude);
var qtd = 0;
if (qtd = fileAux1.match(erIncludes)) qtd = qtd.length;
for (i = 0; i < qtd; i++) {
if (result) {
result = result[1].toString().replace(/\'|\"/g, '');
if (fs.existsSync(caminho + result)) {
fileAux2 = fs.readFileSync(caminho + result, function () {}).toString();
fileAux1 = fileAux1.replace(erKeyInclude, fileAux2)
} else throw `Erro durante processo de inclusao de arquivo:\nNão foi possivel abrir "${caminho}${result}".`;
} else console.log("nao deu" + fileAux1.match(erKeyInclude)[2]); // TODO
}
this.finalyPage = fileAux1;
callback(this);
};
View.prototype.replacePrint = function (callback) { // TODO aperfeicoar codigo para tornar claro
var fileAux1 = this.finalyPage.toString();
var erPrints = /<!--\[\[print .+\]\]-->/g;
var erKeyPrint = /<!--\[\[print (.+|session\..+)\]\]-->/;
var result;
var qtd = 0;
if (qtd = fileAux1.match(erPrints)) qtd = qtd.length;
for (i = 0; i < qtd; i++) {
result = fileAux1.match(erKeyPrint);
if (result) {
result = result[1];
if (this.inputVars && typeof(this.inputVars.output) === "object" && result in this.inputVars.output) {
fileAux1 = fileAux1.replace(erKeyPrint, this.inputVars.output[result]);
} else if (this.inputVars && typeof(this.inputVars.output) === "object" && result.match(/^session\..+/)) {
var aux = result.match(/^session\.(.+)/);
fileAux1 = fileAux1.replace(erKeyPrint, this.inputVars.session[aux[1]]);
/* TRABALHANDO AQUI
* Atualmente a view só podera encontrar as variaveis que estao em output e session
* futuramente deseja-se que o usuario do framework possa declarar qualquer variavel
* e utilizar livremente na view.
*/
} else throw `Erro durante processo de impressão das variaveis.\nA página está esperando pela variavel "${result}".`;
}
}
this.finalyPage = fileAux1;
callback();
};
View.prototype.getPage = function() {
return this.finalyPage;
}
module.exports = View;
|