Static Blog Generator
/* | |
Static Site Generator | |
Author: Cody Shepp 6/27/2014 | |
License: MIT | |
The only outside dependency is mustache (npm install mustache) | |
The basic folder structure I used is as follows: | |
/ | |
/src | |
/articles | |
raw-article-content.html | |
/templates | |
header-template.html | |
footer-template.html | |
/blog | |
/articles | |
(generated articles are placed in folders here) | |
static-blog.js | |
*/ | |
var fs = require('fs') | |
var path = require('path') | |
var mustache = require('mustache') | |
var root = path.join(process.cwd(), "/src") | |
var articles_root = path.join(root, '/articles') | |
var articles = [] | |
//get all article folders | |
var files = fs.readdirSync(articles_root) | |
/* | |
Loop through each raw article file that we found in /src/articles | |
and read the contents - then push that data into articles[] | |
*/ | |
files.forEach(function(article_file){ | |
var body = fs.readFileSync(path.join(articles_root, article_file), {encoding: 'utf-8'}) | |
articles.push({ | |
file_name: article_file | |
, body: body | |
}) | |
}) | |
//pull in our various other templates (header, footer, etc) | |
var top_template = fs.readFileSync(path.join(root, '/templates', 'top.html'), {encoding: 'utf-8'}) | |
var listing_bottom_template = fs.readFileSync(path.join(root, '/templates', 'listing-bottom.html'), {encoding: 'utf-8'}) | |
var single_bottom_template = fs.readFileSync(path.join(root, '/templates', 'single-bottom.html'), {encoding: 'utf-8'}) | |
//the folder where we're going to save the generated article files | |
var generated_articles_root = path.join(process.cwd(), '/blog', '/articles') | |
//we're numbering our article sequentially, but we want them to appear in the | |
// listing in reverse order (most recent first) | |
articles = articles.reverse() | |
//generate all article single views | |
for(var i = 0; i < articles.length; i++){ | |
var a = articles[i] | |
var page_content = top_template + '\r\n' + a.body + '\r\n' + single_bottom_template | |
var a_path = path.join(generated_articles_root, '/' + path.basename(a.file_name, '.html')) | |
try{ | |
fs.mkdirSync(a_path) | |
} | |
catch(err){} | |
fs.writeFileSync(path.join(a_path, 'index.html'), page_content, {encoding: 'utf-8'} ) | |
console.log("Generated an article: ", a.file_name) | |
} | |
// now we need to generate the blog listing pages | |
var posts_per_page = 3 | |
var num_pages = Math.ceil(articles.length / posts_per_page) | |
/* | |
Loop through our listing pages and generate the files | |
we're essentially just generating 3 articles per page | |
instead of just one | |
*/ | |
for(var i = 0; i < num_pages; i++){ | |
var current_article = i * posts_per_page | |
var page_content = top_template | |
page_content += articles[current_article].body | |
console.log(articles[current_article].file_name) | |
if(articles[current_article + 1] != undefined){ | |
page_content += articles[current_article + 1].body | |
console.log(articles[current_article+1].file_name) | |
} | |
if(articles[current_article + 2] != undefined){ | |
page_content += articles[current_article + 2].body | |
console.log(articles[current_article+2].file_name) | |
} | |
if(i == num_pages - 1){ | |
console.log("single") | |
page_content += single_bottom_template | |
} | |
else{ | |
console.log("listing") | |
var bottom_content = mustache.render(listing_bottom_template, {num: i + 2}) | |
page_content += bottom_content | |
} | |
if(i == 0){ | |
fs.writeFileSync(path.join(process.cwd(), '/blog', 'index.html'), page_content, {encoding: 'utf-8'}) | |
} | |
else{ | |
try{ | |
fs.mkdirSync(path.join(process.cwd(), '/blog', '/' + (i+1).toString())) | |
} | |
catch(err){} | |
fs.writeFileSync(path.join(process.cwd(), '/blog', '/' + (i+1).toString(), 'index.html'), page_content, {encoding: 'utf-8'}) | |
} | |
page_content = "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment