summaryrefslogtreecommitdiffstats
path: root/lib/models/summary.js
blob: 3b4694111881c533330154d08d379f286e3f79cb (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
var is = require('is');
var Immutable = require('immutable');

var error = require('../utils/error');
var File = require('./file');
var SummaryPart = require('./summaryPart');
var SummaryArticle = require('./summaryArticle');
var parsers = require('../parsers');

var Summary = Immutable.Record({
    file:       File(),
    parts:      Immutable.List()
}, 'Summary');

Summary.prototype.getFile = function() {
    return this.get('file');
};

Summary.prototype.getParts = function() {
    return this.get('parts');
};

/**
    Return a part by its index

    @param {Number}
    @return {Part}
*/
Summary.prototype.getPart = function(i) {
    var parts = this.getParts();
    return parts.get(i);
};

/**
    Return an article using an iterator to find it.
    if "partIter" is set, it can also return a Part.

    @param {Function} iter
    @param {Function} partIter
    @return {Article|Part}
*/
Summary.prototype.getArticle = function(iter, partIter) {
    var parts = this.getParts();

    return parts.reduce(function(result, part) {
        if (result) return result;

        if (partIter && partIter(part)) return part;
        return SummaryArticle.findArticle(part, iter);
    }, null);
};


/**
    Return a part/article by its level

    @param {String} level
    @return {Article}
*/
Summary.prototype.getByLevel = function(level) {
    function iterByLevel(article) {
        return (article.getLevel() === level);
    }

    return this.getArticle(iterByLevel, iterByLevel);
};

/**
    Return an article by its path

    @param {String} filePath
    @return {Article}
*/
Summary.prototype.getByPath = function(filePath) {
    return this.getArticle(function(article) {
        return (article.getPath() === filePath);
    });
};

/**
    Return the first article

    @return {Article}
*/
Summary.prototype.getFirstArticle = function() {
    return this.getArticle(function(article) {
        return true;
    });
};

/**
    Return next article of an article

    @param {Article} current
    @return {Article}
*/
Summary.prototype.getNextArticle = function(current) {
    var level = is.string(current)? current : current.getLevel();
    var wasPrev = false;

    return this.getArticle(function(article) {
        if (wasPrev) return true;

        wasPrev = article.getLevel() == level;
        return false;
    });
};

/**
    Return previous article of an article

    @param {Article} current
    @return {Article}
*/
Summary.prototype.getPrevArticle = function(current) {
    var level = is.string(current)? current : current.getLevel();
    var prev = undefined;

    this.getArticle(function(article) {
        if (article.getLevel() == level) {
            return true;
        }

        prev = article;
        return false;
    });

    return prev;
};

/**
    Render summary as text

    @return {Promise<String>}
*/
Summary.prototype.toText = function(parser) {
    var file = this.getFile();
    var parts = this.getParts();

    parser = parser? parsers.getByExt(parser) : file.getParser();

    if (!parser) {
        throw error.FileNotParsableError({
            filename: file.getPath()
        });
    }

    return parser.summary.toText({
        parts: parts.toJS()
    });
};

/**
    Create a new summary for a list of parts

    @param {Lust|Array} parts
    @return {Summary}
*/
Summary.createFromParts = function createFromParts(file, parts) {
    parts = parts.map(function(part, i) {
        if (part instanceof SummaryPart) {
            return part;
        }

        return SummaryPart.create(part, i + 1);
    });

    return new Summary({
        file: file,
        parts: new Immutable.List(parts)
    });
};

module.exports = Summary;