Rewrite compiler

This commit is contained in:
LordMathis 2017-10-27 16:20:15 +02:00
parent 876206f7c9
commit c8f23483e8
3 changed files with 45 additions and 40 deletions

View File

@ -31,7 +31,7 @@ app.use(require('express').static('public'))
var api = require('./utils/api'); var api = require('./utils/api');
api(app); api(app);
require('./utils/scanner'); require('./utils/scanner')();
var serverRender = require('./utils/serverRender') var serverRender = require('./utils/serverRender')
app.get("*", serverRender) app.get("*", serverRender)

View File

@ -17,13 +17,6 @@ function writeRenderedFile(renderedpath, result) {
}); });
} }
function writeData(data) {
const dataPath = path.join(process.cwd(), 'server/utils/data.json');
jsonfile.writeFile(dataPath, data, (err) => {
if (err) throw err;
});
}
function render(file) { function render(file) {
const md = new MarkdownIt(); const md = new MarkdownIt();
return md.render(file); return md.render(file);
@ -43,33 +36,40 @@ function fileMetadata(filepath) {
return metadata; return metadata;
} }
const compiler = { module.exports = function Compiler(data) {
add(filepath) { this.data = data;
Step(
function loadFiles() {
readData(this.parallel());
readFile(filepath, this.parallel());
},
(err, data, fileData) => {
if (err) throw err;
const frontMatter = fm(fileData);
const rendered = render(frontMatter.body);
const metadata = fileMetadata(filepath);
const post = {
published: moment().format('MMMM Do YYYY'),
filename: metadata.filename,
title: frontMatter.attributes.title,
summary: frontMatter.attributes.summary,
};
const renderedpath = path.join(config.renderPath, `${metadata.filename}.html`);
data.posts.push(post);
writeData(data);
writeRenderedFile(renderedpath, rendered);
}
);
},
} }
Compiler.prototype.addFile = function (filepath) {
Step(
function loadFiles() {
readFile(filepath, this.parallel());
},
(err, fileData) => {
if (err) throw err;
const frontMatter = fm(fileData);
const rendered = render(frontMatter.body);
const metadata = fileMetadata(filepath);
const post = {
published: moment().format('MMMM Do YYYY'),
filename: metadata.filename,
title: frontMatter.attributes.title,
summary: frontMatter.attributes.summary,
};
const renderedpath = path.join(config.renderPath, `${metadata.filename}.html`);
this.data.posts.push(post);
writeRenderedFile(renderedpath, rendered);
}
);
};
Compiler.prototype.writeData = function () {
const dataPath = path.join(process.cwd(), 'server/utils/data.json');
jsonfile.writeFile(dataPath, data, (err) => {
if (err) throw err;
});
};

View File

@ -1,11 +1,16 @@
const fs = require('fs'); const fs = require('fs');
const compiler = require('./compiler'); const Compiler = require('./compiler');
const config = require('../static/config/config.json'); const config = require('../static/config/config.json');
const data = require('./data.json');
module.exports = function() { module.exports = function() {
var compiler = Compiler(data);
fs.readdir(config.contentPath, (err, files) => { fs.readdir(config.contentPath, (err, files) => {
files.forEach(file => { files.forEach(file => {
console.log(file); compiler.addFile(file);
}) });
compiler.writeData();
}); });
} }