|
@@ -1,9 +1,9 @@
|
|
import pug from "pug";
|
|
import pug from "pug";
|
|
import fs from "fs";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
import path from "path";
|
|
|
|
+
|
|
import { Renderer } from "./Renderer";
|
|
import { Renderer } from "./Renderer";
|
|
-import Lexer from "./compiler/Lexer";
|
|
|
|
-import Parser from "./compiler/Parser";
|
|
|
|
|
|
+import Plugin from "./compiler/Plugin";
|
|
|
|
|
|
export namespace Compiler {
|
|
export namespace Compiler {
|
|
export interface Options {
|
|
export interface Options {
|
|
@@ -22,12 +22,13 @@ export namespace Compiler {
|
|
|
|
|
|
export default class PupperCompiler {
|
|
export default class PupperCompiler {
|
|
/**
|
|
/**
|
|
- * Compiles a single template file
|
|
|
|
|
|
+ * Compiles a single template file into a renderer instance
|
|
* @param file The file to be compiled
|
|
* @param file The file to be compiled
|
|
* @returns
|
|
* @returns
|
|
*/
|
|
*/
|
|
- public compileSync(file: string, options: Compiler.Options = {}): Renderer {
|
|
|
|
|
|
+ public compileFile(file: string, options: Compiler.Options = {}): Renderer {
|
|
return this.compile(fs.readFileSync(file, "utf8"), {
|
|
return this.compile(fs.readFileSync(file, "utf8"), {
|
|
|
|
+ ...options,
|
|
pug: {
|
|
pug: {
|
|
basedir: path.dirname(file),
|
|
basedir: path.dirname(file),
|
|
filename: file,
|
|
filename: file,
|
|
@@ -35,25 +36,40 @@ export default class PupperCompiler {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Parses the compiler options into pug options
|
|
|
|
+ * and put our plugins into it
|
|
|
|
+ * @param options The compiler options
|
|
|
|
+ * @returns
|
|
|
|
+ */
|
|
|
|
+ private getPugOptions(options: Compiler.Options = {}): pug.Options {
|
|
|
|
+ // Create a new parser for this pug instance
|
|
|
|
+ const parser = new Plugin();
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ name: "pupper",
|
|
|
|
+ filename: "pupper.pug",
|
|
|
|
+ compileDebug: options.debug || false,
|
|
|
|
+ // Always use self to prevent conflicts with other compilers
|
|
|
|
+ self: true,
|
|
|
|
+ // @ts-ignore
|
|
|
|
+ plugins: [parser],
|
|
|
|
+ ...options.pug || {}
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Compiles a template string into a renderer instance
|
|
|
|
+ * @param template The template string to be compiled
|
|
|
|
+ * @param options The compiler options
|
|
|
|
+ * @returns
|
|
|
|
+ */
|
|
public compile(template: string, options: Compiler.Options = {}): Renderer {
|
|
public compile(template: string, options: Compiler.Options = {}): Renderer {
|
|
- const parser = new Parser();
|
|
|
|
|
|
+
|
|
|
|
|
|
try {
|
|
try {
|
|
return new Renderer(
|
|
return new Renderer(
|
|
- pug.compile(template, {
|
|
|
|
- name: "pupper",
|
|
|
|
- filename: "pupper.pug",
|
|
|
|
- compileDebug: options.debug || false,
|
|
|
|
- // Always use self to prevent conflicts with other compilers
|
|
|
|
- self: true,
|
|
|
|
- // @ts-ignore
|
|
|
|
- plugins: [{
|
|
|
|
- lex: new Lexer(),
|
|
|
|
- preParse: parser.preParse.bind(this),
|
|
|
|
- postParse: parser.postParse.bind(this)
|
|
|
|
- }],
|
|
|
|
- ...options.pug || {}
|
|
|
|
- })
|
|
|
|
|
|
+ pug.compile(template, this.getPugOptions(options))
|
|
);
|
|
);
|
|
} catch(e) {
|
|
} catch(e) {
|
|
throw (options.debug ? e : new Error("Failed to compile template:" + e.message));
|
|
throw (options.debug ? e : new Error("Failed to compile template:" + e.message));
|
|
@@ -61,28 +77,16 @@ export default class PupperCompiler {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Compiles to a string
|
|
|
|
|
|
+ * Compiles a template string to a string
|
|
* @param template The template to be compiled
|
|
* @param template The template to be compiled
|
|
* @param options
|
|
* @param options
|
|
* @returns
|
|
* @returns
|
|
*/
|
|
*/
|
|
public compileToString(template: string, options: Compiler.Options = {}): string {
|
|
public compileToString(template: string, options: Compiler.Options = {}): string {
|
|
- const parser = new Parser();
|
|
|
|
|
|
+ const parser = new Plugin();
|
|
|
|
|
|
try {
|
|
try {
|
|
- const rendered = pug.compileClient(template, {
|
|
|
|
- name: "pupper",
|
|
|
|
- compileDebug: options.debug || false,
|
|
|
|
- // Always use self to prevent conflicts with other compilers
|
|
|
|
- self: true,
|
|
|
|
- // @ts-ignore
|
|
|
|
- plugins: [{
|
|
|
|
- lex: new Lexer(),
|
|
|
|
- preParse: parser.preParse.bind(this),
|
|
|
|
- preLoad: parser.postParse.bind(this)
|
|
|
|
- }],
|
|
|
|
- ...options.pug || {}
|
|
|
|
- });
|
|
|
|
|
|
+ const rendered = pug.compileClient(template, this.getPugOptions(options));
|
|
|
|
|
|
return /*javascript*/`
|
|
return /*javascript*/`
|
|
${rendered}
|
|
${rendered}
|