This commit is contained in:
2025-05-12 05:38:44 +09:00
parent dced21c3f8
commit 6d78bfa46e
8120 changed files with 1161564 additions and 0 deletions

View File

@ -0,0 +1,14 @@
import nunjucks from "nunjucks";
/**
* Template loader resolving both:
* - relative url ("./test.md")
* - absolute url ("/test.md")
* - git url ("")
*
* @param {string} rootFolder
* @param {Function(filePath, source)} transformFn (optional)
* @param {Logger} logger (optional)
*/
declare const ConrefsLoader: typeof nunjucks.Loader;
export default ConrefsLoader;
//# sourceMappingURL=conrefsLoader.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"conrefsLoader.d.ts","sourceRoot":"","sources":["../../src/templating/conrefsLoader.ts"],"names":[],"mappings":"AACA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAMhC;;;;;;;;;GASG;AACH,QAAA,MAAM,aAAa,wBA4EjB,CAAC;AAEH,eAAe,aAAa,CAAC"}

View File

@ -0,0 +1,88 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const nunjucks_1 = __importDefault(require("nunjucks"));
const fs_1 = __importDefault(require("../utils/fs"));
const git_1 = __importDefault(require("../utils/git"));
const location_1 = __importDefault(require("../utils/location"));
const path_2 = __importDefault(require("../utils/path"));
/**
* Template loader resolving both:
* - relative url ("./test.md")
* - absolute url ("/test.md")
* - git url ("")
*
* @param {string} rootFolder
* @param {Function(filePath, source)} transformFn (optional)
* @param {Logger} logger (optional)
*/
const ConrefsLoader = nunjucks_1.default.Loader.extend({
async: true,
// @ts-expect-error types is wrong
init: function (rootFolder, transformFn, logger) {
this.rootFolder = rootFolder;
this.transformFn = transformFn;
this.logger = logger;
this.git = new git_1.default();
},
getSource: function (sourceURL, callback) {
const that = this;
this.git
.resolve(sourceURL)
.then((filepath) => {
// Is local file
if (!filepath) {
filepath = path_1.default.resolve(sourceURL);
}
else {
if (that.logger)
that.logger.debug.ln("resolve from git", sourceURL, "to", filepath);
}
// Read file from absolute path
return fs_1.default
.readFile(filepath)
.then((source) => {
source = source.toString("utf8");
if (that.transformFn) {
return that.transformFn(filepath, source);
}
return source;
})
.then((source) => {
return {
src: source,
path: filepath
};
});
})
.nodeify(callback);
},
resolve: function (from, to) {
// If origin is in the book, we enforce result file to be in the book
if (path_2.default.isInRoot(this.rootFolder, from)) {
// Path of current template in the rootFolder (not absolute to fs)
const fromRelative = path_1.default.relative(this.rootFolder, from);
// Resolve "to" to a filepath relative to rootFolder
const href = location_1.default.toAbsolute(to, path_1.default.dirname(fromRelative), "");
// Return absolute path
// @ts-expect-error ts-migrate(2554) FIXME: Expected 1 arguments, but got 2.
return path_2.default.resolveInRoot(this.rootFolder, href);
}
// If origin is in a git repository, we resolve file in the git repository
const gitRoot = this.git.resolveRoot(from);
if (gitRoot) {
// @ts-expect-error ts-migrate(2554) FIXME: Expected 1 arguments, but got 2.
return path_2.default.resolveInRoot(gitRoot, to);
}
// If origin is not in the book (include from a git content ref)
return path_1.default.resolve(path_1.default.dirname(from), to);
},
// Handle all files as relative, so that nunjucks pass responsability to 'resolve'
isRelative: function (filename) {
return location_1.default.isRelative(filename);
}
});
exports.default = ConrefsLoader;

14
book/node_modules/honkit/lib/templating/index.d.ts generated vendored Normal file
View File

@ -0,0 +1,14 @@
import render0 from "./render";
import postRender0 from "./postRender";
import renderFile0 from "./renderFile";
import replaceShortcuts0 from "./replaceShortcuts";
declare const _default: {
render: typeof render0;
renderFile: typeof renderFile0;
postRender: typeof postRender0;
replaceShortcuts: typeof replaceShortcuts0;
ConrefsLoader: typeof import("nunjucks").Loader;
ThemesLoader: typeof import("nunjucks").Loader;
};
export default _default;
//# sourceMappingURL=index.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/templating/index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,UAAU,CAAC;AAC/B,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,iBAAiB,MAAM,oBAAoB,CAAC;;;;;;;;;AAInD,wBAQE"}

