Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consultas #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
842 changes: 842 additions & 0 deletions lib/attributeList.js

Large diffs are not rendered by default.

279 changes: 198 additions & 81 deletions lib/build.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,213 @@
var fs = require('fs')
var attributeList = require('./attributeList')
//function templateMethod()

module.exports = function buildOuput(filename){
var objArray = [];
var compArray = [];
//create an array of objects
fs.readdirSync('./toruf/objects').forEach(function(filename){
if(filename.includes(".json")){
let name = filename.split('.json')[0];
objArray.push(name);
}
});
//create an array of components
fs.readdirSync('./toruf/components').forEach(function(filename){
if(filename.includes(".html")){
let name = filename.split('.html')[0];
compArray.push(name);
}
});
//read all templates
//read template
console.log(filename)
if(filename.includes(".html")){
fs.readFile(`./toruf/templates/${filename}`, 'utf8', function(err, originFile){
fs.readFile(`./toruf/templates/${filename}`, 'utf8', function(err, fileContent){
if(err) throw new Error(err)
else{
//replace components
for(let comp of compArray){
let htmlComp = fs.readFileSync(`./toruf/components/${comp}.html`, 'utf8')
let tag = `<#${comp.toUpperCase()}>`
if(originFile.includes(tag)){
let start_tag =
`<!-- ${tag} -->
`;
let end_tag =
`
<!-- !${tag} -->`;
let htmlCom = start_tag + htmlComp + end_tag;
originFile = originFile.replace(tag, htmlCom);
replaceComponents(fileContent, {}, function(err,template){
if(err) throw new Error(err)
else{
console.log('comps done')
//replace objects
replaceObjects(template, function(err, newFile){
if(err) throw new Error(err)
else{
//if template method exists
//execute template method
fs.writeFileSync(`site/${filename}`, newFile)
console.log('success');
return 'success';
}
})
}
})
}
})
}
}

function recourseComponents(parentsArr, parent, component, callback){
var compArray = [];
fs.readdirSync('./toruf/components').forEach(function(filename){
if(filename.includes(".html")){
let name = filename.split('.html')[0];
compArray.push(name);
}
});
for(let comp of compArray){
let tag = '#'+comp.toUpperCase();
let start_tag =
`<!-- ${tag} -->
`;
let end_tag =
`
<!-- !${tag} -->`;
if(typeof component === 'String'){
if(component.includes(`<${tag}>`)){
if(parentsArr.includes(comp)){
throw new Error('warning: you cannot have a child component that calls its parent.')
}
else {
let newComp = fs.readFileSync(`./toruf/components/${comp}.html`)
let htmlComp = '<'+start_tag+'>' + newComp + '<'+end_tag+'>';
let newParent = component.replace(tag, htmlComp)
let newParentsArr = parentsArr.append(comp)
recourseComponents(newParentsArr, newParent, newComp, callback)
}
}
}
else if(parent.includes(tag)){
if(parentsArr.includes(tag)){
throw new Error('warning: you cannot have a child component that calls its parent.')
}
else {
let newComp = fs.readFileSync(`./toruf/components/${comp}.html`)
let htmlComp = '<'+start_tag+'>' + newComp + '<'+end_tag+'>';
let newParent = parent.replace(tag, htmlComp)
recourseComponents(parentsArr, newParent, newComp, callback)
}
//replace objects
for(let obj of objArray){
let tag = obj.toUpperCase();
if(originFile.includes(`<${tag}>`)){
let start_tag =
`<!-- <${tag}> -->
`;
let end_tag =
`
<!-- </${tag}> -->`;
let objTemplate = originFile.split(`<${tag}>`)[1].split(`</${tag}>`)[0];
let htmlObj = "";
if(objTemplate.includes('{') && objTemplate.includes('}')){
let rawdata = fs.readFileSync(`./toruf/objects/${obj}.json`);
let db = JSON.parse(rawdata)
Object.keys(db).map(function(key, index){
let unit = db[key]
let temp = objTemplate;
//insert ID in parent div toruf-OBJ-ID
Object.keys(unit).map(function(key, index){
let temparr = temp.split(`{${key}}`)
if(temparr[0].trim().endsWith('>') || /^[a-zA-Z0-9]/.test(temparr[0].substr(temparr[0].length - 1))){
let newU = `<span class="torufattr_${key}">${unit[key]}</span>`
temp = temp.replace(`{${key}}`, newU);
}
else {
callback(null, parent)
}
}
}



function replaceComponents(template, parents, callback) {
if(template.includes('<#')){
//console.log(template)
var compArray = [];
fs.readdirSync('./toruf/components').forEach(function(filename){
if(filename.includes(".html")){
let name = filename.split('.html')[0];
compArray.push(name);
}
});
for(let i=0; i<=compArray.length; i++){
if(!compArray[i]){
callback(null, template)
}
else{
let tag = '#'+compArray[i].toUpperCase();
let start_tag =
`<!-- ${tag} -->
`;
let end_tag =
`
<!-- !${tag} -->`;
let comp_tag = `<${tag}>`
let comp = fs.readFileSync(`./toruf/components/${compArray[i]}.html`)
let htmlComp = start_tag + comp + end_tag;
template = template.replace(comp_tag, htmlComp)

/* if(template.includes(tag)){
recourseComponents([], template, parents, function(err, data){
if(err) throw new Error(err)
else{
//replace component
template.replace(tag, data)
}
else if(temparr[0].endsWith(`href="`)){
let lastElem = temparr[0].split('<')[temparr[0].split('<').length - 1]
if(lastElem.includes('class=')){
let tempClassArr = temparr[0].split('class="')
let tempClass = tempClassArr[tempClassArr.length - 1]
temp = temp.replace(tempClass+`{${key}}`, `torufhref_${key} `+ tempClass+unit[key]);
}
else{
let newClass = `class="torufhref_${key} href="${unit[key]}`
temp = temp.replace(`href="{${key}}`, newClass);
newTempArr = temparr[0].split()
})
} */
}
}
}
else callback(null, template)
}


function replaceObjects(template, callback){
//console.log('HELLo')
//create an array of objects
let newTemplate = template
var objArray = [];
fs.readdirSync('./toruf/objects').forEach(function(filename){
if(filename.includes(".json")){
let name = filename.split('.json')[0];
objArray.push(name);
}
});
for(let obj of objArray){
console.log(obj)
let tag = obj.toUpperCase();
if(template.includes(`<${tag}>`)){
let start_tag =
`<!-- <${tag}> -->
`;
let end_tag =
`
<!-- </${tag}> -->`;
let objTemplate = template.split(`<${tag}>`)[1].split(`</${tag}>`)[0];
//console.log(objTemplate)
let htmlObj = "";
if(objTemplate.includes('{{') && objTemplate.includes('}}')){
let rawdata = fs.readFileSync(`./toruf/objects/${obj}.json`);
let db = JSON.parse(rawdata)
Object.keys(db).map((key, index)=>{
//console.log(key)
let item = db[key]
let temp = objTemplate;
//insert ID in parent div toruf-OBJ-ID
Object.keys(item).map((key, index)=>{
//console.log(key)
if(temp.includes(`{{${key}}}`)){
let temparr = temp.split(`{{${key}}}`)
//check if its an html attribute value
//console.log(temparr)
let beginAttr = temparr[0].substr(0, temparr[0].lastIndexOf('"'));
//console.log('TEMPARR 0 ',temparr[1], '\n\n')
let endAttr = temparr[1].split('"')[1];
if(endAttr && beginAttr){
if(endAttr.startsWith(' ') && beginAttr.endsWith('=') || endAttr.startsWith('>') && beginAttr.endsWith('=')){
//check for tag in attribute list
let tag = beginAttr.substr(beginAttr.lastIndexOf(' ')+1).split('=')[0]
console.log('TAG ', tag)
if(attributeList[tag]){
//check if element already has a class item
let lastElem = temparr[0].split('<')[temparr[0].split('<').length - 1]
if(lastElem.includes('class=')){
let tempClassArr = temparr[0].split('class="')
let tempClass = tempClassArr[tempClassArr.length - 1]
temp = temp.replace(tempClass+`{{${key}}}`, `toruf${tag}_${key} `+ tempClass+item[key]);
}
else{
let newClass = `class="toruf${tag}_${key} href="${item[key]}`
temp = temp.replace(`${tag}="{{${key}}}`, newClass);
//newTempArr = temparr[0].split()
}
}
else throw new Error(`warning invalid html attribute ${tag}`);
}
else temp = temp.replace(`{${key}}`, unit[key]);
})
let initTemp = temp.split('>')[0]
temp = temp.replace(initTemp+'>', initTemp+` id="torufobj_${key}">`)
htmlObj+= temp;
});
let oldObj = `<${tag}>`+objTemplate+`</${tag}>`;
newHtmlObj = start_tag + htmlObj + end_tag;
let newFile = originFile.replace(oldObj, newHtmlObj);
fs.writeFileSync(`site/${filename}`, newFile)
console.log('success');
return 'success';
else {
let newVal = `<span class="torufattr_${key}">${item[key]}</span>`
temp = temp.replace(`{{${key}}}`, newVal);
}
}
else {
let newVal = `<span class="torufattr_${key}">${item[key]}</span>`
temp = temp.replace(`{{${key}}}`, newVal);
}
}
}
}
})
let initTemp = temp.split('>')[0]
temp = temp.replace(initTemp+'>', initTemp+` id="torufobj_${key}">`)
console.log(temp)
htmlObj+= temp;
});
let oldObj = `<${tag}>`+objTemplate+`</${tag}>`;
newHtmlObj = start_tag + htmlObj + end_tag;
newTemplate = newTemplate.replace(oldObj, newHtmlObj);
//console.log(newHtmlObj)
}
})
}
}
}
callback(null, newTemplate)
}
7 changes: 4 additions & 3 deletions lib/buildAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ module.exports = function buildAllOutput(){
var objArray = [];
var compArray = [];
//create an array of objects
fs.readdirSync('objects').forEach(function(filename){
fs.readdirSync('toruf/objects').forEach(function(filename){
if(filename.includes(".json")){
let name = filename.split('.json')[0];
objArray.push(name);
}
});
//create an array of components
fs.readdirSync('components').forEach(function(filename){
fs.readdirSync('toruf/components').forEach(function(filename){
if(filename.includes(".html")){
let name = filename.split('.html')[0];
compArray.push(name);
}
});
//read all templates
fs.readdirSync('templates').forEach(function(filename){
fs.readdirSync('toruf/templates').forEach(function(filename){
console.log(filename)
build(filename);
});
}
Empty file added lib/copyDirs.js
Empty file.