Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
842 changes: 842 additions & 0 deletions lib/attributeList.js

Large diffs are not rendered by default.

254 changes: 173 additions & 81 deletions lib/build.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,188 @@
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
module.exports = async function buildOuput(filename){
if(filename.includes(".html")){
fs.readFile(`./toruf/templates/${filename}`, 'utf8', function(err, originFile){
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);
let fileContent = await fs.promises.readFile(`./toruf/templates/${filename}`, 'utf8')
let compFile = await replaceComponents(fileContent)
let objFile = await replaceObjects(compFile)
await fs.promises.writeFile(`site/${filename}`, objFile).then(console.log('All Done!'))
}
}

function replaceComponents(template) {
return new Promise(async (resolve, reject) => {
let newTemplate = template
if(template.includes('<#')){
var compArr = template.split('<#')
for(let i=1;i<=compArr.length; i++) {
if(!compArr[i]) {
//console.log(newTemplate)
resolve(newTemplate)
}
else{
let compName = compArr[i].split('>')[0];
let compFileName = compName.toLowerCase();
//console.log(compFileName)
let compFile = await fs.promises.readFile(`./toruf/components/${compFileName}/index.html`, 'utf8').catch((err)=>{reject(err)})
if(compFile){
let htmlComp = `\n<!-- #${compName} -->\n` + compFile + `\n<!-- !${compName} -->\n`;
let comp_tag = `<#${compName}>`
newTemplate = newTemplate.replace(comp_tag, htmlComp)
//console.log(newTemplate)
}
else reject(`component ${compName} doesnt exist`)
}
}
}
else resolve(template)
})
}


async function replaceObjects(template){
return new Promise((resolve, reject) => {
var objArray = [];
fs.promises.readdir('./toruf/objects')
.then((files)=>{
files.forEach((filename) => {
if(filename.includes(".json")){
let name = filename.split('.json')[0];
objArray.push(name);
}
//replace objects
for(let obj of objArray){
})
}).then(() => {
let newTemplate = template
for(let i=0;i<=objArray.length;i++){
if(i===objArray.length) {
resolve(newTemplate)
console.log(i)
}
else {
let obj = objArray[i]
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];
if(newTemplate.includes(`<${tag}>`)){
console.log(tag)
let start_tag = `\n<!-- <${tag}> -->\n`;
let end_tag = `\n<!-- </${tag}> -->\n`;
let objTemplate = newTemplate.split(`<${tag}>`)[1].split(`</${tag}>`)[0];
let htmlObj = "";
if(objTemplate.includes('{') && objTemplate.includes('}')){
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 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()
let db = JSON.parse(rawdata)
Object.keys(db).map((key, index)=>{
let item = db[key]
let temp = objTemplate;
//insert ID in parent div toruf-OBJ-ID
Object.keys(item).map((key, index)=>{
if(temp.includes(`{{${key}}}`)){
temp = replaceHtmlAttr(temp, key, item[key])
}
}
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';
})
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;
newTemplate = newTemplate.replace(oldObj, newHtmlObj);
}
}
}
}
})
}
})
}

function replaceHtmlAttr(temp, key, val){
let temparr = temp.split(`{{${key}}}`)
//console.log('length ', temparr.length)
for(let i=1;i<temparr.length;i++){
console.log(key)
let beginAttr = temparr[i-1].substr(0, temparr[i-1].lastIndexOf('"'));
let endAttr = temparr[i].split('"')[1];
let ending = temparr[i].split('"')[0];
let begining = temparr[i-1].substr(temparr[i-1].lastIndexOf('"')+1)
let newVal = `<span class="torufattr_${key}">${val}</span>`
//console.log(newVal)
//conditions to check if the {{}} is inserted inside an html element
if(endAttr && beginAttr){
if(endAttr.startsWith(' ') && beginAttr.endsWith('=') || endAttr.startsWith('>') && beginAttr.endsWith('=')){
let tag = beginAttr.substr(beginAttr.lastIndexOf(' ')+1).split('=')[0]
let attrVal = begining+`{{${key}}}`+ending
if(attributeList[tag] || tag.startsWith("data-")){
console.log(tag)
//check if element already has a class item
let beginElem = ""
let lastElem = temparr[i-1].split('<')[temparr[i-1].split('<').length - 1]
if(!lastElem.includes(">")) beginElem = temparr[i].split(">")[0];
let elem = lastElem + beginElem;
if(elem.includes('class=')){
console.log('CLASSS ', tag)
let tempClassArr = temparr[i-1].split('class="')
let tempClass = tempClassArr[tempClassArr.length - 1]
//console.log(key, ' ', tempClass)
temp = temp.replace(tempClass+`{{${key}}}`, `toruf${tag}_${key} `+ tempClass+val);
}
else {
temp = temp.replace(`${tag}="${attrVal}"`, `class="toruf${tag}_${key}" ${tag}="${begining+val+ending}"`);
//console.log(newTemp)
return temp
}
}
else throw new Error(`warning invalid html attribute ${tag}`);
}
else temp = temp.replace(`{{${key}}}`, newVal);
}
else temp = temp.replace(`{{${key}}}`, newVal);
}
return temp
}

/* 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)
}
}
else {
callback(null, parent)
}
}
} */
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.
Loading