From 1bf9876f7ea8d309bf952dd26fa9ce2db2a0b349 Mon Sep 17 00:00:00 2001 From: nicco Date: Tue, 6 Feb 2018 19:59:51 +0100 Subject: [PATCH] old proto --- old/actions.ts | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ old/parser.ts | 76 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 old/actions.ts create mode 100644 old/parser.ts diff --git a/old/actions.ts b/old/actions.ts new file mode 100644 index 0000000..1298009 --- /dev/null +++ b/old/actions.ts @@ -0,0 +1,98 @@ + +import { dereduce, getFromData } from "./parser" +import { isRender, Part, re, options, error } from "../options" +import { readFileSync } from "../util" +import * as path from 'path' + +/** + * Removes all the comments from the html string + * + * @param {string} html - HTML to be cleaned + */ +export function comment(parts: Part[]): Part[] { + return dereduce(parts, part => { + if (!isRender(part)) + return part + + return part.replace(new RegExp(`${re.begin}${re.comment}(.|\n)*?${re.comment}${re.ending}`, 'g'), '') + }) +} + +/** + * Inserts partials into the template + * + * @param {string} html - The html to be manipulated + * @param {number} [depth] - How deep the current recursion is + */ +export function importer(parts: Part[], depth = 0): Part[] { + + if (depth > options.max_recursion) + throw new Error(error.parse.import_recursion) + + return dereduce(parts, part => { + if (!isRender(part)) + return part + + const begin = re.begin + re.incude // Beginning charachter + const ending = re.ending // Ending charachter + + // All include statements in the html string + const includes = part.match(new RegExp(`${begin}.*?${ending}`, 'g')) + if (includes !== null) + for (const i of includes) { + + const template_name: string = i.slice(begin.length, -ending.length).trim() + const file_name: string = path.join(options.template_dir, `${template_name}.${options.template_ext}`) + const file: Part = readFileSync(file_name) + const render: Part[] = importer([file], ++depth) + + const str: Part = render[0] + if (isRender(str)) + part = part.replace(i, str) + + } + return part + }) + +} + +export function variables(parts: Part[], depth = 0): Part[] { + + return dereduce(parts, part => { + if (!isRender(part)) + return part + + const reg_begin = new RegExp(re.begin) + const reg_ending = new RegExp(re.ending) + + const ret: Part[] = [] + + let begin = part.match(reg_begin) // Starting char + while (begin !== null) { + if (begin.index === undefined) + throw new Error(error.parse.default) + + // Push text before + ret.push(part.substr(0, begin.index)) + + part = part.slice(begin.index + begin[0].length) + + // Get closing tag + const end = part.match(reg_ending) + if (end === null || end.index === undefined) + throw new Error(error.parse.default) + + const variable_name: string = part.substring(0, end.index) + + ret.push(data => getFromData(data, variable_name)) + + part = part.slice(end.index + end[0].length) + + begin = part.match(reg_begin) // Starting char + } + + ret.push(part) + + return ret + }) +} \ No newline at end of file diff --git a/old/parser.ts b/old/parser.ts new file mode 100644 index 0000000..dfe6bbe --- /dev/null +++ b/old/parser.ts @@ -0,0 +1,76 @@ +import { re, PartFunction, Part, Render, isRender, options } from '../options' +import { readFile } from '../util' +import * as path from 'path' +import * as actions_old from './actions' + +export function getFromData(data: any, name: string): string { + + name = name.trim() + + // If not matches the valid pattern of a getter, return empty string + const valid: boolean = /^[A-z]\w*(\.[A-z]\w*|\[\d+\]|\[('|")\w+\2\]|\[[A-z]\w*\])*$/.test(name) + if (!valid) + return '' + + name = name.replace(/('|")/g, '') + name = name.replace(/\[(\w+)\]/g, '.$1') + + for (const i of name.split('.')) + data = data[i] + + return String(data) +} + +export function dereduce(parts: Part[], func: ((part: Part) => Part[] | Part)): Part[] { + + if (parts.length === 0) + return [] + + let ret = func(parts[0]) + if (!Array.isArray(ret)) + ret = [ret] + return ret.concat(dereduce(parts.slice(1), func)) + +} + +export function doActions(parts: Part[] | Part): Part[] { + + const start_time = Date.now() + + if (!Array.isArray(parts)) + parts = [parts] + + const action_funcs = [actions_old.comment, actions_old.importer] + for (const action of action_funcs) { + parts = action(parts) + } + + console.log(`Rendered in ${Date.now() - start_time}ms`) + return parts +} + +export function computeParts(parts: Part[], data = {}): Render { + if (parts.length === 0) + return '' + + return computePart(parts[0], data) + computeParts(parts.slice(1), data) +} + +function computePart(part: Part, data = {}): Render { + if (isRender(part)) + return part + else + return computePartFunction(part, data) +} + +function computePartFunction(func: PartFunction, data = {}): Render { + if (isRender(func)) + return func; + else { + const ret = func(data) + if (isRender(ret)) + return ret + else return computeParts(ret, data) + // return computePart(, data) + } +} \ No newline at end of file