<commit_before>(ns {{name}}.website
  (:require
   [clojure.pprint :refer (pprint)]
   [tangrammer.component.co-dependency :refer (co-using)]
   [com.stuartsierra.component :refer (using)]
   [modular.ring :refer (WebRequestHandler)]
   [modular.bidi :refer (WebService as-request-handler)]
   [modular.template :refer (render-template template-model)]
   [bidi.bidi :refer (path-for)]
   [bidi.ring :refer (redirect)]
   [hiccup.core :refer (html)]
   [clojure.tools.logging :refer :all]
   [ring.util.response :refer (response)]))

(defn page [templater router req content]
  (response
   (render-template
    templater
    "templates/page.html.mustache"
    {:menu
     (html
      [:ul.nav.masthead-nav
       (for [[k label] [[::index "Home"]
                        [::features "Features"]
                        [::about "About"]]
             ;; router is deref'd because it's a co-dependency
             :let [path (path-for (:routes @router) k)]]
         ;; TODO: class = active
         [:li (when (= path (:uri req)) {:class "active"})
          [:a (merge {:href path}) label]]
         )])
     :content content
     })))

(defn index [templater router]
  (fn [req]
    (page templater router req
          (html
           [:div
            [:h1.cover-heading "Welcome"]
            [:p.lead "Cover is a one-page template for
                  building simple and beautiful home pages. Download,
                  edit the text, and add your own fullscreen background
                  photo to make it your own."]
            ]))))

(defn features [templater router]
  (fn [req]
    (page templater router req
          (html
           [:div
            [:h1.cover-heading "Features"]

            ]))))

(defn about [templater router]
  (fn [req]
    (page templater router req
          (html
           [:div
            [:h1.cover-heading "About"]

            ]))))

(defrecord Website [templater router]
  WebService
  (request-handlers [this]
    {::index (index templater router)
     ::features (features templater router)
     ::about (about templater router)})

  (routes [_] ["/" {"index.html" ::index
                    "" (redirect ::index)
                    "features.html" ::features
                    "about.html" ::about}])

  (uri-context [_] "")

  WebRequestHandler
  (request-handler [this] (as-request-handler this)))

(defn new-website []
  (-> (map->Website {})
      (using [:templater])
      (co-using [:router])))
<commit_msg>Refactoring of cover website<commit_after>(ns {{name}}.website
  (:require
   [bidi.bidi :refer (path-for)]
   [bidi.ring :refer (redirect)]
   [clojure.pprint :refer (pprint)]
   [clojure.tools.logging :refer :all]
   [com.stuartsierra.component :refer (using)]
   [hiccup.core :as hiccup]
   [modular.bidi :refer (WebService as-request-handler)]
   [modular.ring :refer (WebRequestHandler)]
   [modular.template :refer (render-template template-model)]
   [ring.util.response :refer (response)]
   [tangrammer.component.co-dependency :refer (co-using)]))

(defn menu [router uri]
  (hiccup/html
   [:ul.nav.masthead-nav
    (for [[k label] [[::index "Home"]
                     [::features "Features"]
                     [::about "About"]]
          ;; This demonstrates the generation of hyperlinks from
          ;; keywords.

          ;; by the way, router is deref'd because it's a
          ;; co-dependency, this is likely to change to potemkin's
          ;; def-map-type in future releases, so a deref will be
          ;; unnecessary (and deprecated)

          :let [href (path-for (:routes @router) k)]]
      [:li (when (= href uri) {:class "active"})
       [:a (merge {:href href}) label]]
      )]))

(defn page [templater router req content]
  (response
   (render-template
    templater
    "templates/page.html.mustache" ; our Mustache template
    {:menu (menu router (:uri req))
     :content content})))

(defn index [templater router]
  (fn [req]
    (page templater router req
          (hiccup/html
           [:div
            [:h1.cover-heading "Welcome"]
            [:p.lead "Cover is a one-page template for
                  building simple and beautiful home pages. Download,
                  edit the text, and add your own fullscreen background
                  photo to make it your own."]
            [:p "This is a Clojure project called {{name}}, generated
            from modular's bootstrap-cover template. This text can be
            found in " [:code "{{name}}/website.clj"]] ]))))

(defn features [templater router]
  (fn [req]
    (page templater router req
          (hiccup/html
           [:div
            [:h1.cover-heading "Features"]
            [:p.lead "bootstrap-cover exhibits the following :-"]
            [:ul.lead
             [:li "A working Clojure-powered website using Stuart Sierra's 'reloaded' workflow and component library"]
             [:li "A fully-commented route-contributing website component"]
             [:li [:a {:href "https://github.com/juxt/bidi"} "Bidi"] " routing"]
             [:li "Co-dependencies"]
             [:li "Deployable with lein run"]
             ]
            [:p "This list can be found in " [:code "{{name}}/website.clj"]]]))))

(defn about [templater router]
  (fn [req]
    (page templater router req
          (hiccup/html
           [:div
            [:h1.cover-heading "About"]
            [:p.lead "You should
            edit " [:code "{{name}}/website.clj"] ", locate
            the " [:code "about"] " function and edit the function
            defintion to display your details here, describing who you are
            and why you started this project."]]))))

;; Components are defined using defrecord.

(defrecord Website [templater router]

  ; modular.bidi provides a router which dispatches to routes provided
  ; by components that satisfy its WebService protocol
  WebService
  (request-handlers [this]
    ;; Return a map between some keywords and their associated Ring
    ;; handlers
    {::index (index templater router)
     ::features (features templater router)
     ::about (about templater router)})

  ;; Return a bidi route structure, mapping routes to keywords defined
  ;; above. This additional level of indirection means we can generate
  ;; hyperlinks from known keywords.
  (routes [_] ["/" {"index.html" ::index
                    "" (redirect ::index)
                    "features.html" ::features
                    "about.html" ::about}])

  ;; A WebService can be 'mounted' underneath a common uri context
  (uri-context [_] ""))

;; While not mandatory, it is common to use a function to construct an
;; instance of the component. This affords the opportunity to control
;; the construction with parameters, provide defaults and declare
;; dependency relationships with other components.

(defn new-website []
  (-> (map->Website {})
      (using [:templater])
      (co-using [:router])))

{'fraction_non_alphanumeric': 0.1294049682264587, 'fraction_numerical': 0.0008665511265164644, 'mean_word_length': 2.326128722382325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6538074', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2000, 'n_words': 640}