Skip to content

Commit

Permalink
Merge pull request #90 from michaelmcmillan/languages
Browse files Browse the repository at this point in the history
Support other locales than norwegian
  • Loading branch information
michaelmcmillan committed Jun 8, 2015
2 parents 9f23521 + 2214d0f commit 675c4f6
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 36 deletions.
3 changes: 2 additions & 1 deletion bibliographies/bibliographyGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ function BibliographyGenerator (list, done) {

// Find the references to the style and the locale
var styleFile = list.getBibliographyStyle();
var localeFile = list.getBibliographyLocale();
var styleLocation = config.bibliography.styles.location + styleFile;
var localeLocation = config.bibliography.locales.location + 'locales-nb-NO.xml';
var localeLocation = config.bibliography.locales.location + localeFile;

// Call the citeproc wrapper with the citations
Citeproc(citations, styleLocation, localeLocation, function (citeproc) {
Expand Down
22 changes: 22 additions & 0 deletions bibliographies/supportedLocales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var fs = require('fs');

function supportedLocales (allowedLocales, localesLocation, done) {

var locales = [];

fs.readdir(localesLocation, function(err, files) {
if (err) return done(err);

files.forEach(function(file) {
if (allowedLocales[file] !== undefined)
locales.push({
name: allowedLocales[file],
file: file
});
});

return done(undefined, locales);
});
}

module.exports = supportedLocales;
20 changes: 20 additions & 0 deletions bibliographies/supportedStylesAndLocales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var config = require('../config.js');
var supportedStyles = require('./supportedStyles.js');
var supportedLocales = require('./supportedLocales.js');

function supportedStylesAndLocales (done) {

var allowedStyles = config.bibliography.styles.allowed;
var allowedLocales = config.bibliography.locales.allowed;

var stylesLocation = config.bibliography.styles.location;
var localesLocation = config.bibliography.locales.location;

supportedStyles(allowedStyles, stylesLocation, function (err, styles) {
supportedLocales(allowedLocales, localesLocation, function (err, locales) {
return done(undefined, styles, locales);
});
});
}

module.exports = supportedStylesAndLocales;
9 changes: 8 additions & 1 deletion controllers/list/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function UpdateListController (req, res, next) {
}

// Removing reference(s)
// Only if style is not changed
if (req.body.remove !== undefined && req.body.removed !== undefined) {
list.removeReference(req.body.remove);
}
Expand All @@ -27,6 +26,13 @@ function UpdateListController (req, res, next) {
if (allowedStyleFiles.indexOf(req.body.style) !== -1)
list.setBibliographyStyle(req.body.style);
}

// Changing the bibliography locale
if (req.body.locale !== undefined && req.body.localed !== undefined) {
var allowedLocaleFiles = Object.keys(config.bibliography.locales.allowed);
if (allowedLocaleFiles.indexOf(req.body.locale) !== -1)
list.setBibliographyLocale(req.body.locale);
}

// Update the model with the above changes to the database
ListFactory.update(list, function (err, list) {
Expand All @@ -35,6 +41,7 @@ function UpdateListController (req, res, next) {
logger.log('debug', 'Updated list contents', {
id: list.getId(),
style: req.body.style || null,
locale: req.body.locale || null,
added: req.body.add || null,
removed: req.body.remove || null
});
Expand Down
51 changes: 28 additions & 23 deletions controllers/list/view.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var logger = require('../../log/logger.js');
var config = require('../../config.js');
var supportedStyles = require('../../bibliographies/supportedStyles.js');
var ListFactory = require('../../database/factories/list.js');
var BibliographyGenerator = require('../../bibliographies/bibliographyGenerator.js');
var BibliographyGenerator = require('../../bibliographies/bibliographyGenerator.js');
var supportedStylesAndLocales = require('../../bibliographies/supportedStylesAndLocales.js');

function ViewListController (req, res) {

Expand All @@ -23,29 +23,34 @@ function ViewListController (req, res) {
id: list.getId()
});

// Fetch the available reference styles
supportedStyles(config.bibliography.styles.allowed, config.bibliography.styles.location,
function (err, styles) {
// Fetch the available reference styles and locales
supportedStylesAndLocales(function (err, styles, locales) {

// Mark the style used as selected
styles.forEach(function (style) {
style.selected = (style.file === list.getBibliographyStyle());
});

// Find out if feedback form should be displayed
var displayFeedback = req.query.feedback === '' ? true : false;
// Mark the style used as selected
styles.forEach(function (style) {
style.selected = (style.file === list.getBibliographyStyle());
});

// Mark the locale used as selected
locales.forEach(function (locale) {
locale.selected = (locale.file === list.getBibliographyLocale());
});

// Find out if feedback form should be displayed
var displayFeedback = req.query.feedback === '' ? true : false;

// Finally render the list with references,
// the generated bibliography and supported styles
res.render('list', {
references: list.getReferences(),
list: bibliography,
supportedStyles: styles,
referencesInListCount: list.getReferences().length,
displayFeedback: displayFeedback,
expired: list.hasExpired(),
lifetime: list.getHumanFriendlyLifetime()
});
// Finally render the list with references,
// the generated bibliography and supported styles
res.render('list', {
references: list.getReferences(),
list: bibliography,
supportedStyles: styles,
supportedLocales: locales,
referencesInListCount: list.getReferences().length,
displayFeedback: displayFeedback,
expired: list.hasExpired(),
lifetime: list.getHumanFriendlyLifetime()
});
});
});
});
Expand Down
17 changes: 16 additions & 1 deletion database/factories/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var ListFactory = {
list.setId(row.id);
list.setUrl(row.url);
list.setBibliographyStyle(row.style);
list.setBibliographyLocale(row.locale);
list.setCreatedAt(row.created_at);

// Async queue
Expand Down Expand Up @@ -99,7 +100,8 @@ var ListFactory = {
// If no changes has been made, simply return the list as is
if (removed.length === 0
&& added.length === 0
&& list.getBibliographyStyle() === readList.getBibliographyStyle()) {
&& list.getBibliographyStyle() === readList.getBibliographyStyle()
&& list.getBibliographyLocale() === readList.getBibliographyLocale()) {
return self.read(list.getId(), done);
}

Expand All @@ -117,6 +119,18 @@ var ListFactory = {
return self.read(list.getId(), done);
});
}

// Update the bibliography locale
if (list.getBibliographyLocale() !== undefined) {
++queue;
database.query('UPDATE Lists SET locale = ? WHERE id = ?',
[list.getBibliographyLocale(), list.getId()],
function (err, rows, fields) {
if (err) return done(err);
if (--queue === 0)
return self.read(list.getId(), done);
});
}

// Delete the removed references
if (removed.length !== 0) {
Expand Down Expand Up @@ -156,6 +170,7 @@ var ListFactory = {
database.query('INSERT INTO Lists SET ?', {
url: list.getUrl(),
style: list.getBibliographyStyle(),
locale: list.getBibliographyLocale(),
created_at: list.getCreatedAt()
}, function (err, rows, field) {

Expand Down
1 change: 1 addition & 0 deletions database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CREATE TABLE `Lists` (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
url varchar(255),
style varchar(255) NOT NULL,
locale varchar(255) NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Expand Down
14 changes: 13 additions & 1 deletion models/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ function List () {
var url = crypto.randomBytes(5).toString('hex');
var references = [];
var created = new Date();
var bibliographyStyle = 'harvard1.csl';
var bibliographyStyle = 'harvard1.csl';
var bibliographyLocale = 'locales-nb-NO.xml';

this.getId = function () {
return id;
Expand Down Expand Up @@ -65,6 +66,10 @@ function List () {
});
}

this.getBibliographyLocale = function () {
return bibliographyLocale;
}

this.getBibliographyStyle = function () {
return bibliographyStyle;
}
Expand All @@ -76,6 +81,13 @@ function List () {
bibliographyStyle = style;
}

this.setBibliographyLocale = function (locale) {
if (locale.substring(locale.length - 4) !== '.xml')
throw new Error('Locale must end with .xml');

bibliographyLocale = locale;
}

this.setCreatedAt = function (date) {
created = date;
}
Expand Down
11 changes: 10 additions & 1 deletion test/persistence/listFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('listFactory', function () {
});
});


it('can remove contents in an existing list', function (done) {
ListFactory.read(1, function (err, list) {
assert.equal(list.getReferences().length, 1);
Expand Down Expand Up @@ -70,6 +69,16 @@ describe('listFactory', function () {
});
});
});

it('can change the bibliography locale upon update', function (done) {
ListFactory.read(1, function (err, list) {
list.setBibliographyLocale('locale-en-GB.xml');
ListFactory.update(list, function (err, updatedList) {
assert.equal(updatedList.getBibliographyLocale(), 'locale-en-GB.xml');
done();
});
});
});
});

describe('Website', function () {
Expand Down
20 changes: 19 additions & 1 deletion test/unit/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,25 @@ describe('List', function () {
var list = new List();
assert.throws(function () {
list.setBibliographyStyle('harvard yo give me that style');
});
}, /Style/);
});

it('should have .csl as the default bibliography style', function () {
var list = new List();
assert.equal(list.getBibliographyLocale(), 'locales-nb-NO.xml');
});

it('should be possible to set the bibliography locale as long as it is a valid filename', function () {
var list = new List();
list.setBibliographyLocale('locales-en-GB.xml');
assert.equal(list.getBibliographyLocale(), 'locales-en-GB.xml');
});

it('should throw exception if bibliography style is an invalid filename', function () {
var list = new List();
assert.throws(function () {
list.setBibliographyLocale('english yo give me that locale');
}, /Locale/);
});

it('should return days left until the list expires', function () {
Expand Down
13 changes: 6 additions & 7 deletions web/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,11 @@ form#query > input[type="submit"] {
.settings > select {
border-radius: 3.6px;
background: transparent;
width: 150px;
width: 120px;
padding: 5px 35px 5px 5px;
font-size: 14px;
border: 1px solid #ddd;
height: 34px;
height: 30px;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
Expand Down Expand Up @@ -436,16 +436,15 @@ button[name="add"], button[name="remove"] {
background-color: #fff;
}

button.remove {
button.remove, button.change-style {
margin-right:25px;
}

button.change-style, button.copy, button.remove {
font-size:14px;

button.change-style, button.change-locale, button.copy, button.remove {
font-size:12px;
}

select[name="style"], button.change-style, button.remove {
select[name="style"], select[name="locale"], button.change-locale, button.change-style, button.remove {
float:left;
}

Expand Down
10 changes: 10 additions & 0 deletions web/views/list.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@

<form method="post" action="/liste">
<div class="settings">

{{#if referencesInListCount}}
<button type="submit" class="remove" name="removed" value="true">Fjern valgte</button>
{{/if}}

<select name="style">
{{#each supportedStyles}}
<option {{#if selected}} selected {{/if}} value="{{file}}">{{name}}</option>
{{/each}}
</select>
<button class="change-style" type="submit" name="styled" value="true">Bytt referansestil</button>

<select name="locale">
{{#each supportedLocales}}
<option {{#if selected}} selected {{/if}} value="{{file}}">{{name}}</option>
{{/each}}
</select>
<button class="change-locale" type="submit" name="localed" value="true">Bytt språk</button>

<button class="copy">Kopier litteraturlisten</button>
</div>
<div class="a4">
Expand Down

0 comments on commit 675c4f6

Please sign in to comment.