git ssb

30+

cel / git-ssb-web



Tree: 53f3bbbeeaf17e263b3c4ec1a5df0c9493d8a33c

Files: 53f3bbbeeaf17e263b3c4ec1a5df0c9493d8a33c / lib / util.js

3638 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 try {
89 return lang
90 ? Highlight.highlight(lang, code).value
91 : Highlight.highlightAuto(code).value
92 } catch(e) {
93 if (/^Unknown language/.test(e.message))
94 return u.escape(code)
95 throw e
96 }
97}
98
99u.pre = function (text) {
100 return '<pre>' + u.escape(text) + '</pre>'
101}
102
103u.json = function (obj) {
104 return u.linkify(u.pre(JSON.stringify(obj, null, 2)))
105}
106
107u.linkify = function (text) {
108 // regex is from ssb-ref
109 return text.replace(/(@|%|&|&amp;)[A-Za-z0-9\/+]{43}=\.[\w\d]+/g, function (str) {
110 return '<a href="/' + encodeURIComponent(str) + '">' + str + '</a>'
111 })
112}
113
114u.readObjectString = function (obj, cb) {
115 pull(obj.read, pull.collect(function (err, bufs) {
116 if (err) return cb(err)
117 cb(null, Buffer.concat(bufs, obj.length).toString('utf8'))
118 }))
119}
120
121u.pullReverse = function () {
122 return function (read) {
123 return u.readNext(function (cb) {
124 pull(read, pull.collect(function (err, items) {
125 cb(err, items && pull.values(items.reverse()))
126 }))
127 })
128 }
129}
130
131function compareMsgs(a, b) {
132 return (a.value.timestamp - b.value.timestamp) || (a.key - b.key)
133}
134
135u.pullSort = function (comparator, descending) {
136 return function (read) {
137 return u.readNext(function (cb) {
138 pull(read, pull.collect(function (err, items) {
139 if (err) return cb(err)
140 items.sort(comparator)
141 if (descending) items.reverse()
142 cb(null, pull.values(items))
143 }))
144 })
145 }
146}
147
148u.sortMsgs = function (descending) {
149 return u.pullSort(compareMsgs, descending)
150}
151

Built with git-ssb-web