import path from "path" ; import Immutable from "immutable" ; import Logger from "../utils/logger" ; import FS from "./fs" ; import Config from "./config" ; import Readme from "./readme" ; import Summary from "./summary" ; import Glossary from "./glossary" ; import Languages from "./languages" ; import Ignore from "./ignore" ; class Book extends Immutable . Record ( { logger : Logger ( ) , fs : new FS ( ) , ignore : new Ignore ( ) , config : new Config ( ) , readme : new Readme ( ) , summary : new Summary ( ) , glossary : new Glossary ( ) , languages : new Languages ( ) , language : String ( ) , books : Immutable . 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 } getContentFS ( ) { const fs = this . getFS ( ) ; const config = this . getConfig ( ) ; const rootFolder = config . getValue ( "root" ) ; if ( rootFolder ) { return FS . reduceScope ( fs , rootFolder ) ; } return fs ; } return { string } getRoot ( ) { const fs = this . getFS ( ) ; return fs . getRoot ( ) ; } return { string } getContentRoot ( ) { const fs = this . getContentFS ( ) ; return fs . getRoot ( ) ; } param { string } ref return { Page | undefined } isFileIgnored ( filename ) { const ignore = this . getIgnore ( ) ; const language = this . getLanguage ( ) ; if ( language ) { filename = path . join ( language , filename ) ; } return ignore . isFileIgnored ( filename ) ; } param { string } ref return { Page | undefined } isContentFileIgnored ( filename ) { const config = this . getConfig ( ) ; const rootFolder = config . getValue ( "root" ) ; if ( rootFolder ) { filename = path . join ( rootFolder , filename ) ; } return this . isFileIgnored ( filename ) ; } return { boolean } isMultilingual ( ) { return this . getLanguages ( ) . getCount ( ) > 0 ; } return { boolean } isLanguageBook ( ) { return Boolean ( this . getLanguage ( ) ) ; } param { string } language return { Book } getLanguageBook ( language ) { const books = this . getBooks ( ) ; return books . get ( language ) ; } 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 ) ; } param { Summary } return { Book } setSummary ( summary ) { return this . set ( "summary" , summary ) ; } param { Readme } return { Book } setReadme ( readme ) { return this . set ( "readme" , readme ) ; } param { Config } return { Book } setConfig ( config ) { return this . set ( "config" , config ) ; } param { Ignore } return { Book } setIgnore ( ignore ) { return this . set ( "ignore" , ignore ) ; } param { string } level return { Book } setLogLevel ( level ) { this . getLogger ( ) . setLevel ( level ) ; return this ; } param { FS } fs return { Book } static createForFS ( fs ) { return new Book ( { fs : fs , } ) ; } return { string } getDefaultExt ( ) { const clues = [ this . getReadme ( ) , this . getSummary ( ) , this . getGlossary ( ) ] ; const exts = clues . map ( ( clue ) => { const file = clue . getFile ( ) ; if ( file . exists ( ) ) { return file . getParser ( ) . getExtensions ( ) . first ( ) ; } else { return null ; } } ) ; exts . push ( ".md" ) ; return exts . find ( ( e ) => { return e !== null ; } ) ; } param { boolean } absolute return { string } getDefaultReadmePath ( absolute ) { const defaultPath = `README${ this . getDefaultExt ( ) }` ; if ( absolute ) { return path . join ( this . getContentRoot ( ) , defaultPath ) ; } else { return defaultPath ; } } param { boolean } absolute return { string } getDefaultSummaryPath ( absolute ) { const defaultPath = `SUMMARY${ this . getDefaultExt ( ) }` ; if ( absolute ) { return path . join ( this . getContentRoot ( ) , defaultPath ) ; } else { return defaultPath ; } } param { boolean } absolute return { string } getDefaultGlossaryPath ( absolute ) { const defaultPath = `GLOSSARY${ this . getDefaultExt ( ) }` ; if ( absolute ) { return path . join ( this . getContentRoot ( ) , defaultPath ) ; } else { return defaultPath ; } } param { Book } parent param { string } language return { Book } static createFromParent ( parent , language ) { const ignore = parent . getIgnore ( ) ; let config = parent . getConfig ( ) ; config = config . setValue ( "language" , language ) ; return new Book ( { logger : parent . getLogger ( ) , config : config , ignore : ignore , language : language , fs : FS . reduceScope ( parent . getContentFS ( ) , language ) , } ) ; } } export default Book ;