Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Commit c46b8cb

Browse files
author
Rasmus Bengtsson
committed
Add support for metadata in file headers
Solves #10.
1 parent 76bba6b commit c46b8cb

File tree

6 files changed

+94
-2
lines changed

6 files changed

+94
-2
lines changed

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ wpPot({
6161
Description: Name and email address of the last translator (ex: `John Doe <me@example.com>`)
6262
Type: `string`
6363
Default: undefined
64+
- `metadataFile`
65+
Description: Path to file containing plugin/theme metadata header relative to `relativeTo`
66+
Type: `string`
67+
Default: undefined
6468
- `package`
6569
Description: Package name
6670
Type: `string`

test/6-file-headers-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-env node, mocha */
2+
/* global before, after, describe, it */
3+
'use strict';
4+
5+
const assert = require('assert');
6+
const wpPot = require('../');
7+
const testHelper = require('./test-helper');
8+
9+
describe('File Headers tests', () => {
10+
it('Can read theme headers', () => {
11+
const fixturePath = 'test/fixtures/theme-headers.php';
12+
13+
const potContents = wpPot({
14+
src: fixturePath,
15+
writeFile: false,
16+
metadataFile: fixturePath
17+
});
18+
19+
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':2', 'Test Theme', false, false));
20+
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':3', 'Rasmus Bengtsson', false, false));
21+
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':4', '1.0.0', false, false));
22+
});
23+
24+
it('Can read plugin headers', () => {
25+
const fixturePath = 'test/fixtures/plugin-headers.php';
26+
27+
const potContents = wpPot({
28+
src: fixturePath,
29+
writeFile: false,
30+
metadataFile: fixturePath
31+
});
32+
33+
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':2', 'Test Plugin', false, false));
34+
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':3', 'Rasmus Bengtsson', false, false));
35+
assert(testHelper.verifyLanguageBlock(potContents, false, fixturePath + ':4', '1.0.0', false, false));
36+
});
37+
});

test/fixtures/plugin-headers.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
/*
3+
* Plugin Name: Test Plugin
4+
* Author: Rasmus Bengtsson
5+
* Version: 1.0.0
6+
*/

test/fixtures/theme-headers.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
/*
3+
* Theme Name: Test Theme
4+
* Author: Rasmus Bengtsson
5+
* Version: 1.0.0
6+
*/

translation-parser.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,41 @@ class TranslationParser {
1818
this.translations = [];
1919
}
2020

21+
/**
22+
* Parse theme or plugin meta data from file header
23+
*
24+
* @param {string} filecontent
25+
* @param {string} filename
26+
*/
27+
parseFileHeader (filecontent, filename) {
28+
const _this = this;
29+
const lines = filecontent.match(/[^\r\n]+/g);
30+
lines.splice(30);
31+
32+
const headers = [ 'Plugin Name', 'Theme Name', 'Version', 'Author' ];
33+
34+
lines.forEach(function (lineContent, line) {
35+
headers.forEach(function (header, index) {
36+
const regex = new RegExp('^(?:[ \t]*<?php)?[ \t/*#@]*' + header + ':(.*)$', 'i');
37+
const match = regex.exec(lineContent);
38+
39+
if (match) {
40+
headers.splice(index, 1);
41+
const headerValue = match[ 1 ].replace(/\s*(?:\*\/|\?>).*/, '').trim();
42+
43+
const translationCall = {
44+
args: [ headerValue ],
45+
filename,
46+
line,
47+
method: ''
48+
};
49+
50+
_this.addTranslation(translationCall);
51+
}
52+
});
53+
});
54+
}
55+
2156
/**
2257
* Parse comment AST
2358
*
@@ -277,12 +312,16 @@ class TranslationParser {
277312

278313
this.translations = existingTranslations;
279314

315+
const filename = path.relative(this.options.relativeTo || path.dirname(this.options.destFile || __filename), filePath).replace(/\\/g, '/');
316+
317+
if (this.options.metadataFile === filename) {
318+
this.parseFileHeader(filecontent, filename);
319+
}
320+
280321
// Skip file if no translation functions is found
281322
const validFunctionsInFile = new RegExp(this.options.functionCalls.valid.join('|').replace('$', '\\$'));
282323

283324
if (validFunctionsInFile.test(filecontent)) {
284-
const filename = path.relative(this.options.relativeTo || path.dirname(this.options.destFile || __filename), filePath).replace(/\\/g, '/');
285-
286325
try {
287326
const ast = parser.parseCode(filecontent, filename);
288327
this.parseCodeTree(ast, filename);

0 commit comments

Comments
 (0)