diff --git a/swig/swig-tests.ts b/swig/swig-tests.ts index 1f68aa44c..929a67c89 100644 --- a/swig/swig-tests.ts +++ b/swig/swig-tests.ts @@ -2,24 +2,61 @@ import swig = require('swig'); +var swigObj: swig.Swig; var value: any; var str: string; var num: number; var bool: boolean; -var opts: swig.Options = { - allowErrors: bool, - autoescape: bool, - cache: bool, - encoding: str, - filters: value, - root: str, - tags: value, - extensions: value, - tzOffset: num +function replaceMs(input:string): string { + return input.replace(/m/g, 'f'); +} + +var opts: swig.SwigOptions = { + autoescape: bool, + cache: value, + cmtControls: ["", ""], + loader: swig.loaders.fs(), + locals: { testVal: "my test" }, + tagControls: ["", ""], + varControls: ["", ""] }; -swig.init(opts); -value = swig.compileFile(str); -value = swig.compile(str); -value = swig.compile(str, opts); +swig.setDefaults(opts); +swig.setDefaultTZOffset(num); +swigObj = new swig.Swig(opts); +str = swig.compileFile(str)(); +swig.compileFile(str, {}, function (err: Error, render: (locals: any) => string) { + str = render({}); +}); +str = swig.compile(str)(); +str = swig.compile(str, opts)(); +swig.invalidateCache(); +swig.setFilter("replaceMs", replaceMs); +swig.setTag('tacos', function (): boolean { return true; }, function (): string { return 'Tacos' }, true, true); +swig.setExtension("test", function (v: string) { return 'test: ' + v }); +value = swig.precompile(str, opts); +str = swig.render(str, opts); +str = swig.renderFile(str, {}); +swig.renderFile(str, {}, function (err: Error, output: string) { + +}); +console.log(swig.version); + + +str = swigObj.compileFile(str)(); +swigObj.compileFile(str, opts, function (err: Error, render: (locals: any) => string) { + str = render({}); +}); +str = swigObj.compile(str)(); +str = swigObj.compile(str, opts)(); +swigObj.invalidateCache(); +swigObj.setFilter("replaceMs", replaceMs); +swigObj.setTag('tacos', function (): boolean { return true; }, function (): string { return 'Tacos' }, true, true); +swigObj.setExtension("test", function (v: string) { return 'test: ' + v }); +value = swigObj.precompile(str, opts); +str = swigObj.render(str, opts); +str = swigObj.renderFile(str, {}); +swigObj.renderFile(str, {}, function (err: Error, output: string) { + +}); \ No newline at end of file diff --git a/swig/swig.d.ts b/swig/swig.d.ts index 4d8bc9ebe..4f2b22123 100644 --- a/swig/swig.d.ts +++ b/swig/swig.d.ts @@ -1,27 +1,158 @@ // Type definitions for swig // Project: http://github.com/paularmstrong/swig -// Definitions by: Carlos Ballesteros Velasco +// Definitions by: Peter Harris , Carlos Ballesteros Velasco // Definitions: https://github.com/borisyankov/DefinitelyTyped -// Imported from: https://github.com/soywiz/typescript-node-definitions/swig.d.ts - // API Documentation : http://paularmstrong.github.io/swig/docs/api/ declare module "swig" { - export function init(options: Options): void; - export function compileFile(filepath: string): any; - export function compile(source: string, options?: Options): any; - export function renderFile(pathName: string, locals?: any, cb?: (err: any, output: string) => void): string; + export class Swig { + constructor(options?: SwigOptions); - export interface Options { - allowErrors?: boolean; - autoescape?: boolean; - cache?: boolean; - encoding?: string; - filters?: any; - root?: string; - tags?: any; - extensions?: any; - tzOffset?: number; - } -} + setFilter( + name: string, + method: (input: any, ...args: any[]) => string + ): void; + setTag( + name: string, + parse: (str?: string, line?: string, parser?: Object, types?: lexer.TYPES, stack?: any, opts?: Object, swig?: Swig) => boolean, + compile: (compiler?: (content?: string, parents?: any, options?: any, blockName?: string) => string, args?: any[], content?: string, parents?: any, options?: any, blockName?: string) => string, + ends?: boolean, + blockLevel?: boolean + ): void; + setExtension(name: string, object: any): void; + parseFile(pathName: string, options?: any): parser.ParseReturn; + precompile(source: string, options?: SwigOptions): any; + compile(source: string, options?: SwigOptions): (locals?: any) => string; + compileFile(pathname: string, options: SwigOptions, cb: (err: Error, compiledRender: (locals?: any) => string) => void): void; + compileFile(pathname: string, options?: SwigOptions): (locals?: any) => string; + render(source: string, options?: SwigOptions): string; + renderFile(pathName: string, locals: any, cb: (err: Error, output: string) => void): void; + renderFile(pathName: string, locals?: any): string + run(templateFn: Function, locals?: any, filePath?: string): string; + invalidateCache(): void; + + loaders: typeof loaders; + } + + export module lexer { + export enum TYPES { + /** Whitespace */ + WHITESPACE = 0, + /** Plain string */ + STRING = 1, + /** Variable filter */ + FILTER = 2, + /** Empty variable filter */ + FILTEREMPTY = 3, + /** Function */ + FUNCTION = 4, + /** Function with no arguments */ + FUNCTIONEMPTY = 5, + /** Open parenthesis */ + PARENOPEN = 6, + /** Close parenthesis */ + PARENCLOSE = 7, + /** Comma */ + COMMA = 8, + /** Variable */ + VAR = 9, + /** Number */ + NUMBER = 10, + /** Math operator */ + OPERATOR = 11, + /** Open square bracket */ + BRACKETOPEN = 12, + /** Close square bracket */ + BRACKETCLOSE = 13, + /** Key on an object using dot-notation */ + DOTKEY = 14, + /** Start of an array */ + ARRAYOPEN = 15, + /** End of an array + * Currently unused + ARRAYCLOSE = 16, */ + /** Open curly brace */ + CURLYOPEN = 17, + /** Close curly brace */ + CURLYCLOSE = 18, + /** Colon (:) */ + COLON = 19, + /** JavaScript-valid comparator */ + COMPARATOR = 20, + /** Boolean logic */ + LOGIC = 21, + /** Boolean logic "not" */ + NOT = 22, + /** true or false */ + BOOL = 23, + /** Variable assignment */ + ASSIGNMENT = 24, + /** Start of a method */ + METHODOPEN = 25, + /** End of a method + * Currently unused + METHODEND = 26, */ + /** Unknown type */ + UNKNOWN = 100 + } + + export function read(str: string): string[]; + } + + export module parser { + interface ParseReturn { + name: string; + parent: any; + tokens: any[]; + blocks: any; + } + } + + export interface SwigOptions { + autoescape?: boolean; + cache?: any; + cmtControls?: string[]; + loader?: TemplateLoader; + locals?: any; + tagControls?: string[]; + varControls?: string[]; + } + + export interface TemplateLoader { + resolve(to: string, from: string): string; + load(identifier: string, callback?: (err: Error, data: any) => void): void; + load(identifier:string): any; + } + + export module loaders { + export function fs(basepath?: string, encoding?: string): TemplateLoader; + export function memory(mapping: any, basepath?: string): TemplateLoader; + } + + export var version: string; + export function setDefaults(options: SwigOptions): void; + export function setDefaultTZOffset(offset: number): void; + export function setFilter( + name: string, + method: (input: any, ...args: any[]) => string + ): void; + export function setTag( + name: string, + parse: (str?: string, line?: string, parser?: Object, types?: lexer.TYPES, stack?: any, opts?: Object, swig?: Swig) => boolean, + compile: (compiler?: (content?: string, parents?: any, options?: any, blockName?: string) => string, args?: any[], content?: string, parents?: any, options?: any, blockName?: string) => string, + ends?: boolean, + blockLevel?: boolean + ): void; + export function setExtension(name: string, object: any): void; + export function parseFile(pathName: string, options?: any): parser.ParseReturn; + export function precompile(source: string, options?: SwigOptions): any; + export function compile(source: string, options?: SwigOptions): (locals?: any) => string; + export function compileFile(pathname: string, options: SwigOptions, cb: (err: Error, compiledRender: (locals?: any) => string) => void): void; + export function compileFile(pathname: string, options?: SwigOptions): (locals?: any) => string; + export function render(source: string, options?: SwigOptions): string; + export function renderFile(pathName: string, locals: any, cb: (err: Error, output: string) => void): void; + export function renderFile(pathName: string, locals?: any): string + export function run(templateFn: Function, locals?: any, filePath?: string): string; + export function invalidateCache(): void; +} \ No newline at end of file