131 lines
4.4 KiB
JavaScript
131 lines
4.4 KiB
JavaScript
const log = require('color-logs')(true, true, "Categories.class.js"),
|
|
path = require('path'),
|
|
hbs = require('handlebars'),
|
|
promise = require('promise'),
|
|
fs = require("fs");
|
|
|
|
hbs.registerHelper('listArticle', function(tabs, options) {
|
|
var i = 1;
|
|
var out = '';
|
|
for(var x in options['data']['root']){
|
|
|
|
out = out + "" + options.fn(options['data']['root'][x]) + "";
|
|
if(i == 3){
|
|
i= 1;
|
|
out = out + "</div><div class=\"row\">";
|
|
}
|
|
else{
|
|
i++;
|
|
}
|
|
}
|
|
return out;
|
|
});
|
|
|
|
class Categories {
|
|
|
|
/**
|
|
* hydrate global var
|
|
* @param {type} slug : url of categorie
|
|
* @returns {Promise} data of categories
|
|
*/
|
|
getCategorie(slug){
|
|
return new Promise((resolve, reject) => {
|
|
var path = "./ressources/"+slug+"/params.json";
|
|
fs.access(path, fs.constants.F_OK || fs.constants.R_OK, (error) => {
|
|
if(error)
|
|
resolve(new Error("File not exist"));
|
|
else
|
|
resolve(require("."+path));
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @returns {Promise} the list of article
|
|
*/
|
|
getHome(){
|
|
return new Promise((resolve, reject) => {
|
|
fs.readFile('./views/articles/list.hbs', 'utf-8', (error, source) => {
|
|
var data = require('../ressources/lastArticles.json');
|
|
hbs.registerHelper('articles', (articles) =>{
|
|
return articles;
|
|
});
|
|
var template = hbs.compile(source);
|
|
var html = template(data);
|
|
resolve(html);
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
getDisclaimer(){
|
|
return new Promise((resolve, reject) => {
|
|
fs.readFile('./views/articles/disclaimer.hbs', 'utf-8', (error, source) => {
|
|
var data = require('../ressources/disclaimer.json');
|
|
hbs.registerHelper('articles', (articles) =>{
|
|
return articles;
|
|
});
|
|
var template = hbs.compile(source);
|
|
var html = template(data);
|
|
resolve(html);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {type} slug : url of categorie
|
|
* @returns {Promise} the list of article in this categorie
|
|
*/
|
|
getListArticles(slug){
|
|
return new Promise((resolve, reject) => {
|
|
fs.readFile('./views/articles/list.hbs', 'utf-8', (error, source) => {
|
|
var pathOfArticles = "./ressources/"+slug+"";
|
|
var dataObj = {};
|
|
fs.readdir(pathOfArticles, (err, files)=> {
|
|
if(err){
|
|
resolve(new Error("File not exist"));
|
|
}
|
|
else{
|
|
var i = 0;
|
|
files.forEach((file) => {
|
|
if(file != "params.json"){
|
|
if(path.extname(file) == ".json"){
|
|
dataObj[i] = require('.'+pathOfArticles+"/"+file);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
});
|
|
var objectSize = Object.keys(dataObj).length;
|
|
var data = "{";
|
|
for(var x in dataObj){
|
|
if(x < objectSize-1){
|
|
data += "\""+x+"\""+":"+JSON.stringify(dataObj[x])+',';
|
|
}
|
|
else{
|
|
data += "\""+x+"\""+":"+JSON.stringify(dataObj[x])+'';
|
|
}
|
|
}
|
|
data += "}";
|
|
data = JSON.parse(data);
|
|
|
|
hbs.registerHelper('articles', (articles) =>{
|
|
return articles;
|
|
});
|
|
|
|
var template = hbs.compile(source);
|
|
var html = template(data);
|
|
resolve(html);
|
|
}
|
|
});
|
|
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
}
|
|
|
|
module.exports = Categories; |