Import code from gra-web project

This commit is contained in:
doup 2015-07-14 19:59:12 +02:00
parent 3151007a11
commit 28b51a65a0
5 changed files with 167 additions and 22 deletions

22
LICENSE
View file

@ -1,22 +0,0 @@
The MIT License (MIT)
Copyright (c) 2015 Asier Illarramendi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,2 +1,36 @@
# metalsmith-multi-language # metalsmith-multi-language
Create multi-language websites in Metalsmith Create multi-language websites in Metalsmith
## Instalation
## Usage
## Options
## Tests
`npm test` to run the tests.
## License
The MIT License (MIT)
Copyright (c) 2015 Asier Illarramendi <asier@illarra.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

90
lib/index.js Normal file
View file

@ -0,0 +1,90 @@
'use strict';
var extname = require('path').extname;
function getBaseFilename(file) {
var base = file;
var ext = extname(base);
return base.replace(RegExp('_('+ ops.locales.join('|') +')(?:'+ ext +')?$'), '_' + ops.default + ext);
}
function getAltFilename(file, fromLocale, toLocale) {
var ext = extname(file);
return file.replace('_'+ fromLocale + ext, '_'+ toLocale + ext);
}
function getLocale(file) {
return file.match(pattern)[1];
}
function merge(src, dest) {
for (var key in src) {
if (!dest.hasOwnProperty(key)) {
dest[key] = src[key];
}
}
}
module.exports = function (ops) {
var pattern = RegExp('.*_('+ ops.locales.join('|') +')(?:\..*)?$');
return function (files, ms, done) {
ms.metadata().locales = ops;
ms.metadata().filterByLocale = function (arr, locale) {
return arr.filter(function (el, index) {
return el.locale == locale;
});
};
for (var file in files) {
if (pattern.test(file)) {
var base = getBaseFilename(file);
files[file].locale = getLocale(file);
// Add missing properties from base locale
// This lets to have base some generic properties
// applied only in the 'default' locale, e.g.: template
if (base !== file) {
merge(files[base], files[file]);
}
} else {
files[file].locale = ops.default;
}
files[file].altFiles = {};
ops.locales.forEach(function (locale) {
if (locale != files[file].locale) {
files[file].altFiles[locale] = files[getAltFilename(file, files[file].locale, locale)];
} else {
files[file].altFiles[files[file].locale] = files[file];
}
});
}
// Index handling
// Default locale will go in 'index.html'
// Other index-es in '/:locale/index.html'
for (file in files) {
if (/^index/.test(file)) {
var ext = extname(file);
if (files[file].locale == ops.default) {
files[file].path = '';
files['index'+ ext] = files[file];
} else {
files[file].path = files[file].locale +'/';
files[files[file].locale + '/index'+ext] = files[file];
}
// Remove old entry
delete files[file];
}
}
done();
};
};

34
package.json Normal file
View file

@ -0,0 +1,34 @@
{
"name": "metalsmith-multi-language",
"version": "0.1.0",
"description": "Create multi-language websites in Metalsmith",
"homepage": "https://github.com/doup/metalsmith-multi-language#readme",
"author": "doup <asier@illarra.com>",
"license": "MIT",
"keywords": [
"metalsmith",
"multi-language",
"i18n",
"translate",
"locale",
"localize",
"l10n"
],
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/doup/metalsmith-multi-language.git"
},
"bugs": {
"url": "https://github.com/doup/metalsmith-multi-language/issues"
},
"dependencies": {},
"devDependencies": {
"chai": "^3.0.0",
"metalsmith": "^1.7.0",
"mocha": "^2.2.5"
},
"scripts": {
"test": "mocha --ui bdd --reporter spec"
}
}

9
test/index.js Normal file
View file

@ -0,0 +1,9 @@
'use strict';
var expect = require('chai').expect;
describe('/lib/index.js', function () {
it('should pass', function () {
});
});