<commit_before>(ns clojournal.routes.article
  (:require [compojure.core :refer :all]
            [clojure.string :as str]
            [clojournal.layout :as layout]
            [clojournal.models.article :as article]
            [clojournal.models.author :as author]
            [noir.session :as session]))

(defn parse-content [content]
  (lazy-seq
    (let [[_ pre code post] (re-matches #"(?s)(?:(.*?)(?:```(.*?)```))?(.*)" content)]
      (cond (= post "") nil
            (nil? code) [[post nil]]
            :else (cons [pre code] (parse-content post))))))

(defn preprocess-content [content]
  (let [content (-> content
                    (str/replace "<div>" "")
                    (str/replace "</div>" "\n"))]
    (->> (for [[text code] (parse-content content)
               :let [text (-> text
                              (str/replace "\n" "")
                              (str/replace "<br>" "</p>\n<p>"))]]
           (if code
             (let [code (str/replace code "&nbsp;" " ")]
               (format "%s</p>\n<pre class=\"brush: clojure\">%s</pre>\n<p>" text code))
             text))
         str/join
         (format "<p>%s</p>")
         ((fn [s] (str/replace s "<p></p>" ""))))))

(defn article-page [id]
  (when-let [article (article/find-article id)]
    (let [author (author/find-author (:author article))
          path (str "entry/" id)]
      (layout/render
        "article.html"
        {:page-title (str (:title article) " - clojournal")
         :page-description (:content article)
         :page-path path
         :author? (boolean (session/get :author))
         :title (:title article)
         :content (preprocess-content (:content article))
         :author (:name author)
         :tags (str/join ", " (:tags article))
         :path path
         :updated-at (:updated-at article)}))))

(defroutes article-routes
  (GET "/entry/:id" [id]
       (article-page id)))<commit_msg>Make update time displayed in appropriate time zone<commit_after>(ns clojournal.routes.article
  (:require [compojure.core :refer :all]
            [clojure.string :as str]
            [clojournal.layout :as layout]
            [clojournal.models.article :as article]
            [clojournal.models.author :as author]
            [noir.session :as session]
            [clj-time.local :as local]))

(defn parse-content [content]
  (lazy-seq
    (let [[_ pre code post] (re-matches #"(?s)(?:(.*?)(?:```(.*?)```))?(.*)" content)]
      (cond (= post "") nil
            (nil? code) [[post nil]]
            :else (cons [pre code] (parse-content post))))))

(defn preprocess-content [content]
  (let [content (-> content
                    (str/replace "<div>" "")
                    (str/replace "</div>" "\n"))]
    (->> (for [[text code] (parse-content content)
               :let [text (-> text
                              (str/replace "\n" "")
                              (str/replace "<br>" "</p>\n<p>"))]]
           (if code
             (let [code (str/replace code "&nbsp;" " ")]
               (format "%s</p>\n<pre class=\"brush: clojure\">%s</pre>\n<p>" text code))
             text))
         str/join
         (format "<p>%s</p>")
         ((fn [s] (str/replace s "<p></p>" ""))))))

(defn article-page [id]
  (when-let [article (article/find-article id)]
    (let [author (author/find-author (:author article))
          path (str "entry/" id)]
      (layout/render
        "article.html"
        {:page-title (str (:title article) " - clojournal")
         :page-description (:content article)
         :page-path path
         :author? (boolean (session/get :author))
         :title (:title article)
         :content (preprocess-content (:content article))
         :author (:name author)
         :tags (str/join ", " (:tags article))
         :path path
         :updated-at (local/to-local-date-time (:updated-at article))}))))

(defroutes article-routes
  (GET "/entry/:id" [id]
       (article-page id)))
{'fraction_non_alphanumeric': 0.1874369323915237, 'fraction_numerical': 0.0, 'mean_word_length': 1.9501488095238095, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, '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': '1536761', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1217, 'n_words': 308}