<commit_before>(ns clojuredocs.pages.vars
  (:require [clojuredocs.util :as util]
            [clojure.string :as str]
            [somnium.congomongo :as mon]
            [clojuredocs.search :as search]
            [clojuredocs.pages.common :as common]
            [clojuredocs.data :as data]
            [hiccup.core :as hc]))

(defn ellipsis [s n]
  (cond
    (<= (count s) 3) s
    (> n (count s))  s
    :else (str (->> s
                    (take n)
                    (apply str))
               "...")))

(defn library-for [{:keys [ns]}]
  search/clojure-lib)

(defn $arglist [name a]
  [:li.arglist (str
                 "(" name (when-not (empty? a) " ") a ")")])

(defn see-alsos-for [v]
  (->> v
       data/find-see-alsos-for
       (map (fn [{:keys [ns name] :as sa}]
              (assoc sa :doc (-> (str ns "/" name) search/lookup :doc))))))

(defn source-url [{:keys [file line ns]}]
  (when (= "clojure.core" ns)
    (str "https://github.com/clojure/clojure/blob/clojure-1.6.0/src/clj/" file "#L" line)))

(defn lookup-var [ns name]
  (search/lookup (str ns "/" name)))

(defn $note [{:keys [body user created-at]}]
  [:div.note
   [:div.note-meta
    "By "
    (common/$avatar user)
    " "
    (:login user)
    ", "
    (util/timeago created-at)
    " ago."]
   [:div.note-body
    (-> (util/markdown body)
        (str/replace #"<pre><code>" "<pre class=\"brush: clojure\">")
        (str/replace #"</code></pre>" "</pre>"))]])

(defn $notes [notes name]
  [:div.var-notes
   [:h5 (util/pluralize (count notes) "Note" "Notes")]
   [:div
    (if (empty? notes)
      [:div.null-state "No notes for " [:code name]]
      [:ul
       (for [n notes]
         ($note n))])]
   [:div.add-note-widget]])


(defn clean-id [{:keys [_id] :as m}]
  (assoc m :_id (str _id)))

(defn clean-example [{:keys [_id user history] :as m}]
  (-> m
      (update-in [:user] dissoc :email)
      (update-in [:_id] str)))

(defn clean-see-also [m]
  (-> m
      (update-in [:user] dissoc :email)
      (update-in [:_id] str)))

(defn $number-badge [num]
  [:span.badge num])

(defn var-page-handler [ns name]
  (let [name (util/cd-decode name)
        {:keys [arglists name ns doc runtimes added file] :as v} (lookup-var ns name)]
    (fn [{:keys [user session uri]}]
      (when v
        (let [examples (data/find-examples-for v)
              see-alsos (see-alsos-for v)
              library (library-for v)
              recent (:recent session)
              notes (data/find-notes-for v)]
          {:session (update-in session [:recent]
                      #(->> %
                            (concat [{:text name
                                      :href (str "/" ns "/" (util/cd-encode name))}])
                            distinct
                            (filter :text)
                            (take 4)))
           :body
           (common/$main
             {:body-class "var-page"
              :page-data {:examples (map clean-example examples)
                          :var v
                          :notes (map clean-id notes)
                          :see-alsos (map clean-see-also see-alsos)
                          :user (when user (select-keys user [:login :avatar-url :account-source]))}
              :page-uri uri
              :user user
              :mobile-nav [{:title "Nav"
                            :links [[:a {:href "#"
                                         :data-animate-scroll "true"
                                         :data-animate-buffer "70"}
                                     "Top"]
                                    [:a {:href "#examples"
                                         :data-animate-scroll "true"
                                         :data-animate-buffer "70"}
                                     "Examples " ($number-badge (count examples))]
                                    [:a {:href "#see-also"
                                         :data-animate-scroll "true"
                                         :data-animate-buffer "70"}
                                     "See Also " ($number-badge (count see-alsos))]
                                    (when (> (count notes) 0)
                                      [:a {:href "#notes"
                                           :data-animate-scroll "true"
                                           :data-animate-buffer "70"}
                                       "Notes " ($number-badge (count notes))])]}]
              :content [:div
                        [:div.row<commit_msg>Force vector for examples page data<commit_after>(ns clojuredocs.pages.vars
  (:require [clojuredocs.util :as util]
            [clojure.string :as str]
            [somnium.congomongo :as mon]
            [clojuredocs.search :as search]
            [clojuredocs.pages.common :as common]
            [clojuredocs.data :as data]
            [hiccup.core :as hc]))

(defn ellipsis [s n]
  (cond
    (<= (count s) 3) s
    (> n (count s))  s
    :else (str (->> s
                    (take n)
                    (apply str))
               "...")))

(defn library-for [{:keys [ns]}]
  search/clojure-lib)

(defn $arglist [name a]
  [:li.arglist (str
                 "(" name (when-not (empty? a) " ") a ")")])

(defn see-alsos-for [v]
  (->> v
       data/find-see-alsos-for
       (map (fn [{:keys [ns name] :as sa}]
              (assoc sa :doc (-> (str ns "/" name) search/lookup :doc))))))

(defn source-url [{:keys [file line ns]}]
  (when (= "clojure.core" ns)
    (str "https://github.com/clojure/clojure/blob/clojure-1.6.0/src/clj/" file "#L" line)))

(defn lookup-var [ns name]
  (search/lookup (str ns "/" name)))

(defn $note [{:keys [body user created-at]}]
  [:div.note
   [:div.note-meta
    "By "
    (common/$avatar user)
    " "
    (:login user)
    ", "
    (util/timeago created-at)
    " ago."]
   [:div.note-body
    (-> (util/markdown body)
        (str/replace #"<pre><code>" "<pre class=\"brush: clojure\">")
        (str/replace #"</code></pre>" "</pre>"))]])

(defn $notes [notes name]
  [:div.var-notes
   [:h5 (util/pluralize (count notes) "Note" "Notes")]
   [:div
    (if (empty? notes)
      [:div.null-state "No notes for " [:code name]]
      [:ul
       (for [n notes]
         ($note n))])]
   [:div.add-note-widget]])


(defn clean-id [{:keys [_id] :as m}]
  (assoc m :_id (str _id)))

(defn clean-example [{:keys [_id user history] :as m}]
  (-> m
      (update-in [:user] dissoc :email)
      (update-in [:_id] str)))

(defn clean-see-also [m]
  (-> m
      (update-in [:user] dissoc :email)
      (update-in [:_id] str)))

(defn $number-badge [num]
  [:span.badge num])

(defn var-page-handler [ns name]
  (let [name (util/cd-decode name)
        {:keys [arglists name ns doc runtimes added file] :as v} (lookup-var ns name)]
    (fn [{:keys [user session uri]}]
      (when v
        (let [examples (data/find-examples-for v)
              see-alsos (see-alsos-for v)
              library (library-for v)
              recent (:recent session)
              notes (data/find-notes-for v)]
          {:session (update-in session [:recent]
                      #(->> %
                            (concat [{:text name
                                      :href (str "/" ns "/" (util/cd-encode name))}])
                            distinct
                            (filter :text)
                            (take 4)))
           :body
           (common/$main
             {:body-class "var-page"
              :page-data {:examples (mapv clean-example examples)
                          :var v
                          :notes (map clean-id notes)
                          :see-alsos (map clean-see-also see-alsos)
                          :user (when user (select-keys user [:login :avatar-url :account-source]))}
              :page-uri uri
              :user user
              :mobile-nav [{:title "Nav"
                            :links [[:a {:href "#"
                                         :data-animate-scroll "true"
                                         :data-animate-buffer "70"}
                                     "Top"]
                                    [:a {:href "#examples"
                                         :data-animate-scroll "true"
                                         :data-animate-buffer "70"}
                                     "Examples " ($number-badge (count examples))]
                                    [:a {:href "#see-also"
                                         :data-animate-scroll "true"
                                         :data-animate-buffer "70"}
                                     "See Also " ($number-badge (count see-alsos))]
                                    (when (> (count notes) 0)
                                      [:a {:href "#notes"
                                           :data-animate-scroll "true"
                                           :data-animate-buffer "70"}
                                       "Notes " ($number-badge (count notes))])]}]
              :content [:div
                        [:div.row
{'fraction_non_alphanumeric': 0.15296577528337185, 'fraction_numerical': 0.003301419610432486, 'mean_word_length': 1.2439506172839505, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 39, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 54, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4493586', 'n_tokens_mistral': 2835, 'n_tokens_neox': 2696, 'n_words': 692}