19
book/node_modules/honkit/lib/templating/index.js generated vendored Normal file
View File

@ -0,0 +1,19 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const render_1 = __importDefault(require("./render"));
const postRender_1 = __importDefault(require("./postRender"));
const renderFile_1 = __importDefault(require("./renderFile"));
const replaceShortcuts_1 = __importDefault(require("./replaceShortcuts"));
const conrefsLoader_1 = __importDefault(require("./conrefsLoader"));
const themesLoader_1 = __importDefault(require("./themesLoader"));
exports.default = {
render: render_1.default,
renderFile: renderFile_1.default,
postRender: postRender_1.default,
replaceShortcuts: replaceShortcuts_1.default,
ConrefsLoader: conrefsLoader_1.default,
ThemesLoader: themesLoader_1.default
};

View File

@ -0,0 +1,11 @@
/**
* Return a list of all shortcuts that can apply
* to a file for a TemplatEngine
*
* @param {List<TemplateBlock>} engine
* @param {string} filePath
* @return {List<TemplateShortcut>}
*/
declare function listShortcuts(blocks: any, filePath: any): any;
export default listShortcuts;
//# sourceMappingURL=listShortcuts.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"listShortcuts.d.ts","sourceRoot":"","sources":["../../src/templating/listShortcuts.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AAEH,iBAAS,aAAa,CAAC,MAAM,KAAA,EAAE,QAAQ,KAAA,OActC;AAED,eAAe,aAAa,CAAC"}

View File

@ -0,0 +1,29 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const immutable_1 = __importDefault(require("immutable"));
const parsers_1 = __importDefault(require("../parsers"));
/**
* Return a list of all shortcuts that can apply
* to a file for a TemplatEngine
*
* @param {List<TemplateBlock>} engine
* @param {string} filePath
* @return {List<TemplateShortcut>}
*/
function listShortcuts(blocks, filePath) {
const parser = parsers_1.default.getForFile(filePath);
if (!parser) {
return immutable_1.default.List();
}
return blocks
.map((block) => {
return block.getShortcuts();
})
.filter((shortcuts) => {
return shortcuts && shortcuts.acceptParser(parser.getName());
});
}
exports.default = listShortcuts;

View File

@ -0,0 +1,12 @@
/**
* Post render a template:
* - Execute "post" for blocks
* - Replace block content
*
* @param {TemplateEngine} engine
* @param {TemplateOutput} content
* @return {Promise<String>}
*/
declare function postRender(engine: any, output: any): any;
export default postRender;
//# sourceMappingURL=postRender.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"postRender.d.ts","sourceRoot":"","sources":["../../src/templating/postRender.ts"],"names":[],"mappings":"AAwBA;;;;;;;;GAQG;AACH,iBAAS,UAAU,CAAC,MAAM,KAAA,EAAE,MAAM,KAAA,OAejC;AAED,eAAe,UAAU,CAAC"}

46
book/node_modules/honkit/lib/templating/postRender.js generated vendored Normal file
View File

@ -0,0 +1,46 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const promise_1 = __importDefault(require("../utils/promise"));
/**
* Replace position markers of blocks by body after processing
* This is done to avoid that markdown/asciidoc processer parse the block content
*
* @param {string} content
* @return {Object} {blocks: Set, content: String}
*/
function replaceBlocks(content, blocks) {
const newContent = content.replace(/\{\{\-\%([\s\S]+?)\%\-\}\}/g, (match, key) => {
let replacedWith = match;
const block = blocks.get(key);
if (block) {
replacedWith = replaceBlocks(block.get("body"), blocks);
}
return replacedWith;
});
return newContent;
}
/**
* Post render a template:
* - Execute "post" for blocks
* - Replace block content
*
* @param {TemplateEngine} engine
* @param {TemplateOutput} content
* @return {Promise<String>}
*/
function postRender(engine, output) {
const content = output.getContent();
const blocks = output.getBlocks();
const result = replaceBlocks(content, blocks);
return promise_1.default.forEach(blocks, (block) => {
const post = block.get("post");
if (!post) {
return;
}
return post();
}).thenResolve(result);
}
exports.default = postRender;

12
book/node_modules/honkit/lib/templating/render.d.ts generated vendored Normal file
View File

