git ssb

30+

cel / git-ssb-web



Tree: 4b685a1454e2107ae586ba5cb063bffc4b3a9771

Files: 4b685a1454e2107ae586ba5cb063bffc4b3a9771 / lib / util.js

4151 bytesRaw
1var pull = require('pull-stream')
2var Highlight = require('highlight.js')
3var u = exports
4
5u.imgMimes = {
6 png: 'image/png',
7 jpeg: 'image/jpeg',
8 jpg: 'image/jpeg',
9 gif: 'image/gif',
10 tif: 'image/tiff',
11 svg: 'image/svg+xml',
12 bmp: 'image/bmp'
13}
14
15u.getExtension = function(filename) {
16 return (/\.([^.]+)$/.exec(filename) || [,filename])[1]
17}
18
19u.readNext = function (fn) {
20 var next
21 return function (end, cb) {
22 if (next) return next(end, cb)
23 fn(function (err, _next) {
24 if (err) return cb(err)
25 next = _next
26 next(null, cb)
27 })
28 }
29}
30
31u.readOnce = function (fn) {
32 var ended
33 return function (end, cb) {
34 fn(function (err, data) {
35 if (err || ended) return cb(err || ended)
36 ended = true
37 cb(null, data)
38 })
39 }
40}
41
42u.escape = function (str) {
43 return String(str)
44 .replace(/&/g, '&')
45 .replace(/</g, '&lt;')
46 .replace(/>/g, '&gt;')
47 .replace(/"/g, '&quot;')
48}
49
50u.encodeLink = function (url) {
51 if (!Array.isArray(url)) url = [url]
52 return '/' + url.map(encodeURIComponent).join('/')
53}
54
55u.link = function (parts, text, raw, props) {
56 if (text == null) text = parts[parts.length-1]
57 if (!raw) text = u.escape(text)
58 return '<a href="' + u.encodeLink(parts) + '"' +
59 (props ? ' ' + props : '') +
60 '>' + text + '</a>'
61}
62
63u.timestamp = function (time, req) {
64 time = Number(time)
65 var d = new Date(time)
66 return '<span title="' + time + '">' +
67 d.toLocaleString(req._locale) + '</span>'
68}
69
70u.nav = function (links, page, after) {
71 return ['<nav>'].concat(
72 links.map(function (link) {
73 var href = typeof link[0] == 'string' ? link[0] : u.encodeLink(link[0])
74 var props = link[2] == page ? ' class="active"' : ''
75 return '<a href="' + href + '"' + props + '>' + link[1] + '</a>'
76 }), after || '', '</nav>').join('')
77}
78
79u.hiddenInputs = function (values) {
80 return Object.keys(values).map(function (key) {
81 return '<input type="hidden"' +
82 ' name="' + u.escape(key) + '"' +
83 ' value="' + u.escape(values[key]) + '"/>'
84 }).join('')
85}
86
87u.highlight = function(code, lang) {
88 if (code.length > 100000) return u.escape(code)
89 try {
90 return lang
91 ? Highlight.highlight(lang, code).value
92 : Highlight.highlightAuto(code).value
93 } catch(e) {
94 if (/^Unknown language/.test(e.message))
95 return u.escape(code)
96 throw e
97 }
98}
99
100u.pre = function (text) {
101 return '<pre>' + u.escape(text) + '</pre>'
102}
103
104u.json = function (obj) {
105 return u.linkify(u.pre(JSON.stringify(obj, null, 2)))
106}
107
108u.linkify = function (text) {
109 // regex is from ssb-ref
110 return text.replace(/(@|%|&|&amp;)[A-Za-z0-9\/+]{43}=\.[\w\d]+/g, function (str) {
111 return '<a href="/' + encodeURIComponent(str) + '">' + str + '</a>'
112 })
113}
114
115u.readObjectString = function (obj, cb) {
116 pull(obj.read, pull.collect(function (err, bufs) {
117 if (err) return cb(err)
118 cb(null, Buffer.concat(bufs, obj.length).toString('utf8'))
119 }))
120}
121
122u.pullReverse = function () {
123 return function (read) {
124 return u.readNext(function (cb) {
125 pull(read, pull.collect(function (err, items) {
126 cb(err, items && pull.values(items.reverse()))
127 }))
128 })
129 }
130}
131
132function compareMsgs(a, b) {
133 return (a.value.timestamp - b.value.timestamp) || (a.key - b.key)
134}
135
136u.pullSort = function (comparator, descending) {
137 return function (read) {
138 return u.readNext(function (cb) {
139 pull(read, pull.collect(function (err, items) {
140 if (err) return cb(err)
141 items.sort(comparator)
142 if (descending) items.reverse()
143 cb(null, pull.values(items))
144 }))
145 })
146 }
147}
148
149u.sortMsgs = function (descending) {
150 return u.pullSort(compareMsgs, descending)
151}
152
153u.truncate = function (str, len) {
154 str = String(str)
155 return str.length < len ? str : str.substr(0, len) + '…'
156}
157
158u.messageTitle = function (msg) {
159 var c = msg.value.content
160 return u.truncate(c.title || c.text || msg.key, 40)
161}
162
163u.ifModifiedSince = function (req, lastMod) {
164 var ifModSince = req.headers['if-modified-since']
165 if (!ifModSince) return false
166 var d = new Date(ifModSince)
167 return d && Math.floor(d/1000) >= Math.floor(lastMod/1000)
168}
169

Built with git-ssb-web