This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
forked from luto/ep_defaultPadText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (52 loc) · 1.51 KB
/
index.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
var settings = require('ep_etherpad-lite/node/utils/Settings');
var strftime = require('strftime');
exports.padCreate = function(hook, context)
{
var id = context.pad.id;
var found = false;
// abort if ID is undefined
if(!id) {
return;
}
// check all specific templates
for(var show in settings.ep_defaultPadText)
{
if(id.indexOf(show) == 0)
{
var number = id.substring(show.length);
var text = settings.ep_defaultPadText[show].text;
text = prepareText(text, id, number);
context.pad.setText(text);
found = true;
}
}
// no template was found, see if there is a general one, '*'
if(!found)
{
if(settings.ep_defaultPadText){
var templ = settings.ep_defaultPadText['*'];
if(templ)
{
var text = templ.text;
text = prepareText(text, id, number);
context.pad.setText(text);
}
}
else{ // no settings.. lazy admin!
var text = "No template found for ep_defaultPadText, please provide one!";
console.warn(text);
context.pad.setText(text);
}
}
}
function prepareText(text, id, number)
{
text = text.replace("$num$", number);
text = text.replace("$padId$", id);
// Extract the $date$-placeholder out of our template
var dateTokenRegex = /\$date:([^$]+)\$/;
var dateToken = dateTokenRegex.exec(text);
// replace the full placeholder by the current date formatted using the given format
if(dateToken) text = text.replace(dateToken[0], strftime(dateToken[1]));
return text;
}