summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-19 22:39:23 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-19 22:39:23 +0100
commitc15399613cf3e589eb106d37221fae9c54002d74 (patch)
tree0ae3f7140eccbbbe0a3b156400179acec29a8e0e /lib/utils
parentd6f7f1f4fb12ac1c24624aa8358f8149ec720622 (diff)
downloadgitbook-c15399613cf3e589eb106d37221fae9c54002d74.zip
gitbook-c15399613cf3e589eb106d37221fae9c54002d74.tar.gz
gitbook-c15399613cf3e589eb106d37221fae9c54002d74.tar.bz2
Normalize paths in html content
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/links.js67
-rw-r--r--lib/utils/page.js66
2 files changed, 133 insertions, 0 deletions
diff --git a/lib/utils/links.js b/lib/utils/links.js
new file mode 100644
index 0000000..e607c94
--- /dev/null
+++ b/lib/utils/links.js
@@ -0,0 +1,67 @@
+var url = require('url');
+var path = require('path');
+
+// Is the link an external link
+var isExternal = function(href) {
+ try {
+ return Boolean(url.parse(href).protocol);
+ } catch(err) { }
+
+ return false;
+};
+
+// Return true if the link is relative
+var isRelative = function(href) {
+ try {
+ var parsed = url.parse(href);
+
+ return !parsed.protocol && parsed.path && parsed.path[0] != '/';
+ } catch(err) {}
+
+ return true;
+};
+
+// Relative to absolute path
+// dir: directory parent of the file currently in rendering process
+// outdir: directory parent from the html output
+
+var toAbsolute = function(_href, dir, outdir) {
+ // Absolute file in source
+ _href = path.join(dir, _href);
+
+ // make it relative to output
+ _href = path.relative(outdir, _href);
+
+ if (process.platform === 'win32') {
+ _href = _href.replace(/\\/g, '/');
+ }
+
+ return _href;
+};
+
+// Join links
+var join = function() {
+ var _href = path.join.apply(path, arguments);
+
+ if (process.platform === 'win32') {
+ _href = _href.replace(/\\/g, '/');
+ }
+
+ return _href;
+};
+
+// Change extension
+var changeExtension = function(filename, newext) {
+ return path.join(
+ path.dirname(filename),
+ path.basename(filename, path.extname(filename))+newext
+ );
+};
+
+module.exports = {
+ isRelative: isRelative,
+ isExternal: isExternal,
+ toAbsolute: toAbsolute,
+ join: join,
+ changeExtension: changeExtension
+};
diff --git a/lib/utils/page.js b/lib/utils/page.js
new file mode 100644
index 0000000..00e4757
--- /dev/null
+++ b/lib/utils/page.js
@@ -0,0 +1,66 @@
+var _ = require('lodash');
+var path = require('path');
+var cheerio = require('cheerio');
+
+var links = require('./links');
+
+// Adapt an html snippet to be relative to a base folder
+function normalizeHtml(src, options) {
+ var $ = cheerio.load(src);
+
+ $("img").each(function() {
+ var src = $(this).attr("src");
+
+ // Transform as absolute
+ if (links.isRelative(src)) {
+ src = links.toAbsolute(src, options.base, options.output);
+ }
+
+ $(this).attr("src", src);
+ });
+
+ $("a").each(function() {
+ var href = $(this).attr("href");
+
+ if (links.isRelative(href)) {
+ var absolutePath = path.join(options.base, href);
+
+ // If is in navigation relative: change extsnio nto be .html
+ if (options.navigation[absolutePath]) href = links.changeExtension(href, ".html");
+
+ // Transform as absolute
+ href = links.toAbsolute(href, options.base, options.output);
+ } else {
+ $(this).attr("target", "_blank");
+ }
+
+ // Transform extension
+ $(this).attr("href", href);
+ });
+
+ return $.html();
+};
+
+// Adapt page content to be relative to a base folder
+function normalizePage(sections, options) {
+ options = _.defaults(options || {}, {
+ // Navigation to use to transform path
+ navigation: {},
+
+ // Directory parent of the file currently in rendering process
+ base: "./",
+
+ // Directory parent from the html output
+ output: "./"
+ });
+
+ return _.map(sections, function(section) {
+ if (section.type != "normal") return section;
+ section.content = normalizeHtml(section.content, options);
+ return section;
+ });
+};
+
+module.exports = {
+ normalize: normalizePage
+};