summaryrefslogtreecommitdiffstats
path: root/lib/models/book.js
blob: 3c5144f9d9d6e118e41a17361ddef8a863ffa4b0 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var path = require('path');
var Immutable = require('immutable');
var Ignore = require('ignore');

var Logger = require('../utils/logger');

var FS = require('./fs');
var Config = require('./config');
var Readme = require('./readme');
var Summary = require('./summary');
var Glossary = require('./glossary');
var Languages = require('./languages');


var Book = Immutable.Record({
    // Logger for outptu message
    logger:         Logger(),

    // Filesystem binded to the book scope to read files/directories
    fs:             FS(),

    // Ignore files parser
    ignore:         Ignore(),

    // Structure files
    config:         Config(),
    readme:         Readme(),
    summary:        Summary(),
    glossary:       Glossary(),
    languages:      Languages(),


    // Parent of this book, if multilingual
    parent:         null,

    // List of children, if multilingual
    books:          Immutable.List()
});

Book.prototype.getLogger = function() {
    return this.get('logger');
};

Book.prototype.getFS = function() {
    return this.get('fs');
};

Book.prototype.getIgnore = function() {
    return this.get('ignore');
};

Book.prototype.getConfig = function() {
    return this.get('config');
};

Book.prototype.getReadme = function() {
    return this.get('readme');
};

Book.prototype.getSummary = function() {
    return this.get('summary');
};

Book.prototype.getGlossary = function() {
    return this.get('glossary');
};

Book.prototype.getLanguages = function() {
    return this.get('languages');
};

Book.prototype.getParent = function() {
    return this.get('parent');
};

Book.prototype.getBooks = function() {
    return this.get('books');
};

/**
    Return FS instance to access the content

    @return {FS}
*/
Book.prototype.getContentFS = function() {
    var fs = this.getFS();
    var config = this.getConfig();
    var rootFolder = config.getValue('root');

    if (rootFolder) {
        return FS.reduceScope(fs, rootFolder);
    }

    return fs;
};

/**
    Return root of the book

    @return {String}
*/
Book.prototype.getRoot = function() {
    var fs = this.getFS();
    return fs.getRoot();
};

/**
    Return root for content of the book

    @return {String}
*/
Book.prototype.getContentRoot = function() {
    var fs = this.getContentFS();
    return fs.getRoot();
};

/**
    Check if a file is ignore (should not being parsed, etc)

    @param {String} ref
    @return {Page|undefined}
*/
Book.prototype.isFileIgnored = function(filename) {
    var ignore = this.getIgnore();
    return ignore.filter([filename]).length == 0;
};

/**
    Check if a content file is ignore (should not being parsed, etc)

    @param {String} ref
    @return {Page|undefined}
*/
Book.prototype.isContentFileIgnored = function(filename) {
    var config = this.getConfig();
    var rootFolder = config.getValue('root');

    if (rootFolder) {
        filename = path.join(rootFolder, filename);
    }

    return this.isFileIgnored(filename);
};

/**
    Return a page from a book by its path

    @param {String} ref
    @return {Page|undefined}
*/
Book.prototype.getPage = function(ref) {
    return this.getPages().get(ref);
};

/**
    Is this book the parent of language's books

    @return {Boolean}
*/
Book.prototype.isMultilingual = function() {
    return (this.getLanguages().getList().size > 0);
};

/**
    Change log level

    @param {Book} book
    @param {String} level
    @return {Book}
*/
Book.setLogLevel = function setLogLevel(book, level) {
    book.getLogger().setLevel(level);
    return book;
};

/**
    Create a book using a filesystem

    @param {FS} fs
    @return {Book}
*/
Book.createForFS = function createForFS(fs) {
    return new Book({
        fs: fs
    });
};

/**
    Create a language book from a parent

    @param {Book} parent
    @param {String} basePath: folder o the book in the parent
    @return {Book}
*/
Book.createFromParent = function createFromParent(parent, basePath) {
    return new Book({
        logger: parent.getLogger(),
        parent: parent,
        fs: FS.reduceScope(parent.getFS(), basePath)
    });
};

module.exports = Book;