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