UNPKG

hikaru-coffee

Version:

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

192 lines (174 loc) 6.38 kB
// Generated by CoffeeScript 2.3.1 (function() { var Category, File, Promise, Site, Tag, URL, escapeHTML, fm, getContentType, getPathFn, getURLFn, isCurrentPathFn, paginate, paginateCategories, parseFrontMatter, path, removeControlChars, sortCategories; fm = require("front-matter"); path = require("path"); ({URL} = require("url")); Promise = require("bluebird"); ({Site, File, Category, Tag} = require("./type")); 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, ""); }; parseFrontMatter = function(file) { var parsed; if (typeof file["raw"] !== "string") { return file; } parsed = fm(file["raw"]); file["text"] = parsed["body"]; file["frontMatter"] = parsed["attributes"]; file = Object.assign(file, parsed["attributes"]); if (file["date"] != null) { // Fix js-yaml"s bug that ignore timezone while parsing. // https://github.com/nodeca/js-yaml/issues/91 file["date"] = new Date(file["date"].getTime() + file["date"].getTimezoneOffset() * 60000); } else { file["date"] = new Date(); } if (file["title"] != null) { file["title"] = file["title"].toString(); } return file; }; getContentType = function(docPath) { switch (path.extname(docPath)) { case ".html": return "text/html; charset=UTF-8"; case ".html": return "text/html; charset=UTF-8"; case ".js": return "application/javascript; charset=UTF-8"; case ".css": return "text/css; charset=UTF-8"; case ".txt": return "text/plain; charset=UTF-8"; case ".manifest": return "text/cache-manifest; charset=UTF-8"; default: return "application/octet-stream"; } }; paginate = function(p, posts, perPage, ctx) { 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(new File(), p, ctx, { "posts": perPagePosts })); perPagePosts = []; } perPagePosts.push(post); } results.push(Object.assign(new File(), p, ctx, { "posts": perPagePosts })); results[0]["pageArray"] = results; results[0]["pageIndex"] = 0; results[0]["docPath"] = p["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(p["docPath"]), `${path.basename(p["docPath"], path.extname(p["docPath"]))}-${i + 1}.html`); } return results; }; sortCategories = function(category) { var j, len, ref, results1, sub; category["posts"].sort(function(a, b) { return -(a["date"] - b["date"]); }); 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, parentPath, perPage, site) { var j, len, ref, results, sp, sub; results = []; sp = Object.assign(new File(site.get("docDir")), { "layout": "category", "docPath": path.join(parentPath, `${category["name"]}`, "index.html"), "title": "category", "name": category["name"].toString() }); category["docPath"] = sp["docPath"]; results = results.concat(paginate(sp, category["posts"], perPage)); ref = category["subs"]; for (j = 0, len = ref.length; j < len; j++) { sub = ref[j]; results = results.concat(paginateCategories(sub, path.join(parentPath, `${category["name"]}`), perPage, site)); } return results; }; getPathFn = 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 getPath; getPath = getPathFn(rootDir); return function(docPath = "") { return new URL(getPath(docPath), baseURL); }; }; isCurrentPathFn = function(rootDir = path.posix.sep, currentPath) { var currentToken, getPath; // Must join a "/" before resolve or it will join current work dir. getPath = getPathFn(rootDir); currentPath = getPath(currentPath); 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 = getPath(testPath); 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; } // testPath is shorter and usually be a menu link. for (i = j = 0, ref = testToken.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, "parseFrontMatter": parseFrontMatter, "getContentType": getContentType, "paginate": paginate, "sortCategories": sortCategories, "paginateCategories": paginateCategories, "getPathFn": getPathFn, "getURLFn": getURLFn, "isCurrentPathFn": isCurrentPathFn }; }).call(this);