import path from "path" ; import nunjucks from "nunjucks" ; import nunjucks_do from "nunjucks-do" ; const DoExtension = nunjucks_do ( nunjucks ) ; import memoizeOne from "memoize-one" ; import Api from "../../api" ; import deprecate from "../../api/deprecate" ; import JSONUtils from "../../json" ; import LocationUtils from "../../utils/location" ; import fs from "../../utils/fs" ; import PathUtils from "../../utils/path" ; import TemplateEngine from "../../models/templateEngine" ; import templatesFolder from "../../constants/templatesFolder" ; import defaultFilters from "../../constants/defaultFilters" ; import Templating from "../../templating" ; import listSearchPaths from "./listSearchPaths" ; import fileToURL from "../helper/fileToURL" ; import resolveFileToURL from "../helper/resolveFileToURL" ; import Output from "../../models/output" ; /**
 * Directory for a theme with the templates
 */ function templateFolder ( dir ) { return path . join ( dir , templatesFolder ) ; } param { Output } output param { string } currentFile return { TemplateEngine } function createTemplateEngine ( output , currentFile ) { const book = output . getBook ( ) ; const state = output . getState ( ) ; const i18n = state . getI18n ( ) ; const config = book . getConfig ( ) ; const summary = book . getSummary ( ) ; const outputFolder = output . getRoot ( ) ; const searchPaths = listSearchPaths ( output ) ; const tplSearchPaths = searchPaths . map ( templateFolder ) ; const loader = new Templating . ThemesLoader ( tplSearchPaths ) ; const language = config . getValue ( "language" ) ; const context = Api . encodeGlobal ( output ) ; param { string } fileName return { boolean } function fileExists ( fileName ) { if ( ! fileName ) { return false ; } const filePath = PathUtils . resolveInRoot ( outputFolder , fileName ) ; return fs . existsSync ( filePath ) ; } param { string } filePath return { Object | undefined } function getArticleByPath ( filePath ) { const article = summary . getByPath ( filePath ) ; if ( ! article ) return undefined ; return JSONUtils . encodeSummaryArticle ( article ) ; } param { string } filePath return { Object | undefined } function getPageByPath ( filePath ) { const page = output . getPage ( filePath ) ; if ( ! page ) return undefined ; return JSONUtils . encodePage ( page , summary ) ; } return TemplateEngine . create ( { loader : loader , context : context , globals : { getArticleByPath : memoizeOne ( getArticleByPath ) , getPageByPath : memoizeOne ( getPageByPath ) , fileExists : fileExists , } , filters : defaultFilters . merge ( { /**
             * Translate a sentence
             */ t : function t ( s ) { return i18n . t ( language , s ) ; } , /**
             * Resolve an absolute file path into a
             * relative path.
             * it also resolve pages
             */ resolveFile : function ( filePath ) { filePath = resolveFileToURL ( output , filePath ) ; return LocationUtils . relativeForFile ( currentFile , filePath ) ; } , resolveAsset : function ( filePath ) { filePath = LocationUtils . toAbsolute ( filePath , "" , "" ) ; filePath = path . join ( "gitbook" , filePath ) ; filePath = LocationUtils . relativeForFile ( currentFile , filePath ) ; if ( book . isLanguageBook ( ) ) { filePath = path . join ( "../" , filePath ) ; } return LocationUtils . normalize ( filePath ) ; } , fileExists : deprecate . method ( book , "fileExists" , fileExists , 'Filter "fileExists" is deprecated, use "fileExists(filename)" ' ) , getArticleByPath : deprecate . method ( book , "getArticleByPath" , fileExists , 'Filter "getArticleByPath" is deprecated, use "getArticleByPath(filename)" ' ) , contentURL : function ( filePath ) { return fileToURL ( output , filePath ) ; } , } ) , extensions : { DoExtension : new DoExtension ( ) , } , } ) ; } export default createTemplateEngine ;