forked from microjs/microjs.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.js
70 lines (60 loc) · 2.03 KB
/
atom.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
const uuid = require('node-uuid')
, fs = require('fs')
, memo = require('async-memo')
, moment = require('moment')
, xml = require('./xml')
const file = 'atom.xml'
, footRegex = /(\n<\/feed>\n?)/
, dateStr = moment.utc().format('YYYY-MM-DD\'T\'HH:mm:ssZ')
, entryTemplate =
'\t<entry>\n'
+ '\t\t<title>{xmlEncodedName}</title>\n'
+ '\t\t<link href="{url}" />\n'
+ '\t\t<id>urn:uuid:{atomUuid}</id>\n'
+ '\t\t<updated>{atomDate}</updated>\n'
+ '\t\t<summary>{xmlEncodedDescription}</summary>\n'
+ '\t</entry>\n\n'
+ '$1'
// memoize so we only read once, but keep contents global so we can edit it
, fileContents = memo(
function (callback) {
fs.readFile(file, 'utf-8', function (err, _contents) {
contents = _contents
callback(err)
})
})
var contents
function escapeRegExp (string) {
return string.replace(/[\-\[\]\{\}\(\)*+?.\\\^$|,#\s]/g, function (match) { return '\\' + match })
}
function exists (contents, lib) {
return new RegExp(escapeRegExp('<title>' + lib.xmlEncodedName + '</title>')).test(contents)
}
function t (s, d) {
for (var p in d)
s = s.replace(new RegExp('{' + p + '}', 'g'), d[p])
return s
}
function processLibrary (lib, callback) {
fileContents(function (err) {
if (err)
throw err
lib.xmlEncodedName = xml.encodeXmlEntities(lib.name)
if (exists(contents, lib))
return callback()
lib.atomUuid = uuid.v1()
lib.atomDate = dateStr
lib.xmlEncodedDescription = xml.encodeXmlEntities(lib.description)
contents = contents.replace(footRegex, t(entryTemplate, lib))
;delete lib.atomUuid
;delete lib.atomDate
;delete lib.xmlEncodedName
;delete lib.xmlEncodedDescription
callback()
})
}
function write (callback) {
fs.writeFile(file, contents, 'utf-8', callback)
}
module.exports.processLibrary = processLibrary
module.exports.write = write