1
0
mirror of https://github.com/GenderDysphoria/GenderDysphoria.fyi.git synced 2025-11-25 12:32:42 +00:00

Added tweet translation mechanism (#96)

* Added tweet translation mechanism.

* Removed test translation.

It was done via Google Translate which I don't trust for this application.
This commit is contained in:
G Queiroz
2022-06-01 00:06:53 -03:00
committed by GitHub
parent 3687239cd9
commit 93743fc532
15 changed files with 1363 additions and 46 deletions

View File

@@ -13,6 +13,9 @@ const schema = {
protected: true,
},
html: true,
html_i18n: true,
full_text: true,
full_text_i18n: true,
quoted_status_id_str: true,
entities: { media: [ {
type: true,
@@ -31,6 +34,12 @@ var entityProcessors = {
hashtags (tags, tweet) {
tags.forEach((tagObj) => {
tweet.html = tweet.html.replace('#' + tagObj.text, `<a href="https://twitter.com/hashtag/{tagObj.text}" class="hashtag">#${tagObj.text}</a>`);
if (tweet.html_i18n !== undefined) {
const langs = Object.keys(tweet.html_i18n);
for (const lang of langs) {
tweet.html_i18n[lang] = tweet.html_i18n[lang].replace('#' + tagObj.text, `<a href="https://twitter.com/hashtag/{tagObj.text}" class="hashtag">#${tagObj.text}</a>`);
}
}
});
},
@@ -41,7 +50,14 @@ var entityProcessors = {
user_mentions (users, tweet) {
users.forEach((userObj) => {
var regex = new RegExp('@' + userObj.screen_name, 'gi' );
tweet.html = tweet.html.replace(regex, `<a href="https://twitter.com/${userObj.screen_name}" class="mention">@${userObj.screen_name}</a>`);
const mention_html = `<a href="https://twitter.com/${userObj.screen_name}" class="mention">@${userObj.screen_name}</a>`;
tweet.html = tweet.html.replace(regex, mention_html);
if (tweet.html_i18n !== undefined) {
const langs = Object.keys(tweet.html_i18n);
for (const lang of langs) {
tweet.html_i18n[lang] = tweet.html_i18n[lang].replace(regex, mention_html);
}
}
});
},
@@ -49,13 +65,26 @@ var entityProcessors = {
urls.forEach(({ url, expanded_url, display_url }) => {
const isQT = tweet.quoted_status_permalink && url === tweet.quoted_status_permalink.url;
const className = isQT ? 'quoted-tweet' : 'url';
tweet.html = tweet.html.replace(url, isQT ? '' : `<a href="${expanded_url}" class="${className}">${display_url}</a>`);
const fancy_html = `<a href="${expanded_url}" class="${className}">${display_url}</a>`;
tweet.html = tweet.html.replace(url, isQT ? '' : fancy_html);
if (tweet.html_i18n !== undefined) {
const langs = Object.keys(tweet.html_i18n);
for (const lang of langs) {
tweet.html_i18n[lang] = tweet.html_i18n[lang].replace(url, isQT ? '' : fancy_html);
}
}
});
},
media (media, tweet) {
media.forEach((m) => {
tweet.html = tweet.html.replace(m.url, '');
if (tweet.html_i18n !== undefined) {
const langs = Object.keys(tweet.html_i18n);
for (const lang of langs) {
tweet.html_i18n[lang] = tweet.html_i18n[lang].replace(m.url, '');
}
}
let width, height;
if (has(m, 'video_info.aspect_ratio')) {
@@ -90,6 +119,13 @@ var entityProcessors = {
module.exports = exports = function (tweets) {
return tweets.length ? tweets.map(parseTweet) : parseTweet(tweets);
function parseStep1 (text) {
return text.split(/(\r\n|\n\r|\r|\n)+/)
.map((s) => s.trim() && '<p>' + s + '</p>')
.filter(Boolean)
.join('');
}
function parseTweet (tweet) {
// clone the tweet so we're not altering the original
tweet = JSON.parse(JSON.stringify(tweet));
@@ -105,11 +141,29 @@ module.exports = exports = function (tweets) {
];
// Copying text value to a new property html. The final output will be set to this property
tweet.html = (tweet.full_text || tweet.text)
.split(/(\r\n|\n\r|\r|\n)+/)
.map((s) => s.trim() && '<p>' + s + '</p>')
.filter(Boolean)
.join('');
if (tweet.full_text !== undefined || tweet.text !== undefined) {
tweet.html = parseStep1(tweet.full_text || tweet.text);
}
if (tweet.html_i18n === undefined) {
tweet.html_i18n = {};
}
if (tweet.full_text_i18n === undefined) {
tweet.full_text_i18n = {};
}
// Find which languages we actually have translations for
const possible_langs = Object.keys(tweet.full_text_i18n);
const langs = [];
for (const lang of possible_langs) {
const trimed = tweet.full_text_i18n[lang].trim();
if (trimed.length > 0) {
langs.push(lang);
}
}
for (const lang of langs) {
tweet.html_i18n[lang] = parseStep1(tweet.full_text_i18n[lang]);
}
if (tweet.quoted_status) {
tweet.quoted_status = parseTweet(tweet.quoted_status);
@@ -160,8 +214,15 @@ module.exports = exports = function (tweets) {
}
// Process Emoji's
tweet.html = twemoji.parse(tweet.html);
tweet.user.name_html = twemoji.parse(tweet.user.name);
if (tweet.html) {
tweet.html = twemoji.parse(tweet.html);
}
for (const lang of langs) {
tweet.html_i18n[lang] = twemoji.parse(tweet.html_i18n[lang]);
}
if (tweet.user !== undefined && tweet.user.name !== undefined) {
tweet.user.name_html = twemoji.parse(tweet.user.name);
}
return deepPick(tweet, schema);
}