summaryrefslogtreecommitdiffstats
path: root/lib/backbone/summary.js
blob: a79b1e9a29cbfc44778e439d8dfac6f14f1ef4e3 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var _ = require('lodash');
var util = require('util');
var url = require('url');

var location = require('../utils/location');
var error = require('../utils/error');
var BackboneFile = require('./file');

/*
    An article represent an entry in the Summary.
    It's defined by a title, a reference, and children articles,
    the reference (ref) can be a filename + anchor or an external file (optional)
*/
function TOCArticle(def, parent) {
    // Title
    this.title = def.title;

    // Parent TOCPart or TOCArticle
    this.parent = parent;

    // As string indicating the overall position
    // ex: '1.0.0'
    this.level;
    this._next;
    this._prev;

    // When README has been automatically added
    this.isAutoIntro = def.isAutoIntro;
    this.isIntroduction = def.isIntroduction;

    this.validate();

    // Path can be a relative path or an url, or nothing
    this.ref = def.path;
    if (this.ref) {
        var parts = url.parse(this.ref);

        if (!this.isExternal()) {
            var parts = this.ref.split('#');
            this.path = (parts.length > 1? parts.slice(0, -1).join('#') : this.ref);
            this.anchor = (parts.length > 1? '#' + _.last(parts) : null);
        }
    }

    this.articles = _.map(def.articles || [], function(article) {
        if (article instanceof TOCArticle) return article;
        return new TOCArticle(article, this);
    }, this);
}

// Validate the article
TOCArticle.prototype.validate = function() {
    if (!this.title) {
        throw error.ParsingError(new Error('SUMMARY entries should have an non-empty title'));
    }
};

// Iterate over all articles in this articles
TOCArticle.prototype.walk = function(iter, base) {
    base = base || this.level;

    _.each(this.articles, function(article, i) {
        var level = levelId(base, i);

        if (iter(article, level) === false) {
            return false;
        }
        article.walk(iter, level);
    });
};

// Return templating context for an article
TOCArticle.prototype.getContext = function() {
    return {
        level: this.level,
        title: this.title,
        depth: this.depth(),
        path: this.isExternal()? undefined : this.path,
        anchor: this.isExternal()? undefined : this.anchor,
        url: this.isExternal()? this.ref : undefined
    };
};

// Return true if is pointing to a file
TOCArticle.prototype.hasLocation = function() {
    return Boolean(this.path);
};

// Return true if is pointing to an external location
TOCArticle.prototype.isExternal = function() {
    return location.isExternal(this.ref);
};

// Return true if this article is the introduction
TOCArticle.prototype.isIntro = function() {
    return Boolean(this.isIntroduction);
};

// Return true if has children
TOCArticle.prototype.hasChildren = function() {
    return this.articles.length > 0;
};

// Return true if has an article as parent
TOCArticle.prototype.hasParent = function() {
    return !(this.parent instanceof TOCPart);
};

// Return depth of this article
TOCArticle.prototype.depth = function() {
    return this.level.split('.').length;
};

// Return next article in the TOC
TOCArticle.prototype.next = function() {
    return this._next;
};

// Return previous article in the TOC
TOCArticle.prototype.prev = function() {
    return this._prev;
};

// Map over all articles
TOCArticle.prototype.map = function(iter) {
    return _.map(this.articles, iter);
};


/*
    A part of a ToC is a composed of a tree of articles and an optiona title
*/
function TOCPart(part, parent) {
    if (!(this instanceof TOCPart)) return new TOCPart(part, parent);

    TOCArticle.apply(this, arguments);
}
util.inherits(TOCPart, TOCArticle);

// Validate the part
TOCPart.prototype.validate = function() { };

// Return a sibling (next or prev) of this part
TOCPart.prototype.sibling = function(direction) {
    var parts = this.parent.parts;
    var pos = _.findIndex(parts, this);

    if (parts[pos + direction]) {
        return parts[pos + direction];
    }

    return null;
};

