mirror of
https://github.com/cupcakearmy/cometa.git
synced 2025-03-12 22:37:28 +00:00
old proto
This commit is contained in:
parent
15d1fd58cd
commit
1bf9876f7e
98
old/actions.ts
Normal file
98
old/actions.ts
Normal file
@ -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
|
||||||
|
})
|
||||||
|
}
|
76
old/parser.ts
Normal file
76
old/parser.ts
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user