diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-11 11:53:32 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-11 11:53:32 +0100 |
commit | 02cff8c72ca742cd06a65d171657eb230eedbc5b (patch) | |
tree | a56125291f2fbb8a54d3e68210818133e53701fd | |
parent | a67993ac0b08665502c67d9852124d17d66f4072 (diff) | |
download | gitbook-02cff8c72ca742cd06a65d171657eb230eedbc5b.zip gitbook-02cff8c72ca742cd06a65d171657eb230eedbc5b.tar.gz gitbook-02cff8c72ca742cd06a65d171657eb230eedbc5b.tar.bz2 |
Add base for page parsing
-rw-r--r-- | lib/backbone/page.js | 61 | ||||
-rw-r--r-- | lib/book.js | 4 | ||||
-rw-r--r-- | lib/template/index.js | 7 | ||||
-rw-r--r-- | test/output-json.js | 16 |
4 files changed, 76 insertions, 12 deletions
diff --git a/lib/backbone/page.js b/lib/backbone/page.js index 13b9c59..7872181 100644 --- a/lib/backbone/page.js +++ b/lib/backbone/page.js @@ -1,4 +1,8 @@ +var _ = require('lodash'); var path = require('path'); +var parsers = require('gitbook-parsers'); + +var error = require('../utils/error'); /* A page represent a parsable file in the book (Markdown, Asciidoc, etc) @@ -6,28 +10,75 @@ A page represent a parsable file in the book (Markdown, Asciidoc, etc) function Page(book, filename) { if (!(this instanceof Page)) return new Page(book, filename); + var extension; + _.bindAll(this); this.book = book; - this.filename = filename; + this.log = this.book.log; + + this.content = ''; + this.path = filename; + this.rawPath = this.book.resolve(filename); + + // Can we parse it? + extension = path.extname(this.path); + this.parser = parsers.get(extension); + if (!this.parser) throw error.ParsingError(new Error('Can\'t parse file "'+this.path+'"')); + + this.type = this.parser.name; } // Return the filename of the page with another extension // "README.md" -> "README.html" Page.prototype.withExtension = function(ext) { return path.join( - path.dirname(this.filename), - path.basename(this.filename, path.extname(this.filename)) + ext + path.dirname(this.path), + path.basename(this.path, path.extname(this.path)) + ext ); }; +// Update content of the page +Page.prototype.update = function(content) { + this.content = content; +}; + // Read the page as a string Page.prototype.read = function() { - return this.book.readFile(this.filename); + return this.book.readFile(this.path) + .then(this.update); }; // Parse the page and return its content Page.prototype.parse = function() { - return this.read(); + var that = this; + + this.log.debug.ln('start parsing file', this.path); + + return this.read() + + // Pre-process page with parser + .then(function() { + return that.parser.page.prepare(that.content) + .then(that.update); + }) + + // Render template + .then(function() { + return that.book.template.renderString(that.content, { + file: { + path: that.path, + mtime: 0 //todo: stat.mtime + } + }, { + file: that.path + }) + .then(that.update); + }) + + // Render markup + .then(function() { + return that.parser.page(that.content); + }); }; diff --git a/lib/book.js b/lib/book.js index def724b..e762ad9 100644 --- a/lib/book.js +++ b/lib/book.js @@ -10,6 +10,7 @@ var Glossary = require('./backbone/glossary'); var Summary = require('./backbone/summary'); var Langs = require('./backbone/langs'); var Page = require('./backbone/page'); +var TemplateEngine = require('./template'); var pathUtil = require('./utils/path'); var error = require('./utils/error'); var Promise = require('./utils/promise'); @@ -89,6 +90,9 @@ function Book(opts) { // List of page in the book this.pages = {}; + // Templating engine + this.template = new TemplateEngine(this); + _.bindAll(this); } diff --git a/lib/template/index.js b/lib/template/index.js index cd2735f..833ff76 100644 --- a/lib/template/index.js +++ b/lib/template/index.js @@ -292,8 +292,7 @@ TemplateEngine.prototype.applyBlock = function(name, blk, ctx) { // Render a string TemplateEngine.prototype.renderString = function(content, context, options) { options = _.defaults(options || {}, { - path: null, - type: null + path: null }); // Setup context for the template @@ -315,9 +314,9 @@ TemplateEngine.prototype.renderString = function(content, context, options) { if (options.path) { options.path = this.book.resolve(options.path); } - if (!options.type && options.path) { + if (!context.type && options.path) { var parser = parsers.get(path.extname(options.path)); - options.type = parser? parser.name : null; + context.type = parser? parser.name : null; } // Replace shortcuts diff --git a/test/output-json.js b/test/output-json.js index ee345e2..b2387a7 100644 --- a/test/output-json.js +++ b/test/output-json.js @@ -1,11 +1,21 @@ var mock = require('./mock'); describe('JSON Output', function() { - it('should correctly generate a default book', function() { - return mock.outputDefaultBook('json') - .then(function(output) { + + describe('Sample book', function() { + var output; + + before(function() { + return mock.outputDefaultBook('json') + .then(function(_output) { + output = _output; + }); + }); + + it('should correctly generate a README.json', function() { output.should.have.file('README.json'); }); + }); }); |