summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/api/encodeGlobal.js2
-rw-r--r--lib/models/book.js4
-rw-r--r--lib/output/getModifiers.js4
-rw-r--r--lib/output/helper/fileToOutput.js32
-rw-r--r--lib/output/helper/fileToURL.js31
-rw-r--r--lib/output/helper/index.js2
-rw-r--r--lib/output/helper/resolveFileToUrl.js27
-rw-r--r--lib/output/helper/writeFile.js23
-rw-r--r--lib/output/json/onPage.js17
-rw-r--r--lib/output/resolveFile.js21
-rw-r--r--lib/output/website/createTemplateEngine.js44
-rw-r--r--lib/output/website/onPage.js9
-rw-r--r--lib/output/writer/index.js4
-rw-r--r--lib/output/writer/writePage.js37
-rw-r--r--lib/utils/location.js15
15 files changed, 178 insertions, 94 deletions
diff --git a/lib/api/encodeGlobal.js b/lib/api/encodeGlobal.js
index 915350c..9e91c73 100644
--- a/lib/api/encodeGlobal.js
+++ b/lib/api/encodeGlobal.js
@@ -1,6 +1,6 @@
var fs = require('../utils/fs');
var Promise = require('../utils/promise');
-var PathUtils = require('../utils/promise');
+var PathUtils = require('../utils/path');
var deprecate = require('./deprecate');
var encodeConfig = require('./encodeConfig');
diff --git a/lib/models/book.js b/lib/models/book.js
index 3c5144f..6daa684 100644
--- a/lib/models/book.js
+++ b/lib/models/book.js
@@ -33,8 +33,8 @@ var Book = Immutable.Record({
// Parent of this book, if multilingual
parent: null,
- // List of children, if multilingual
- books: Immutable.List()
+ // List of children, if multilingual (String -> Book)
+ books: Immutable.OrderedMap()
});
Book.prototype.getLogger = function() {
diff --git a/lib/output/getModifiers.js b/lib/output/getModifiers.js
index 21a0692..34a0b9e 100644
--- a/lib/output/getModifiers.js
+++ b/lib/output/getModifiers.js
@@ -1,5 +1,5 @@
var Modifiers = require('./modifiers');
-var resolveFile = require('./resolveFile');
+var resolveFileToURL = require('./helper/resolveFileToURL');
/**
Return default modifier to prepare a page
@@ -17,7 +17,7 @@ function getModifiers(output, page) {
Modifiers.addHeadingId,
Modifiers.resolveLinks.bind(null,
file.getPath(),
- resolveFile.bind(null, output)
+ resolveFileToURL.bind(null, output)
),
Modifiers.annotateText.bind(null, entries)
];
diff --git a/lib/output/helper/fileToOutput.js b/lib/output/helper/fileToOutput.js
new file mode 100644
index 0000000..9673162
--- /dev/null
+++ b/lib/output/helper/fileToOutput.js
@@ -0,0 +1,32 @@
+var path = require('path');
+
+var PathUtils = require('../../utils/path');
+var LocationUtils = require('../../utils/location');
+
+var OUTPUT_EXTENSION = '.html';
+
+/**
+ Convert a filePath (absolute) to a filename for output
+
+ @param {Output} output
+ @param {String} filePath
+ @return {String}
+*/
+function fileToOutput(output, filePath) {
+ var book = output.getBook();
+ var readme = book.getReadme();
+ var fileReadme = readme.getFile();
+
+ if (
+ path.basename(filePath, path.extname(filePath)) == 'README' ||
+ (fileReadme.exists() && filePath == fileReadme.getPath())
+ ) {
+ filePath = path.join(path.dirname(filePath), 'index' + OUTPUT_EXTENSION);
+ } else {
+ filePath = PathUtils.setExtension(filePath, OUTPUT_EXTENSION);
+ }
+
+ return LocationUtils.normalize(filePath);
+}
+
+module.exports = fileToOutput;
diff --git a/lib/output/helper/fileToURL.js b/lib/output/helper/fileToURL.js
new file mode 100644
index 0000000..44ad2d8
--- /dev/null
+++ b/lib/output/helper/fileToURL.js
@@ -0,0 +1,31 @@
+var path = require('path');
+var LocationUtils = require('../../utils/location');
+
+var fileToOutput = require('./fileToOutput');
+
+/**
+ Convert a filePath (absolute) to an url (without hostname).
+ It returns an absolute path.
+
+ "README.md" -> "/"
+ "test/hello.md" -> "test/hello.html"
+ "test/README.md" -> "test/"
+
+ @param {Output} output
+ @param {String} filePath
+ @return {String}
+*/
+function fileToURL(output, filePath) {
+ var options = output.getOptions();
+ var directoryIndex = options.get('directoryIndex');
+
+ filePath = fileToOutput(output, filePath);
+
+ if (directoryIndex && path.basename(filePath) == 'index.html') {
+ filePath = path.dirname(filePath) + '/';
+ }
+
+ return LocationUtils.normalize(filePath);
+}
+
+module.exports = fileToURL;
diff --git a/lib/output/helper/index.js b/lib/output/helper/index.js
new file mode 100644
index 0000000..f8bc109
--- /dev/null
+++ b/lib/output/helper/index.js
@@ -0,0 +1,2 @@
+
+module.exports = {};
diff --git a/lib/output/helper/resolveFileToUrl.js b/lib/output/helper/resolveFileToUrl.js
new file mode 100644
index 0000000..3dba8f7
--- /dev/null
+++ b/lib/output/helper/resolveFileToUrl.js
@@ -0,0 +1,27 @@
+var LocationUtils = require('../../utils/location');
+
+var fileToURL = require('./fileToURL');
+
+/**
+ Resolve an absolute path (extracted from a link)
+
+ @param {Output} output
+ @param {String} filePath
+ @return {String}
+*/
+function resolveFileToURL(output, filePath) {
+ // Convert /test.png -> test.png
+ filePath = LocationUtils.toAbsolute(filePath, '', '');
+
+ var pages = output.getPages();
+ var page = pages.get(filePath);
+
+ // if file is a page, return correct .html url
+ if (page) {
+ filePath = fileToURL(output, filePath);
+ }
+
+ return LocationUtils.normalize(filePath);
+}
+
+module.exports = resolveFileToURL;
diff --git a/lib/output/helper/writeFile.js b/lib/output/helper/writeFile.js
new file mode 100644
index 0000000..3e2261d
--- /dev/null
+++ b/lib/output/helper/writeFile.js
@@ -0,0 +1,23 @@
+var path = require('path');
+var fs = require('../../utils/fs');
+
+/**
+ Write a file to the output folder
+
+ @param {Output} output
+ @param {String} filePath
+ @param {Buffer|String} content
+ @return {Promise}
+*/
+function writeFile(output, filePath, content) {
+ var rootFolder = output.getRoot();
+ filePath = path.join(rootFolder, filePath);
+
+ return fs.ensure(filePath)
+ .then(function() {
+ return fs.writeFile(filePath, content);
+ })
+ .thenResolve(output);
+}
+
+module.exports = writeFile;
diff --git a/lib/output/json/onPage.js b/lib/output/json/onPage.js
index 5e45662..54179c6 100644
--- a/lib/output/json/onPage.js
+++ b/lib/output/json/onPage.js
@@ -1,6 +1,7 @@
var JSONUtils = require('../../json');
+var PathUtils = require('../../utils/path');
var Modifiers = require('../modifiers');
-var Writer = require('../writer');
+var writeFile = require('../helper/writeFile');
var getModifiers = require('../getModifiers');
/**
@@ -10,19 +11,21 @@ var getModifiers = require('../getModifiers');
@param {Page} page
*/
function onPage(output, page) {
+ var file = page.getFile();
+
return Modifiers.modifyHTML(page, getModifiers(output, page))
.then(function(resultPage) {
// Generate the JSON
var json = JSONUtils.encodeBookWithPage(output.getBook(), resultPage);
+ // File path in the output folder
+ var filePath = PathUtils.setExtension(file.getPath(), '.json');
+
// Write it to the disk
- return Writer.writePage(
+ return writeFile(
output,
- page,
- JSON.stringify(json, null, 4),
- {
- extension: '.json'
- }
+ filePath,
+ JSON.stringify(json, null, 4)
);
});
}
diff --git a/lib/output/resolveFile.js b/lib/output/resolveFile.js
deleted file mode 100644
index a3a49c5..0000000
--- a/lib/output/resolveFile.js
+++ /dev/null
@@ -1,21 +0,0 @@
-var PathUtils = require('../utils/path');
-
-/**
- Resolve an absolute path (extracted from a link)
-
- @param {Output} output
- @param {String} filePath
- @return {String}
-*/
-function resolveFile(output, filePath) {
- var pages = output.getPages();
- var page = pages.get(filePath);
-
- if (!page) {
- return filePath;
- }
-
- return PathUtils.setExtension(filePath, '.html');
-}
-
-module.exports = resolveFile;
diff --git a/lib/output/website/createTemplateEngine.js b/lib/output/website/createTemplateEngine.js
index 2c2ac8d..7d87248 100644
--- a/lib/output/website/createTemplateEngine.js
+++ b/lib/output/website/createTemplateEngine.js
@@ -4,12 +4,17 @@ var DoExtension = require('nunjucks-do')(nunjucks);
var JSONUtils = require('../../json');
var LocationUtils = require('../../utils/location');
+var fs = require('../../utils/fs');
+var PathUtils = require('../../utils/path');
var TemplateEngine = require('../../models/templateEngine');
var templatesFolder = require('../../constants/templatesFolder');
var defaultFilters = require('../../constants/defaultFilters');
var Templating = require('../../templating');
var listSearchPaths = require('./listSearchPaths');
+var fileToURL = require('../helper/fileToURL');
+var resolveFileToURL = require('../helper/resolveFileToURL');
+
/**
Directory for a theme with the templates
*/
@@ -30,6 +35,8 @@ function createTemplateEngine(output, currentFile) {
var i18n = state.getI18n();
var config = book.getConfig();
var summary = book.getSummary();
+ var pages = output.getPages();
+ var outputFolder = output.getRoot();
// Search paths for templates
var searchPaths = listSearchPaths(output);
@@ -54,28 +61,31 @@ function createTemplateEngine(output, currentFile) {
/**
Resolve an absolute file path into a
- relative path
+ relative path.
+ it also resolve pages
*/
- resolveFile: function(s) {
- // Convert /test.png -> test.png
- s = LocationUtils.toAbsolute(s, '', '');
+ resolveFile: function(filePath) {
+ filePath = resolveFileToURL(output, filePath);
+ return LocationUtils.relativeForFile(currentFile, filePath);
+ },
- // Convert to relative
- s = LocationUtils.relative(
- this.resolve('.') + '/',
- this.book.resolve(name)
- );
+ resolveAsset: function(filePath) {
+ filePath = LocationUtils.toAbsolute(filePath, '', '');
+ filePath = path.join('gitbook', filePath);
- return LocationUtils.normalize(s);
+ return LocationUtils.relativeForFile(currentFile, filePath);
},
- resolveAsset: function(s) {
- return s;
- },
- fileExists: function() {
- return false;
+
+ /**
+ Check if a file exists
+ */
+ fileExists: function(fileName) {
+ var filePath = PathUtils.resolveInRoot(outputFolder, fileName);
+ return fs.existsSync(filePath);
},
- contentURL: function(s) {
- return s;
+
+ contentURL: function(filePath) {
+ return fileToURL(output, filePath);
},
/**
diff --git a/lib/output/website/onPage.js b/lib/output/website/onPage.js
index aedb8c0..a5598e4 100644
--- a/lib/output/website/onPage.js
+++ b/lib/output/website/onPage.js
@@ -1,9 +1,10 @@
var Templating = require('../../templating');
var JSONUtils = require('../../json');
var Modifiers = require('../modifiers');
-var Writer = require('../writer');
+var writeFile = require('../helper/writeFile');
var getModifiers = require('../getModifiers');
var createTemplateEngine = require('./createTemplateEngine');
+var fileToOutput = require('../helper/fileToOutput');
/**
Write a page as a json file
@@ -13,6 +14,7 @@ var createTemplateEngine = require('./createTemplateEngine');
*/
function onPage(output, page) {
var options = output.getOptions();
+ var file = page.getFile();
var prefix = options.get('prefix');
var engine = createTemplateEngine(output, page.getPath());
@@ -27,12 +29,15 @@ function onPage(output, page) {
}
};
+ // Output file path
+ var filePath = fileToOutput(output, file.getPath());
+
// Render the theme
return Templating.renderFile(engine, prefix + '/page.html', context)
// Write it to the disk
.then(function(html) {
- return Writer.writePage(output, resultPage, html);
+ return writeFile(output, filePath, html);
});
});
}
diff --git a/lib/output/writer/index.js b/lib/output/writer/index.js
deleted file mode 100644
index 2ef3364..0000000
--- a/lib/output/writer/index.js
+++ /dev/null
@@ -1,4 +0,0 @@
-
-module.exports = {
- writePage: require('./writePage')
-};
diff --git a/lib/output/writer/writePage.js b/lib/output/writer/writePage.js
deleted file mode 100644
index 6bcc1ba..0000000
--- a/lib/output/writer/writePage.js
+++ /dev/null
@@ -1,37 +0,0 @@
-var path = require('path');
-var Immutable = require('immutable');
-
-var fs = require('../../utils/fs');
-var PathUtils = require('../../utils/path');
-
-var WriteOptions = Immutable.Record({
- extension: '.html'
-});
-
-/**
- Write a file to the output folder
-
- @param {Output} output
- @param {Page} page
- @param {Buffer|String} content
- @return {Promise}
-*/
-function writePage(output, page, content, options) {
- var file = page.getFile();
- var outputOpts = output.getOptions();
- var rootFolder = outputOpts.get('root');
-
- options = WriteOptions(options);
-
- // Get filename for file to write
- var fileName = PathUtils.setExtension(file.getPath(), options.get('extension'));
- var filePath = path.join(rootFolder, fileName);
-
- return fs.ensure(filePath)
- .then(function() {
- return fs.writeFile(filePath, content);
- })
- .thenResolve(output);
-}
-
-module.exports = writePage;
diff --git a/lib/utils/location.js b/lib/utils/location.js
index f1159f1..94d9b2a 100644
--- a/lib/utils/location.js
+++ b/lib/utils/location.js
@@ -71,11 +71,24 @@ function relative(dir, file) {
return normalize(path.relative(dir, file));
}
+/**
+ Convert an absolute path to a relative path for a specific folder (dir)
+ ('test/test.md', 'hello.md') -> '../hello.md'
+
+ @param {String} baseFile: current file
+ @param {String} file: absolute path of file
+ @return {String}
+*/
+function relativeForFile(baseFile, file) {
+ return relative(path.dirname(baseFile), file);
+}
+
module.exports = {
isExternal: isExternal,
isRelative: isRelative,
isAnchor: isAnchor,
normalize: normalize,
toAbsolute: toAbsolute,
- relative: relative
+ relative: relative,
+ relativeForFile: relativeForFile
};