0.2.2 • Published 6 years ago
whatstpl-toolkit v0.2.2
WhatsTPL Toolkit
A toolkit for WhatsTPL.
This package contains the WhatsTPL parser (JavaScript implementation) and some useful functions that both work with NodeJS and browsers, and dependency free.
Example
const fs = require("fs");
const WhatsTplToolkit = require("whatstpl-toolkit");
var filename = "./example.html";
var parser = new WhatsTplToolkit.Parser(filename);
var node = parser.parse(fs.readFileSync(filename, "utf8"));
console.log(node);API
Parser
new Parser(filename?: string)thefilenameare only for error catching at parsing time.Parser.prototye.parse(tpl: string): NodeNodeis a TypeScript interface that contains:tag?: stringthe block tag name, only appears when the current node is a block or thetypeisvar.type: stringcould beroot,text,var,block,commentorsnippet.line: numberthe line number of the current node.column: numberthe column number of the current node.attributes?: { [name: string]: Attribute }Attributecontains:name: stringvalue: stringline: numbercolumn: number
contents: string | Node[]if thetypeisrootorblock, this property is an array ofNodethat contains children nodes.close?: booleanWhether the block tag is closed, both/>and</block-name>are considered to close the tag.
Parser.prototype.on(event: string, listner: (...args) => void)exactly the same as NodeJS emitter, but bring support to browsers. eventstext,var,block,commentorsnippetwill be fired when parsing the type (rootis not a blocktypeexactly), and the first argument passed tolistneris the current parsing node.
Constants
IsBrowser: booleanWhether the program is runing in a browser.Separator: "\\" | "/"Path separator.
Functions
escape(html: string): stringEscapes HTML tags.dirname(path: string): stringGets the dirname according to the given path.basename(filename: string, extname: string = ""): stringGets the basename of a file.extname(filename: string): stringGets the extension name of a file.normalizePath(path: string): stringNormalizes the given path, strips../and./, and corrects the path separator.getCwd(): stringGets the current working directory.isAbsPath(path: string): booleanChecks if the given path is absolute.getAbsPath(filename: string): stringGets the absolute path of a file.getObjectValues(obj: any): any[]Gets the values of an object.
UnclosedTagError
This is an error class used when passing a template, when it's thrown, the
error object will carry a filename, a line number and a column number,
so the program can know where the problem is in the template.
Node and Attribute interface
They've already been talked above, this package also exports them, so they could be used in the compile implementation.