UNPKG

hikaru-coffee

Version:

A static site generator that generates routes based on directories naturally.

144 lines (130 loc) 4.98 kB
// Generated by CoffeeScript 2.3.1 (function() { var URL, dateStrCompare, escapeHTML, getAbsPathFn, getURLFn, isCurrentPathFn, paginate, paginateCategories, path, removeControlChars, sortCategories; path = require("path"); ({URL} = require("url")); escapeHTML = function(str) { return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"); }; removeControlChars = function(str) { return str.replace(/[\x00-\x1F\x7F]/g, ""); }; paginate = function(page, posts, ctx, perPage) { var i, j, k, len, perPagePosts, post, ref, results; if (!perPage) { perPage = 10; } results = []; perPagePosts = []; for (j = 0, len = posts.length; j < len; j++) { post = posts[j]; if (perPagePosts.length === perPage) { results.push(Object.assign({}, page, ctx, { "posts": perPagePosts })); perPagePosts = []; } perPagePosts.push(post); } results.push(Object.assign({}, page, ctx, { "posts": perPagePosts })); results[0]["pageArray"] = results; results[0]["pageIndex"] = 0; results[0]["docPath"] = page["docPath"]; for (i = k = 1, ref = results.length; (1 <= ref ? k < ref : k > ref); i = 1 <= ref ? ++k : --k) { results[i]["pageArray"] = results; results[i]["pageIndex"] = i; results[i]["docPath"] = path.join(path.dirname(page["docPath"]), `${path.basename(page["docPath"], path.extname(page["docPath"]))}-${i + 1}.html`); } return results; }; dateStrCompare = function(a, b) { return -(new Date(a["date"]) - new Date(b["date"])); }; sortCategories = function(category) { var j, len, ref, results1, sub; category["posts"].sort(dateStrCompare); category["subs"].sort(function(a, b) { return a["name"].localeCompare(b["name"]); }); ref = category["subs"]; results1 = []; for (j = 0, len = ref.length; j < len; j++) { sub = ref[j]; results1.push(sortCategories(sub)); } return results1; }; paginateCategories = function(category, page, parentPath, perPage, ctx) { var j, len, p, ref, results, sub; results = []; p = Object.assign({}, page); p["layout"] = "category"; p["docPath"] = path.join(parentPath, `${category["name"]}`, "index.html"); category["docPath"] = p["docPath"]; p["title"] = "category"; p["name"] = category["name"].toString(); results = results.concat(paginate(p, category["posts"], ctx, perPage)); ref = category["subs"]; for (j = 0, len = ref.length; j < len; j++) { sub = ref[j]; results = results.concat(paginateCategories(sub, page, path.join(parentPath, `${category["name"]}`), perPage, ctx)); } return results; }; getAbsPathFn = function(rootDir = path.posix.sep) { rootDir = rootDir.replace(path.win32.sep, path.posix.sep); return function(docPath = "") { if (!path.posix.isAbsolute(rootDir)) { rootDir = path.posix.join(path.posix.sep, rootDir); } if (docPath.endsWith("index.html")) { docPath = docPath.substring(0, docPath.length - "index.html".length); } return encodeURI(path.posix.join(rootDir, docPath.replace(path.win32.sep, path.posix.sep))); }; }; getURLFn = function(baseURL, rootDir = path.posix.sep) { var getAbsPath; getAbsPath = getAbsPathFn(rootDir); return function(docPath = "") { return new URL(getAbsPath(docPath), baseURL); }; }; isCurrentPathFn = function(rootDir = path.posix.sep, currentPath) { var currentToken, getAbsPath; // Must join a "/" before resolve or it will join current work dir. getAbsPath = getAbsPathFn(rootDir); currentPath = getAbsPath(currentPath).toLowerCase(); currentToken = path.posix.resolve(path.posix.join(path.posix.sep, currentPath.replace(path.win32.sep, path.posix.sep))).split(path.posix.sep); return function(testPath = "", strict = false) { var i, j, ref, testToken; testPath = getAbsPath(testPath).toLowerCase(); if (currentPath === testPath) { return true; } testToken = path.posix.resolve(path.posix.join(path.posix.sep, testPath.replace(path.win32.sep, path.posix.sep))).split(path.posix.sep); if (strict && testToken.length !== currentToken.length) { return false; } for (i = j = 0, ref = currentToken.length; (0 <= ref ? j < ref : j > ref); i = 0 <= ref ? ++j : --j) { if (testToken[i] !== currentToken[i]) { return false; } } return true; }; }; module.exports = { "escapeHTML": escapeHTML, "removeControlChars": removeControlChars, "paginate": paginate, "dateStrCompare": dateStrCompare, "sortCategories": sortCategories, "paginateCategories": paginateCategories, "getAbsPathFn": getAbsPathFn, "getURLFn": getURLFn, "isCurrentPathFn": isCurrentPathFn }; }).call(this);