55 lines
1.7 KiB
Hy
55 lines
1.7 KiB
Hy
(import bleach [clean])
|
|
(import mimetypes [guess-type])
|
|
(import subprocess [check-output])
|
|
(import re)
|
|
(import os [environ :as hy-env])
|
|
(import asyncio)
|
|
(import aiofiles)
|
|
|
|
(setv (get hy-env "PATH") (+ (get hy-env "PATH") ":./www/site/scripts"))
|
|
|
|
(defn :async execute-bash [data]
|
|
|
|
(defn :async process-match [match]
|
|
(let [command (.group match 1)
|
|
process (await (asyncio.create-subprocess-shell
|
|
command
|
|
:stdout asyncio.subprocess.PIPE
|
|
:stderr asyncio.subprocess.PIPE
|
|
:shell True
|
|
:executable "/bin/bash"
|
|
:env hy-env))]
|
|
(let [[stdout stderr] (await (.communicate process))]
|
|
(.decode stdout :errors "ignore"))))
|
|
|
|
(setv matches (list (re.finditer r"\$\[(.*?)\]" data)))
|
|
(setv replacements (await (asyncio.gather #* (lfor match matches (process-match match)))))
|
|
|
|
(setv result (list data))
|
|
(for [match (reversed matches)]
|
|
(setv (cut result (match.start) (match.end)) (get replacements (.index matches match))))
|
|
(.join "" result))
|
|
|
|
(defn :async parse-html-file [path [no-exec False] #** kwargs]
|
|
(with [:async f (aiofiles.open path "r")]
|
|
(setv data (await (.read f))))
|
|
|
|
(for [[k v] (.items kwargs)]
|
|
(setv data (.replace data f"{"{"}{k}{"}"}" (str v))))
|
|
|
|
(when no-exec
|
|
(return data))
|
|
|
|
(await (execute-bash data)))
|
|
|
|
(defn :async send-raw-file [path]
|
|
(setv [mime-type _] (guess-type path))
|
|
|
|
(when (not mime-type)
|
|
(setv mime-type "text/plain"))
|
|
|
|
(with [:async f (aiofiles.open path "rb")]
|
|
(setv data (await (.read f))))
|
|
|
|
(return #({"Content-Type" mime-type "Cache-Control" "max-age=300, stale-while-revalidate=3600"} data)))
|