summaryrefslogtreecommitdiffstats
path: root/lib/backbone/file.js
blob: 34cf066915604b4f199efefab351940cc2f37904 (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

function BackboneFile(book) {
    if (!(this instanceof BackboneFile)) return new BackboneFile(book);

    this.book = book;
    this.log = this.book.log;

    // Filename in the book
    this.filename = '';
    this.parser;
}

// Type of the backbone file
BackboneFile.prototype.type = '';

// Parse a backbone file
BackboneFile.prototype.parse = function() {
    // To be implemented by each child
};

// Return true if backbone file exists
BackboneFile.prototype.exists = function() {
    return Boolean(this.filename);
};

// Locate a backbone file, could be .md, .asciidoc, etc
BackboneFile.prototype.locate = function() {
    var that = this;
    var filename = this.book.config.getStructure(this.type, true);
    this.log.debug.ln('locating', this.type, ':', filename);

    return this.book.findParsableFile(filename)
    .then(function(result) {
        if (!result) return;

        that.filename = result.path;
        that.parser = result.parser;
    });
};

// Read and parse the file
BackboneFile.prototype.load = function() {
    var that = this;
    this.log.debug.ln('loading', this.type, ':', that.filename);

    return this.locate()
    .then(function() {
        if (!that.filename) return;

        that.log.debug.ln(that.type, 'located at', that.filename);

        return that.book.readFile(that.filename)

        // Parse it
        .then(function(content) {
            return that.parse(content);
        });
    });
};

module.exports = BackboneFile;