Initial commit
This commit is contained in:
25
src/parsers/html.parser.ts
Normal file
25
src/parsers/html.parser.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
|
||||
export class HtmlParser implements ParserInterface {
|
||||
|
||||
public patterns = {
|
||||
PipeSingleQuote: '{{\\s*\'((?:\\\\.|[^\'\\\\])*)\'\\s*\\|\\s*translate(:.*?)?\\s*}}',
|
||||
PipeDoubleQuote: '{{\\s*"((?:\\\\.|[^"\\\\])*)"\\s*\\|\\s*translate(:.*?)?\\s*}}'
|
||||
}
|
||||
|
||||
public process(contents: string): string[] {
|
||||
let results: string[] = [];
|
||||
|
||||
for (let patternName in this.patterns) {
|
||||
const regExp = new RegExp(this.patterns[patternName], 'g');
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
results.push(matches[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
5
src/parsers/parser.interface.ts
Normal file
5
src/parsers/parser.interface.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export interface ParserInterface {
|
||||
|
||||
process(contents: string): string[];
|
||||
|
||||
}
|
||||
60
src/parsers/typescript.parser.ts
Normal file
60
src/parsers/typescript.parser.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
|
||||
export class TypescriptParser implements ParserInterface {
|
||||
|
||||
public patterns = {
|
||||
TranslateServiceMethodsSingleQuote: '{{TRANSLATE_SERVICE}}\.(?:get|instant)\\s*\\\(\\s*\'((?:\\\\.|[^\'\\\\])*)\\s*\'',
|
||||
TranslateServiceMethodsDoubleQuote: '{{TRANSLATE_SERVICE}}\.(?:get|instant)\\s*\\\(\\s*"((?:\\\\.|[^"\\\\])*)\\s*"',
|
||||
}
|
||||
|
||||
public process(contents: string): string[] {
|
||||
let results: string[] = [];
|
||||
|
||||
const translateServiceVar = this._extractTranslateServiceVar(contents);
|
||||
if (!translateServiceVar) {
|
||||
return [];
|
||||
}
|
||||
|
||||
for (let patternName in this.patterns) {
|
||||
const regExp = this._createRegExp(patternName, {
|
||||
TRANSLATE_SERVICE: translateServiceVar
|
||||
});
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
results.push(matches[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create regular expression, replacing placeholders with real values
|
||||
*/
|
||||
protected _createRegExp(patternName: string, replaceVars: {} = {}): RegExp {
|
||||
if (!this.patterns.hasOwnProperty(patternName)) {
|
||||
throw new Error('Invalid pattern name');
|
||||
}
|
||||
|
||||
let pattern = this.patterns[patternName];
|
||||
Object.keys(replaceVars).forEach(key => {
|
||||
pattern = pattern.replace('{{' + key + '}}', replaceVars[key]);
|
||||
});
|
||||
|
||||
return new RegExp(pattern, 'gi');
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract name of TranslateService variable for use in patterns
|
||||
*/
|
||||
protected _extractTranslateServiceVar(contents: string): string {
|
||||
const matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i)
|
||||
if (matches === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return matches[1];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user