<commit_before>(ns leiningen.midje-doc.renderer
  (:require [hiccup.core :as html]
            [markdown.core :refer [md-to-html-string]]
            [me.raynes.conch :refer [programs]]))

(programs pygmentize)

(def ^:dynamic *plain* true)

(defn adjust-facts-code [s spaces]
  (let [i-arrow (.lastIndexOf s "=>")]
    (if (<= 0 i-arrow)
        (let [i-nl  (.lastIndexOf s "\n" i-arrow)
              rhs (-> (.substring s i-nl)
                      (.replaceAll (str "\n" spaces) "\n"))
              lhs (.substring s 0 i-nl)]
          (str lhs rhs))
        s)))

(defn adjust-indented-code [s spaces]
  (let [fst-idx (.indexOf s "\n")]
    (if (< fst-idx 0) s
      (let [rst-ori (.substring s fst-idx)
          rst-new (.replaceAll rst-ori (str "\n" spaces) "\n")
          fst-str (.substring s 0 fst-idx)]
        (str fst-str rst-new)))))

(defn render-element [elem]
  (condp = (:type elem)

    :chapter
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     [:h2 [:b (str (:num elem) " &nbsp;&nbsp; " (:title elem))]]]
    :section
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     [:h3 (str (:num elem) " &nbsp;&nbsp; " (:title elem))]]
    :subsection
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     [:h3 [:i (str (:num elem) " &nbsp;&nbsp; " (:title elem))]]]
    :subsubsection
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     [:h3 [:i (str (:num elem) " &nbsp;&nbsp; " (:title elem))]]]
    :paragraph [:div (md-to-html-string (:content elem))]
    :image
    [:div {:class "figure"}
     (if (:tag elem) [:a {:name (:tag elem)}])
     (if (:num elem)
       [:h4 [:i (str "fig." (:num elem)
                     (if-let [t (:title elem)] (str "  &nbsp;-&nbsp; " t)))]])
     [:div {:class "img"} [:img (dissoc elem :num :type :tag)]]
     [:p]]
    :ns
    [:div
     (if *plain*
       [:pre (:content elem)]
       (pygmentize  "-f" "html" "-l" (or (:lang elem) "clojure")
                    {:in (:content elem)}))]
    :code
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     (if (:num elem)
       [:h4 [:i (str "e." (:num elem)
                     (if-let [t (:title elem)] (str "  &nbsp;-&nbsp; " t)))]])
     (if *plain*
       [:pre (adjust-indented-code (:content elem)
                                   (apply str (repeat (or (:fact-level elem) 0) "  ")))]
       (pygmentize  "-f" "html" "-l" (or (:lang elem) "clojure")
                   {:in (adjust-indented-code (:content elem)
                                              (apply str (repeat (or (:fact-level elem) 0) "  ")))}))]))


(defn render-toc-element [elem]
  (case (:type elem)
    :chapter [:h4
              [:a {:href (str "#" (:tag elem))} (str (:num elem) " &nbsp; " (:title elem))]]
<commit_msg>added basic HTML escaping to 'plain' mode.<commit_after>(ns leiningen.midje-doc.renderer
  (:require [hiccup.core :as html]
            [markdown.core :refer [md-to-html-string]]
            [me.raynes.conch :refer [programs]]))

(programs pygmentize)

(def ^:dynamic *plain* true)

(defn basic-html-escape
  [data]
  (clojure.string/escape data { \< "&lt;" \> "&gt;" \& "&amp;" \" "&quot;" }))

(defn adjust-facts-code [s spaces]
  (let [i-arrow (.lastIndexOf s "=>")]
    (if (<= 0 i-arrow)
        (let [i-nl  (.lastIndexOf s "\n" i-arrow)
              rhs (-> (.substring s i-nl)
                      (.replaceAll (str "\n" spaces) "\n"))
              lhs (.substring s 0 i-nl)]
          (str lhs rhs))
        s)))

(defn adjust-indented-code [s spaces]
  (let [fst-idx (.indexOf s "\n")]
    (if (< fst-idx 0) s
      (let [rst-ori (.substring s fst-idx)
          rst-new (.replaceAll rst-ori (str "\n" spaces) "\n")
          fst-str (.substring s 0 fst-idx)]
        (str fst-str rst-new)))))

(defn render-element [elem]
  (condp = (:type elem)

    :chapter
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     [:h2 [:b (str (:num elem) " &nbsp;&nbsp; " (:title elem))]]]
    :section
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     [:h3 (str (:num elem) " &nbsp;&nbsp; " (:title elem))]]
    :subsection
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     [:h3 [:i (str (:num elem) " &nbsp;&nbsp; " (:title elem))]]]
    :subsubsection
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     [:h3 [:i (str (:num elem) " &nbsp;&nbsp; " (:title elem))]]]
    :paragraph [:div (md-to-html-string (:content elem))]
    :image
    [:div {:class "figure"}
     (if (:tag elem) [:a {:name (:tag elem)}])
     (if (:num elem)
       [:h4 [:i (str "fig." (:num elem)
                     (if-let [t (:title elem)] (str "  &nbsp;-&nbsp; " t)))]])
     [:div {:class "img"} [:img (dissoc elem :num :type :tag)]]
     [:p]]
    :ns
    [:div
     (if *plain*
       [:pre (-> elem :content basic-html-escape)]
       (pygmentize  "-f" "html" "-l" (or (:lang elem) "clojure")
                    {:in (:content elem)}))]
    :code
    [:div
     (if (:tag elem) [:a {:name (:tag elem)}])
     (if (:num elem)
       [:h4 [:i (str "e." (:num elem)
                     (if-let [t (:title elem)] (str "  &nbsp;-&nbsp; " t)))]])
     (if *plain*
       [:pre (-> (:content elem)
                 (basic-html-escape)
                 (adjust-indented-code (apply str (repeat (or (:fact-level elem) 0) "  "))))]
       (pygmentize  "-f" "html" "-l" (or (:lang elem) "clojure")
                   {:in (adjust-indented-code (:content elem)
                                              (apply str (repeat (or (:fact-level elem) 0) "  ")))}))]))


(defn render-toc-element [elem]
  (case (:type elem)
    :chapter [:h4
              [:a {:href (str "#" (:tag elem))} (str (:num elem) " &nbsp; " (:title elem))]]

{'fraction_non_alphanumeric': 0.2192044857192921, 'fraction_numerical': 0.004555808656036446, 'mean_word_length': 2.0410229088971765, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3730240', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2054, 'n_words': 607}