xonecas.io
818
Du

dustymarkdown / commit daaba70

rewrote a bunch of things

xonecas2011-04-28 daaba70a333e8a0367b6df5fc0048170bb2e345d parent 966143d

4 files changed

build.js+63−109
@@ -9,140 +9,94 @@ var fs = require('fs'),
99 var ins = require('util').inspect;
1010 function log (data) { console.log(ins(data)); };
1111
12-/*
13-var index = fs.readFileSync('./templates/index.dust', 'utf8');
14-var compiledIndex = dust.compile(index, 'index');
15-dust.loadSource(compiledIndex);
16-*/
17-
1812 // get all my templates and compile them
1913 function readTemplates (callback) {
2014 fs.readdir('./templates', function (err, templates) {
2115 if (err) throw err;
2216
23- var len = templates.length, dir = "./templates/";
24-
25- // asynchronous loop
26- (function loop (i) {
27- if (i < len) {
28- var template = templates[i];
29- if (/\.dust$/.test(template)) {
30- fs.readFile(dir+template, 'utf8', function (err, file) {
31- if (err) throw err;
32-
33- template = template.replace('.dust', '');
34- var compiled = dust.compile(file, template);
35- dust.loadSource(compiled);
36-
37- loop(i+1);
38- });
39- } else {
40- loop(i+1);
41- }
42- }
43- else {
44- callback(templates);
17+ templates.forEach(function (template, index) {
18+ if (template.match(/\.dust$/)) {
19+ fs.readFile('./templates/'+template, 'utf8', function (err2, file) {
20+ if (err) throw err;
21+
22+ template = template.replace('.dust', '');
23+ var compiled = dust.compile(file, template);
24+ dust.loadSource(compiled);
25+
26+ if (index === templates.length-1)
27+ callback(templates);
28+ });
4529 }
46- }) (0);
30+ });
4731 });
4832 };
4933
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);
43+ });
44+ });
45+}
46+*/
47+
5048 // get all markdown files and parse them
5149 function readMarkdown (callback) {
50+ var htmlContent = {};
51+
5252 fs.readdir('./markdown', function (err, files) {
5353 if (err) throw err;
5454
55- var len = files.length, html = {};
56-
57- (function reader (i) {
58- if (i < len) {
59- var filename = files[i];
60- if (filename.match(/\.md$/)) {
61- fs.readFile('./markdown/'+filename, 'utf8', function (err2, file) {
62- if (err2) throw err2;
63-
64- if (filename.match(/.+\_.+/)) {
65- var split = filename.split('_'),
66- section = split[0];
67- filename = split[1];
68-
69- html[section] = html[section] || [];
70- html[section].push(markdown(file));
71- } else {
72- filename = filename.replace('.', '_');
73- html[filename] = markdown(file);
74- }
75-
76- reader(i+1);
77- });
78- }
79- else {
80- reader(i+1);
81- }
55+ files.forEach(function (filename, index) {
56+ if (filename.match(/\.md$/)) {
57+ fs.readFile('./markdown/'+filename, 'utf8', function (err, file) {
58+ // look for section pieces
59+ if (filename.match(/.+\_.+/)) {
60+ // @TODO need to sort files by create date
61+ // its a section piece
62+ var split = filename.split('_'), section = split[0];
63+
64+ htmlContent[section] = htmlContent[section] || [];
65+ htmlContent[section].push(markdown(file));
66+ }
67+ else {
68+ filename = filename.replace('.', '_');
69+ htmlContent[filename] = markdown(file);
70+ }
71+
72+ if (index === files.length-1)
73+ callback(htmlContent);
74+ });
8275 }
83- else
84- callback(html);
85- }) (0);
76+ });
8677 });
8778 };
8879
80+// write the files to static folder.
8981 function writeFiles (tmpls, html) {
90- var len = tmpls.length;
91-
92- (function writer (i) {
93- if (i < len) {
94- var filename = tmpls[i].replace('.dust', '.html');
95- dust.render(filename.replace('.html',''), html, function (err, htmlFile) {
96- fs.writeFile('./htdocs/'+filename, htmlFile, function (err) {
97- if (err) throw err;
98-
99- console.log('+++ Wrote: '+filename);
100- writer(i+1);
101- });
82+ tmpls.forEach(function (filename, index) {
83+ var templateName = filename.replace('.dust', '');
84+ dust.render(templateName, html, function (err, htmlFile) {
85+ filename = './htdocs/'+filename.replace('.dust', '.html');
86+ fs.writeFile(filename, htmlFile, function (err) {
87+ if (err) throw err;
88+
89+ console.log(" -!- Wrote: "+filename+" -!- ");
10290 });
103- }
104- }) (0);
91+
92+ });
93+ });
10594 };
10695
96+// get the ball going
10797 readTemplates(function (tmpls) {
10898 readMarkdown(function (html) {
10999 writeFiles(tmpls, html);
110100 });
111101 });
112102
113-/*
114-fs.readdir('./markdown', function (err, files) {
115- if (err) throw err;
116-
117- log(files);
118-
119- var html = {}, len = files.length;
120-
121- function writer (html) {
122- log(html);
123- dust.render("index", html, function (err, indexHtml) {
124- log (indexHtml);
125- fs.writeFile('./htdocs/index.html', indexHtml, function (err2) {
126- console.log("+++ wrote index");
127- });
128- });
129- }
130-
131- (function reader (i) {
132- var filename = files[i];
133- if (i < len && filename.match(/\.md$/)) {
134- fs.readFile('./markdown/'+filename, 'utf8', function (err2, file) {
135- if (err2) throw err2;
136- filename = filename.replace('.', '_');
137- html[filename] = markdown(file);
138- reader(i+1);
139- });
140- }
141- else if (i < len)
142- reader(i+1);
143- else
144- writer(html);
145- }) (0);
146-});
147-*/
148-
htdocs/index.html+3−1
@@ -12,7 +12,9 @@
1212 <blockquote>
1313 <p>He said something
1414 Yep he did.</p>
15-</blockquote><h2>check out new code!!</h2>
15+</blockquote><h1>Hi BEN!</h1>
16+
17+<p>How are you today?</p> <h2>check out new code!!</h2>
1618
1719 <p><code>this + that</code></p> <h2>HI!</h2>
1820
markdown/ahah.md+0−0
markdown/posts_blabla.md+3−0
@@ -0,0 +1,3 @@
1+# Hi BEN!
2+
3+How are you today?