diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-04-27 14:11:45 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-04-27 14:11:45 +0200 |
commit | a9d558e89181b3b18151f6344a309ea444526e03 (patch) | |
tree | e8a870fb54453aeb5185816352e8afc6ba42788c /lib/models | |
parent | 8291eb58dffad58ab51abcdf13bc9cfd96d30b91 (diff) | |
download | gitbook-a9d558e89181b3b18151f6344a309ea444526e03.zip gitbook-a9d558e89181b3b18151f6344a309ea444526e03.tar.gz gitbook-a9d558e89181b3b18151f6344a309ea444526e03.tar.bz2 |
Add method ".toText" on glossary and summary
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/__tests__/glossary.js | 31 | ||||
-rw-r--r-- | lib/models/__tests__/summary.js | 9 | ||||
-rw-r--r-- | lib/models/glossary.js | 23 | ||||
-rw-r--r-- | lib/models/summary.js | 24 |
4 files changed, 76 insertions, 11 deletions
diff --git a/lib/models/__tests__/glossary.js b/lib/models/__tests__/glossary.js index 0345627..2ce224c 100644 --- a/lib/models/__tests__/glossary.js +++ b/lib/models/__tests__/glossary.js @@ -5,18 +5,18 @@ describe('Glossary', function() { var Glossary = require('../glossary'); var GlossaryEntry = require('../glossaryEntry'); - describe('createFromEntries', function() { - var glossary = Glossary.createFromEntries(File(), [ - { - name: 'Hello World', - description: 'Awesome!' - }, - { - name: 'JavaScript', - description: 'This is a cool language' - } - ]); + var glossary = Glossary.createFromEntries(File(), [ + { + name: 'Hello World', + description: 'Awesome!' + }, + { + name: 'JavaScript', + description: 'This is a cool language' + } + ]); + describe('createFromEntries', function() { it('must add all entries', function() { var entries = glossary.getEntries(); expect(entries.size).toBe(2); @@ -28,6 +28,15 @@ describe('Glossary', function() { expect(entry instanceof GlossaryEntry).toBeTruthy(); }); }); + + describe('toText', function() { + pit('return as markdown', function() { + return glossary.toText('.md') + .then(function(text) { + expect(text).toContain('# Glossary'); + }); + }); + }); }); diff --git a/lib/models/__tests__/summary.js b/lib/models/__tests__/summary.js index b808307..e7b496e 100644 --- a/lib/models/__tests__/summary.js +++ b/lib/models/__tests__/summary.js @@ -68,6 +68,15 @@ describe('Summary', function() { expect(article.getTitle()).toBe('My Second Article'); }); }); + + describe('toText', function() { + pit('return as markdown', function() { + return summary.toText('.md') + .then(function(text) { + expect(text).toContain('# Summary'); + }); + }); + }); }); diff --git a/lib/models/glossary.js b/lib/models/glossary.js index 51aa370..bb4407d 100644 --- a/lib/models/glossary.js +++ b/lib/models/glossary.js @@ -1,7 +1,9 @@ var Immutable = require('immutable'); +var error = require('../utils/error'); var File = require('./file'); var GlossaryEntry = require('./glossaryEntry'); +var parsers = require('../parsers'); var Glossary = Immutable.Record({ file: File(), @@ -30,6 +32,27 @@ Glossary.prototype.getEntry = function(name) { }; /** + Render glossary as text + + @return {Promise<String>} +*/ +Glossary.prototype.toText = function(parser) { + var file = this.getFile(); + var entries = this.getEntries(); + + parser = parser? parsers.getByExt(parser) : file.getParser(); + + if (!parser) { + throw error.FileNotParsableError({ + filename: file.getPath() + }); + } + + return parser.glossary.toText(entries.toJS()); +}; + + +/** Add/Replace an entry to a glossary @param {Glossary} glossary diff --git a/lib/models/summary.js b/lib/models/summary.js index d8d0603..079c535 100644 --- a/lib/models/summary.js +++ b/lib/models/summary.js @@ -1,8 +1,10 @@ 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(), @@ -64,6 +66,28 @@ Summary.prototype.getByPath = function(filePath) { }; /** + 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 |