-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
144 lines (117 loc) · 3.51 KB
/
server.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
var express = require('express');
var exphbs = require('express-handlebars');
var app = express();
var bodyParser = require('body-parser');
var phantom = require('phantom');
var fs = require('fs');
var marked = require('marked');
var htmlparser = require("htmlparser");
var _ = require('underscore');
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.set('view engine', 'handlebars');
// main index, allows you to drop in your file
app.get('/', function (req, res) {
res.render('home', {
layout: 'app'
});
});
// Post to /convert with your content so that we can
// let the magic happen
app.post('/convert', function (req, res) {
var content = marked(req.body.content);
var fileName = req.body.fileName;
var meta = findMetaInfo(content);
var savePath = handlebarsPath(fileName);
createTempFile(content, savePath, meta, req, fileName);
res.render('download', {
layout: 'app',
fileName: fileName,
fileLayout: meta.layout
});
});
// take the html output by marked and drop it into the
// layout that the file specifies.
app.get('/file', function (req, res) {
var data = fs.readFile(handlebarsPath(req.query.fileName), function (err, data){
if (err) { throw err; }
return data.toString;
});
file = findMetaInfo(data);
res.render('../tmp/' + req.query.fileName, {
layout: layoutPath(file.layout),
title: file.title,
content: file.content
});
});
// send the file to the user
app.get('/download/tmpFile', function (req, res) {
res.download(pdfPath(req.query.fileName));
});
//
// Helper Functions
//
// Pathing that was repeated over and over
var handlebarsPath = function(name){
return tmpPath(name, 'handlebars');
}
var pdfPath = function(name){
return tmpPath(name, 'pdf')
}
var tmpPath = function(name, ext){
return 'tmp/' + name + '.' + ext;
}
var layoutPath = function(layout){
return 'themes/' + layout;
}
// Break down the content, find the first item and attempt to parse it as
// front matter
var findMetaInfo = function (content) {
var handler = new htmlparser.DefaultHandler(function (error, dom) {
if (error){ console.log(error); }
});
var parser = new htmlparser.Parser(handler);
parser.parseComplete(content);
var frontMatter = handler.dom[0].data;
var defaults = {
title: null,
layout: 'crimson',
pageWidth: '8.5in',
pageHeight: '11in',
pageMargin: '.5in'
}
var file = {}
_.each(defaults, function (value, key){
var re = new RegExp(key + ': (.*)');
var match = re.exec(frontMatter);
file[key] = (match && match[1]) || value;
});
return file;
}
var createTempFile = function (content, savePath, meta, req, fileName) {
fs.writeFile(savePath, content, function(err) {
if(err) {
console.log(err);
} else {
renderPDF(meta, req, fileName);
}
});
}
var renderPDF = function (meta, req, fileName) {
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open(req.protocol + '://' + req.get('host') + '/file?fileName=' + fileName, function (status) {
page.set('paperSize', {
width: meta.pageWidth,
height: meta.pageHeight,
margin: meta.pageMargin
});
page.render(pdfPath(fileName));
});
});
});
}
// Allow public directory to be used for static files
app.use(express.static(__dirname + '/public'));
// Start me up on 3000 locally, or wherever heroku wants
app.listen(process.env.PORT || 3000);