2016-03-31 21:18:08 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-29 10:26:29 +00:00
|
|
|
const marked = require('marked');
|
|
|
|
|
2016-04-07 20:54:45 +00:00
|
|
|
function* range(start=0, end=null, step=1) {
|
|
|
|
if (end == null) {
|
|
|
|
end = start;
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = start; i < end; i += step) {
|
|
|
|
yield i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 20:39:31 +00:00
|
|
|
function _formatUnits(number, base, suffixes, callback) {
|
|
|
|
if (!number && number !== 0) {
|
|
|
|
return NaN;
|
|
|
|
}
|
|
|
|
number *= 1.0;
|
|
|
|
let suffix = suffixes.shift();
|
|
|
|
while (number >= base && suffixes.length > 0) {
|
|
|
|
suffix = suffixes.shift();
|
|
|
|
number /= base;
|
|
|
|
}
|
|
|
|
if (callback === undefined) {
|
|
|
|
callback = (number, suffix) => {
|
|
|
|
return suffix ? number.toFixed(1) + suffix : number;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return callback(number, suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatFileSize(fileSize) {
|
|
|
|
return _formatUnits(
|
|
|
|
fileSize,
|
|
|
|
1024,
|
|
|
|
['B', 'K', 'M', 'G'],
|
|
|
|
(number, suffix) => {
|
|
|
|
const decimalPlaces = number < 20 && suffix !== 'B' ? 1 : 0;
|
|
|
|
return number.toFixed(decimalPlaces) + suffix;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-31 21:18:08 +00:00
|
|
|
function formatRelativeTime(timeString) {
|
|
|
|
if (!timeString) {
|
|
|
|
return 'never';
|
|
|
|
}
|
|
|
|
|
|
|
|
const then = Date.parse(timeString);
|
|
|
|
const now = Date.now();
|
|
|
|
const difference = Math.abs(now - then) / 1000.0;
|
|
|
|
const future = now < then;
|
|
|
|
|
|
|
|
const descriptions = [
|
2016-04-05 15:57:26 +00:00
|
|
|
[60, 'a few seconds', null],
|
|
|
|
[60 * 2, 'a minute', null],
|
|
|
|
[60 * 60, '% minutes', 60],
|
|
|
|
[60 * 60 * 2, 'an hour', null],
|
|
|
|
[60 * 60 * 24, '% hours', 60 * 60],
|
|
|
|
[60 * 60 * 24 * 2, 'a day', null],
|
|
|
|
[60 * 60 * 24 * 30.42, '% days', 60 * 60 * 24],
|
|
|
|
[60 * 60 * 24 * 30.42 * 2, 'a month', null],
|
|
|
|
[60 * 60 * 24 * 30.42 * 12, '% months', 60 * 60 * 24 * 30.42],
|
|
|
|
[60 * 60 * 24 * 30.42 * 12 * 2, 'a year', null],
|
|
|
|
[8640000000000000 /*max*/, '% years', 60 * 60 * 24 * 30.42 * 12],
|
2016-03-31 21:18:08 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let text = null;
|
|
|
|
for (let kv of descriptions) {
|
|
|
|
const multiplier = kv[0];
|
|
|
|
const template = kv[1];
|
|
|
|
const divider = kv[2];
|
|
|
|
if (difference < multiplier) {
|
|
|
|
text = template.replace(/%/, Math.round(difference / divider));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text === 'a day') {
|
|
|
|
return future ? 'tomorrow' : 'yesterday';
|
|
|
|
}
|
|
|
|
return future ? 'in ' + text : text + ' ago';
|
|
|
|
}
|
|
|
|
|
2016-05-29 10:26:29 +00:00
|
|
|
function formatMarkdown(text) {
|
|
|
|
const renderer = new marked.Renderer();
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
renderer: renderer,
|
|
|
|
breaks: true,
|
|
|
|
sanitize: true,
|
|
|
|
smartypants: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const sjis = [];
|
|
|
|
|
|
|
|
const preDecorator = text => {
|
|
|
|
text = text.replace(
|
|
|
|
/\[sjis\]((?:[^\[]|\[(?!\/?sjis\]))+)\[\/sjis\]/ig,
|
|
|
|
(match, capture) => {
|
|
|
|
var ret = '%%%SJIS' + sjis.length;
|
|
|
|
sjis.push(capture);
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
//prevent ^#... from being treated as headers, due to tag permalinks
|
|
|
|
text = text.replace(/^#/g, '%%%#');
|
|
|
|
//fix \ before ~ being stripped away
|
|
|
|
text = text.replace(/\\~/g, '%%%T');
|
|
|
|
//post, user and tags premalinks
|
|
|
|
text = text.replace(
|
|
|
|
/(^|^\(|(?:[^\]])\(|[\s<>\[\]\)])([+#@][a-zA-Z0-9_-]+)/g,
|
|
|
|
'$1[$2]($2)');
|
|
|
|
text = text.replace(/\]\(@(\d+)\)/g, '](#/post/$1)');
|
|
|
|
text = text.replace(/\]\(\+([a-zA-Z0-9_-]+)\)/g, '](#/user/$1)');
|
|
|
|
text = text.replace(/\]\(#([a-zA-Z0-9_-]+)\)/g, '](#/posts/query=$1)');
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
const postDecorator = text => {
|
|
|
|
//restore fixes
|
|
|
|
text = text.replace(/%%%T/g, '\\~');
|
|
|
|
text = text.replace(/%%%#/g, '#');
|
|
|
|
|
|
|
|
text = text.replace(
|
2016-06-11 15:59:25 +00:00
|
|
|
/(?:<p>)?%%%SJIS(\d+)(?:<\/p>)?/,
|
2016-05-29 10:26:29 +00:00
|
|
|
(match, capture) => {
|
|
|
|
return '<div class="sjis">' + sjis[capture] + '</div>';
|
|
|
|
});
|
|
|
|
|
|
|
|
//search permalinks
|
|
|
|
text = text.replace(
|
|
|
|
/\[search\]((?:[^\[]|\[(?!\/?search\]))+)\[\/search\]/ig,
|
|
|
|
'<a href="#/posts/query=$1"><code>$1</code></a>');
|
|
|
|
//spoilers
|
|
|
|
text = text.replace(
|
|
|
|
/\[spoiler\]((?:[^\[]|\[(?!\/?spoiler\]))+)\[\/spoiler\]/ig,
|
|
|
|
'<span class="spoiler">$1</span>');
|
|
|
|
//[small]
|
|
|
|
text = text.replace(
|
|
|
|
/\[small\]((?:[^\[]|\[(?!\/?small\]))+)\[\/small\]/ig,
|
|
|
|
'<small>$1</small>');
|
|
|
|
//strike-through
|
|
|
|
text = text.replace(/(^|[^\\])(~~|~)([^~]+)\2/g, '$1<del>$3</del>');
|
|
|
|
text = text.replace(/\\~/g, '~');
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
return postDecorator(marked(preDecorator(text), options));
|
|
|
|
}
|
|
|
|
|
2016-04-13 20:14:00 +00:00
|
|
|
function formatSearchQuery(dict) {
|
|
|
|
let result = [];
|
|
|
|
for (let key of Object.keys(dict)) {
|
|
|
|
const value = dict[key];
|
|
|
|
if (value) {
|
2016-05-20 21:59:24 +00:00
|
|
|
result.push(`${key}=${value}`);
|
2016-04-13 20:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-14 10:11:49 +00:00
|
|
|
return result.join(';');
|
2016-04-13 20:14:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 20:13:01 +00:00
|
|
|
function parseSearchQuery(query) {
|
|
|
|
let result = {};
|
|
|
|
for (let word of (query || '').split(/;/)) {
|
|
|
|
const [key, value] = word.split(/=/, 2);
|
|
|
|
result[key] = value;
|
|
|
|
}
|
|
|
|
result.text = result.text || '';
|
|
|
|
result.page = parseInt(result.page || '1');
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseSearchQueryRoute(ctx, next) {
|
|
|
|
ctx.searchQuery = parseSearchQuery(ctx.params.query || '');
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2016-05-20 21:59:24 +00:00
|
|
|
function unindent(callSite, ...args) {
|
|
|
|
function format(str) {
|
|
|
|
let size = -1;
|
|
|
|
return str.replace(/\n(\s+)/g, (m, m1) => {
|
|
|
|
if (size < 0) {
|
|
|
|
size = m1.replace(/\t/g, ' ').length;
|
|
|
|
}
|
|
|
|
return "\n" + m1.slice(Math.min(m1.length, size));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (typeof callSite === 'string') {
|
|
|
|
return format(callSite);
|
|
|
|
}
|
|
|
|
if (typeof callSite === 'function') {
|
|
|
|
return (...args) => format(callSite(...args));
|
|
|
|
}
|
|
|
|
let output = callSite
|
|
|
|
.slice(0, args.length + 1)
|
|
|
|
.map((text, i) => (i === 0 ? '' : args[i - 1]) + text)
|
|
|
|
.join('');
|
|
|
|
return format(output);
|
|
|
|
}
|
|
|
|
|
2016-04-07 20:54:45 +00:00
|
|
|
module.exports = {
|
|
|
|
range: range,
|
2016-04-13 20:14:00 +00:00
|
|
|
formatSearchQuery: formatSearchQuery,
|
2016-04-10 20:13:01 +00:00
|
|
|
parseSearchQuery: parseSearchQuery,
|
|
|
|
parseSearchQueryRoute: parseSearchQueryRoute,
|
2016-04-07 20:54:45 +00:00
|
|
|
formatRelativeTime: formatRelativeTime,
|
2016-05-22 20:39:31 +00:00
|
|
|
formatFileSize: formatFileSize,
|
2016-05-29 10:26:29 +00:00
|
|
|
formatMarkdown: formatMarkdown,
|
2016-05-20 21:59:24 +00:00
|
|
|
unindent: unindent,
|
2016-04-07 20:54:45 +00:00
|
|
|
};
|