Merge branch 'master' of github.com:GitbookIO/gitbook into fork

Conflicts:
	lib/parse/page.js
This commit is contained in:
codepiano
2014-11-02 01:34:28 +08:00
13 changed files with 20806 additions and 53 deletions
+3
View File
@@ -1,5 +1,8 @@
# Release notes
## 1.3.0
- Bundle gitbook parsing library as a client side library in `gitbook.js` and `gitbook.min.js`
## 1.2.0
- Improvements on ebook generation
- Fix incorrect follow of links in ebook generation
+34 -1
View File
@@ -6,6 +6,8 @@ module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks("grunt-bower-install-simple");
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-contrib-uglify');
// Init GRUNT configuraton
grunt.initConfig({
@@ -77,6 +79,30 @@ module.exports = function (grunt) {
}
]
}
},
browserify: {
dist: {
files: {
'gitbook.js': [
'./lib/parse/index.js'
],
},
options: {
postBundleCB: function (err, src, next) {
return next(null, '(function () { var define = undefined; '+src+'; })();')
},
browserifyOptions: {
'standalone': "gitbook"
}
}
}
},
uglify: {
dist: {
files: {
'gitbook.min.js': ['gitbook.js']
}
}
}
});
@@ -90,7 +116,14 @@ module.exports = function (grunt) {
'copy:vendors'
]);
// Bundle the library
grunt.registerTask('bundle', [
'bower-install',
'uglify'
]);
grunt.registerTask('default', [
'build'
'build',
'bundle'
]);
};
+20719
View File
File diff suppressed because one or more lines are too long
+10
View File
File diff suppressed because one or more lines are too long
+1
View File
@@ -74,4 +74,5 @@ module.exports = {
fs.exists(path, d.resolve);
return d.promise;
},
readFileSync: fs.readFileSync.bind(fs)
};
+7 -4
View File
@@ -123,12 +123,15 @@ Generator.prototype.prepareFile = function(content, _input) {
return parse.page(page, {
// Local files path
dir: path.dirname(_input) || '/',
// Project's include folder
includes_dir: path.join(that.options.input, '_includes'),
// Output directory
outdir: path.dirname(_input) || '/',
// Templating variables
variables: that.options.variables,
// Includer for templating
includer: parse.includer(that.options.variables, [
path.dirname(_input) || '/',
path.join(that.options.input, '_includes'),
], path.join, fs.readFileSync)
});
})
.then(function(sections) {
+3 -39
View File
@@ -1,48 +1,12 @@
var _ = require('lodash');
var fs = require('graceful-fs');
var path = require('path');
// Include helper
function importInclude(name, paths) {
return paths
.map(function(folder) {
// Try including snippet from FS
try {
var fname = path.join(folder, name);
// Trim trailing newlines/space of imported snippets
return fs.readFileSync(fname, 'utf8').trimRight();
} catch(err) {}
})
.filter(Boolean)[0];
}
function includer(ctx, folders) {
return function(key) {
key = key.trim();
return ctx[key] || importInclude(key, folders);
};
}
module.exports = function(markdown, folder, ctx) {
// List of folders to search for includes
var folders = [];
// Handle folder arg (string or array)
if(_.isString(folder)) {
folders = [folder];
} else if(_.isArray(folder)) {
folders = folder;
}
// variable context
ctx = ctx || {};
module.exports = function(markdown, includer) {
// Memoized include function (to cache lookups)
var _include = _.memoize(includer(ctx, folders));
var _include = _.memoize(includer);
return markdown.replace(/{{([\s\S]+?)}}/g, function(match, key) {
// If fails leave content as is
key = key.trim();
return _include(key) || match;
});
};
+15
View File
@@ -0,0 +1,15 @@
// Return a fs inclduer
module.exports = function(ctx, folders, resolveFile, readFile) {
return function(name) {
return ctx[name] ||
folders.map(function(folder) {
// Try including snippet from FS
try {
var fname = resolveFile(folder, name);
// Trim trailing newlines/space of imported snippets
return readFile(fname, 'utf8').trimRight();
} catch(err) {}
})
.filter(Boolean)[0];
}
};
+2 -1
View File
@@ -6,5 +6,6 @@ module.exports = {
lex: require('./lex'),
progress: require('./progress'),
navigation: require('./navigation'),
readme: require('./readme')
readme: require('./readme'),
includer: require('./includer')
};
+1 -1
View File
@@ -51,7 +51,7 @@ function parsePage(page, options) {
options = options || {};
// Lex if not already lexed
page.lexed = (_.isArray(page.content) ? page.content : lex(include(page.content, [options.dir, options.includes_dir], options.variables)))
page.lexed = (_.isArray(page.content) ? page.content : lex(include(page.content, options.includer || function() { return undefined; })))
return page.lexed
.map(function(section) {
// Transform given type
+3 -5
View File
@@ -1,9 +1,7 @@
var url = require('url');
var _ = require('lodash');
var inherits = require('util').inherits;
var links = require('../utils').links;
var path = require('path');
var kramed = require('kramed');
var rendererId = 0;
@@ -50,11 +48,11 @@ GitBookRenderer.prototype.link = function(href, title, text) {
// Parsed version of the url
var parsed = url.parse(href);
var o = this._extra_options;
var extname = _.last(parsed.path.split("."));
// Relative link, rewrite it to point to github repo
if(links.isRelative(_href) && path.extname(parsed.path) == ".md") {
if(links.isRelative(_href) && extname == "md") {
_href = links.toAbsolute(_href, o.dir || "./", o.outdir || "./");
_href = _href.replace(".md", ".html");
}
+4 -2
View File
@@ -1,6 +1,6 @@
{
"name": "gitbook",
"version": "1.2.0",
"version": "1.3.0",
"homepage": "http://www.gitbook.io/",
"description": "Library and cmd utility to generate GitBooks",
"main": "lib/index.js",
@@ -35,7 +35,9 @@
"grunt-contrib-copy": "0.5.0",
"grunt-contrib-less": "~0.5.0",
"grunt-contrib-requirejs": "0.4.1",
"grunt-bower-install-simple": "0.9.2"
"grunt-bower-install-simple": "0.9.2",
"grunt-browserify": "3.1.0",
"grunt-contrib-uglify": "0.6.0"
},
"scripts": {
"test": "export TESTING=true; mocha --reporter list"
+4
View File
@@ -3,6 +3,7 @@ var path = require('path');
var assert = require('assert');
var page = require('../').parse.page;
var includer = require('../').parse.includer;
var FIXTURES_DIR = path.join(__dirname, './fixtures/');
@@ -16,6 +17,9 @@ describe('Code includes', function() {
var LEXED = loadPage('INCLUDES', {
'dir': FIXTURES_DIR,
'includer': includer({}, [
FIXTURES_DIR
], path.join, fs.readFileSync)
});
var INCLUDED_C = fs.readFileSync(path.join(FIXTURES_DIR, 'included.c'), 'utf8');