diff --git a/README.md b/README.md index c054680..9644c1f 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Documentation Build Status ------------ -[![Build Status](https://secure.travis-ci.org/yui/shifter.png?branch=master)](http://travis-ci.org/yui/shifter) +[![Build Status](https://secure.travis-ci.org/dmi3y/shifter.png?branch=master)](http://travis-ci.org/dmi3y/shifter) Install ------- diff --git a/lib/module.js b/lib/module.js index 562db4f..b0e7e5c 100644 --- a/lib/module.js +++ b/lib/module.js @@ -403,11 +403,105 @@ var buildCoverage = function (mod, name, callback) { } callback(); }); - }; exports.coverage = buildCoverage; +/** + * Extract heredoc block + * + * @param {Sting} value start line of heredoc with token + * @param {Int} ix index of the buffer where heredoc starts + * @param {Array} buffer raw array from YRB(.pres) + * @return {Object} heredoc value from heredoc block plus new index to continue into parsePrestoJson method + */ +var getHeredocString = function(value, ix, buffer) { + var + token = value.replace('<<<', '').trim(), + heredoc = {}; + value = []; + + while ( buffer[ix].indexOf(token) === -1) { + el = buffer[ix]; + if (el.indexOf('#') !== 0) { + value.push(el); + } + ix += 1; + } + heredoc.value = value.join('\\n'); + heredoc.ix = ix + 1; + return heredoc; +}; + +/** + * Parsing YRB(.pres) file content string to a la Json format + * http://yuilibrary.com/yui/docs/intl/#yrb + * + * @param {String} presStr YRB formatted string + * @return {String} buffer Json parsed string from YRB + */ +var parsePresToJson = function (presStr) { + var + buffer, + key, + value, + ixof, + el, ix = 0, len, + heredoc, + line = [], + buff = []; + + buffer = presStr.split(/\r\n|\n|\r/); + len = buffer.length; + + for (;ix < len;) { + el = buffer[ix++]; + line = []; + + el = el.trim(); + if (el && el.indexOf('#') !== 0) { + key = el.split('=', 1).pop().trim(); + line.push(key); + + ixof = el.indexOf('='); + value = el.slice(++ixof).trim(); + + if ( value.indexOf('<<<') === 0 ) { + heredoc = getHeredocString(value, ix, buffer); + value = heredoc.value; + ix = heredoc.ix; + } + + line.push('"' + value + '"'); + + buff.push(line.join(':')); + } + } + + buffer = '{' + buff.join(',') + '}'; + return buffer; +}; + +/** + * Reads the file in either a la Json(.js) or YRB(.pres) + * formats, and return content in Json + * + * @param {String} modName + * @return {String} output + */ +var getLangJsonSting = function (modName) { + var + output, + fileExtention = ((fs.existsSync(path.join(shifter.cwd(), 'lang', modName + '.js'))? '.js': '.pres')); + fileNameReadFrom = modName + fileExtention; + output = fs.readFileSync(path.join(shifter.cwd(), 'lang', fileNameReadFrom), 'utf8'); + if (fileExtention === '.pres') { + log.info('parsing lang Json from YRB source ' + fileNameReadFrom ); + output = parsePresToJson(output); + } + return output; +}; + var buildLang = function (mod, name, callback) { var langs = [''].concat(mod.config.lang), stack = new Stack(); @@ -421,7 +515,7 @@ var buildLang = function (mod, name, callback) { }), modName = name + (lang ? '_' + lang : ''), fileName = modName + '.js', - strings = fs.readFileSync(path.join(shifter.cwd(), 'lang', fileName), 'utf8'); + strings = getLangJsonSting(modName); queue.read([path.join(__dirname, '../files/langtemplate.txt')]) .replace(replaceOptions)