(ns chef.utils (:require [dotenv :as env] [ring.util.response :as ruresp])) (defn gen-page [title & content] [:html [:head [:meta {:name "viewport" :content "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"}] [:title title] [:link {:rel :stylesheet :href "/static/style.css"}] [:meta {:http-equiv "content-type" :content "text/html; charset=utf-8"}] [:meta {:name "robots" :content "noindex,nofollow"}]] (apply conj [:body] content [[:script {:src "/static/htmx.js"}] [:script {:src "/static/hyperscript.js"}]])]) (defmacro auth-only [request & body] `(if (some? (get-in ~request [:session :oauth-token])) (do ~@body) ~(-> (ruresp/response "Unauthorized.") (ruresp/status 302) (ruresp/header "Location" "/auth")))) (defn s->int-or-nil [s] (try (Integer/parseInt s) (catch Exception _ nil))) (defn parse-ingredients [s] (->> s (re-seq #"([A-z0-9 ]*)=([0-9]*) ?([A-z]*)") (map #(hash-map :description (nth % 1) :amount (Integer/parseInt (nth % 2)) :unit (nth % 3))))) (defn valid-ingredients? [s] (and (string? s) (->> s (re-matches #"(([A-z0-9 ]*)=([0-9]*) ?([A-z]*)\n?)*") some?))) (defn dprintln [& content] (when (= "TRUE" (env/env "DEBUG_MODE")) (apply println content)))