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

Better language detection #68

Open
wants to merge 1 commit 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
31 changes: 19 additions & 12 deletions lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var cheerio = require('cheerio');
var Po = require('pofile');
var esprima = require('esprima');
var langDetect = require('language-detect');
var langMap = require('language-map');
var _ = require('lodash');

var escapeRegex = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
Expand Down Expand Up @@ -58,15 +60,7 @@ var Extractor = (function () {
markerName: 'gettext',
markerNames: [],
lineNumbers: true,
extensions: {
htm: 'html',
html: 'html',
php: 'html',
phtml: 'html',
tml: 'html',
erb: 'html',
js: 'js'
},
extensions: {},
postProcess: function (po) {}
}, options);
this.options.markerNames.unshift(this.options.markerName);
Expand Down Expand Up @@ -283,13 +277,26 @@ var Extractor = (function () {
};

Extractor.prototype.parse = function (filename, content) {
// check list of supported extensions
var extension = filename.split('.').pop();

if (this.isSupportedByStrategy('html', extension)) {
this.extractHtml(filename, content);
return this.extractHtml(filename, content);
} else if (this.isSupportedByStrategy('js', extension)) {
return this.extractJs(filename, content);
}
if (this.isSupportedByStrategy('js', extension)) {

// better language detection when all else fails
var lang = langDetect.filename(filename) || langDetect.classify(content);
if (lang === 'JavaScript') {
this.extractJs(filename, content);
} else {
var langAttrs = langMap[lang];
if (langAttrs.type === 'markup' || // covers HTML, most templating langs, and XML
lang === 'PHP' ||
extension.indexOf('html') !== -1) /* .cshtml */ {

this.extractHtml(filename, content);
}
}
};

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"dependencies": {
"cheerio": "~0.18.0",
"esprima": "~1.1.1",
"language-detect": "^1.0.0",
"language-map": "^1.0.0",
"lodash": "~2.4.1",
"pofile": "~0.2.8"
}
Expand Down