@ -0,0 +1,12 @@
/**
* Render a template
*
* @param {TemplateEngine} engine
* @param {string} filePath: absolute path for the loader
* @param {string} content
* @param {Object} context (optional)
* @return {Promise<TemplateOutput>}
*/
declare function renderTemplate(engine: any, filePath: any, content: any, context: any): any;
export default renderTemplate;
//# sourceMappingURL=render.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../../src/templating/render.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,iBAAS,cAAc,CAAC,MAAM,KAAA,EAAE,QAAQ,KAAA,EAAE,OAAO,KAAA,EAAE,OAAO,KAAA,OA6BzD;AAED,eAAe,cAAc,CAAC"}

41
book/node_modules/honkit/lib/templating/render.js generated vendored Normal file
View File

@ -0,0 +1,41 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const promise_1 = __importDefault(require("../utils/promise"));
const timing_1 = __importDefault(require("../utils/timing"));
const templateOutput_1 = __importDefault(require("../models/templateOutput"));
const replaceShortcuts_1 = __importDefault(require("./replaceShortcuts"));
/**
* Render a template
*
* @param {TemplateEngine} engine
* @param {string} filePath: absolute path for the loader
* @param {string} content
* @param {Object} context (optional)
* @return {Promise<TemplateOutput>}
*/
function renderTemplate(engine, filePath, content, context) {
context = context || {};
// Mutable objects to contains all blocks requiring post-processing
const blocks = {};
// Create nunjucks environment
const env = engine.toNunjucks(blocks);
// Replace shortcuts from plugin's blocks
content = (0, replaceShortcuts_1.default)(engine.getBlocks(), filePath, content);
return timing_1.default.measure("template.render", promise_1.default.nfcall(env.renderString.bind(env), content, context, {
path: filePath
})
.then((content) => {
return templateOutput_1.default.create(content, blocks);
})
.catch((error) => {
console.log("env:", env);
console.log("context:", context);
console.log("content:", content);
console.error("rendering error:", error);
return promise_1.default.reject(error);
}));
}
exports.default = renderTemplate;

View File

@ -0,0 +1,11 @@
/**
* Render a template
*
* @param {TemplateEngine} engine
* @param {string} filePath
* @param {Object} context
* @return {Promise<TemplateOutput>}
*/
declare function renderTemplateFile(engine: any, filePath: any, context: any): any;
export default renderTemplateFile;
//# sourceMappingURL=renderFile.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"renderFile.d.ts","sourceRoot":"","sources":["../../src/templating/renderFile.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,iBAAS,kBAAkB,CAAC,MAAM,KAAA,EAAE,QAAQ,KAAA,EAAE,OAAO,KAAA,OAyBpD;AAED,eAAe,kBAAkB,CAAC"}

39
book/node_modules/honkit/lib/templating/renderFile.js generated vendored Normal file
View File

@ -0,0 +1,39 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const promise_1 = __importDefault(require("../utils/promise"));
const error_1 = __importDefault(require("../utils/error"));
const render_1 = __importDefault(require("./render"));
/**
* Render a template
*
* @param {TemplateEngine} engine
* @param {string} filePath
* @param {Object} context
* @return {Promise<TemplateOutput>}
*/
function renderTemplateFile(engine, filePath, context) {
const loader = engine.getLoader();
// Resolve the filePath
const resolvedFilePath = loader.resolve(null, filePath);
return (0, promise_1.default)()
.then(() => {
if (!loader.async) {
return loader.getSource(resolvedFilePath);
}
const deferred = promise_1.default.defer();
loader.getSource(resolvedFilePath, deferred.makeNodeResolver());
return deferred.promise;
})
.then((result) => {
if (!result) {
throw error_1.default.TemplateError(new Error("Not found"), {
filename: filePath
});
}
return (0, render_1.default)(engine, result.path, result.src, context);
});
}
exports.default = renderTemplateFile;

View File

@ -0,0 +1,11 @@
/**
* Replace shortcuts from blocks in a string
*
* @param {List<TemplateBlock>} engine
* @param {string} filePath
* @param {string} content
* @return {string}
*/
declare function replaceShortcuts(blocks: any, filePath: any, content: any): any;
export default replaceShortcuts;
//# sourceMappingURL=replaceShortcuts.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"replaceShortcuts.d.ts","sourceRoot":"","sources":["../../src/templating/replaceShortcuts.ts"],"names":[],"mappings":"AAsBA;;;;;;;GAOG;AAEH,iBAAS,gBAAgB,CAAC,MAAM,KAAA,EAAE,QAAQ,KAAA,EAAE,OAAO,KAAA,OAGlD;AAED,eAAe,gBAAgB,CAAC"}

View File

