Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 81x 81x 81x 81x 81x 11x 11x 10x 10x 10x 10x 10x 10x 10x 92x 92x 92x 92x 48x 92x 103x 103x 103x 59x 103x 103x 102x 1513x 144x 144x 144x 1369x 15x 15x 15x 12x 12x 103x 17x 86x 86x 86x 86x 103x 144x 46x 98x 98x 144x 15x 2x 13x 13x 15x 26x 93x 93x 15x 15x 17x 17x 7x 7x 7x 20x 18x 19x 19x 19x 19x 3x 2x | import {ResourceParser} from './module/resourceParser'; import {HtmlCompiler} from './module/htmlCompiler'; import { DocumentNode, Fragment, FragmentContext, HtmlFormatter, MimeType, Page, Pipeline, PipelineCache, PipelineContext, PipelineIO, EvalContext, EvalFunction, getResourceTypeExtension } from '..'; import { invokeEvalFunc, isExpressionString, parseExpression, parseScript } from './module/evalEngine'; import {StandardHtmlFormatter} from './module/standardHtmlFormatter'; import {buildPage} from './module/pageBuilder'; import * as FsUtils from '../fs/fsUtils'; import Path from 'path'; import {computeRelativeResPath, resolveResPath} from '../fs/pathUtils'; import {StandardPipelineCache} from './pipelineCache'; import {hashMD5} from '../util/encodingUtils'; /** * Primary compilation pipeline. * Takes one raw, uncompiled page from the pipeline input, and writes one compiled, pure-html page to the pipeline output. * The produced page is fully compiled and ready to be rendered by a standard web browser. * Additionally, it will be formatted by any HTML Formatter provided. * * Any incidental resources (such as stylesheets) will be fed to the pipeline interface via createResource(). */ export class StandardPipeline implements Pipeline { readonly cache: PipelineCache; readonly pipelineIO: PipelineIOImpl; readonly htmlFormatter: HtmlFormatter; /** * General content parser */ readonly resourceParser: ResourceParser; /** * HTML compilation support */ readonly htmlCompiler: HtmlCompiler; /** * Create a new instance of the pipeline * @param pipelineIO? Optional override for standard pipelineIO * @param htmlFormatter?? Optional HTML formatter to use * @param resourceParser?? Optional override for standard ResourceParser * @param htmlCompiler?? Optional override for standard HtmlCompiler */ constructor(pipelineIO: PipelineIOImpl, htmlFormatter?: HtmlFormatter, resourceParser?: ResourceParser, htmlCompiler?: HtmlCompiler) { this.cache = new StandardPipelineCache(); this.pipelineIO = pipelineIO; this.htmlFormatter = htmlFormatter ?? new StandardHtmlFormatter(); this.resourceParser = resourceParser ?? new ResourceParser(); this.htmlCompiler = htmlCompiler ?? new HtmlCompiler(); } async compilePage(resPath: string): Promise<Page> { // resolve path to page resPath = resolveResPath(resPath); // compile fragment const pageFragment: Fragment = await this.compileFragmentOnly(resPath); const pageDom: DocumentNode = pageFragment.dom; // compile as page buildPage(pageDom); // format page this.htmlFormatter.formatDom(pageDom); // serialize to html const rawHtml: string = pageDom.toHtml(); // format html const formattedHtml: string = this.htmlFormatter.formatHtml(rawHtml); // write HTML await this.pipelineIO.writeResource(MimeType.HTML, resPath, formattedHtml); // create and return page return { path: resPath, dom: pageDom, html: formattedHtml }; } async compileFragment(resPath: string, fragmentContext?: FragmentContext): Promise<Fragment> { // resolve path to fragment resPath = resolveResPath(resPath); // compile fragment const fragment = await this.compileFragmentOnly(resPath, fragmentContext); // Only format if this fragment is being compiled standalone instead of as part of a page. // If this fragment is being used elsewhere, then it will be formatted there. // Formatting is an expensive operation and should not be duplicated if possible. const isTopLevel = fragmentContext === undefined; if (isTopLevel) { this.htmlFormatter.formatDom(fragment.dom); } return fragment; } private async compileFragmentOnly(resPath: string, fragmentContext?: FragmentContext): Promise<Fragment> { // get the root res path. If there is no context, then this fragment is the root resource. const rootResPath = fragmentContext?.rootResPath ?? resPath; // get fragment from cache or htmlSource const fragment = await this.getOrParseFragment(resPath); // create usage context if not provided if (fragmentContext === undefined) { fragmentContext = { slotContents: new Map(), scope: {}, fragmentResPath: resPath, rootResPath: rootResPath }; } // create pipeline context const pipelineContext: StandardPipelineContext = { pipeline: this, fragment: fragment, fragmentContext: fragmentContext, linksInPage: new Set(), stylesInPage: new Set() }; // compile under current context await this.htmlCompiler.compileFragment(fragment, pipelineContext); return fragment; } /** * Compiles a JS expression * Embedded scripts will be evaluated in the current context. * Plain text will be returned as-is. * * @param value Text value * @param evalContext Current compilation context * @returns compiled value of the input value */ async compileExpression(value: string, evalContext: EvalContext): Promise<unknown> { // check if this text contains JS code to evaluate if (isExpressionString(value)) { const expression = value.trim(); // get function for script const expressionFunc = this.getOrParseExpression(expression); // execute it return await invokeEvalFunc(expressionFunc, evalContext); } // value is plain string return value; } /** * Compiles and executes javascript. * The script can be multiple lines, and use any JS feature that is available within a function body. * Script will execute with the provided eval context. * * @param script Javascript code * @param evalContext Context to execute in * @returns The return value of the script, if any. */ async compileScript(script: string, evalContext: EvalContext): Promise<unknown> { const scriptText = script.trim(); // get function for script const scriptFunc = this.getOrParseScript(scriptText); // execute it return await invokeEvalFunc(scriptFunc, evalContext); } /** * Compiles CSS. Currently a no-op. * * @param css CSS text * @returns Compile CSS text */ compileCss(css: string): string { return css; } private async createLinkableResource(type: MimeType, contents: string): Promise<string> { // hash contents const contentsHash = hashMD5(contents); // get from cache, if present if (this.cache.createdResourceCache.has(contentsHash)) { return this.cache.createdResourceCache.get(contentsHash); } // if not in cache, then call PI to create resource const createdResourcePath = await this.pipelineIO.createResource(type, contents); const resPath = `@${ Path.sep }${ createdResourcePath }`; // store in cache this.cache.createdResourceCache.store(contentsHash, resPath); // Append "root-relative" prefix return resPath; } /** * Links a created resource to the compilation output. * This method ONLY handles saving the contents, it does not compile them or attach them to the actual HTML. * This method is ONLY for created (incidental) resources. * * @param type Type of resource * @param contents Contents as a UTF-8 string * @param rootResPath Path to the "root" fragment where this resources will be referenced * @returns path to reference linked resource */ async linkResource(type: MimeType, contents: string, rootResPath: string): Promise<string> { // create the resource and get the raw path const rawResPath = await this.createLinkableResource(type, contents); // adjust path relative to the output page and directory const resolvedResPath = resolveResPath(rawResPath, rootResPath); // Compute path relative from root return computeRelativeResPath(rootResPath, resolvedResPath); } /** * Gets a raw (uncompiled) text-based resource * * @param resPath Path to resource * @param mimeType Type of resource. */ async getRawText(resPath: string, mimeType: MimeType): Promise<string> { // resolve path to fragment resPath = resolveResPath(resPath); // get contents return await this.pipelineIO.getResource(mimeType, resPath); } reset(): void { // clear cache to reset state this.cache.clear(); } private async getOrParseFragment(resPath: string): Promise<Fragment> { let fragment: Fragment; if (this.cache.fragmentCache.has(resPath)) { // use cached fragment fragment = this.cache.fragmentCache.get(resPath); } else { // read HTML const html = await this.pipelineIO.getResource(MimeType.HTML, resPath); // parse fragment const parsedFragment = this.resourceParser.parseFragment(resPath, html); // keep in cache this.cache.fragmentCache.store(parsedFragment.path, parsedFragment); fragment = parsedFragment; } // return a clone return { path: fragment.path, dom: fragment.dom.clone() }; } private getOrParseExpression(expression: string): EvalFunction<unknown> { let expressionFunc: EvalFunction<unknown>; // get from cache, if present if (this.cache.expressionCache.has(expression)) { expressionFunc = this.cache.expressionCache.get(expression); } else { // compile text expressionFunc = parseExpression(expression); // store in cache this.cache.expressionCache.store(expression, expressionFunc); } return expressionFunc; } private getOrParseScript(script: string): EvalFunction<unknown> { let scriptFunc: EvalFunction<unknown>; // get from cache, if present if (this.cache.scriptCache.has(script)) { scriptFunc = this.cache.scriptCache.get(script); } else { // compile script scriptFunc = parseScript(script); // store in cache this.cache.scriptCache.store(script, scriptFunc); } return scriptFunc; } } /** * Extension of {@link PipelineContext} that narrows the pipeline to a StandardPipeline */ export interface StandardPipelineContext extends PipelineContext { readonly pipeline: StandardPipeline; } /** * Standard implementation of Pipeline IO. * This uses node.js APIs to access files on the local filesystem. */ export class PipelineIOImpl implements PipelineIO { readonly sourcePath: string; readonly destinationPath: string; /** * Creates a new NodePipelineInterface. * * @param sourcePath Path to source directory * @param destinationPath Path to destination directory */ constructor(sourcePath: string, destinationPath: string) { this.sourcePath = sourcePath; this.destinationPath = destinationPath; } async getResource(type: MimeType, resPath: string): Promise<string> { const htmlPath = this.resolveSourceResource(resPath); return await FsUtils.readFile(htmlPath); } async writeResource(type: MimeType, resPath: string, contents: string): Promise<void> { const htmlPath = this.resolveDestinationResource(resPath); await FsUtils.writeFile(htmlPath, contents, true); } async createResource(type: MimeType, contents: string): Promise<string> { const resPath = this.createResPath(type, contents); await this.writeResource(type, resPath, contents); return resPath; } resolveSourceResource(resPath: string): string { return Path.resolve(this.sourcePath, resPath); } resolveDestinationResource(resPath: string): string { return Path.resolve(this.destinationPath, resPath); } createResPath(type: MimeType, contents: string): string { const contentHash = hashMD5(contents); const extension = getResourceTypeExtension(type); const fileName = `${ contentHash }.${ extension }`; return Path.join('resources', fileName); } getSourceResPathForAbsolutePath(rawPath: string): string { return Path.relative(this.sourcePath, rawPath); } getDestinationResPathForAbsolutePath(rawPath: string): string { return Path.relative(this.destinationPath, rawPath); } } |