2018-04-17 22:18:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
2018-04-22 21:46:14 +00:00
|
|
|
const path = require('path');
|
|
|
|
const url = require('url');
|
2018-04-17 22:18:04 +00:00
|
|
|
const dateFns = require('date-fns');
|
2018-05-10 23:46:08 +00:00
|
|
|
const mime = require('mime-types');
|
2019-11-01 03:22:36 +00:00
|
|
|
const format = require('template-format');
|
2018-04-17 22:18:04 +00:00
|
|
|
|
2019-11-02 01:19:23 +00:00
|
|
|
const args = require('./cli')();
|
|
|
|
|
2019-11-01 03:22:36 +00:00
|
|
|
function interpolate(pattern, item = null, content = null, host = null, post = null, user = null, strip = true, dateFormat = config.library.dateFormat) {
|
|
|
|
const data = {
|
|
|
|
tags: {},
|
2018-04-24 20:25:36 +00:00
|
|
|
};
|
2018-04-22 21:46:14 +00:00
|
|
|
|
2019-11-01 03:22:36 +00:00
|
|
|
if (item) {
|
|
|
|
Object.assign(data, {
|
|
|
|
item: {
|
|
|
|
id: item.id,
|
|
|
|
title: item.title && item.title.slice(0, config.library.titleLength),
|
|
|
|
description: item.description,
|
|
|
|
date: dateFns.format(item.datetime, dateFormat),
|
|
|
|
index: item.index + config.library.indexOffset,
|
|
|
|
},
|
|
|
|
ext: item.type
|
|
|
|
? `.${mime.extension(item.type)}`
|
|
|
|
: path.extname(url.parse(item.url).pathname),
|
|
|
|
tags: {
|
|
|
|
...data.tags,
|
|
|
|
extracted: item.extracted && config.library.tags.extracted,
|
|
|
|
},
|
2018-04-22 21:46:14 +00:00
|
|
|
});
|
2019-11-01 03:22:36 +00:00
|
|
|
}
|
2018-04-23 12:57:07 +00:00
|
|
|
|
2019-11-01 03:22:36 +00:00
|
|
|
if (host) {
|
|
|
|
Object.assign(data, {
|
|
|
|
host: {
|
|
|
|
id: host.id,
|
|
|
|
label: host.label,
|
|
|
|
name: host.label,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content && content.album) {
|
|
|
|
Object.assign(data, {
|
|
|
|
album: {
|
|
|
|
id: content.album.id,
|
|
|
|
title: content.album.title && content.album.title.slice(0, config.library.titleLength),
|
|
|
|
description: content.album.description,
|
|
|
|
date: dateFns.format(content.album.datetime, dateFormat),
|
|
|
|
},
|
|
|
|
});
|
2018-04-22 21:46:14 +00:00
|
|
|
}
|
2018-04-17 22:18:04 +00:00
|
|
|
|
2018-06-14 23:06:31 +00:00
|
|
|
if (post) {
|
2019-11-01 03:22:36 +00:00
|
|
|
Object.assign(data, {
|
|
|
|
post: {
|
|
|
|
id: post.id,
|
|
|
|
title: post.title && post.title.slice(0, config.library.titleLength),
|
|
|
|
url: post.url,
|
|
|
|
date: dateFns.format(post.datetime, dateFormat),
|
|
|
|
index: post.index + config.library.indexOffset,
|
|
|
|
hash: post.hash,
|
|
|
|
score: post.score,
|
|
|
|
subreddit: post.subreddit,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
Object.assign(data, {
|
|
|
|
user: {
|
|
|
|
name: user.name,
|
|
|
|
username: user.name,
|
|
|
|
id: user.id,
|
|
|
|
created: dateFns.format(user.created, dateFormat),
|
|
|
|
},
|
|
|
|
tags: {
|
|
|
|
...data.tags,
|
|
|
|
verified: user.verified && config.library.tags.verified,
|
|
|
|
verifiedEmail: user.verifiedEmail && config.library.tags.verifiedEmail,
|
|
|
|
gold: user.gold && config.library.tags.gold,
|
|
|
|
},
|
2018-04-17 22:18:04 +00:00
|
|
|
});
|
2018-04-22 21:46:14 +00:00
|
|
|
|
2019-11-01 03:22:36 +00:00
|
|
|
if (user.profile) {
|
|
|
|
Object.assign(data, {
|
|
|
|
profile: {
|
|
|
|
id: user.profile.id,
|
|
|
|
title: user.profile.title,
|
|
|
|
description: user.profile.description,
|
|
|
|
},
|
|
|
|
tags: {
|
|
|
|
...data.tags,
|
|
|
|
over18: user.profile.over18 && config.library.tags.over18,
|
|
|
|
},
|
2018-04-22 21:46:14 +00:00
|
|
|
});
|
|
|
|
}
|
2018-04-17 22:18:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 03:22:36 +00:00
|
|
|
const strippedData = strip
|
|
|
|
? Object.entries(data).reduce((acc, [key, value]) => {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
return {
|
|
|
|
...acc,
|
2019-11-02 01:19:23 +00:00
|
|
|
[key]: value ? value.toString().replace(/\//g, config.library.slashSubstitute) : '',
|
2019-11-01 03:22:36 +00:00
|
|
|
};
|
|
|
|
}
|
2018-04-17 22:18:04 +00:00
|
|
|
|
2019-11-01 03:22:36 +00:00
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[key]: Object.entries(value).reduce((subacc, [subkey, subvalue]) => ({
|
|
|
|
...subacc,
|
2019-11-02 01:19:23 +00:00
|
|
|
[subkey]: subvalue ? subvalue.toString().replace(/\//g, config.library.slashSubstitute) : '',
|
2019-11-01 03:22:36 +00:00
|
|
|
}), {}),
|
|
|
|
};
|
|
|
|
}, {})
|
|
|
|
: data;
|
|
|
|
|
2019-11-02 01:19:23 +00:00
|
|
|
const dividers = Object.entries(data).reduce((acc, [key, value]) => {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[key]: value ? config.library.divider : '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[key]: Object.entries(value).reduce((subacc, [subkey, subvalue]) => ({
|
|
|
|
...subacc,
|
|
|
|
[subkey]: subvalue ? config.library.divider : '',
|
|
|
|
}), {}),
|
|
|
|
};
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
if (args.label) {
|
|
|
|
Object.assign(strippedData, {
|
|
|
|
label: format(args.label, strippedData),
|
|
|
|
});
|
|
|
|
}
|
2019-11-01 03:22:36 +00:00
|
|
|
|
|
|
|
const interpolated = format(pattern, {
|
|
|
|
...strippedData,
|
2019-11-02 01:19:23 +00:00
|
|
|
base: {
|
|
|
|
posts: format(args.base || config.library.base.posts, strippedData),
|
|
|
|
direct: format(args.base || config.library.base.direct, strippedData),
|
|
|
|
},
|
|
|
|
dividers,
|
|
|
|
divider: config.library.divider,
|
|
|
|
divs: dividers,
|
|
|
|
div: config.library.divider,
|
2019-11-01 03:22:36 +00:00
|
|
|
});
|
2018-04-17 22:18:04 +00:00
|
|
|
|
2019-11-01 03:22:36 +00:00
|
|
|
return interpolated;
|
2018-06-14 23:06:31 +00:00
|
|
|
}
|
2018-04-17 22:18:04 +00:00
|
|
|
|
|
|
|
module.exports = interpolate;
|