@ -0,0 +1,36 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const escape_string_regexp_1 = __importDefault(require("escape-string-regexp"));
const listShortcuts_1 = __importDefault(require("./listShortcuts"));
/**
* Apply a shortcut of block to a template
* @param {string} content
* @param {Shortcut} shortcut
* @return {string}
*/
function applyShortcut(content, shortcut) {
const start = shortcut.getStart();
const end = shortcut.getEnd();
const tagStart = shortcut.getStartTag();
const tagEnd = shortcut.getEndTag();
const regex = new RegExp(`${(0, escape_string_regexp_1.default)(start)}([\\s\\S]*?[^\\$])${(0, escape_string_regexp_1.default)(end)}`, "g");
return content.replace(regex, (all, match) => {
return `{% ${tagStart} %}${match}{% ${tagEnd} %}`;
});
}
/**
* Replace shortcuts from blocks in a string
*
* @param {List<TemplateBlock>} engine
* @param {string} filePath
* @param {string} content
* @return {string}
*/
function replaceShortcuts(blocks, filePath, content) {
const shortcuts = (0, listShortcuts_1.default)(blocks, filePath);
return shortcuts.reduce(applyShortcut, content);
}
exports.default = replaceShortcuts;

View File

@ -0,0 +1,4 @@
import nunjucks from "nunjucks";
declare const ThemesLoader: typeof nunjucks.Loader;
export default ThemesLoader;
//# sourceMappingURL=themesLoader.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"themesLoader.d.ts","sourceRoot":"","sources":["../../src/templating/themesLoader.ts"],"names":[],"mappings":"AACA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAKhC,QAAA,MAAM,YAAY,wBAqGhB,CAAC;AAEH,eAAe,YAAY,CAAC"}

View File

@ -0,0 +1,98 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const immutable_1 = __importDefault(require("immutable"));
const nunjucks_1 = __importDefault(require("nunjucks"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const path_2 = __importDefault(require("../utils/path"));
const ThemesLoader = nunjucks_1.default.Loader.extend({
// @ts-expect-error: Property 'extend' does not exist on type 'typeof Loader'.
init: function (searchPaths) {
this.searchPaths = immutable_1.default.List(searchPaths).map(path_1.default.normalize);
},
/**
* Read source of a resolved filepath
* @param {string}
* @return {Object}
*/
getSource: function (fullpath) {
if (!fullpath)
return null;
fullpath = this.resolve(null, fullpath);
const templateName = this.getTemplateName(fullpath);
if (!fullpath) {
return null;
}
let src = fs_1.default.readFileSync(fullpath, "utf-8");
src = `{% do %}var template = template || {}; template.stack = template.stack || []; template.stack.push(template.self); template.self = ${JSON.stringify(templateName)}{% enddo %}\n${src}\n{% do %}template.self = template.stack.pop();{% enddo %}`;
return {
src: src,
path: fullpath,
noCache: true
};
},
/**
* Nunjucks calls "isRelative" to determine when to call "resolve".
* We handle absolute paths ourselves in ".resolve" so we always return true
*/
isRelative: function () {
return true;
},
/**
* Get original search path containing a template
* @param {string} filepath
* @return {string} searchPath
*/
getSearchPath: function (filepath) {
return this.searchPaths
.sortBy((s) => {
return -s.length;
})
.find((basePath) => {
return filepath && filepath.indexOf(basePath) === 0;
});
},
/**
* Get template name from a filepath
* @param {string} filepath
* @return {string} name
*/
getTemplateName: function (filepath) {
const originalSearchPath = this.getSearchPath(filepath);
return originalSearchPath ? path_1.default.relative(originalSearchPath, filepath) : null;
},
/**
* Resolve a template from a current template
* @param {String|null} from
* @param {string} to
* @return {String|null}
*/
resolve: function (from, to) {
let searchPaths = this.searchPaths;
// Relative template like "./test.html"
if (path_2.default.isPureRelative(to) && from) {
return path_1.default.resolve(path_1.default.dirname(from), to);
}
// Determine in which search folder we currently are
const originalSearchPath = this.getSearchPath(from);
const originalFilename = this.getTemplateName(from);
// If we are including same file from a different search path
// Slice the search paths to avoid including from previous ones
if (originalFilename == to) {
const currentIndex = searchPaths.indexOf(originalSearchPath);
searchPaths = searchPaths.slice(currentIndex + 1);
}
// Absolute template to resolve in root folder
const resultFolder = searchPaths.find((basePath) => {
const p = path_1.default.resolve(basePath, to);
return p.indexOf(basePath) === 0 && fs_1.default.existsSync(p);
});
if (!resultFolder)
return null;
return path_1.default.resolve(resultFolder, to);
}
});
exports.default = ThemesLoader;