mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-24 03:16:40 +00:00
Use block code for highlight pre/code
Cleanup correctly predefined blocks
This commit is contained in:
+1
-1
@@ -79,7 +79,7 @@ PluginsList.prototype.load = function(plugin, options) {
|
||||
}
|
||||
|
||||
// Extract filters
|
||||
that.book.template.addFilters(plugin.getFilters());
|
||||
that.book.template.addFilters(plugin.getFilters());
|
||||
|
||||
// Extract blocks
|
||||
that.book.template.addBlocks(plugin.getBlocks());
|
||||
|
||||
+67
-11
@@ -10,6 +10,11 @@ var batch = require("./utils/batch");
|
||||
var pkg = require("../package.json");
|
||||
var defaultBlocks = require("./blocks");
|
||||
|
||||
// Normalize result from a block
|
||||
function normBlockResult(blk) {
|
||||
if (_.isString(blk)) blk = { body: blk };
|
||||
return blk;
|
||||
}
|
||||
|
||||
// The loader should handle relative and git url
|
||||
var BookLoader = nunjucks.Loader.extend({
|
||||
@@ -85,10 +90,8 @@ var TemplateEngine = function(book) {
|
||||
this.addBlocks(defaultBlocks);
|
||||
};
|
||||
|
||||
// Process a block in a context
|
||||
// Process the result of block in a context
|
||||
TemplateEngine.prototype.processBlock = function(blk) {
|
||||
if (_.isString(blk)) blk = { body: blk };
|
||||
|
||||
blk = _.defaults(blk, {
|
||||
parse: false,
|
||||
post: undefined
|
||||
@@ -169,9 +172,32 @@ TemplateEngine.prototype.addFilters = function(filters) {
|
||||
}, this);
|
||||
};
|
||||
|
||||
// Return nunjucks extension name of a block
|
||||
TemplateEngine.prototype.blockExtName = function(name) {
|
||||
return 'Block'+name+'Extension';
|
||||
};
|
||||
|
||||
// Test if a block is defined
|
||||
TemplateEngine.prototype.hasBlock = function(name) {
|
||||
return this.env.hasExtension(this.blockExtName(name));
|
||||
};
|
||||
|
||||
// Remove a block
|
||||
TemplateEngine.prototype.removeBlock = function(name) {
|
||||
if (!this.hasBlock(name)) return;
|
||||
|
||||
// Remove nunjucks extension
|
||||
this.env.removeExtension(this.blockExtName(name));
|
||||
|
||||
// Cleanup shortcuts
|
||||
this.shortcuts = _.reject(this.shortcuts, {
|
||||
block: name
|
||||
});
|
||||
};
|
||||
|
||||
// Add a block
|
||||
TemplateEngine.prototype.addBlock = function(name, block) {
|
||||
var that = this;
|
||||
var that = this, Ext, extName;
|
||||
|
||||
if (_.isFunction(block)) block = { process: block };
|
||||
|
||||
@@ -182,12 +208,15 @@ TemplateEngine.prototype.addBlock = function(name, block) {
|
||||
blocks: []
|
||||
});
|
||||
|
||||
var extName = 'Block'+name+'Extension';
|
||||
if (this.env.getExtension(extName)) {
|
||||
var extName = this.blockExtName(name);
|
||||
|
||||
if (this.hasBlock(name) && !defaultBlocks[name]) {
|
||||
this.log.warn.ln("conflict in blocks, '"+name+"' is already defined");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Cleanup previous block
|
||||
this.removeBlock(name);
|
||||
|
||||
this.log.debug.ln("add block '"+name+"'");
|
||||
this.blocks[name] = block;
|
||||
|
||||
@@ -272,11 +301,9 @@ TemplateEngine.prototype.addBlock = function(name, block) {
|
||||
};
|
||||
});
|
||||
|
||||
var func = that.bindContext(block.process);
|
||||
|
||||
Q()
|
||||
.then(function() {
|
||||
return func.call(context, {
|
||||
return that.applyBlock(name, {
|
||||
body: body(),
|
||||
args: args,
|
||||
kwargs: kwargs,
|
||||
@@ -290,7 +317,6 @@ TemplateEngine.prototype.addBlock = function(name, block) {
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
// Add the Extension
|
||||
this.env.addExtension(extName, new Ext());
|
||||
|
||||
@@ -299,6 +325,7 @@ TemplateEngine.prototype.addBlock = function(name, block) {
|
||||
_.each(block.shortcuts, function(shortcut) {
|
||||
this.log.debug.ln("add template shortcut from '"+shortcut.start+"' to block '"+name+"' for parsers ", shortcut.parsers);
|
||||
this.shortcuts.push({
|
||||
block: name,
|
||||
parsers: shortcut.parsers,
|
||||
start: shortcut.start,
|
||||
end: shortcut.end,
|
||||
@@ -312,11 +339,40 @@ TemplateEngine.prototype.addBlock = function(name, block) {
|
||||
|
||||
// Add multiple blocks
|
||||
TemplateEngine.prototype.addBlocks = function(blocks) {
|
||||
console.log('add blocks', blocks);
|
||||
_.each(blocks, function(block, name) {
|
||||
this.addBlock(name, block);
|
||||
}, this);
|
||||
};
|
||||
|
||||
// Apply a block to some content
|
||||
// This method result depends on the type of block (async or sync)
|
||||
TemplateEngine.prototype.applyBlock = function(name, blk) {
|
||||
var func, block, func, r;
|
||||
|
||||
block = this.blocks[name];
|
||||
console.log('applyBlock', name, block);
|
||||
if (!block) throw new Error('Block not found "'+name+'"');
|
||||
if (_.isString(blk)) {
|
||||
blk = {
|
||||
body: blk
|
||||
};
|
||||
}
|
||||
|
||||
blk = _.defaults(blk, {
|
||||
args: [],
|
||||
kwargs: {},
|
||||
blocks: []
|
||||
});
|
||||
|
||||
// Bind and call block processor
|
||||
func = this.bindContext(block.process);
|
||||
r = func.call(context, blk);
|
||||
|
||||
if (Q.isPromise(r)) return r.then(normBlockResult);
|
||||
else return normBlockResult(r);
|
||||
};
|
||||
|
||||
// Apply a shortcut to a string
|
||||
TemplateEngine.prototype._applyShortcut = function(parser, content, shortcut) {
|
||||
if (!_.contains(shortcut.parsers, parser)) return content;
|
||||
|
||||
+4
-5
@@ -223,7 +223,7 @@ function normalizeHtml(src, options) {
|
||||
|
||||
// Highlight code blocks
|
||||
$("code").each(function() {
|
||||
// Extract language
|
||||
// Normalize language
|
||||
var lang = _.chain(
|
||||
($(this).attr("class") || "").split(" ")
|
||||
)
|
||||
@@ -240,11 +240,10 @@ function normalizeHtml(src, options) {
|
||||
.first()
|
||||
.value();
|
||||
|
||||
if (lang) {
|
||||
var html = $(this).text();
|
||||
var source = $(this).text();
|
||||
var html = options.book.template.applyBlock('code', source).body;
|
||||
|
||||
$(this).html(html);
|
||||
}
|
||||
$(this).html(html);
|
||||
});
|
||||
|
||||
// Replace glossary terms
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
"fs-extra": "0.16.5",
|
||||
"fstream-ignore": "1.0.2",
|
||||
"gitbook-parsers": "0.8.2",
|
||||
"nunjucks": "mozilla/nunjucks#b4351ec0752b102ebd02c5e2cbc2898a6e0e4839",
|
||||
"nunjucks": "mozilla/nunjucks#146a63571f1910d57cc391dfe64f3789ba1f1fd1",
|
||||
"nunjucks-autoescape": "1.0.0",
|
||||
"nunjucks-filter": "1.0.0",
|
||||
"i18n": "0.5.0",
|
||||
|
||||
Reference in New Issue
Block a user