summaryrefslogtreecommitdiffstats
path: root/lib/backbone/langs.js
blob: e339fa9847cb7619de9c88fbf173aa4ea7c65310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var _ = require('lodash');
var path = require('path');
var util = require('util');
var BackboneFile = require('./file');

function Language(title, folder) {
    var that = this;

    this.title = title;
    this.folder = folder;

    Object.defineProperty(this, 'id', {
        get: function() {
            return path.basename(that.folder);
        }
    });
}

/*
A Langs is a list of languages stored in a LANGS.md file
*/
function Langs() {
    BackboneFile.apply(this, arguments);

    this.languages = [];
}
util.inherits(Langs, BackboneFile);

Langs.prototype.type = 'langs';

// Parse the readme content
Langs.prototype.parse = function(content) {
    var that = this;

    return this.parser.langs(content)
    .then(function(langs) {
        that.languages = _.map(langs, function(entry) {
            return new Language(entry.title, entry.path);
        });
    });
};

// Return the list of languages
Langs.prototype.list = function() {
    return this.languages;
};

// Return default/main language for the book
Langs.prototype.getDefault = function() {
    return _.first(this.languages);
};

// Return true if a language is the default one
// "lang" cam be a string (id) or a Language entry
Langs.prototype.isDefault = function(lang) {
    lang = lang.id || lang;
    return (this.cound() > 0 && this.getDefault().id == lang);
};

// Return the count of languages
Langs.prototype.count = function() {
    return _.size(this.languages);
};

// Return templating context for the languages list
Langs.prototype.getContext = function() {
    if (this.count() == 0) return {};

    return {
        languages: {
            list: _.map(this.languages, function(lang) {
                return {
                    id: lang.id,
                    title: lang.title
                };
            })
        }
    };
};

module.exports = Langs;