ripunzel/src/interpolate.js

161 lines
4.8 KiB
JavaScript

'use strict';
const config = require('config');
const path = require('path');
const url = require('url');
const dateFns = require('date-fns');
const mime = require('mime-types');
const format = require('template-format');
const args = require('./cli')();
function interpolate(pattern, item = null, content = null, host = null, post = null, user = null, strip = true, dateFormat = config.library.dateFormat) {
const data = {
tags: {},
};
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,
},
});
}
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),
},
});
}
if (post) {
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,
},
});
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,
},
});
}
}
const strippedData = strip
? Object.entries(data).reduce((acc, [key, value]) => {
if (typeof value === 'string') {
return {
...acc,
[key]: value ? value.toString().replace(/\//g, config.library.slashSubstitute) : '',
};
}
return {
...acc,
[key]: Object.entries(value).reduce((subacc, [subkey, subvalue]) => ({
...subacc,
[subkey]: subvalue ? subvalue.toString().replace(/\//g, config.library.slashSubstitute) : '',
}), {}),
};
}, {})
: data;
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),
});
}
const interpolated = format(pattern, {
...strippedData,
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,
});
return interpolated;
}
module.exports = interpolate;