Refactor
This commit is contained in:
54
src/misc/fa.ts
Normal file
54
src/misc/fa.ts
Normal file
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Replace fontawesome symbols
|
||||
*/
|
||||
|
||||
import * as fontawesome from '@fortawesome/fontawesome';
|
||||
import regular from '@fortawesome/fontawesome-free-regular';
|
||||
import solid from '@fortawesome/fontawesome-free-solid';
|
||||
import brands from '@fortawesome/fontawesome-free-brands';
|
||||
|
||||
fontawesome.library.add(regular, solid, brands);
|
||||
|
||||
export const pattern = /%fa:(.+?)%/g;
|
||||
|
||||
export const replacement = (match: string, key: string) => {
|
||||
const args = key.split(' ');
|
||||
let prefix = 'fas';
|
||||
const classes: string[] = [];
|
||||
let transform = '';
|
||||
let name;
|
||||
|
||||
args.forEach(arg => {
|
||||
if (arg == 'R' || arg == 'S' || arg == 'B') {
|
||||
prefix =
|
||||
arg == 'R' ? 'far' :
|
||||
arg == 'S' ? 'fas' :
|
||||
arg == 'B' ? 'fab' :
|
||||
'';
|
||||
} else if (arg[0] == '.') {
|
||||
classes.push('fa-' + arg.substr(1));
|
||||
} else if (arg[0] == '-') {
|
||||
transform = arg.substr(1).split('|').join(' ');
|
||||
} else {
|
||||
name = arg;
|
||||
}
|
||||
});
|
||||
|
||||
const icon = fontawesome.icon({ prefix, iconName: name } as fontawesome.IconLookup, {
|
||||
classes: classes,
|
||||
transform: fontawesome.parse.transform(transform)
|
||||
});
|
||||
|
||||
if (icon) {
|
||||
return `<i data-fa class="${name}">${icon.html[0]}</i>`;
|
||||
} else {
|
||||
console.warn(`'${name}' not found in fa`);
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
export default (src: string) => {
|
||||
return src.replace(pattern, replacement);
|
||||
};
|
||||
|
||||
export const fa = fontawesome;
|
@ -1,5 +1,5 @@
|
||||
import { IUser, isLocalUser } from '../models/user';
|
||||
import getAcct from '../misc/acct/render';
|
||||
import getAcct from './acct/render
|
||||
import getUserName from './get-user-name';
|
||||
|
||||
/**
|
||||
|
71
src/misc/i18n.ts
Normal file
71
src/misc/i18n.ts
Normal file
@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Replace i18n texts
|
||||
*/
|
||||
|
||||
const locale = require('../../locales');
|
||||
|
||||
export default class Replacer {
|
||||
private lang: string;
|
||||
|
||||
public pattern = /%i18n:([a-z0-9_\-\.\/\|]+?)%/g;
|
||||
|
||||
constructor(lang: string) {
|
||||
this.lang = lang;
|
||||
|
||||
this.get = this.get.bind(this);
|
||||
this.replacement = this.replacement.bind(this);
|
||||
}
|
||||
|
||||
public get(path: string, key: string): string {
|
||||
if (!(this.lang in locale)) {
|
||||
console.warn(`lang '${this.lang}' is not supported`);
|
||||
return key; // Fallback
|
||||
}
|
||||
|
||||
const texts = locale[this.lang];
|
||||
|
||||
let text = texts;
|
||||
|
||||
if (path) {
|
||||
if (text.hasOwnProperty(path)) {
|
||||
text = text[path];
|
||||
} else {
|
||||
console.warn(`path '${path}' not found in '${this.lang}'`);
|
||||
return key; // Fallback
|
||||
}
|
||||
}
|
||||
|
||||
// Check the key existance
|
||||
const error = key.split('.').some(k => {
|
||||
if (text.hasOwnProperty(k)) {
|
||||
text = text[k];
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.warn(`key '${key}' not found in '${path}' of '${this.lang}'`);
|
||||
return key; // Fallback
|
||||
} else if (typeof text !== 'string') {
|
||||
console.warn(`key '${key}' is not string in '${path}' of '${this.lang}'`);
|
||||
return key; // Fallback
|
||||
} else {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
public replacement(match: string, key: string) {
|
||||
let path = null;
|
||||
|
||||
if (key.indexOf('|') != -1) {
|
||||
path = key.split('|')[0];
|
||||
key = key.split('|')[1];
|
||||
}
|
||||
|
||||
const txt = this.get(path, key);
|
||||
|
||||
return txt.replace(/'/g, '\\x27').replace(/"/g, '\\x22');
|
||||
}
|
||||
}
|
13
src/misc/license.ts
Normal file
13
src/misc/license.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import * as fs from 'fs';
|
||||
|
||||
const license = fs.readFileSync(__dirname + '/../../LICENSE', 'utf-8');
|
||||
|
||||
const licenseHtml = license
|
||||
.replace(/\r\n/g, '\n')
|
||||
.replace(/(.)\n(.)/g, '$1 $2')
|
||||
.replace(/(^|\n)(.*?)($|\n)/g, '<p>$2</p>');
|
||||
|
||||
export {
|
||||
license,
|
||||
licenseHtml
|
||||
};
|
Reference in New Issue
Block a user