solidity-docgen
Version:
Solidity API documentation automatic generator.
96 lines • 3.04 kB
JavaScript
"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 minimatch_1 = __importDefault(require("minimatch"));
const lodash_1 = require("lodash");
const page_1 = require("./page");
class Sitemap {
static generate(source, readmes, ext, contractPages) {
if (contractPages) {
return new ContractSitemap(source, ext);
}
else if (readmes.length > 0) {
return new ReadmeSitemap(source, readmes);
}
else {
return new DefaultSitemap(source, ext);
}
}
links(origin) {
function* generate(sitemap) {
for (const { path, contracts } of sitemap.pages) {
const relativePath = relative(origin.path, path);
for (const c of contracts) {
for (const target of c.linkable) {
yield { target, path, relativePath };
}
}
}
}
return Array.from(generate(this));
}
}
exports.Sitemap = Sitemap;
class DefaultSitemap extends Sitemap {
constructor(source, ext) {
super();
this.source = source;
this.ext = ext;
}
get pages() {
return [new page_1.DefaultPage(this, this.ext, this.source.contracts)];
}
}
class ReadmeSitemap extends Sitemap {
constructor(source, readmes) {
super();
this.source = source;
this.readmes = readmes;
}
get pages() {
const contracts = groupBy(this.source.contracts, c => this.locate(c));
return this.readmes.map(r => new page_1.ReadmePage(this, r, contracts[path_1.default.dirname(r.path)] || []));
}
locate(contract) {
const matches = this.locations.filter(l => isContainedIn(l, contract.file.path));
return lodash_1.maxBy(matches, l => l.length);
}
get locations() {
return this.readmes.map(r => path_1.default.dirname(r.path));
}
}
class ContractSitemap extends Sitemap {
constructor(source, ext) {
super();
this.source = source;
this.ext = ext;
}
get pages() {
return this.source.contracts.map(c => new page_1.ContractPage(this, c, this.ext));
}
}
function isContainedIn(location, file) {
return minimatch_1.default(file, path_1.default.join(location, '**/*'));
}
function relative(origin, target) {
if (path_1.default.normalize(origin) === path_1.default.normalize(target)) {
return '';
}
else {
return path_1.default.relative(path_1.default.dirname(origin), target);
}
}
function groupBy(collection, key) {
const res = {};
for (const elem of collection) {
const k = key(elem);
if (k !== undefined) {
(res[k] || (res[k] = [])).push(elem);
}
}
return res;
}
//# sourceMappingURL=sitemap.js.map