@@ -14,50 +14,67 @@ function readTemplates (callback) {
1414 fs.readdir('./templates', function (err, templates) {
1515 if (err) throw err;
1616
17+ var tmpls = [];
1718 templates.forEach(function (template, index) {
18- if (template.match(/\.dust$/)) {
19+ if (template.match(/\.dust$/) && !template.match(/(^|\/)\./)) {
1920 fs.readFile('./templates/'+template, 'utf8', function (err2, file) {
2021 if (err) throw err;
2122
2223 template = template.replace('.dust', '');
2324 var compiled = dust.compile(file, template);
2425 dust.loadSource(compiled);
26+ tmpls.push(template+'.dust');
2527
26- if (index === templates.length-1)
27- callback(templates);
28+ if (index === templates.length-1) {
29+ callback(tmpls);
30+ }
2831 });
2932 }
3033 });
3134 });
3235 };
3336
34-/*
35-function getStats(dir, files, callback) {
36- var result = [];
37- files.forEach(function (file, index) {
38- fs.stat(dir+file, function (err, stats) {
39- result.push(stats);
40-
41- if (index === files.length-1)
42- callback(result);
37+// reads the filenames and stats from a dir
38+function readdir(dir, callback) {
39+ fs.readdir(dir, function (err, files) {
40+ if (err) throw err;
41+
42+ var result = [];
43+ files.forEach(function (file, index) {
44+ if (/^\.|\/\./.test(file)) return;
45+
46+ fs.stat(dir+'/'+file, function (err2, stats) {
47+ if (err2) throw err2;
48+
49+ stats.name = file;
50+ result.push(stats);
51+
52+ // sort files by creation date
53+ if (index === files.length-1) {
54+ result.sort(function (a, b) {
55+ return a.ctime - b.ctime;
56+ });
57+ callback(result.reverse());
58+ }
59+ });
4360 });
4461 });
4562 }
46-*/
4763
4864 // get all markdown files and parse them
4965 function readMarkdown (callback) {
5066 var htmlContent = {};
5167
52- fs.readdir('./markdown', function (err, files) {
53- if (err) throw err;
54-
55- files.forEach(function (filename, index) {
68+ readdir('./markdown', function (files) {
69+ var counter = files.length;
70+ files.forEach(function (fileStats, index) {
71+ var filename = fileStats.name;
5672 if (filename.match(/\.md$/)) {
5773 fs.readFile('./markdown/'+filename, 'utf8', function (err, file) {
74+ if (err) throw err;
75+
5876 // look for section pieces
5977 if (filename.match(/.+\_.+/)) {
60- // @TODO need to sort files by create date
6178 // its a section piece
6279 var split = filename.split('_'), section = split[0];
6380
@@ -69,8 +86,10 @@ function readMarkdown (callback) {
6986 htmlContent[filename] = markdown(file);
7087 }
7188
72- if (index === files.length-1)
89+ counter--;
90+ if (counter === 0) {
7391 callback(htmlContent);
92+ }
7493 });
7594 }
7695 });
@@ -80,8 +99,12 @@ function readMarkdown (callback) {
8099 // write the files to static folder.
81100 function writeFiles (tmpls, html) {
82101 tmpls.forEach(function (filename, index) {
102+ if (filename === undefined) return;
103+
83104 var templateName = filename.replace('.dust', '');
84105 dust.render(templateName, html, function (err, htmlFile) {
106+ if (err) throw err;
107+
85108 filename = './htdocs/'+filename.replace('.dust', '.html');
86109 fs.writeFile(filename, htmlFile, function (err) {
87110 if (err) throw err;