2025-05-12 05:38:44 +09:00

313 lines
8.1 KiB
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 immutable_1 = __importDefault(require("immutable"));
const logger_1 = __importDefault(require("../utils/logger"));
const fs_1 = __importDefault(require("./fs"));
const config_1 = __importDefault(require("./config"));
const readme_1 = __importDefault(require("./readme"));
const summary_1 = __importDefault(require("./summary"));
const glossary_1 = __importDefault(require("./glossary"));
const languages_1 = __importDefault(require("./languages"));
const ignore_1 = __importDefault(require("./ignore"));
class Book extends immutable_1.default.Record({
// Logger for outptut message
// @ts-expect-error ts-migrate(2554) FIXME: Expected 2 arguments, but got 0.
logger: (0, logger_1.default)(),
// Filesystem binded to the book scope to read files/directories
fs: new fs_1.default(),
// Ignore files parser
ignore: new ignore_1.default(),
// Structure files
config: new config_1.default(),
readme: new readme_1.default(),
summary: new summary_1.default(),
glossary: new glossary_1.default(),
languages: new languages_1.default(),
// ID of the language for language books
language: String(),
// List of children, if multilingual (String -> Book)
books: immutable_1.default.OrderedMap()
}) {
getLogger() {
return this.get("logger");
}
getFS() {
return this.get("fs");
}
getIgnore() {
return this.get("ignore");
}
getConfig() {
return this.get("config");
}
getReadme() {
return this.get("readme");
}
getSummary() {
return this.get("summary");
}
getGlossary() {
return this.get("glossary");
}
getLanguages() {
return this.get("languages");
}
getBooks() {
return this.get("books");
}
getLanguage() {
return this.get("language");
}
/**
Return FS instance to access the content
@return {FS}
*/
getContentFS() {
const fs = this.getFS();
const config = this.getConfig();
const rootFolder = config.getValue("root");
if (rootFolder) {
return fs_1.default.reduceScope(fs, rootFolder);
}
return fs;
}
/**
Return root of the book
@return {string}
*/
getRoot() {
const fs = this.getFS();
return fs.getRoot();
}
/**
Return root for content of the book
@return {string}
*/
getContentRoot() {
const fs = this.getContentFS();
return fs.getRoot();
}
/**
Check if a file is ignore (should not being parsed, etc)
@param {string} ref
@return {Page|undefined}
*/
isFileIgnored(filename) {
const ignore = this.getIgnore();
const language = this.getLanguage();
// Ignore is always relative to the root of the main book
if (language) {
filename = path_1.default.join(language, filename);
}
return ignore.isFileIgnored(filename);
}
/**
Check if a content file is ignore (should not being parsed, etc)
@param {string} ref
@return {Page|undefined}
*/
isContentFileIgnored(filename) {
const config = this.getConfig();
const rootFolder = config.getValue("root");
if (rootFolder) {
filename = path_1.default.join(rootFolder, filename);
}
return this.isFileIgnored(filename);
}
/**
Is this book the parent of language's books
@return {boolean}
*/
isMultilingual() {
return this.getLanguages().getCount() > 0;
}
/**
Return true if book is associated to a language
@return {boolean}
*/
isLanguageBook() {
return Boolean(this.getLanguage());
}
/**
Return a languages book
@param {string} language
@return {Book}
*/
getLanguageBook(language) {
const books = this.getBooks();
return books.get(language);
}
/**
Add a new language book
@param {string} language
@param {Book} book
@return {Book}
*/
addLanguageBook(language, book) {
let books = this.getBooks();
books = books.set(language, book);
return this.set("books", books);
}
/**
Set the summary for this book
@param {Summary}
@return {Book}
*/
setSummary(summary) {
return this.set("summary", summary);
}
/**
Set the readme for this book
@param {Readme}
@return {Book}
*/
setReadme(readme) {
return this.set("readme", readme);
}
/**
Set the configuration for this book
@param {Config}
@return {Book}
*/
setConfig(config) {
return this.set("config", config);
}
/**
Set the ignore instance for this book
@param {Ignore}
@return {Book}
*/
setIgnore(ignore) {
return this.set("ignore", ignore);
}
/**
Change log level
@param {string} level
@return {Book}
*/
setLogLevel(level) {
this.getLogger().setLevel(level);
return this;
}
/**
Create a book using a filesystem
@param {FS} fs
@return {Book}
*/
static createForFS(fs) {
return new Book({
fs: fs
});
}
/**
Infers the default extension for files
@return {string}
*/
getDefaultExt() {
// Inferring sources
const clues = [this.getReadme(), this.getSummary(), this.getGlossary()];
// List their extensions
const exts = clues.map((clue) => {
const file = clue.getFile();
if (file.exists()) {
return file.getParser().getExtensions().first();
}
else {
return null;
}
});
// Adds the general default extension
exts.push(".md");
// Choose the first non null
return exts.find((e) => {
return e !== null;
});
}
/**
Infer the default path for a Readme.
@param {boolean} [absolute=false] False for a path relative to
this book's content root
@return {string}
*/
getDefaultReadmePath(absolute) {
const defaultPath = `README${this.getDefaultExt()}`;
if (absolute) {
return path_1.default.join(this.getContentRoot(), defaultPath);
}
else {
return defaultPath;
}
}
/**
Infer the default path for a Summary.
@param {boolean} [absolute=false] False for a path relative to
this book's content root
@return {string}
*/
getDefaultSummaryPath(absolute) {
const defaultPath = `SUMMARY${this.getDefaultExt()}`;
if (absolute) {
return path_1.default.join(this.getContentRoot(), defaultPath);
}
else {
return defaultPath;
}
}
/**
Infer the default path for a Glossary.
@param {boolean} [absolute=false] False for a path relative to
this book's content root
@return {string}
*/
getDefaultGlossaryPath(absolute) {
const defaultPath = `GLOSSARY${this.getDefaultExt()}`;
if (absolute) {
return path_1.default.join(this.getContentRoot(), defaultPath);
}
else {
return defaultPath;
}
}
/**
Create a language book from a parent
@param {Book} parent
@param {string} language
@return {Book}
*/
static createFromParent(parent, language) {
const ignore = parent.getIgnore();
let config = parent.getConfig();
// Set language in configuration
config = config.setValue("language", language);
return new Book({
// Inherits config. logegr and list of ignored files
logger: parent.getLogger(),
config: config,
ignore: ignore,
language: language,
fs: fs_1.default.reduceScope(parent.getContentFS(), language)
});
}
}
exports.default = Book;