diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-09-05 11:04:18 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-09-05 11:04:18 +0200 |
commit | a14ca3e268e95a7eab59fb205b41da7331d57631 (patch) | |
tree | 9c84b2cbd561345335fca3e26af961b2ea23d8ec /lib/parse | |
parent | 9c071dade573aa6990878006f83c89b6065a1395 (diff) | |
download | gitbook-a14ca3e268e95a7eab59fb205b41da7331d57631.zip gitbook-a14ca3e268e95a7eab59fb205b41da7331d57631.tar.gz gitbook-a14ca3e268e95a7eab59fb205b41da7331d57631.tar.bz2 |
Switch to lerna
Diffstat (limited to 'lib/parse')
-rw-r--r-- | lib/parse/__tests__/listAssets.js | 29 | ||||
-rw-r--r-- | lib/parse/__tests__/parseBook.js | 90 | ||||
-rw-r--r-- | lib/parse/__tests__/parseGlossary.js | 36 | ||||
-rw-r--r-- | lib/parse/__tests__/parseIgnore.js | 40 | ||||
-rw-r--r-- | lib/parse/__tests__/parsePageFromString.js | 37 | ||||
-rw-r--r-- | lib/parse/__tests__/parseReadme.js | 36 | ||||
-rw-r--r-- | lib/parse/__tests__/parseSummary.js | 34 | ||||
-rw-r--r-- | lib/parse/findParsableFile.js | 36 | ||||
-rw-r--r-- | lib/parse/index.js | 15 | ||||
-rw-r--r-- | lib/parse/listAssets.js | 43 | ||||
-rw-r--r-- | lib/parse/lookupStructureFile.js | 20 | ||||
-rw-r--r-- | lib/parse/parseBook.js | 77 | ||||
-rw-r--r-- | lib/parse/parseConfig.js | 55 | ||||
-rw-r--r-- | lib/parse/parseGlossary.js | 26 | ||||
-rw-r--r-- | lib/parse/parseIgnore.js | 51 | ||||
-rw-r--r-- | lib/parse/parseLanguages.js | 28 | ||||
-rw-r--r-- | lib/parse/parsePage.js | 21 | ||||
-rw-r--r-- | lib/parse/parsePageFromString.js | 22 | ||||
-rw-r--r-- | lib/parse/parsePagesList.js | 78 | ||||
-rw-r--r-- | lib/parse/parseReadme.js | 28 | ||||
-rw-r--r-- | lib/parse/parseStructureFile.js | 67 | ||||
-rw-r--r-- | lib/parse/parseSummary.js | 44 | ||||
-rw-r--r-- | lib/parse/validateConfig.js | 31 | ||||
-rw-r--r-- | lib/parse/walkSummary.js | 34 |
24 files changed, 0 insertions, 978 deletions
diff --git a/lib/parse/__tests__/listAssets.js b/lib/parse/__tests__/listAssets.js deleted file mode 100644 index 4c5b0a0..0000000 --- a/lib/parse/__tests__/listAssets.js +++ /dev/null @@ -1,29 +0,0 @@ -var Immutable = require('immutable'); - -var Book = require('../../models/book'); -var createMockFS = require('../../fs/mock'); -var listAssets = require('../listAssets'); -var parseGlossary = require('../parseGlossary'); - -describe('listAssets', function() { - it('should not list glossary as asset', function() { - var fs = createMockFS({ - 'GLOSSARY.md': '# Glossary\n\n## Hello\nDescription for hello', - 'assetFile.js': '', - 'assets': { - 'file.js': '' - } - }); - var book = Book.createForFS(fs); - - return parseGlossary(book) - .then(function(resultBook) { - return listAssets(resultBook, Immutable.Map()); - }) - .then(function(assets) { - expect(assets.size).toBe(2); - expect(assets.includes('assetFile.js')); - expect(assets.includes('assets/file.js')); - }); - }); -}); diff --git a/lib/parse/__tests__/parseBook.js b/lib/parse/__tests__/parseBook.js deleted file mode 100644 index b1236c9..0000000 --- a/lib/parse/__tests__/parseBook.js +++ /dev/null @@ -1,90 +0,0 @@ -var Book = require('../../models/book'); -var createMockFS = require('../../fs/mock'); - -describe('parseBook', function() { - var parseBook = require('../parseBook'); - - it('should parse multilingual book', function() { - var fs = createMockFS({ - 'LANGS.md': '# Languages\n\n* [en](en)\n* [fr](fr)', - 'en': { - 'README.md': 'Hello' - }, - 'fr': { - 'README.md': 'Bonjour' - } - }); - var book = Book.createForFS(fs); - - return parseBook(book) - .then(function(resultBook) { - var languages = resultBook.getLanguages(); - var books = resultBook.getBooks(); - - expect(resultBook.isMultilingual()).toBe(true); - expect(languages.getList().size).toBe(2); - expect(books.size).toBe(2); - }); - }); - - it('should extend configuration for multilingual book', function() { - var fs = createMockFS({ - 'LANGS.md': '# Languages\n\n* [en](en)\n* [fr](fr)', - 'book.json': '{ "title": "Test", "author": "GitBook" }', - 'en': { - 'README.md': 'Hello', - 'book.json': '{ "title": "Test EN" }' - }, - 'fr': { - 'README.md': 'Bonjour' - } - }); - var book = Book.createForFS(fs); - - return parseBook(book) - .then(function(resultBook) { - var books = resultBook.getBooks(); - - expect(resultBook.isMultilingual()).toBe(true); - expect(books.size).toBe(2); - - var en = books.get('en'); - var fr = books.get('fr'); - - var enConfig = en.getConfig(); - var frConfig = fr.getConfig(); - - expect(enConfig.getValue('title')).toBe('Test EN'); - expect(enConfig.getValue('author')).toBe('GitBook'); - - expect(frConfig.getValue('title')).toBe('Test'); - expect(frConfig.getValue('author')).toBe('GitBook'); - }); - }); - - it('should parse book in a directory', function() { - var fs = createMockFS({ - 'book.json': JSON.stringify({ - root: './test' - }), - 'test': { - 'README.md': 'Hello World', - 'SUMMARY.md': '# Summary\n\n* [Page](page.md)\n', - 'page.md': 'Page' - } - }); - var book = Book.createForFS(fs); - - return parseBook(book) - .then(function(resultBook) { - var readme = resultBook.getReadme(); - var summary = resultBook.getSummary(); - var articles = summary.getArticlesAsList(); - - expect(summary.getFile().exists()).toBe(true); - expect(readme.getFile().exists()).toBe(true); - expect(articles.size).toBe(2); - }); - }); - -}); diff --git a/lib/parse/__tests__/parseGlossary.js b/lib/parse/__tests__/parseGlossary.js deleted file mode 100644 index 9069af6..0000000 --- a/lib/parse/__tests__/parseGlossary.js +++ /dev/null @@ -1,36 +0,0 @@ -var Book = require('../../models/book'); -var createMockFS = require('../../fs/mock'); - -describe('parseGlossary', function() { - var parseGlossary = require('../parseGlossary'); - - it('should parse glossary if exists', function() { - var fs = createMockFS({ - 'GLOSSARY.md': '# Glossary\n\n## Hello\nDescription for hello' - }); - var book = Book.createForFS(fs); - - return parseGlossary(book) - .then(function(resultBook) { - var glossary = resultBook.getGlossary(); - var file = glossary.getFile(); - var entries = glossary.getEntries(); - - expect(file.exists()).toBeTruthy(); - expect(entries.size).toBe(1); - }); - }); - - it('should not fail if doesn\'t exist', function() { - var fs = createMockFS({}); - var book = Book.createForFS(fs); - - return parseGlossary(book) - .then(function(resultBook) { - var glossary = resultBook.getGlossary(); - var file = glossary.getFile(); - - expect(file.exists()).toBeFalsy(); - }); - }); -}); diff --git a/lib/parse/__tests__/parseIgnore.js b/lib/parse/__tests__/parseIgnore.js deleted file mode 100644 index 54e7dae..0000000 --- a/lib/parse/__tests__/parseIgnore.js +++ /dev/null @@ -1,40 +0,0 @@ -var Book = require('../../models/book'); -var createMockFS = require('../../fs/mock'); - -describe('parseIgnore', function() { - var parseIgnore = require('../parseIgnore'); - var fs = createMockFS({ - '.ignore': 'test-1.js', - '.gitignore': 'test-2.js\ntest-3.js', - '.bookignore': '!test-3.js', - 'test-1.js': '1', - 'test-2.js': '2', - 'test-3.js': '3' - }); - - function getBook() { - var book = Book.createForFS(fs); - return parseIgnore(book); - } - - it('should load rules from .ignore', function() { - return getBook() - .then(function(book) { - expect(book.isFileIgnored('test-1.js')).toBeTruthy(); - }); - }); - - it('should load rules from .gitignore', function() { - return getBook() - .then(function(book) { - expect(book.isFileIgnored('test-2.js')).toBeTruthy(); - }); - }); - - it('should load rules from .bookignore', function() { - return getBook() - .then(function(book) { - expect(book.isFileIgnored('test-3.js')).toBeFalsy(); - }); - }); -}); diff --git a/lib/parse/__tests__/parsePageFromString.js b/lib/parse/__tests__/parsePageFromString.js deleted file mode 100644 index 2911fa3..0000000 --- a/lib/parse/__tests__/parsePageFromString.js +++ /dev/null @@ -1,37 +0,0 @@ -var parsePageFromString = require('../parsePageFromString'); -var Page = require('../../models/page'); - -describe('parsePageFromString', function() { - var page = new Page(); - - it('should parse YAML frontmatter', function() { - var CONTENT = '---\nhello: true\nworld: "cool"\n---\n# Hello World\n'; - var newPage = parsePageFromString(page, CONTENT); - - expect(newPage.getDir()).toBe('ltr'); - expect(newPage.getContent()).toBe('# Hello World\n'); - - var attrs = newPage.getAttributes(); - expect(attrs.size).toBe(2); - expect(attrs.get('hello')).toBe(true); - expect(attrs.get('world')).toBe('cool'); - }); - - it('should parse text direction (english)', function() { - var CONTENT = 'Hello World'; - var newPage = parsePageFromString(page, CONTENT); - - expect(newPage.getDir()).toBe('ltr'); - expect(newPage.getContent()).toBe('Hello World'); - expect(newPage.getAttributes().size).toBe(0); - }); - - it('should parse text direction (arab)', function() { - var CONTENT = 'مرحبا بالعالم'; - var newPage = parsePageFromString(page, CONTENT); - - expect(newPage.getDir()).toBe('rtl'); - expect(newPage.getContent()).toBe('مرحبا بالعالم'); - expect(newPage.getAttributes().size).toBe(0); - }); -}); diff --git a/lib/parse/__tests__/parseReadme.js b/lib/parse/__tests__/parseReadme.js deleted file mode 100644 index 4270ea3..0000000 --- a/lib/parse/__tests__/parseReadme.js +++ /dev/null @@ -1,36 +0,0 @@ -var Promise = require('../../utils/promise'); -var Book = require('../../models/book'); -var createMockFS = require('../../fs/mock'); - -describe('parseReadme', function() { - var parseReadme = require('../parseReadme'); - - it('should parse summary if exists', function() { - var fs = createMockFS({ - 'README.md': '# Hello\n\nAnd here is the description.' - }); - var book = Book.createForFS(fs); - - return parseReadme(book) - .then(function(resultBook) { - var readme = resultBook.getReadme(); - var file = readme.getFile(); - - expect(file.exists()).toBeTruthy(); - expect(readme.getTitle()).toBe('Hello'); - expect(readme.getDescription()).toBe('And here is the description.'); - }); - }); - - it('should fail if doesn\'t exist', function() { - var fs = createMockFS({}); - var book = Book.createForFS(fs); - - return parseReadme(book) - .then(function(resultBook) { - throw new Error('It should have fail'); - }, function() { - return Promise(); - }); - }); -}); diff --git a/lib/parse/__tests__/parseSummary.js b/lib/parse/__tests__/parseSummary.js deleted file mode 100644 index 55a445e..0000000 --- a/lib/parse/__tests__/parseSummary.js +++ /dev/null @@ -1,34 +0,0 @@ -var Book = require('../../models/book'); -var createMockFS = require('../../fs/mock'); - -describe('parseSummary', function() { - var parseSummary = require('../parseSummary'); - - it('should parse summary if exists', function() { - var fs = createMockFS({ - 'SUMMARY.md': '# Summary\n\n* [Hello](hello.md)' - }); - var book = Book.createForFS(fs); - - return parseSummary(book) - .then(function(resultBook) { - var summary = resultBook.getSummary(); - var file = summary.getFile(); - - expect(file.exists()).toBeTruthy(); - }); - }); - - it('should not fail if doesn\'t exist', function() { - var fs = createMockFS({}); - var book = Book.createForFS(fs); - - return parseSummary(book) - .then(function(resultBook) { - var summary = resultBook.getSummary(); - var file = summary.getFile(); - - expect(file.exists()).toBeFalsy(); - }); - }); -}); diff --git a/lib/parse/findParsableFile.js b/lib/parse/findParsableFile.js deleted file mode 100644 index 51e2dd0..0000000 --- a/lib/parse/findParsableFile.js +++ /dev/null @@ -1,36 +0,0 @@ -var path = require('path'); - -var Promise = require('../utils/promise'); -var parsers = require('../parsers'); - -/** - Find a file parsable (Markdown or AsciiDoc) in a book - - @param {Book} book - @param {String} filename - @return {Promise<File | Undefined>} -*/ -function findParsableFile(book, filename) { - var fs = book.getContentFS(); - var ext = path.extname(filename); - var basename = path.basename(filename, ext); - var basedir = path.dirname(filename); - - // Ordered list of extensions to test - var exts = parsers.extensions; - - return Promise.some(exts, function(ext) { - var filepath = basename + ext; - - return fs.findFile(basedir, filepath) - .then(function(found) { - if (!found || book.isContentFileIgnored(found)) { - return undefined; - } - - return fs.statFile(found); - }); - }); -} - -module.exports = findParsableFile; diff --git a/lib/parse/index.js b/lib/parse/index.js deleted file mode 100644 index 1f73946..0000000 --- a/lib/parse/index.js +++ /dev/null @@ -1,15 +0,0 @@ - -module.exports = { - parseBook: require('./parseBook'), - parseSummary: require('./parseSummary'), - parseGlossary: require('./parseGlossary'), - parseReadme: require('./parseReadme'), - parseConfig: require('./parseConfig'), - parsePagesList: require('./parsePagesList'), - parseIgnore: require('./parseIgnore'), - listAssets: require('./listAssets'), - parseLanguages: require('./parseLanguages'), - parsePage: require('./parsePage'), - parsePageFromString: require('./parsePageFromString'), - lookupStructureFile: require('./lookupStructureFile') -}; diff --git a/lib/parse/listAssets.js b/lib/parse/listAssets.js deleted file mode 100644 index d83d8fd..0000000 --- a/lib/parse/listAssets.js +++ /dev/null @@ -1,43 +0,0 @@ -var timing = require('../utils/timing'); - -/** - List all assets in a book - Assets are file not ignored and not a page - - @param {Book} book - @param {List<String>} pages - @param -*/ -function listAssets(book, pages) { - var fs = book.getContentFS(); - - var summary = book.getSummary(); - var summaryFile = summary.getFile().getPath(); - - var glossary = book.getGlossary(); - var glossaryFile = glossary.getFile().getPath(); - - var langs = book.getLanguages(); - var langsFile = langs.getFile().getPath(); - - var config = book.getConfig(); - var configFile = config.getFile().getPath(); - - function filterFile(file) { - return !( - file === summaryFile || - file === glossaryFile || - file === langsFile || - file === configFile || - book.isContentFileIgnored(file) || - pages.has(file) - ); - } - - return timing.measure( - 'parse.listAssets', - fs.listAllFiles('.', filterFile) - ); -} - -module.exports = listAssets; diff --git a/lib/parse/lookupStructureFile.js b/lib/parse/lookupStructureFile.js deleted file mode 100644 index 36b37f8..0000000 --- a/lib/parse/lookupStructureFile.js +++ /dev/null @@ -1,20 +0,0 @@ -var findParsableFile = require('./findParsableFile'); - -/** - Lookup a structure file (ex: SUMMARY.md, GLOSSARY.md) in a book. Uses - book's config to find it. - - @param {Book} book - @param {String} type: one of ["glossary", "readme", "summary", "langs"] - @return {Promise<File | Undefined>} The path of the file found, relative - to the book content root. -*/ -function lookupStructureFile(book, type) { - var config = book.getConfig(); - - var fileToSearch = config.getValue(['structure', type]); - - return findParsableFile(book, fileToSearch); -} - -module.exports = lookupStructureFile; diff --git a/lib/parse/parseBook.js b/lib/parse/parseBook.js deleted file mode 100644 index a92f39e..0000000 --- a/lib/parse/parseBook.js +++ /dev/null @@ -1,77 +0,0 @@ -var Promise = require('../utils/promise'); -var timing = require('../utils/timing'); -var Book = require('../models/book'); - -var parseIgnore = require('./parseIgnore'); -var parseConfig = require('./parseConfig'); -var parseGlossary = require('./parseGlossary'); -var parseSummary = require('./parseSummary'); -var parseReadme = require('./parseReadme'); -var parseLanguages = require('./parseLanguages'); - -/** - Parse content of a book - - @param {Book} book - @return {Promise<Book>} -*/ -function parseBookContent(book) { - return Promise(book) - .then(parseReadme) - .then(parseSummary) - .then(parseGlossary); -} - -/** - Parse a multilingual book - - @param {Book} book - @return {Promise<Book>} -*/ -function parseMultilingualBook(book) { - var languages = book.getLanguages(); - var langList = languages.getList(); - - return Promise.reduce(langList, function(currentBook, lang) { - var langID = lang.getID(); - var child = Book.createFromParent(currentBook, langID); - var ignore = currentBook.getIgnore(); - - return Promise(child) - .then(parseConfig) - .then(parseBookContent) - .then(function(result) { - // Ignore content of this book when generating parent book - ignore = ignore.add(langID + '/**'); - currentBook = currentBook.set('ignore', ignore); - - return currentBook.addLanguageBook(langID, result); - }); - }, book); -} - - -/** - Parse a whole book from a filesystem - - @param {Book} book - @return {Promise<Book>} -*/ -function parseBook(book) { - return timing.measure( - 'parse.book', - Promise(book) - .then(parseIgnore) - .then(parseConfig) - .then(parseLanguages) - .then(function(resultBook) { - if (resultBook.isMultilingual()) { - return parseMultilingualBook(resultBook); - } else { - return parseBookContent(resultBook); - } - }) - ); -} - -module.exports = parseBook; diff --git a/lib/parse/parseConfig.js b/lib/parse/parseConfig.js deleted file mode 100644 index a411af8..0000000 --- a/lib/parse/parseConfig.js +++ /dev/null @@ -1,55 +0,0 @@ -var Promise = require('../utils/promise'); - -var validateConfig = require('./validateConfig'); -var CONFIG_FILES = require('../constants/configFiles'); - -/** - Parse configuration from "book.json" or "book.js" - - @param {Book} book - @return {Promise<Book>} -*/ -function parseConfig(book) { - var fs = book.getFS(); - var config = book.getConfig(); - - return Promise.some(CONFIG_FILES, function(filename) { - // Is this file ignored? - if (book.isFileIgnored(filename)) { - return; - } - - // Try loading it - return fs.loadAsObject(filename) - .then(function(cfg) { - return fs.statFile(filename) - .then(function(file) { - return { - file: file, - values: cfg - }; - }); - }) - .fail(function(err) { - if (err.code != 'MODULE_NOT_FOUND') throw(err); - else return Promise(false); - }); - }) - - .then(function(result) { - var values = result? result.values : {}; - values = validateConfig(values); - - // Set the file - if (result.file) { - config = config.setFile(result.file); - } - - // Merge with old values - config = config.mergeValues(values); - - return book.setConfig(config); - }); -} - -module.exports = parseConfig; diff --git a/lib/parse/parseGlossary.js b/lib/parse/parseGlossary.js deleted file mode 100644 index a96e5fc..0000000 --- a/lib/parse/parseGlossary.js +++ /dev/null @@ -1,26 +0,0 @@ -var parseStructureFile = require('./parseStructureFile'); -var Glossary = require('../models/glossary'); - -/** - Parse glossary - - @param {Book} book - @return {Promise<Book>} -*/ -function parseGlossary(book) { - var logger = book.getLogger(); - - return parseStructureFile(book, 'glossary') - .spread(function(file, entries) { - if (!file) { - return book; - } - - logger.debug.ln('glossary index file found at', file.getPath()); - - var glossary = Glossary.createFromEntries(file, entries); - return book.set('glossary', glossary); - }); -} - -module.exports = parseGlossary; diff --git a/lib/parse/parseIgnore.js b/lib/parse/parseIgnore.js deleted file mode 100644 index 84d8c33..0000000 --- a/lib/parse/parseIgnore.js +++ /dev/null @@ -1,51 +0,0 @@ -var Promise = require('../utils/promise'); -var IGNORE_FILES = require('../constants/ignoreFiles'); - -var DEFAULT_IGNORES = [ - // Skip Git stuff - '.git/', - - // Skip OS X meta data - '.DS_Store', - - // Skip stuff installed by plugins - 'node_modules', - - // Skip book outputs - '_book', - - // Ignore files in the templates folder - '_layouts' -]; - -/** - Parse ignore files - - @param {Book} - @return {Book} -*/ -function parseIgnore(book) { - if (book.isLanguageBook()) { - return Promise.reject(new Error('Ignore files could be parsed for language books')); - } - - var fs = book.getFS(); - var ignore = book.getIgnore(); - - ignore = ignore.add(DEFAULT_IGNORES); - - return Promise.serie(IGNORE_FILES, function(filename) { - return fs.readAsString(filename) - .then(function(content) { - ignore = ignore.add(content.toString().split(/\r?\n/)); - }, function(err) { - return Promise(); - }); - }) - - .then(function() { - return book.setIgnore(ignore); - }); -} - -module.exports = parseIgnore; diff --git a/lib/parse/parseLanguages.js b/lib/parse/parseLanguages.js deleted file mode 100644 index 346f3a3..0000000 --- a/lib/parse/parseLanguages.js +++ /dev/null @@ -1,28 +0,0 @@ -var parseStructureFile = require('./parseStructureFile'); -var Languages = require('../models/languages'); - -/** - Parse languages list from book - - @param {Book} book - @return {Promise<Book>} -*/ -function parseLanguages(book) { - var logger = book.getLogger(); - - return parseStructureFile(book, 'langs') - .spread(function(file, result) { - if (!file) { - return book; - } - - var languages = Languages.createFromList(file, result); - - logger.debug.ln('languages index file found at', file.getPath()); - logger.info.ln('parsing multilingual book, with', languages.getList().size, 'languages'); - - return book.set('languages', languages); - }); -} - -module.exports = parseLanguages; diff --git a/lib/parse/parsePage.js b/lib/parse/parsePage.js deleted file mode 100644 index fdc56a3..0000000 --- a/lib/parse/parsePage.js +++ /dev/null @@ -1,21 +0,0 @@ -var parsePageFromString = require('./parsePageFromString'); - -/** - * Parse a page, read its content and parse the YAMl header - * - * @param {Book} book - * @param {Page} page - * @return {Promise<Page>} - */ -function parsePage(book, page) { - var fs = book.getContentFS(); - var file = page.getFile(); - - return fs.readAsString(file.getPath()) - .then(function(content) { - return parsePageFromString(page, content); - }); -} - - -module.exports = parsePage; diff --git a/lib/parse/parsePageFromString.js b/lib/parse/parsePageFromString.js deleted file mode 100644 index 80c147b..0000000 --- a/lib/parse/parsePageFromString.js +++ /dev/null @@ -1,22 +0,0 @@ -var Immutable = require('immutable'); -var fm = require('front-matter'); -var direction = require('direction'); - -/** - * Parse a page, its content and the YAMl header - * - * @param {Page} page - * @return {Page} - */ -function parsePageFromString(page, content) { - var parsed = fm(content); - - return page.merge({ - content: parsed.body, - attributes: Immutable.fromJS(parsed.attributes), - dir: direction(parsed.body) - }); -} - - -module.exports = parsePageFromString; diff --git a/lib/parse/parsePagesList.js b/lib/parse/parsePagesList.js deleted file mode 100644 index 1cf42f5..0000000 --- a/lib/parse/parsePagesList.js +++ /dev/null @@ -1,78 +0,0 @@ -var Immutable = require('immutable'); - -var timing = require('../utils/timing'); -var Page = require('../models/page'); -var walkSummary = require('./walkSummary'); -var parsePage = require('./parsePage'); - - -/** - Parse a page from a path - - @param {Book} book - @param {String} filePath - @return {Page} -*/ -function parseFilePage(book, filePath) { - var fs = book.getContentFS(); - - return fs.statFile(filePath) - .then(function(file) { - var page = Page.createForFile(file); - return parsePage(book, page); - }); -} - - -/** - Parse all pages from a book as an OrderedMap - - @param {Book} book - @return {Promise<OrderedMap<Page>>} -*/ -function parsePagesList(book) { - var summary = book.getSummary(); - var glossary = book.getGlossary(); - var map = Immutable.OrderedMap(); - - // Parse pages from summary - return timing.measure( - 'parse.listPages', - walkSummary(summary, function(article) { - if (!article.isPage()) return; - - var filepath = article.getPath(); - - // Is the page ignored? - if (book.isContentFileIgnored(filepath)) return; - - return parseFilePage(book, filepath) - .then(function(page) { - map = map.set(filepath, page); - }, function() { - // file doesn't exist - }); - }) - ) - - // Parse glossary - .then(function() { - var file = glossary.getFile(); - - if (!file.exists()) { - return; - } - - return parseFilePage(book, file.getPath()) - .then(function(page) { - map = map.set(file.getPath(), page); - }); - }) - - .then(function() { - return map; - }); -} - - -module.exports = parsePagesList; diff --git a/lib/parse/parseReadme.js b/lib/parse/parseReadme.js deleted file mode 100644 index a2ede77..0000000 --- a/lib/parse/parseReadme.js +++ /dev/null @@ -1,28 +0,0 @@ -var parseStructureFile = require('./parseStructureFile'); -var Readme = require('../models/readme'); - -var error = require('../utils/error'); - -/** - Parse readme from book - - @param {Book} book - @return {Promise<Book>} -*/ -function parseReadme(book) { - var logger = book.getLogger(); - - return parseStructureFile(book, 'readme') - .spread(function(file, result) { - if (!file) { - throw new error.FileNotFoundError({ filename: 'README' }); - } - - logger.debug.ln('readme found at', file.getPath()); - - var readme = Readme.create(file, result); - return book.set('readme', readme); - }); -} - -module.exports = parseReadme; diff --git a/lib/parse/parseStructureFile.js b/lib/parse/parseStructureFile.js deleted file mode 100644 index 718f731..0000000 --- a/lib/parse/parseStructureFile.js +++ /dev/null @@ -1,67 +0,0 @@ -var Promise = require('../utils/promise'); -var error = require('../utils/error'); -var lookupStructureFile = require('./lookupStructureFile'); - -/** - Parse a ParsableFile using a specific method - - @param {FS} fs - @param {ParsableFile} file - @param {String} type - @return {Promise<Array<String, List|Map>>} -*/ -function parseFile(fs, file, type) { - var filepath = file.getPath(); - var parser = file.getParser(); - - if (!parser) { - return Promise.reject( - error.FileNotParsableError({ - filename: filepath - }) - ); - } - - return fs.readAsString(filepath) - .then(function(content) { - if (type === 'readme') { - return parser.parseReadme(content); - } else if (type === 'glossary') { - return parser.parseGlossary(content); - } else if (type === 'summary') { - return parser.parseSummary(content); - } else if (type === 'langs') { - return parser.parseLanguages(content); - } else { - throw new Error('Parsing invalid type "' + type + '"'); - } - }) - .then(function(result) { - return [ - file, - result - ]; - }); -} - - -/** - Parse a structure file (ex: SUMMARY.md, GLOSSARY.md). - It uses the configuration to find the specified file. - - @param {Book} book - @param {String} type: one of ["glossary", "readme", "summary"] - @return {Promise<List|Map>} -*/ -function parseStructureFile(book, type) { - var fs = book.getContentFS(); - - return lookupStructureFile(book, type) - .then(function(file) { - if (!file) return [undefined, undefined]; - - return parseFile(fs, file, type); - }); -} - -module.exports = parseStructureFile; diff --git a/lib/parse/parseSummary.js b/lib/parse/parseSummary.js deleted file mode 100644 index 2c1e3b3..0000000 --- a/lib/parse/parseSummary.js +++ /dev/null @@ -1,44 +0,0 @@ -var parseStructureFile = require('./parseStructureFile'); -var Summary = require('../models/summary'); -var SummaryModifier = require('../modifiers').Summary; - -/** - Parse summary in a book, the summary can only be parsed - if the readme as be detected before. - - @param {Book} book - @return {Promise<Book>} -*/ -function parseSummary(book) { - var readme = book.getReadme(); - var logger = book.getLogger(); - var readmeFile = readme.getFile(); - - return parseStructureFile(book, 'summary') - .spread(function(file, result) { - var summary; - - if (!file) { - logger.warn.ln('no summary file in this book'); - summary = Summary(); - } else { - logger.debug.ln('summary file found at', file.getPath()); - summary = Summary.createFromParts(file, result.parts); - } - - // Insert readme as first entry if not in SUMMARY.md - var readmeArticle = summary.getByPath(readmeFile.getPath()); - - if (readmeFile.exists() && !readmeArticle) { - summary = SummaryModifier.unshiftArticle(summary, { - title: 'Introduction', - ref: readmeFile.getPath() - }); - } - - // Set new summary - return book.setSummary(summary); - }); -} - -module.exports = parseSummary; diff --git a/lib/parse/validateConfig.js b/lib/parse/validateConfig.js deleted file mode 100644 index 21294ac..0000000 --- a/lib/parse/validateConfig.js +++ /dev/null @@ -1,31 +0,0 @@ -var jsonschema = require('jsonschema'); -var jsonSchemaDefaults = require('json-schema-defaults'); - -var schema = require('../constants/configSchema'); -var error = require('../utils/error'); -var mergeDefaults = require('../utils/mergeDefaults'); - -/** - Validate a book.json content - And return a mix with the default value - - @param {Object} bookJson - @return {Object} -*/ -function validateConfig(bookJson) { - var v = new jsonschema.Validator(); - var result = v.validate(bookJson, schema, { - propertyName: 'config' - }); - - // Throw error - if (result.errors.length > 0) { - throw new error.ConfigurationError(new Error(result.errors[0].stack)); - } - - // Insert default values - var defaults = jsonSchemaDefaults(schema); - return mergeDefaults(bookJson, defaults); -} - -module.exports = validateConfig; diff --git a/lib/parse/walkSummary.js b/lib/parse/walkSummary.js deleted file mode 100644 index 0117752..0000000 --- a/lib/parse/walkSummary.js +++ /dev/null @@ -1,34 +0,0 @@ -var Promise = require('../utils/promise'); - -/** - Walk over a list of articles - - @param {List<Article>} articles - @param {Function(article)} - @return {Promise} -*/ -function walkArticles(articles, fn) { - return Promise.forEach(articles, function(article) { - return Promise(fn(article)) - .then(function() { - return walkArticles(article.getArticles(), fn); - }); - }); -} - -/** - Walk over summary and execute "fn" on each article - - @param {Summary} summary - @param {Function(article)} - @return {Promise} -*/ -function walkSummary(summary, fn) { - var parts = summary.getParts(); - - return Promise.forEach(parts, function(part) { - return walkArticles(part.getArticles(), fn); - }); -} - -module.exports = walkSummary; |