Add StringCollection to make it easier to work with strings
This commit is contained in:
@@ -4,8 +4,8 @@ export abstract class AbstractTemplateParser {
|
||||
* Checks if file is of type javascript or typescript and
|
||||
* makes the assumption that it is an Angular Component
|
||||
*/
|
||||
protected _isAngularComponent(filePath: string): boolean {
|
||||
return new RegExp(/\.(ts|js)$/, 'i').test(filePath);
|
||||
protected _isAngularComponent(path: string): boolean {
|
||||
return new RegExp(/\.(ts|js)$/, 'i').test(path);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractTemplateParser } from './abstract-template.parser';
|
||||
import { StringCollection } from '../utils/string.collection';
|
||||
|
||||
import * as $ from 'cheerio';
|
||||
|
||||
export class DirectiveParser extends AbstractTemplateParser implements ParserInterface {
|
||||
|
||||
public process(filePath: string, contents: string): string[] {
|
||||
if (this._isAngularComponent(filePath)) {
|
||||
public extract(contents: string, path?: string): StringCollection {
|
||||
if (path && this._isAngularComponent(path)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
}
|
||||
|
||||
return this._parseTemplate(contents);
|
||||
}
|
||||
|
||||
protected _parseTemplate(template: string): string[] {
|
||||
let results: string[] = [];
|
||||
protected _parseTemplate(template: string): StringCollection {
|
||||
const collection = new StringCollection();
|
||||
|
||||
template = this._normalizeTemplateAttributes(template);
|
||||
$(template)
|
||||
@@ -25,7 +26,7 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
|
||||
const attr = $element.attr('translate') || $element.attr('ng2-translate');
|
||||
|
||||
if (attr) {
|
||||
results.push(attr);
|
||||
collection.add(attr);
|
||||
} else {
|
||||
$element
|
||||
.contents()
|
||||
@@ -33,11 +34,11 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
|
||||
.filter(textNode => textNode.type === 'text')
|
||||
.map(textNode => textNode.nodeValue.trim())
|
||||
.filter(text => text.length > 0)
|
||||
.forEach(text => results.push(text));
|
||||
.forEach(text => collection.add(text));
|
||||
}
|
||||
});
|
||||
|
||||
return results;
|
||||
return collection;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { StringCollection } from '../utils/string.collection';
|
||||
|
||||
export interface ParserInterface {
|
||||
|
||||
process(filePath: string, contents: string): string[];
|
||||
extract(contents: string, path?: string): StringCollection;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,27 +1,28 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractTemplateParser } from './abstract-template.parser';
|
||||
import { StringCollection } from '../utils/string.collection';
|
||||
|
||||
export class PipeParser extends AbstractTemplateParser implements ParserInterface {
|
||||
|
||||
public process(filePath: string, contents: string): string[] {
|
||||
if (this._isAngularComponent(filePath)) {
|
||||
public extract(contents: string, path?: string): StringCollection {
|
||||
if (path && this._isAngularComponent(path)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
}
|
||||
|
||||
return this._parseTemplate(contents);
|
||||
}
|
||||
|
||||
protected _parseTemplate(template: string): string[] {
|
||||
let results: string[] = [];
|
||||
protected _parseTemplate(template: string): StringCollection {
|
||||
const collection = new StringCollection();
|
||||
|
||||
const regExp = new RegExp(/([\'"`])([^\1\r\n]*)\1\s+\|\s*translate(:.*?)?/, 'g');
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(template)) {
|
||||
results.push(matches[2]);
|
||||
collection.add(matches[2]);
|
||||
}
|
||||
|
||||
return results;
|
||||
return collection;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { StringCollection } from '../utils/string.collection';
|
||||
|
||||
export class ServiceParser implements ParserInterface {
|
||||
|
||||
public process(filePath: string, contents: string): string[] {
|
||||
let results: string[] = [];
|
||||
public extract(contents: string, path?: string): StringCollection {
|
||||
const collection = new StringCollection();
|
||||
|
||||
const translateServiceVar = this._extractTranslateServiceVar(contents);
|
||||
if (!translateServiceVar) {
|
||||
return results;
|
||||
return collection;
|
||||
}
|
||||
|
||||
const methodRegExp: RegExp = new RegExp(/(?:get|instant)\s*\(\s*(\[?([\'"`])([^\1\r\n]+)\2\]?)/);
|
||||
@@ -16,13 +17,13 @@ export class ServiceParser implements ParserInterface {
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
if (this._stringContainsArray(matches[1])) {
|
||||
results.push(...this._stringToArray(matches[1]));
|
||||
collection.add(this._stringToArray(matches[1]));
|
||||
} else {
|
||||
results.push(matches[3]);
|
||||
collection.add(matches[3]);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user