-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.js
170 lines (143 loc) · 3.27 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Dependencies.
*/
var express = require('express');
var partials = require('express-partials');
var app = express();
var template = require('tower-template');
var content = require('tower-content');
var router = require('tower-router');
var route = require('tower-route');
var text = require('tower-text');
var agent = require('superagent');
var fs = require('fs');
var exec = require('child_process').exec;
var guide = require('./lib/guide');
var render = require('./lib/render');
var doc = require('./lib/doc');
/**
* Directives.
*/
require('tower-list-directive').document = render.document;
//require('tower-markdown-directive');
require('tower-interpolation-directive');
/**
* Module dependencies.
*/
var directive = require('tower-directive');
var md = require('marked');
var hl = require('highlight.js');
var langs = { js: 'javascript', html: 'xml' };
md.setOptions({
highlight: function(code, lang){
if (!hl.LANGUAGES[lang]) {
if (langs[lang]) lang = langs[lang];
else return code;
}
return hl.highlight(lang, code).value;
}
});
/**
* Expose `markdownDirective`.
*/
//module.exports = directive('[type="text/markdown"]', markdownDirective);
directive('data-markdown', markdownDirective);
/**
* Define `markdownDirective`.
*/
function markdownDirective(scope, el, attr) {
// XXX: todo, expression
var val = attr.value
? scope.get(attr.value)
: el.textContent;
if (val) el.innerHTML = md(val);
}
/**
* Configuration.
*/
app.configure(function(){
app.use(partials());
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use('/public', express.static(__dirname + '/public'));
app.engine('html', render);
app.use(router);
});
/**
* Guides.
*/
guide('resource');
guide('adapter');
guide('query');
guide('template');
guide('content');
guide('directive');
guide('element');
guide('expression');
guide('route');
guide('cookbook');
//guide('validator');
//guide('type');
//guide('text');
guide('cli');
var overview = guide.compile('overview');
/**
* Docs.
*/
doc('adapter');
doc('cli');
doc('content');
doc('cookbook');
doc('directive');
doc('element');
doc('expression');
doc('query');
doc('resource');
doc('route');
doc('template');
doc('text');
doc('type');
doc('validator');
// doc.compile();
/**
* Content.
*/
content('body')
.attr('overview', 'string', overview.content)
.attr('guides', 'array', guide.collection);
/**
* Routes.
*/
route('/', function(context){
context.res.render('index');
});
route('/guide', function(context){
context.res.render('guides');
});
route('/api', function(context){
context.res.render('api');
});
/**
* Listen.
*/
app.listen(process.env.PORT || 3000, function(){
// wait conservatively for markdown to compile,
// then save static html files for gh-pages.
setTimeout(function(){
fetch('/', 'index.html', function(){
fetch('/guide', 'guide.html', function(){
fetch('/api', 'api.html', function(){
exec('cleancss -o public/css/index.css public/css/index.css', function(){
console.log('cached.');
});
});
});
});
}, 1000);
});
function fetch(path, name, fn) {
agent.get('http://localhost:3000' + path).end(function(res){
fs.writeFileSync(name, res.text);
process.nextTick(fn);
});
}