Adding a directive parser, and refactoring code a bit
This commit is contained in:
66
src/parsers/directive.parser.ts
Normal file
66
src/parsers/directive.parser.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import {ParserInterface} from './parser.interface';
|
||||
import * as $ from 'cheerio';
|
||||
|
||||
export class DirectiveParser implements ParserInterface {
|
||||
|
||||
public patterns = {
|
||||
template: `template:\\s?(("|'|\`)(.|[\\r\\n])+?[^\\\\]\\2)`
|
||||
};
|
||||
|
||||
protected _parseTemplate(content) {
|
||||
let results: string[] = [],
|
||||
template = content.trim()
|
||||
// hack for cheerio that doesn't support wrapped attributes
|
||||
.replace('[translate]=', '__translate__=');
|
||||
|
||||
$(template).find('[translate],[__translate__]').contents().filter(function() {
|
||||
return this.nodeType === 3; // node type 3 = text node
|
||||
}).each(function() {
|
||||
let key,
|
||||
$this = $(this),
|
||||
element = $(this).parent(),
|
||||
wrappedAttr = element.attr('__translate__'), // previously [translate]=
|
||||
attr = element.attr('translate'); // translate=
|
||||
|
||||
// only support string values for now
|
||||
if(wrappedAttr && wrappedAttr.match(/^['"].*['"]$/)) {
|
||||
key = wrappedAttr.substr(1, wrappedAttr.length - 2);
|
||||
} else if(attr) {
|
||||
key = attr;
|
||||
}
|
||||
|
||||
if(!key) {
|
||||
key = $this.text().replace(/\\n/gi, '').trim();
|
||||
}
|
||||
|
||||
if(key) {
|
||||
results.push(key);
|
||||
}
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public process(contents: string): string[] {
|
||||
const regExp = new RegExp(this.patterns.template, 'gi');
|
||||
|
||||
let results: string[] = [],
|
||||
hasTemplate = false,
|
||||
matches;
|
||||
|
||||
while(matches = regExp.exec(contents)) {
|
||||
let content = matches[1]
|
||||
.substr(1, matches[1].length - 2);
|
||||
|
||||
hasTemplate = true;
|
||||
results = results.concat(this._parseTemplate(content));
|
||||
}
|
||||
|
||||
if(!hasTemplate) {
|
||||
this._parseTemplate(contents);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
|
||||
export class HtmlParser implements ParserInterface {
|
||||
export class PipeParser implements ParserInterface {
|
||||
|
||||
public patterns = {
|
||||
PipeSingleQuote: '\'((?:\\\\.|[^\'\\\\])*)\'\\s*\\|\\s*translate(:.*?)?',
|
||||
PipeDoubleQuote: '"((?:\\\\.|[^"\\\\])*)"\\s*\\|\\s*translate(:.*?)?'
|
||||
}
|
||||
pipe: `(['"\`])([^\\1\\r\\n]*)\\1\\s+\\|\\s*translate(:.*?)?`
|
||||
};
|
||||
|
||||
public process(contents: string): string[] {
|
||||
let results: string[] = [];
|
||||
@@ -15,7 +14,7 @@ export class HtmlParser implements ParserInterface {
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
results.push(matches[1]);
|
||||
results.push(matches[2]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
|
||||
export class TypescriptParser implements ParserInterface {
|
||||
export class ServiceParser implements ParserInterface {
|
||||
|
||||
public patterns = {
|
||||
TranslateServiceMethodsSingleQuote: '{{TRANSLATE_SERVICE}}\.(?:get|instant)\\s*\\\(\\s*\'((?:\\\\.|[^\'\\\\])*)\\s*\'',
|
||||
TranslateServiceMethodsDoubleQuote: '{{TRANSLATE_SERVICE}}\.(?:get|instant)\\s*\\\(\\s*"((?:\\\\.|[^"\\\\])*)\\s*"',
|
||||
}
|
||||
translateServiceMethods: `{{TRANSLATE_SERVICE}}\.(?:get|instant)\\s*\\\(\\s*(['"\`])([^\\1\\r\\n]+)\\1`,
|
||||
};
|
||||
|
||||
public process(contents: string): string[] {
|
||||
let results: string[] = [];
|
||||
@@ -22,7 +21,7 @@ export class TypescriptParser implements ParserInterface {
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
results.push(matches[1]);
|
||||
results.push(matches[2]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +48,7 @@ export class TypescriptParser implements ParserInterface {
|
||||
* 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)
|
||||
const matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i);
|
||||
if (matches === null) {
|
||||
return '';
|
||||
}
|
||||
Reference in New Issue
Block a user