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

YRB(.pres) support for language bundles #96

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do this, please. We can't take this pull request because of this change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I did not realize it will be included in request, that's my first pull request on github

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, no need to change this anyway, since all pushes to a given pull request branch will cause builds under yui3's Travis space.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am struggling with running tests on local env, so as intern solution trying to use travis (better than nothing). Thanks for comments, I will be back :)


Install
-------
Expand Down
98 changes: 96 additions & 2 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Sting/String/

* @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();
Expand All @@ -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)
Expand Down