jsxgettext
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -47,3 +47,3 @@ #!/usr/bin/env node | ||
default: 'JavaScript', | ||
help: 'recognise the specified language (JavaScript, EJS, Jinja)' | ||
help: 'recognise the specified language (JavaScript, EJS, Jinja, Jade, Handlebars)' | ||
}) | ||
@@ -82,2 +82,4 @@ .parse(); | ||
result = jsxgettext.generateFromJade(sources, opts); | ||
} else if (opts.language.toUpperCase() === 'HANDLEBARS') { | ||
result = jsxgettext.generateFromHandlebars(sources, opts); | ||
} else { | ||
@@ -84,0 +86,0 @@ result = jsxgettext.generate(sources, opts); |
@@ -163,2 +163,3 @@ /* jshint node: true, undef: true */ | ||
// generate extracted strings file from Jade templates | ||
function genJade (jadeSources, options) { | ||
@@ -171,2 +172,11 @@ Object.keys(jadeSources).forEach(function (filename) { | ||
// generate extracted strings file from Handlebars/Mustache templates | ||
function genHandlebars (hbSources, options) { | ||
Object.keys(hbSources).forEach(function (filename) { | ||
hbSources[filename] = parseHandlebars(hbSources[filename]); | ||
}); | ||
return gen(hbSources, options); | ||
} | ||
// generate extracted strings file from Jinja2 templates | ||
@@ -275,5 +285,100 @@ function genJinja (jinjaSources, options) { | ||
exports.generate = gen; | ||
exports.generateFromEJS = genEJS; | ||
exports.generateFromJade = genJade; | ||
exports.generateFromJinja = genJinja; | ||
// Turn handlebars helper calls into javascript-syntax functions. | ||
// Also comment blocks are turned into javascript comments. | ||
function parseHandlebars(str, options) { | ||
// Using regexes for parsing, ooooh yeeeahhh! | ||
// Short comments: {{! this is a comment }} | ||
var shortCommentRE = /\{\{\!(.*?)\}\}/ | ||
// Long comments: {{!-- this comment has {{markup}} in it --}} | ||
var longCommentRE = /\{\{\!--(.*?)--\}\}/ | ||
// Block helpers: {{#helper}}template content{{/helper}} | ||
var blockHelperStartRE = /\{\{#(\w+)\}\}/ | ||
var blockHelperEndRE = /\{\{\/(\w+)\}\}/ | ||
// Function helpers: {{ helper value }} or {{ helper "some string" }} | ||
var singleQuotedStringWithEscapes = "'(([^']*?(\\\\')?)+)'"; | ||
var doubleQuotedStringWithEscapes = '"(([^"]*?(\\\\")?)+)"'; | ||
var funcHelperRE = new RegExp("\\{\\{\\s*(\\w+)\\s+((\\w+)|(" + | ||
singleQuotedStringWithEscapes + ")|(" + | ||
doubleQuotedStringWithEscapes + "))\\s*\\}\\}") | ||
var buf = []; | ||
var match = null; | ||
while (str.length) { | ||
// Find the earliest match of any type of tag in the string. | ||
match = str.match(shortCommentRE); | ||
if (match) { | ||
match.type = 'comment'; | ||
} | ||
var nextMatch = str.match(longCommentRE); | ||
if (nextMatch) { | ||
if (!match || nextMatch.index < match.index) { | ||
match = nextMatch; | ||
match.type = 'comment'; | ||
} | ||
} | ||
nextMatch = str.match(blockHelperStartRE); | ||
if (nextMatch) { | ||
if (!match || nextMatch.index < match.index) { | ||
match = nextMatch; | ||
match.type = 'block'; | ||
} | ||
} | ||
nextMatch = str.match(funcHelperRE); | ||
if (nextMatch) { | ||
if (!match || nextMatch.index < match.index) { | ||
match = nextMatch; | ||
match.type = 'func'; | ||
} | ||
} | ||
if (!match) { | ||
break; | ||
} | ||
str = str.substring(match.index + match[0].length); | ||
// Translate the match into an appropriate chunk of javascript. | ||
if (match.type == 'comment') { | ||
// Template comment => javascript comment | ||
match[1].split("\n").forEach(function(comment) { | ||
buf.push("//"); | ||
buf.push(comment); | ||
buf.push("\n"); | ||
}) | ||
} else if (match.type == 'block') { | ||
// Template block helper => javascript function call | ||
var helperName = match[1]; | ||
buf.push(helperName) | ||
buf.push('("') | ||
var endMatch = str.match(blockHelperEndRE); | ||
while (endMatch && endMatch[1] !== helperName) { | ||
var skipTo = endMatch.index + endMatch[0].length; | ||
buf.push(str.substring(0, skipTo).replace('"', '\\"')); | ||
str = str.substring(skipTo); | ||
endMatch = str.match(blockHelperEndRE); | ||
} | ||
if (endMatch) { | ||
buf.push(str.substring(0, endMatch.index).replace('"', '\\"')); | ||
str = str.substring(endMatch.index + endMatch[0].length); | ||
} else { | ||
buf.push(str.replace('"', '\\"')); | ||
str = ''; | ||
} | ||
buf.push('")\n'); | ||
} else if (match.type == 'func') { | ||
// Template function helper => javascript function call | ||
buf.push(match[1]); | ||
buf.push('('); | ||
buf.push(match[2]); | ||
buf.push(')\n'); | ||
} | ||
} | ||
return buf.join(''); | ||
} | ||
exports.generate = gen; | ||
exports.generateFromEJS = genEJS; | ||
exports.generateFromJade = genJade; | ||
exports.generateFromHandlebars = genHandlebars; | ||
exports.generateFromJinja = genJinja; |
@@ -5,3 +5,3 @@ { | ||
"description": "Extract gettext calls from JavaScript and EJS files", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -12,2 +12,3 @@ // TODO figure out why | ||
exports.testAll['test ejs'] = require('./ejs'); | ||
exports.testAll['test handlebars'] = require('./handlebars'); | ||
exports.testAll['test comments'] = require('./test_comment'); | ||
@@ -14,0 +15,0 @@ exports.testAll['test po_quotes'] = require("./po_quotes"); |
50486
41
778
13