Core components of the Canner
Core components of the Canner, including init, build, watch, allin and read API.
A static webpage generator based on template engines, which aimed to deal with the maintenance difficulties between data and webpages.
We isolate all the text from files so people can maintain their websites more easily, we also work hard on lower the barriers for different fields of people to collaborate with ease.
if you want to send request to localhost, run export CANNER_ENV='dev'
var canner= require('canner');
Create initial files and folders, under a directory.
return a promise
canner.init(dir, generator)
.then(function(){
// do your stuff
})
.catch(function(err){
// deal with error
});
Build a canner from a canner.json
return a promise
canner.build(dir, options)
.then(function(){
// do your stuff
})
.catch(function(err){
// deal with error
});;
Watching any changes in a canner and recompiled
return a promise
canner.watch(posts, {
cwd: __dirname ,
serve: __dirname + '/blog',
returnContent: true,
watchCallback: function (html) {
console.log(html)
},
reloader: function () {
return loadJson();
},
changeFilter: function (row, f, curr, prev) {
// if md equals, return true
if(path.extname(f)=='.md'){
return (f==path.resolve(__dirname, row.data.content))
}
}
})
the directory you want to watch
everytime we call build
in watch
, we'll call watchCallback
after build
finish
if you give us object in canner.watch
parameters, we'll need a reloader
to reload the object
function loadJson () {
var obj = JSON.parse(fs.readFileSync(__dirname + '/blog/blog-multi.json', 'utf8'));
var posts = obj.posts;
var post_layout = obj.post_settings.layout;
var post_root_path = obj.post_settings.path;
if(!_.isArray(posts))
posts = [posts];
posts = _.map(posts, function(post) {
var post_date = post.date;
var post_url = post.url_name + '.html';
var file_path = generatePath(post_root_path, post_date, post_url);
var canner_obj = {
layout: post_layout,
filename: file_path,
data: post
};
return canner_obj;
})
return posts
}
file operation filters are called after we reload the settings, and we'll filter the datas using the filter. we keep the data row if filter return true, else we delete the row
for example, this function first see if the extenstion of changed file is .md
if true, it will compare changed file path with the markdown file in row.data
function (row, f, curr, prev) {
// if md equals, return true
if(path.extname(f)=='.md'){
return (f==path.resolve(__dirname, row.data.content))
}
}
called when a file is created
row
means the data row you currenly compared withf
is the filename of file createdstat
is stat of file createdcalled when a file is created
curr
is the current file statprev
is the previous file statcalled when a file is removed
Make html include files all warp allin
canner.allin(htmlfile, options);
Create a canner.json from hbs_file
return a promise
canner.create(dir, options)
.then(function(){
// do your stuff
})
.catch(function(err){
// deal with error
});;