diff options
Diffstat (limited to 'lib/utils')
-rw-r--r-- | lib/utils/code.js | 36 | ||||
-rw-r--r-- | lib/utils/fs.js | 4 | ||||
-rw-r--r-- | lib/utils/git.js | 2 | ||||
-rw-r--r-- | lib/utils/page.js | 41 | ||||
-rw-r--r-- | lib/utils/watch.js | 1 |
5 files changed, 37 insertions, 47 deletions
diff --git a/lib/utils/code.js b/lib/utils/code.js deleted file mode 100644 index 0d98869..0000000 --- a/lib/utils/code.js +++ /dev/null @@ -1,36 +0,0 @@ -var hljs = require('highlight.js'); - -var MAP = { - 'py': 'python', - 'js': 'javascript', - 'json': 'javascript', - 'rb': 'ruby', - 'csharp': 'cs', -}; - -function normalize(lang) { - if(!lang) { return null; } - - var lower = lang.toLowerCase(); - return MAP[lower] || lower; -} - -function highlight(lang, code) { - if(!lang) return code; - - // Normalize lang - lang = normalize(lang); - - try { - return hljs.highlight(lang, code).value; - } catch(e) { } - - return code; -} - -// Exports -module.exports = { - highlight: highlight, - normalize: normalize, - MAP: MAP -}; diff --git a/lib/utils/fs.js b/lib/utils/fs.js index 98a3a87..176a215 100644 --- a/lib/utils/fs.js +++ b/lib/utils/fs.js @@ -64,6 +64,10 @@ function writeStream(filename, st) { d.reject(err); }); + st.on('error', function(err) { + d.reject(err); + }); + st.pipe(wstream); return d.promise; diff --git a/lib/utils/git.js b/lib/utils/git.js index 2c3dd3f..5f17395 100644 --- a/lib/utils/git.js +++ b/lib/utils/git.js @@ -83,7 +83,7 @@ function cloneGitRepo(host, ref) { return exec("git clone "+host+" "+repoPath) .then(function() { return exec("git checkout "+ref, { cwd: repoPath }); - }) + }); }) .thenResolve(repoPath); }); diff --git a/lib/utils/page.js b/lib/utils/page.js index f24013f..5b4eca8 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -11,7 +11,8 @@ var links = require('./links'); var imgUtils = require('./images'); var fs = require('./fs'); var batch = require('./batch'); -var code = require('./code'); + +var parsableExtensions = require('gitbook-parsers').extensions; // Render a cheerio dom as html var renderDom = function($, dom, options) { @@ -196,11 +197,19 @@ function normalizeHtml(src, options) { var absolutePath = links.join(options.base, parts.pathname); var anchor = parts.hash || ""; + // If is in navigation relative: transform as content if (options.navigation[absolutePath]) { absolutePath = options.book.contentLink(absolutePath); } + // If md/adoc/rst files is not in summary + // or for ebook, signal all files that are outside the summary + else if (_.contains(parsableExtensions, path.extname(absolutePath)) + || _.contains(['epub', 'pdf', 'mobi'], options.book.options.generator)) { + options.book.log.warn.ln("page", options.input, "contains an hyperlink to resource outside spine '"+href+"'"); + } + // Transform as absolute href = links.toAbsolute("/"+absolutePath, options.base, options.output)+anchor; } else { @@ -214,26 +223,32 @@ function normalizeHtml(src, options) { // Highlight code blocks $("code").each(function() { - // Extract language + // Normalize language var lang = _.chain( ($(this).attr("class") || "").split(" ") ) .map(function(cl) { + // Markdown if (cl.search("lang-") === 0) return cl.slice("lang-".length); + + // Asciidoc + if (cl.search("language-") === 0) return cl.slice("language-".length); + return null; }) .compact() .first() .value(); - if (lang) { - var html = code.highlight( - lang, - $(this).text() - ); + var source = $(this).text(); + var html = options.book.template.applyBlock('code', { + body: source, + kwargs: { + language: lang + } + }).body; - $(this).html(html); - } + $(this).html(html); }); // Replace glossary terms @@ -285,7 +300,13 @@ function convertImages(images, options) { if (!image.origin && !_.contains(downloaded, image.origin)) return; options.book.log.debug("download image", image.origin, "..."); downloaded.push(image.origin); - return options.book.log.debug.promise(fs.writeStream(imgin, request(image.origin))); + return options.book.log.debug.promise(fs.writeStream(imgin, request(image.origin))) + .fail(function(err) { + if (!_.isError(err)) err = new Error(err); + + err.message = 'Fail downloading '+image.origin+': '+err.message; + throw err; + }); }) // Write svg if content diff --git a/lib/utils/watch.js b/lib/utils/watch.js index b6e18e7..3e73e47 100644 --- a/lib/utils/watch.js +++ b/lib/utils/watch.js @@ -19,6 +19,7 @@ function watch(dir) { var watcher = chokidar.watch(toWatch, { cwd: dir, + ignored: '_book/**', ignoreInitial: true }); |