Mercurial > hg > nodescore
comparison node_modules/static/index.js @ 69:333afcfd3f3a
added node_modules to project and fixed path to chronometer
also added deps to installer script
author | tzara <rc-web@kiben.net> |
---|---|
date | Sat, 26 Oct 2013 14:12:50 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
68:b076cd17638c | 69:333afcfd3f3a |
---|---|
1 var handlebars = require('handlebars'), | |
2 async = require('async'), | |
3 cheerio = require('cheerio'), | |
4 marked = require('marked'), | |
5 path = require('path'), | |
6 fs = require('fs'), | |
7 _ = require('underscore'), | |
8 highlight = require('highlight.js'); | |
9 | |
10 var config = { | |
11 addIdsToHeadings: true, | |
12 gfm: true, //github flavored markdown | |
13 highlight: function(code, lang) { | |
14 return highlight.highlight('javascript', code).value; | |
15 } | |
16 }; | |
17 | |
18 var jqueryPath = './lib/jquery.js', | |
19 asyncTolkens = {}, | |
20 markdownCallbacks = []; | |
21 | |
22 var transforms = {}; | |
23 | |
24 function addTransform(name, callback) { | |
25 transforms[name] = callback; | |
26 } | |
27 | |
28 addTransform('html', function(buffer, complete, context, data) { | |
29 return complete(buffer.toString()); | |
30 }); | |
31 | |
32 addTransform('md', function(buffer, complete, context, data) { | |
33 var html = marked(buffer.toString(), { | |
34 gfm: config.gfm, | |
35 highlight: config.highlight | |
36 }); | |
37 async.series(_.map(markdownCallbacks, function(callback) { | |
38 return function(next) { | |
39 callback(html, function(modifiedHTML) { | |
40 html = modifiedHTML; | |
41 next(); | |
42 }); | |
43 } | |
44 }), function() { | |
45 complete(html); | |
46 }); | |
47 }); | |
48 | |
49 addTransform('hbs', function(buffer, complete, context, data) { | |
50 var output = handlebars.compile(buffer.toString(), { | |
51 data: true | |
52 })(context, { | |
53 data: data | |
54 }); | |
55 var filteredAsyncTolkens = {}; | |
56 _.each(asyncTolkens, function(tolkenData, tolken) { | |
57 if (data.file === tolkenData.file) { | |
58 filteredAsyncTolkens[tolken] = tolkenData; | |
59 } | |
60 }); | |
61 if (!_.keys(filteredAsyncTolkens).length) { | |
62 complete(output); | |
63 } else { | |
64 async.series(_.map(filteredAsyncTolkens, function(tolkenData, tolken) { | |
65 return function(next) { | |
66 var args = tolkenData.args; | |
67 args.push(function(callbackOutput) { | |
68 output = output.replace(tolken, callbackOutput.toString()); | |
69 next(); | |
70 }); | |
71 tolkenData.callback.apply(tolkenData.callback, args); | |
72 }; | |
73 }), function() { | |
74 complete(output); | |
75 }); | |
76 } | |
77 }); | |
78 | |
79 function $(html, callback) { | |
80 callback(cheerio.load(html)); | |
81 } | |
82 | |
83 function modifyDocumentFragment(html, callback, next) { | |
84 $(html, function($) { | |
85 callback($); | |
86 next($.html()); | |
87 }); | |
88 } | |
89 | |
90 function removeOuterBodyTag(html) { | |
91 return html.replace(/^\s*\<body\>/, '').replace(/\<\/body\>\s*$/, ''); | |
92 } | |
93 | |
94 handlebars.registerAsyncHelper = function(name, callback) { | |
95 handlebars.registerHelper(name, function() { | |
96 var tolken = String(new Date().getTime() + Math.random()); | |
97 var args = _.toArray(arguments), | |
98 data = args[args.length - 1].data; | |
99 asyncTolkens[tolken] = { | |
100 file: data.file, | |
101 args: args, | |
102 callback: callback | |
103 }; | |
104 return tolken; | |
105 }); | |
106 }; | |
107 | |
108 handlebars.registerHelper('require', function(file, options) { | |
109 var filePath = path.join(path.dirname(options.data.file), file); | |
110 require(filePath)(module.exports); | |
111 return ''; | |
112 }); | |
113 | |
114 handlebars.registerAsyncHelper('include', function(file, options, callback) { | |
115 var filePath = path.join(path.dirname(options.data.file), file); | |
116 transform(filePath, function(fileData) { | |
117 var selector = options.hash.select; | |
118 if (selector) { | |
119 $(fileData.toString(), function($) { | |
120 var generatedHTML = ''; | |
121 $(selector).each(function() { | |
122 // make more like a regular dom object | |
123 this.attributes = this[0].attribs; | |
124 this.id = this[0].attribs.id; | |
125 this.tagName = this[0].name; | |
126 this.innerHTML = this.html(); | |
127 generatedHTML += options.fn(this); | |
128 }); | |
129 callback(generatedHTML); | |
130 }); | |
131 } else { | |
132 callback(fileData.toString()); | |
133 } | |
134 }, options.hash, options.data); | |
135 }); | |
136 | |
137 function transform(source, callback, options) { | |
138 fs.readFile(source, function(err, data) { | |
139 if (err) { | |
140 console.trace(); | |
141 throw err; | |
142 } | |
143 var extensions = source.split('/').pop().split('.'); | |
144 var callbacks = _.filter(extensions, function(extension) { | |
145 return extension in transforms; | |
146 }).map(function(extension) { | |
147 return function(next) { | |
148 transforms[extension](data, next, options || {}, { | |
149 file: source | |
150 }); | |
151 }; | |
152 }); | |
153 async.series(callbacks, callback); | |
154 }); | |
155 } | |
156 | |
157 function onMarkdown(callback) { | |
158 markdownCallbacks.push(callback); | |
159 } | |
160 | |
161 onMarkdown(function(html, next) { | |
162 if (config.addIdsToHeadings) { | |
163 modifyDocumentFragment(html, function($) { | |
164 if (config.addIdsToHeadings) { | |
165 addIdsToHeadings($); | |
166 } | |
167 }, next); | |
168 } else { | |
169 next(html); | |
170 } | |
171 }); | |
172 | |
173 function addIdsToHeadings($) { | |
174 $('h1,h2,h3,h4,h5,h6').each(function() { | |
175 var text = $(this).html().split('<').shift(); | |
176 var id = text.replace(/(^\s+|\s+$)/g, '').replace(/[\s]+/g, '-').replace(/([a-z])([A-Z])/g, function() { | |
177 return arguments[1] + '-' + arguments[2].toLowerCase(); | |
178 }).toLowerCase(); | |
179 if (id.match(/^\s+$/) || !id) { | |
180 return; | |
181 } | |
182 $(this).attr('id', id); | |
183 }); | |
184 } | |
185 | |
186 module.exports = { | |
187 config: config, | |
188 transform: transform, | |
189 handlebars: handlebars, | |
190 $: $, | |
191 modifyDocumentFragment: modifyDocumentFragment, | |
192 onMarkdown: onMarkdown, | |
193 addTransform: addTransform | |
194 }; |