<commit_before>(ns workflo.macros.screen.bidi
  (:require [bidi.bidi :as bidi]
            [workflo.macros.screen :as scr]))

(defn route
  "Returns a bidi route for the given screen."
  [[screen-name screen]]
  (let [segments (:segments (:url screen))]
    [(-> segments
         (interleave (repeat "/"))
         (butlast)
         (vec))
     screen-name]))

(defn routes
  "Returns combined bidi routes for all registered screens."
  []
  ["/" (mapv route (scr/registered-screens))])

(defn match
  "Matches a URL against all screen routes. Returns a
   {:params <route params> :screen <screen>} map, where :screen
   holds the screen for the URL and the route params map all
   parameterizable URL segments (e.g. :user-id) to their values
   in the URL."
  [url]
  (when-let [result (bidi/match-route (routes) url)]
    {:params (:route-params result)
     :screen (scr/resolve-screen (-> result :handler name symbol))}))

(defn path
  "Returns a URL path for the given screen and the given URL
   parameters. Accepts both screen names and screens. For example,

   (path 'user :user-id 1)
   (path (workflo.macros.screen/resolve-screen 'user) :user-id 1)

   are both acceptable uses of this function."
  [screen-or-name & params]
  (let [screen-name (cond-> screen-or-name
                      (map? screen-or-name)
                      :name)]
    (apply (partial bidi/path-for (routes) screen-name)
           params)))<commit_msg>Add a bidi router based on screens defined with defscreen<commit_after>(ns workflo.macros.screen.bidi
  (:require [bidi.bidi :as bidi]
            [bidi.router :as br]
            [workflo.macros.screen :as scr]))

(defn route
  "Returns a bidi route for the given screen."
  [[screen-name screen]]
  (let [segments (:segments (:url screen))]
    [(-> segments
         (interleave (repeat "/"))
         (butlast)
         (vec))
     screen-name]))

(defn routes
  "Returns combined bidi routes for all registered screens."
  []
  ["/" (mapv route (scr/registered-screens))])

(defn match-location
  [location]
  {:screen (scr/resolve-screen (-> location :handler name symbol))
   :params (:route-params location)})

(defn match
  "Matches a URL against all screen routes. Returns a
   {:params <route params> :screen <screen>} map, where :screen
   holds the screen for the URL and the route params map all
   parameterizable URL segments (e.g. :user-id) to their values
   in the URL."
  [url]
  (when-let [location (bidi/match-route (routes) url)]
    (match-location location)))

(defn path
  "Returns a URL path for the given screen and the given URL
   parameters. Accepts both screen names and screens. For example,

   (path 'user :user-id 1)
   (path (workflo.macros.screen/resolve-screen 'user) :user-id 1)

   are both acceptable uses of this function."
  [screen-or-name & params]
  (let [screen-name (cond-> screen-or-name
                      (map? screen-or-name)
                      :name)]
    (apply (partial bidi/path-for (routes) screen-name)
           params)))

(defn- on-navigate
  [env location]
  (let [{:keys [screen params]} (match-location location)]
    (some-> env :mount-screen (apply [screen params]))))

(defn router
  [{:keys [default-screen
           mount-screen]
    :or   {default-screen 'home}
    :as   env}]
  (br/start-router! (routes)
                    {:on-navigate #(on-navigate env %)
                     :default-location {:handler default-screen}}))
{'fraction_non_alphanumeric': 0.1289855072463768, 'fraction_numerical': 0.0011594202898550724, 'mean_word_length': 2.7388949079089926, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3940949', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1064, 'n_words': 362}