mirror of
https://github.com/cupcakearmy/cometa.git
synced 2025-03-12 22:37:28 +00:00
Testing
This commit is contained in:
parent
353be262e3
commit
00f35a7a98
13
src/app.ts
13
src/app.ts
@ -50,6 +50,7 @@ async function render(template_name: string, data?: any): Promise<string> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Render the template and return the html
|
||||||
const compiled = cache.get(template_name)
|
const compiled = cache.get(template_name)
|
||||||
if (compiled)
|
if (compiled)
|
||||||
return parser.computeParts(compiled.template, data)
|
return parser.computeParts(compiled.template, data)
|
||||||
@ -59,17 +60,7 @@ async function render(template_name: string, data?: any): Promise<string> {
|
|||||||
|
|
||||||
async function go() {
|
async function go() {
|
||||||
const ret = await render('new', {
|
const ret = await render('new', {
|
||||||
test: true,
|
arr: [{ test: true }, { test: false }]
|
||||||
testa: true,
|
|
||||||
title: 'test',
|
|
||||||
body: {
|
|
||||||
p: [
|
|
||||||
'omg',
|
|
||||||
{
|
|
||||||
check: 'let goo'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
ret.log()
|
ret.log()
|
||||||
|
@ -1,76 +0,0 @@
|
|||||||
import { Part, re, error, ActionFunction, ActionReturn } from './options'
|
|
||||||
import * as actions from './actions'
|
|
||||||
|
|
||||||
const rexp = Object.freeze({
|
|
||||||
begin: new RegExp(re.begin, 'g'),
|
|
||||||
end: new RegExp(re.ending, 'g'),
|
|
||||||
})
|
|
||||||
|
|
||||||
interface Next {
|
|
||||||
start: number
|
|
||||||
end: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export const compileBlock: ActionFunction = part => {
|
|
||||||
|
|
||||||
let next: Next
|
|
||||||
const getNext = (s: string): Next => Object.freeze({
|
|
||||||
start: s.search(rexp.begin),
|
|
||||||
end: s.search(rexp.end),
|
|
||||||
})
|
|
||||||
|
|
||||||
let ret: ActionReturn = {
|
|
||||||
parts: [],
|
|
||||||
length: NaN
|
|
||||||
}
|
|
||||||
|
|
||||||
function addToRet(item: any) {
|
|
||||||
ret.parts = ret.parts.concat(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
next = getNext(part)
|
|
||||||
while (next.start !== -1) {
|
|
||||||
|
|
||||||
if (next.start === null || next.end === null)
|
|
||||||
throw new Error(error.parse.default)
|
|
||||||
|
|
||||||
addToRet(part.substr(0, next.start))
|
|
||||||
part = part.slice(next.start)
|
|
||||||
|
|
||||||
let func: ActionFunction
|
|
||||||
|
|
||||||
switch (part[re.begin.length]) {
|
|
||||||
case re.comment:
|
|
||||||
func = actions.comment
|
|
||||||
break
|
|
||||||
case re.if:
|
|
||||||
func = actions.logic
|
|
||||||
break
|
|
||||||
// case re.for:
|
|
||||||
// func = actions.loop
|
|
||||||
// break
|
|
||||||
case re.incude:
|
|
||||||
func = actions.importer
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
func = actions.variables
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = func(part)
|
|
||||||
addToRet(result.parts)
|
|
||||||
part = part.slice(result.length)
|
|
||||||
|
|
||||||
next = getNext(part)
|
|
||||||
}
|
|
||||||
|
|
||||||
addToRet(part)
|
|
||||||
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
export function compile(html: string, options = {}): Part[] {
|
|
||||||
const parts: Part[] = compileBlock(html).parts
|
|
||||||
return parts
|
|
||||||
|
|
||||||
}
|
|
@ -1,21 +1,5 @@
|
|||||||
<!doctype html>
|
<ul>
|
||||||
<html>
|
{{* i in arr}}
|
||||||
|
<li>{{i}}</li>
|
||||||
<head>
|
{{/*}}
|
||||||
|
</ul>
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
|
|
||||||
|
|
||||||
<section>
|
|
||||||
a{{# kkk #}b {{#mhh}} #}}b {{test}} {{> sub/form }}
|
|
||||||
<h2>{{ title }}</h2>
|
|
||||||
<p>{{ body.p[1].check }}</p>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
a{{? test }}b{{? !testa }}c{{/?}} {{? title }} {{? title }}e{{/?}} c{{/?}}d{{/?}}e
|
|
||||||
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
Loading…
x
Reference in New Issue
Block a user