UNPKG

solidity-docgen

Version:

Documentation generator for Solidity smart contracts.

124 lines 5.12 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readTemplates = exports.loadTemplates = void 0; const path_1 = __importDefault(require("path")); const fs_1 = __importStar(require("fs")); const map_keys_1 = require("./utils/map-keys"); const defaultProperties = __importStar(require("./common/properties")); /** * Loads the templates that will be used for rendering a site based on a * default theme and user templates. * * The result contains all partials, helpers, and property getters defined in * the user templates and the default theme, where the user's take precedence * if there is a clash. Additionally, all theme partials and helpers are * included with the theme prefix, e.g. `markdown/contract` will be a partial. */ async function loadTemplates(defaultTheme, root, userTemplatesPath) { const themes = await readThemes(); // Initialize templates with the default theme. const templates = { partials: { ...themes[defaultTheme]?.partials }, helpers: { ...themes[defaultTheme]?.helpers }, properties: { ...defaultProperties }, }; // Overwrite default theme with user templates. if (userTemplatesPath) { const userTemplates = await readTemplates(path_1.default.resolve(root, userTemplatesPath)); Object.assign(templates.partials, userTemplates.partials); Object.assign(templates.helpers, userTemplates.helpers); Object.assign(templates.properties, userTemplates.properties); } // Add partials and helpers from all themes, prefixed with the theme name. for (const [themeName, theme] of Object.entries(themes)) { const addPrefix = (k) => `${themeName}/${k}`; Object.assign(templates.partials, (0, map_keys_1.mapKeys)(theme.partials, addPrefix)); Object.assign(templates.helpers, (0, map_keys_1.mapKeys)(theme.helpers, addPrefix)); } return templates; } exports.loadTemplates = loadTemplates; /** * Read templates and helpers from a directory. */ async function readTemplates(partialsDir, helpersDir = partialsDir) { return { partials: await readPartials(partialsDir), helpers: await readHelpers(helpersDir, 'helpers'), properties: await readHelpers(helpersDir, 'properties'), }; } exports.readTemplates = readTemplates; async function readPartials(dir) { const partials = {}; for (const p of await fs_1.promises.readdir(dir)) { const { name, ext } = path_1.default.parse(p); if (ext === '.hbs') { partials[name] = () => fs_1.default.readFileSync(path_1.default.join(dir, p), 'utf8'); } } return partials; } async function readHelpers(dir, name) { var _a; let helpersPath; try { helpersPath = require.resolve(path_1.default.join(dir, name)); } catch { return {}; } const h = await (_a = helpersPath, Promise.resolve().then(() => __importStar(require(_a)))); const helpers = {}; for (const name in h) { if (typeof h[name] === 'function') { helpers[name] = h[name]; } } return helpers; } /** * Reads all built-in themes into an object. Partials will always be found in * src/themes, whereas helpers may instead be found in dist/themes if TypeScript * can't be imported directly. */ async function readThemes() { const themes = {}; // Handlebars partials are located in src and not in dist const srcThemes = path_1.default.resolve(__dirname, '../src/themes'); const distThemes = path_1.default.resolve(__dirname, 'themes'); for (const theme of await fs_1.promises.readdir(srcThemes, { withFileTypes: true })) { if (theme.isDirectory()) { const { name } = theme; themes[name] = await readTemplates(path_1.default.join(srcThemes, name), path_1.default.join(distThemes, name)); } } return themes; } //# sourceMappingURL=templates.js.map