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