-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (84 loc) · 3.1 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
let capFirst = (s) =>
s[0].toUpperCase() + s.slice(1);
//=================================================================
// SETTINGS
// choose which grammar file to use
//let FILENAME = 'artreview';
//let FILENAME = 'bookreview';
//let FILENAME = 'headlines';
//let FILENAME = 'insults';
//let FILENAME = 'lovetester';
let FILENAME = 'wiggleworld';
//let FILENAME = 'wizardschool';
// how many messages to generate
let N = 50;
// if this is set, you'll get the same results every time.
// set to null to get different results every time.
let RANDOM_SEED = 'abc';
// draw boxes around the rule structure?
let SHOW_STRUCTURE = false;
// title to show at the top of the page
let TITLE = capFirst(FILENAME);
//=================================================================
// SERVER CODE
let fs = require('fs');
let Filigree = require('filigree-text').Filigree;
let loadFil = () => {
// read filigree file
console.log('loading ' + FILENAME);
let src = fs.readFileSync(`grammars/${FILENAME}.filigree`, 'utf-8');
let fil = new Filigree(src, RANDOM_SEED);
return fil;
}
// declare a couple of wrapper functions to use, depending on whather we want to show the structure
// in this default version we neuter the angle brackets so we can see unreplaced rules in the output,
// otherwise they would be treated as actual HTML tags and hidden by the browser
let doNotWrapperFn = (rule, text) =>
text
.split('<').join('<');
// in this fancier wrapper function, we show the rule name in a box.
// but we can't show leftover unmatched <rules> in the output because we can't
// mess with their angle brackets without breaking the real HTML we're adding here.
let fancyWrapperFn = (rule, text) =>
`<div class="container">
<sup class="ruleName">${rule}</sup>
<div class="output">${text}</div>
</div>`;
// function to generate one message
let generateOneMessage = (fil) => {
if (fil.err) {
// report error message
return '<div class="error">' + fil.err.message.split('<').join('<') + '</div>';
}
// generate text
let text = fil.generate('start', SHOW_STRUCTURE ? fancyWrapperFn : doNotWrapperFn);
return text;
}
// create webserver
const express = require('express');
const app = express();
app.use(express.static('public'));
// when the page is loaded...
app.get('/', function(request, response) {
// generate some texts
let fil = loadFil();
let texts = [];
for (let ii = 0; ii < N; ii++) {
texts.push(generateOneMessage(fil));
}
// put them each in divs for css styling
let text = texts.map(t => '<div class="filigreeMessage">' + t + '</div>').join('\n');
// add the title above the messages
text = '<div class="title">' + TITLE + '</div>\n' + text;
// load html template from file
let template = fs.readFileSync('views/index.html', 'utf-8');
// insert content into template
let html = template.split('{{slot}}').join(text);
// return it!
response.set('Content-Type', 'text/html');
response.send(html);
});
// start the server
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});