Skip to content

Commit

Permalink
Add support for metadata in file headers
Browse files Browse the repository at this point in the history
Solves #10.
  • Loading branch information
Rasmus Bengtsson committed Jun 3, 2017
1 parent 76bba6b commit c46b8cb
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ wpPot({
Description: Name and email address of the last translator (ex: `John Doe <me@example.com>`)
Type: `string`
Default: undefined
- `metadataFile`
Description: Path to file containing plugin/theme metadata header relative to `relativeTo`
Type: `string`
Default: undefined
- `package`
Description: Package name
Type: `string`
Expand Down
37 changes: 37 additions & 0 deletions test/6-file-headers-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-env node, mocha */
/* global before, after, describe, it */
'use strict';

const assert = require('assert');
const wpPot = require('../');
const testHelper = require('./test-helper');

describe('File Headers tests', () => {
it('Can read theme headers', () => {
const fixturePath = 'test/fixtures/theme-headers.php';

const potContents = wpPot({
src: fixturePath,
writeFile: false,
metadataFile: fixturePath
});

assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':2', 'Test Theme', false, false));
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':3', 'Rasmus Bengtsson', false, false));
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':4', '1.0.0', false, false));
});

it('Can read plugin headers', () => {
const fixturePath = 'test/fixtures/plugin-headers.php';

const potContents = wpPot({
src: fixturePath,
writeFile: false,
metadataFile: fixturePath
});

assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':2', 'Test Plugin', false, false));
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':3', 'Rasmus Bengtsson', false, false));
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':4', '1.0.0', false, false));
});
});
File renamed without changes.
6 changes: 6 additions & 0 deletions test/fixtures/plugin-headers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
/*
* Plugin Name: Test Plugin
* Author: Rasmus Bengtsson
* Version: 1.0.0
*/
6 changes: 6 additions & 0 deletions test/fixtures/theme-headers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
/*
* Theme Name: Test Theme
* Author: Rasmus Bengtsson
* Version: 1.0.0
*/
43 changes: 41 additions & 2 deletions translation-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,41 @@ class TranslationParser {
this.translations = [];
}

/**
* Parse theme or plugin meta data from file header
*
* @param {string} filecontent
* @param {string} filename
*/
parseFileHeader (filecontent, filename) {
const _this = this;
const lines = filecontent.match(/[^\r\n]+/g);
lines.splice(30);

const headers = [ 'Plugin Name', 'Theme Name', 'Version', 'Author' ];

lines.forEach(function (lineContent, line) {
headers.forEach(function (header, index) {
const regex = new RegExp('^(?:[ \t]*<?php)?[ \t/*#@]*' + header + ':(.*)$', 'i');
const match = regex.exec(lineContent);

if (match) {
headers.splice(index, 1);
const headerValue = match[ 1 ].replace(/\s*(?:\*\/|\?>).*/, '').trim();

const translationCall = {
args: [ headerValue ],
filename,
line,
method: ''
};

_this.addTranslation(translationCall);
}
});
});
}

/**
* Parse comment AST
*
Expand Down Expand Up @@ -277,12 +312,16 @@ class TranslationParser {

this.translations = existingTranslations;

const filename = path.relative(this.options.relativeTo || path.dirname(this.options.destFile || __filename), filePath).replace(/\\/g, '/');

if (this.options.metadataFile === filename) {
this.parseFileHeader(filecontent, filename);
}

// Skip file if no translation functions is found
const validFunctionsInFile = new RegExp(this.options.functionCalls.valid.join('|').replace('$', '\\$'));

if (validFunctionsInFile.test(filecontent)) {
const filename = path.relative(this.options.relativeTo || path.dirname(this.options.destFile || __filename), filePath).replace(/\\/g, '/');

try {
const ast = parser.parseCode(filecontent, filename);
this.parseCodeTree(ast, filename);
Expand Down

0 comments on commit c46b8cb

Please sign in to comment.