Files: 8bbc500d10ef5932154209b21315dcd393632943 / lib / util.js
3926 bytesRaw
1 | var pull = require('pull-stream') |
2 | var Highlight = require('highlight.js') |
3 | var u = exports |
4 | |
5 | u.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 | |
15 | u.getExtension = function(filename) { |
16 | return (/\.([^.]+)$/.exec(filename) || [,filename])[1] |
17 | } |
18 | |
19 | u.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 | |
31 | u.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 | |
42 | u.escape = function (str) { |
43 | return String(str) |
44 | .replace(/&/g, '&') |
45 | .replace(/</g, '<') |
46 | .replace(/>/g, '>') |
47 | .replace(/"/g, '"') |
48 | } |
49 | |
50 | u.encodeLink = function (url) { |
51 | if (!Array.isArray(url)) url = [url] |
52 | return '/' + url.map(encodeURIComponent).join('/') |
53 | } |
54 | |
55 | u.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 | |
63 | u.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 | |
70 | u.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 | |
79 | u.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 | |
87 | u.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 | |
100 | u.pre = function (text) { |
101 | return '<pre>' + u.escape(text) + '</pre>' |
102 | } |
103 | |
104 | u.json = function (obj) { |
105 | return u.linkify(u.pre(JSON.stringify(obj, null, 2))) |
106 | } |
107 | |
108 | u.linkify = function (text) { |
109 | // regex is from ssb-ref |
110 | return text.replace(/(@|%|&|&)[A-Za-z0-9\/+]{43}=\.[\w\d]+/g, function (str) { |
111 | return '<a href="/' + encodeURIComponent(str) + '">' + str + '</a>' |
112 | }) |
113 | } |
114 | |
115 | u.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 | |
122 | u.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 | |
132 | function compareMsgs(a, b) { |
133 | return (a.value.timestamp - b.value.timestamp) || (a.key - b.key) |
134 | } |
135 | |
136 | u.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 | |
149 | u.sortMsgs = function (descending) { |
150 | return u.pullSort(compareMsgs, descending) |
151 | } |
152 | |
153 | u.truncate = function (str, len) { |
154 | str = String(str) |
155 | return str.length < len ? str : str.substr(0, len) + '…' |
156 | } |
157 | |
158 | u.messageTitle = function (msg) { |
159 | var c = msg.value.content |
160 | return u.truncate(c.title || c.text || msg.key, 40) |
161 | } |
162 |
Built with git-ssb-web