Initial commit

This commit is contained in:
Kim Biesbjerg
2016-12-03 15:09:39 +01:00
parent 916ba888aa
commit b5e1125efb
12 changed files with 342 additions and 1 deletions

View 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;
}
}

View File

@@ -0,0 +1,5 @@
export interface ParserInterface {
process(contents: string): string[];
}

View 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];
}
}