// Iterate over all entries of the part
TOCPart.prototype.walk = function(iter, base) {
    var articles = this.articles;

    if (articles.length == 0) return;

    // Has introduction?
    if (articles[0].isIntro()) {
        if (iter(articles[0], '0') === false) {
            return;
        }

        articles = articles.slice(1);
    }


    _.each(articles, function(article, i) {
        var level = levelId(base, i);

        if (iter(article, level) === false) {
            return false;
        }

        article.walk(iter, level);
    });
};

// Return templating context for a part
TOCPart.prototype.getContext = function(onArticle) {
    onArticle = onArticle || function(article) {
        return article.getContext();
    };

    return {
        title: this.title,
        articles: this.map(onArticle)
    };
};

/*
A summary is composed of a list of parts, each composed wit a tree of articles.
*/
function Summary() {
    BackboneFile.apply(this, arguments);

    this.parts = [];
    this._length = 0;
}
util.inherits(Summary, BackboneFile);

Summary.prototype.type = 'summary';

// Prepare summary when non existant
Summary.prototype.parseNotFound = function() {
    this.update([]);
};

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

    return this.parser.summary(content)

    .then(function(summary) {
        that.update(summary.parts);
    });
};

// Return templating context for the summary
Summary.prototype.getContext = function() {
    function onArticle(article) {
        var result = article.getContext();
        if (article.hasChildren()) {
            result.articles = article.map(onArticle);
        }

        return result;
    }

    return {
        summary: {
            parts: _.map(this.parts, function(part) {
                return part.getContext(onArticle);
            })
        }
    };
};

// Iterate over all entries of the summary
// iter is called with an TOCArticle
Summary.prototype.walk = function(iter) {
    var hasMultipleParts = this.parts.length > 1;

    _.each(this.parts, function(part, i) {
        part.walk(iter, hasMultipleParts? levelId('', i) : null);
    });
};

// Find a specific article using a filter
Summary.prototype.find = function(filter) {
    var result;

    this.walk(function(article) {
        if (filter(article)) {
            result = article;
            return false;
        }
    });

    return result;
};

// Flatten the list of articles
Summary.prototype.flatten = function() {
    var result = [];

    this.walk(function(article) {
        result.push(article);
    });

    return result;
};

// Return the first TOCArticle for a specific page (or path)
Summary.prototype.getArticle = function(page) {
    if (!_.isString(page)) page = page.path;

    return this.find(function(article) {
        return article.path == page;
    });
};

// Return the first TOCArticle for a specific level
Summary.prototype.getArticleByLevel = function(lvl) {
    return this.find(function(article) {
        return article.level == lvl;
    });
};

// Return the count of articles in the summary
Summary.prototype.count = function() {
    return this._length;
};

// Prepare the summary
Summary.prototype.update = function(parts) {
    var that = this;


    that.parts = _.map(parts, function(part) {
        return new TOCPart(part, that);
    });

    // Create first part if none
    if (that.parts.length == 0) {
        that.parts.push(new TOCPart({}, that));
    }

    // Add README as first entry
    var firstArticle = that.parts[0].articles[0];
    if (!firstArticle || firstArticle.path != that.book.readme.path) {
        that.parts[0].articles.unshift(new TOCArticle({
            title: 'Introduction',
            path: that.book.readme.path,
            isAutoIntro: true
        }, that.parts[0]));
    }
    that.parts[0].articles[0].isIntroduction = true;


    // Update the count and indexing of "level"
    var prev = undefined;

    that._length = 0;
    that.walk(function(article, level) {
        // Index level
        article.level = level;

        // Chain articles
        article._prev = prev;
        if (prev) prev._next = article;

        prev = article;

        that._length += 1;
    });
};


// Return a level string from a base level and an index
function levelId(base, i) {
    i = i + 1;
    return (base? [base || '', i] : [i]).join('.');
}

module.exports = Summary;