improve layout
This commit is contained in:
parent
6045144e9e
commit
d5b7bc16e0
|
|
@ -1,5 +1,3 @@
|
|||
import { runCode } from 'shrimp'
|
||||
|
||||
const buffer: string[] = []
|
||||
const NOSPACE_TOKEN = '!!ribbit-nospace!!'
|
||||
const TAG_TOKEN = '!!ribbit-tag!!'
|
||||
|
|
@ -20,8 +18,8 @@ const HTML5_TAGS = [
|
|||
"time", "title", "tr", "track", "u", "ul", "var", "video", "wbr"
|
||||
]
|
||||
|
||||
export async function wrapAndRunCode(code: string, globals?: Record<string, any>): Promise<any> {
|
||||
return await runCode("ribbit do:\n " + code + "\nend", Object.assign({}, ribbitGlobals, globals))
|
||||
export function wrapCode(code: string): string {
|
||||
return "ribbit do:\n " + code + "\nend"
|
||||
}
|
||||
|
||||
export const ribbitGlobals = {
|
||||
|
|
@ -34,7 +32,8 @@ export const ribbitGlobals = {
|
|||
tag: async (tagFn: Function, atDefaults = {}) =>
|
||||
(atNamed = {}, ...args: any[]) => tagFn(Object.assign({}, atDefaults, atNamed), ...args),
|
||||
nospace: () => NOSPACE_TOKEN,
|
||||
echo: (...args: any[]) => console.log(...args)
|
||||
echo: (...args: any[]) => console.log(...args),
|
||||
'page-title': '🦐'
|
||||
}
|
||||
|
||||
for (const name of HTML5_TAGS) {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
import { AnsiUp } from 'ansi_up'
|
||||
import { Hono } from 'hono'
|
||||
import { serveStatic } from 'hono/bun'
|
||||
import { join, resolve } from 'path'
|
||||
import { wrapAndRunCode } from './ribbit'
|
||||
import { AnsiUp } from 'ansi_up'
|
||||
import { wrapCode, ribbitGlobals } from './ribbit'
|
||||
import { Shrimp } from 'shrimp'
|
||||
|
||||
export function startWeb(rootPath: string) {
|
||||
const root = resolve(rootPath)
|
||||
|
|
@ -31,25 +32,25 @@ export function startWeb(rootPath: string) {
|
|||
const layoutFile = Bun.file(layoutPath)
|
||||
let layoutCode = await layoutFile.exists() ? await layoutFile.text() : ''
|
||||
|
||||
const vm = new Shrimp(Object.assign({}, ribbitGlobals, { params }))
|
||||
|
||||
if (await file.exists()) {
|
||||
let content = ''
|
||||
|
||||
try {
|
||||
content = await wrapAndRunCode(await file.text(), { params })
|
||||
const code = wrapCode(await file.text())
|
||||
content = await vm.run(code)
|
||||
} catch (err) {
|
||||
return c.html(shrimpError(path, err), 500)
|
||||
}
|
||||
|
||||
if (layoutCode) {
|
||||
if (!layoutCode) return c.html(content)
|
||||
|
||||
try {
|
||||
return c.html(await wrapAndRunCode(layoutCode, { params, content }))
|
||||
return c.html(await vm.run(wrapCode(layoutCode), { content }))
|
||||
} catch (err) {
|
||||
return c.html(shrimpError(layoutPath, err), 500)
|
||||
}
|
||||
} else {
|
||||
return c.html(content)
|
||||
}
|
||||
|
||||
} else {
|
||||
return c.text('404 Not Found', 404)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user