Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pug support #158

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Options:
[array] [default: current working path]
[array] [required] [default: current working path]
--patterns, -p Extract strings from the following file patterns
[array] [default: ["/**/*.html","/**/*.ts"]]
[array] [default: ["/**/*.html", "/**/*.ts", "/**/*.pug"]
--output, -o Paths where you would like to save extracted
strings. You can use path expansion, glob
patterns and multiple paths [array] [required]
Expand Down
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
"watch": "npm run clean && tsc --watch",
"clean": "rm -rf ./dist",
"lint": "tslint --force './src/**/*.ts'",
"test": "mocha -r ts-node/register tests/**/*.spec.ts"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm test"
}
"test": "mocha -r ts-node/register tests/**/*.spec.ts",
"dev": "ts-node -D 7016 src -p '/**/*.html' -p '/**/*.pug' -p '/**/*.ts' --input /home/ido/projects/angular/ui-streaming-scrnz/src --output /home/ido/projects/angular/ui-streaming-scrnz/src/assets/i18n/*.json --clean --sort --format namespaced-json"
},
"prettier": {
"trailingComma": "none",
Expand Down Expand Up @@ -93,6 +89,7 @@
"glob": "^7.1.4",
"mkdirp": "^0.5.1",
"path": "^0.12.7",
"pug": "^2.0.4",
"terminal-link": "^2.0.0",
"typescript": "^3.6.3",
"yargs": "^14.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const cli = yargs
alias: 'p',
describe: 'Extract strings from the following file patterns',
type: 'array',
default: ['/**/*.html', '/**/*.ts']
default: ['/**/*.html', '/**/*.ts', '/**/*.pug']
})
.option('output', {
alias: 'o',
Expand Down
20 changes: 13 additions & 7 deletions src/parsers/directive.parser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ParserInterface } from './parser.interface';
import { TranslationCollection } from '../utils/translation.collection';
import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
import { TranslationCollection } from '..';
import { isPathAngularComponent, extractComponentInlineTemplate, pugConverterParser } from '..';

import { parseTemplate, TmplAstNode, TmplAstElement, TmplAstTextAttribute } from '@angular/compiler';

export class DirectiveParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null {
source = pugConverterParser(source, filePath);

if (filePath && isPathAngularComponent(filePath)) {
source = extractComponentInlineTemplate(source);
}
Expand All @@ -14,7 +16,14 @@ export class DirectiveParser implements ParserInterface {

const nodes: TmplAstNode[] = this.parseTemplate(source, filePath);
this.getTranslatableElements(nodes).forEach(element => {
const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element);
const getElementTranslateAttrValue = this.getElementTranslateAttrValue(element); // translate
const getElementContents = this.getElementContents(element);
let key;
if (getElementTranslateAttrValue && getElementContents) {
key = getElementTranslateAttrValue === 'translate' ? getElementContents : getElementTranslateAttrValue;
} else {
key = getElementTranslateAttrValue || getElementContents;
}
collection = collection.add(key);
});

Expand Down Expand Up @@ -62,10 +71,7 @@ export class DirectiveParser implements ParserInterface {
}

protected isTranslatable(node: TmplAstNode): boolean {
if (this.isElement(node) && node.attributes.some(attribute => attribute.name === 'translate')) {
return true;
}
return false;
return this.isElement(node) && node.attributes.some(attribute => attribute.name === 'translate');
}

protected getElementTranslateAttrValue(element: TmplAstElement): string {
Expand Down
2 changes: 2 additions & 0 deletions src/parsers/marker.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { tsquery } from '@phenomnomnominal/tsquery';
import { ParserInterface } from './parser.interface';
import { TranslationCollection } from '../utils/translation.collection';
import { getNamedImportAlias, findFunctionCallExpressions, getStringsFromExpression } from '../utils/ast-helpers';
import { pugConverterParser } from '../utils/utils';

const MARKER_MODULE_NAME = '@biesbjerg/ngx-translate-extract-marker';
const MARKER_IMPORT_NAME = 'marker';

export class MarkerParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null {
source = pugConverterParser(source, filePath);
const sourceFile = tsquery.ast(source, filePath);

const markerImportName = getNamedImportAlias(sourceFile, MARKER_MODULE_NAME, MARKER_IMPORT_NAME);
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/pipe.parser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ParserInterface } from './parser.interface';
import { TranslationCollection } from '../utils/translation.collection';
import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
import { isPathAngularComponent, extractComponentInlineTemplate, pugConverterParser } from '../utils/utils';

export class PipeParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null {
if (filePath && isPathAngularComponent(filePath)) {
source = extractComponentInlineTemplate(source);
}

source = pugConverterParser(source, filePath);
return this.parseTemplate(source);
}

Expand Down
2 changes: 2 additions & 0 deletions src/parsers/service.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { tsquery } from '@phenomnomnominal/tsquery';
import { ParserInterface } from './parser.interface';
import { TranslationCollection } from '../utils/translation.collection';
import { findClassDeclarations, findClassPropertyByType, findMethodCallExpressions, getStringsFromExpression } from '../utils/ast-helpers';
import { pugConverterParser } from '../utils/utils';

const TRANSLATE_SERVICE_TYPE_REFERENCE = 'TranslateService';
const TRANSLATE_SERVICE_METHOD_NAMES = ['get', 'instant', 'stream'];

export class ServiceParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null {
source = pugConverterParser(source, filePath);
const sourceFile = tsquery.ast(source, filePath);

const classDeclarations = findClassDeclarations(sourceFile);
Expand Down
20 changes: 20 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ export function isPathAngularComponent(path: string): boolean {
return /\.ts|js$/i.test(path);
}

export function pugConverterParser(source: string, filePath: string) {
function isPathPugComponent(path: string): boolean {
return /\.pug/i.test(path);
}

let isPugModuleInstalled: boolean;
let pug: any;
try {
pug = require('pug');
isPugModuleInstalled = true;
} catch (err) {
isPugModuleInstalled = false;
}

if (isPathPugComponent(filePath) && isPugModuleInstalled) {
return pug.renderFile(filePath);
}
return source;
}

/**
* Extract inline template from a component
*/
Expand Down
Loading