-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
161 lines (131 loc) · 4.74 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
/**
* (c) 2013 OGI-IT.
* User: ogi
* Date: 07.11.13
* Time: 13:32
*/
"use strict";
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var issues = require('./routes/issues');
var getIssues = require('./routes/getIssues');
var http = require('http');
var path = require('path');
var dust_h = require('dustjs-helpers'); //this is needed!! there is a try load in consolidate...
var dust = require('dustjs-linkedin');
// dust.helpers = require('dustjs-helpers').helpers; //HACK
// DO NOT UPGRADE DUST UNTIL issue https://github.com/linkedin/dustjs-helpers/issues/72 is resolved and consolidate is changed..
// could be that consolidate will NEVER be updated since we ahve a new express version just leave it that way for now!!!!
dust.isDebug = true;
//dust.debugLevel = 'DEBUG';
var dateFormat = require('dateformat');
// add my format helper usage: <p>Today: {@formatDate value="{today}" format="dddd, mmmm dS, yyyy, h:MM:ss TT" /}</p>
dust.helpers.formatDate = function (chunk, context, bodies, params) {
var value = dust.helpers.tap(params.value, chunk, context),
format = dust.helpers.tap(params.format, chunk, context),
timestamp;
timestamp = new Date(value); // this converts correctly from UTC to the local timezone...
//console.log("value: "+value+" timestamp: "+timestamp);
return chunk.write(dateFormat(timestamp, format));
};
var marked = require('marked');
var renderer = new marked.Renderer();
var markedHeaderStartingLevel = 3;
renderer.heading = function (text, level) {
var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
var newLevel = level + markedHeaderStartingLevel;
return '<h' + newLevel + '><a name="' +
escapedText +
'" class="anchor" href="#' +
escapedText +
'"><span class="header-link"></span></a>' +
text + '</h' + newLevel + '>';
};
// we could also do syntax highlighting see https://github.com/chjj/marked
marked.setOptions({
gfm: true,
tables: true,
breaks: true,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false,
renderer: renderer
});
var ent = require('ent');
dust.helpers.marked = function (chunk, context, bodies, params) {
if (bodies.block) {
return chunk.capture(bodies.block, context, function (string, t_chunk) { //TODO check why we have here a chunk.capture
var str;
try {
var theString = ent.decode(string)
//console.log ("the marked string to decode: %s",theString);
str = marked(theString);
t_chunk.end("<span class='marked'>" + str + "</span>");
} catch (err) {
str = "MARKED ERROR START: " + err + " MARKED ERROR END."
console.log(str);
t_chunk.end(str);
}
});
}
return chunk;
};
var cons = require('consolidate');
var app = express();
// all environments
app.set('port', process.env.PORT || 3001);
app.set('views', path.join(__dirname, 'views'));
app.engine('dust', cons.dust);
app.set('view engine', 'dust');
//app.use(express.compress()); //not a good idea; not chunked...
app.use(express.favicon()); //TODO change to custom one
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.disable('x-powered-by'); //get rid of the x-powerd-by = express header...
app.get('/', routes.index);
app.get('/issues/:filename', issues.report);
app.get('/getIssues', getIssues.getIssues);
app.post('/getIssues', getIssues.getIssuesDo);
var wkhtml = require('./node-wkhtml.js');
app.get('/pdf', function (req, res) {
console.log("link: %s", req.param("link"));
var source = 'http://localhost:3001/issues/' + req.param("link");
var line = [
'--outline-depth',
'2',
'--header-right',
'[title]',
'--footer-right',
'page [page] of [topage]',
'toc',
source
];
console.log("line: %j", line);
var spwn = wkhtml
.spawn('pdf', line);
//this is for debug to show the error which is put on stdout...
// spwn.stdout.on('data', function (data) {
// console.log('>> ' + data);
// });
spwn.stdout.pipe(res);
// this shows the progress indicator in the log...
spwn.stderr.on('data', function (data) {
//console.log('>>>> ' + data);
console.log(data.toString());
});
});
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});