diff options
Diffstat (limited to 'lib/utils/page.js')
-rw-r--r-- | lib/utils/page.js | 75 |
1 files changed, 39 insertions, 36 deletions
diff --git a/lib/utils/page.js b/lib/utils/page.js index 5b4eca8..a17c6a2 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -1,18 +1,18 @@ -var Q = require('q'); -var _ = require('lodash'); -var url = require('url'); -var path = require('path'); -var cheerio = require('cheerio'); -var domSerializer = require('dom-serializer'); -var request = require('request'); +var Q = require("q"); +var _ = require("lodash"); +var url = require("url"); +var path = require("path"); +var cheerio = require("cheerio"); +var domSerializer = require("dom-serializer"); +var request = require("request"); var crc = require("crc"); -var links = require('./links'); -var imgUtils = require('./images'); -var fs = require('./fs'); -var batch = require('./batch'); +var links = require("./links"); +var imgUtils = require("./images"); +var fs = require("./fs"); +var batch = require("./batch"); -var parsableExtensions = require('gitbook-parsers').extensions; +var parsableExtensions = require("gitbook-parsers").extensions; // Render a cheerio dom as html var renderDom = function($, dom, options) { @@ -40,7 +40,8 @@ function replaceText($, el, search, replace, text_only ) { if ( node ) { // Loop over all childNodes. - do { + while (node) { + // Only process text nodes. if ( node.nodeType === 3 ) { @@ -58,7 +59,7 @@ function replaceText($, el, search, replace, text_only ) { // robust way. $(node).before( new_val ); - // Don't remove the node yet, or the loop will lose its place. + // Don"t remove the node yet, or the loop will lose its place. remove.push( node ); } else { // The new value contains no HTML, so it can be set in this @@ -68,17 +69,18 @@ function replaceText($, el, search, replace, text_only ) { } } - } while ( node = node.nextSibling ); + node = node.nextSibling; + } } // Time to remove those elements! - remove.length && $(remove).remove(); + if (remove.length) $(remove).remove(); }); -}; +} function pregQuote( str ) { - return (str+'').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1"); -}; + return (str+"").replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1"); +} // Adapt an html snippet to be relative to a base folder @@ -107,15 +109,16 @@ function normalizeHtml(src, options) { // Generate filename dest = "/"+fs.getUniqueFilename(outputRoot, dest); - svgContent[dest] = '<?xml version="1.0" encoding="UTF-8"?>'+content; + svgContent[dest] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+content; $(this).replaceWith($("<img>").attr("src", dest)); }); } // Find images to normalize $("img").each(function() { - var origin = undefined; + var origin; var src = $(this).attr("src"); + if (!src) return; var isExternal = links.isExternal(src); @@ -150,7 +153,7 @@ function normalizeHtml(src, options) { dest = links.join(path.dirname(srcAbs), path.basename(srcAbs, ext)+".png"); dest = dest[0] == "/"? dest.slice(1) : dest; - // Get a name that doesn't exists + // Get a name that doesn"t exists dest = fs.getUniqueFilename(outputRoot, dest); options.book.log.debug.ln("detect invalid image (will be converted to png):", srcAbs); @@ -205,9 +208,9 @@ function normalizeHtml(src, options) { // 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+"'"); + 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 @@ -241,7 +244,7 @@ function normalizeHtml(src, options) { .value(); var source = $(this).text(); - var html = options.book.template.applyBlock('code', { + var html = options.book.template.applyBlock("code", { body: source, kwargs: { language: lang @@ -255,8 +258,9 @@ function normalizeHtml(src, options) { var glossary = _.sortBy(options.glossary, function(term) { return -term.name.length; }); + _.each(glossary, function(term) { - var r = new RegExp( "\\b(" + pregQuote(term.name.toLowerCase()) + ")\\b" , 'gi' ); + var r = new RegExp( "\\b(" + pregQuote(term.name.toLowerCase()) + ")\\b" , "gi" ); var includedInFiles = false; $("*").each(function() { @@ -270,7 +274,7 @@ function normalizeHtml(src, options) { term.files = term.files || []; term.files.push(options.navigation[options.input]); } - return "<a href='"+links.toAbsolute("/GLOSSARY.html", options.base, options.output)+"#"+term.id+"' class='glossary-term' title='"+_.escape(term.description)+"'>"+match+"</a>"; + return "<a href=\""+links.toAbsolute("/GLOSSARY.html", options.base, options.output) + "#" + term.id+"\" class=\"glossary-term\" title=\""+_.escape(term.description)+"\">"+match+"</a>"; }); }); }); @@ -279,7 +283,7 @@ function normalizeHtml(src, options) { html: renderDom($), images: toConvert }; -}; +} // Convert svg images to png function convertImages(images, options) { @@ -304,7 +308,7 @@ function convertImages(images, options) { .fail(function(err) { if (!_.isError(err)) err = new Error(err); - err.message = 'Fail downloading '+image.origin+': '+err.message; + err.message = "Fail downloading "+image.origin+": "+err.message; throw err; }); }) @@ -327,8 +331,7 @@ function convertImages(images, options) { .then(function() { options.book.log.debug.ok(images.length+" images converted with success"); }); -}; - +} // Adapt page content to be relative to a base folder function normalizePage(sections, options) { @@ -361,7 +364,7 @@ function normalizePage(sections, options) { sections = _.map(sections, function(section) { if (section.type != "normal") return section; - var out = normalizeHtml(section.content, options);; + var out = normalizeHtml(section.content, options); toConvert = toConvert.concat(out.images); section.content = out.html; @@ -370,11 +373,11 @@ function normalizePage(sections, options) { return Q() .then(function() { - toConvert = _.uniq(toConvert, 'source'); + toConvert = _.uniq(toConvert, "source"); return convertImages(toConvert, options); }) .thenResolve(sections); -}; +} // Extract text from sections function extractText(sections) { @@ -388,7 +391,7 @@ function extractText(sections) { return prev; }, ""); -}; +} module.exports = { normalize: normalizePage, |