ChangelogImporter.js
1.91 KB
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
module.exports = function(host, filename, forceFetch) {
this.filename = filename
this.host = host
this.forceFetch = forceFetch
}
module.exports.prototype = {
markdown: require('markdown'),
changelogFile: __dirname + "/../../views/partials/changelog.ejs",
fs: require("fs"),
fetchChangelog: function(callback) {
var self = this
, https = require("https")
, changelog = []
https.get({ host: this.host, path: this.filename }, function(res) {
res.setEncoding('utf8')
res.on('data', function(data) { changelog.push(data.toString()) })
res.on("end", function() { callback(changelog.join("")) })
}).on("error", function(err) {
console.log(err)
})
},
transformChangelog: function(markdownChangelog) {
return this.markdown
.toHTML(markdownChangelog)
.replace(/h1/g, 'h2')
.replace(/<ul>/g, "<div class='pre_like'><ul>")
.replace(/<\/ul>/g, "</ul></div>")
},
writeChangelog: function(callback) {
var self = this
this.fetchChangelog(function(data) {
var dataParts = data.toString().split(/# (.*?) #/).reverse(),
changelog = []
dataParts.pop()
for(var i = 0; i < dataParts.length; i++) {
if(i%2 != 1) continue
var headline = "# " + dataParts[i] + " #\n",
content = dataParts[i-1].replace (/^\s+/, '').replace (/\s+$/, '')
changelog.push('<div><a name="' + dataParts[i] + '"></a>' + self.transformChangelog(headline + content) + '<br></div>')
}
self.fs.writeFile(self.changelogFile, changelog.join('<div class="seperator"></div>'), callback)
})
},
run: function() {
var self = this,
sys = require("sys")
sys.log("Changelog will be updated now!")
self.writeChangelog(function() {
sys.log("Changelog written!")
})
}
}
/*
Call it:
var changelogImporter = new ChangelogImporter(filename, host)
changelogImporter.run()
*/