<commit_before>(ns kioo.core
  (:refer-clojure :exclude [compile])
  (:require
   [kioo.util :refer [convert-attrs flatten-nodes]]
   [net.cgrand.enlive-html :refer [at html-resource select
                                   any-node]]
   [clojure.string :as string]
   [kioo.html-parser :as parser]
   [hickory.core :as hickory]
   [clojure.java.io :as io]))

(declare compile component*)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BASE REACT EMIT FUNCTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn get-react-sym [tag]
  (symbol "js" (str "React.DOM." (name tag))))


(defn emit-trans [node children]
  `((kioo.core/handle-wrapper kioo.core/make-dom)
    (~(:trans node) ~(-> node
                         (dissoc :trans)
                         (assoc :attrs (convert-attrs (:attrs node))
                                :content children
                                :sym (get-react-sym (:tag node)))))))

(defn emit-node [node children]
  `(apply ~(get-react-sym (:tag node))
          (cljs.core/clj->js ~(convert-attrs (:attrs node)))
          (kioo.util/flatten-nodes ~children)))


(defn wrap-fragment [tag child-sym]
  `(apply ~(get-react-sym tag) nil ~child-sym))


(def resource-wrapper-fns {:html parser/->StandardHtml
                           :mini-html parser/->MiniHtml})


(def react-emit-opts {:emit-trans emit-trans
                      :emit-node emit-node
                      :wrap-fragment wrap-fragment
                      :resource-wrapper :mini-html})

(defmacro component
  "React base component definition"
  ([path trans]
     (component* path trans react-emit-opts))
  ([path sel trans]
     (component* path sel trans react-emit-opts))
  ([path sel trans opts]
     (component* path sel trans (merge react-emit-opts (eval opts)))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Using Enlive to do selection and
;; attache base transforms
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn attach-transform [trans]
  (fn [node]
    (if (:trans node)
      (assoc node :trans `(fn [node#]
                           (cljs.core/-> node#
                               (~(:trans node))
                               (~trans))))
      (assoc node :trans trans))))


(defn resolve-enlive-var [sym]
  (ns-resolve 'net.cgrand.enlive-html sym))

(defn eval-selector [sel]
  (reduce
   #(conj %1
          (cond
           (list? %2) (apply (resolve-enlive-var (first %2)) (eval-selector (rest %2)))
           (or (vector? %2)
               (map? %2)
               (set? %2)) (eval-selector %2)
           (symbol? %2) (resolve-enlive-var %2)
            :else %2))
   (cond
    (vector? sel) []
    (set? sel) #{}
    (map? sel) {}
    :else []) sel))

(defn map-trans [node trans-lst]
  (reduce (fn [node [sel trans]]
            (at node (eval-selector sel) (attach-transform trans)))
          node
          trans-lst))


(defn resolve-resource-fn [resource {wrapper :resource-wrapper}]
  (cond
   (not (string? resource)) identity
   (keyword? wrapper) (resource-wrapper-fns wrapper)
   (symbol? wrapper) (resolve wrapper)
   :else parser/->MiniHtml))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main Structure of Compiler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defn parse-html
  "parses html with a pre-processing step provided by resource-fn<commit_msg>Make variable naming in `eval-selector` more self-explanatory.<commit_after>(ns kioo.core
  (:refer-clojure :exclude [compile])
  (:require
   [kioo.util :refer [convert-attrs flatten-nodes]]
   [net.cgrand.enlive-html :refer [at html-resource select
                                   any-node]]
   [clojure.string :as string]
   [kioo.html-parser :as parser]
   [hickory.core :as hickory]
   [clojure.java.io :as io]))

(declare compile component*)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BASE REACT EMIT FUNCTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn get-react-sym [tag]
  (symbol "js" (str "React.DOM." (name tag))))


(defn emit-trans [node children]
  `((kioo.core/handle-wrapper kioo.core/make-dom)
    (~(:trans node) ~(-> node
                         (dissoc :trans)
                         (assoc :attrs (convert-attrs (:attrs node))
                                :content children
                                :sym (get-react-sym (:tag node)))))))

(defn emit-node [node children]
  `(apply ~(get-react-sym (:tag node))
          (cljs.core/clj->js ~(convert-attrs (:attrs node)))
          (kioo.util/flatten-nodes ~children)))


(defn wrap-fragment [tag child-sym]
  `(apply ~(get-react-sym tag) nil ~child-sym))


(def resource-wrapper-fns {:html parser/->StandardHtml
                           :mini-html parser/->MiniHtml})


(def react-emit-opts {:emit-trans emit-trans
                      :emit-node emit-node
                      :wrap-fragment wrap-fragment
                      :resource-wrapper :mini-html})

(defmacro component
  "React base component definition"
  ([path trans]
     (component* path trans react-emit-opts))
  ([path sel trans]
     (component* path sel trans react-emit-opts))
  ([path sel trans opts]
     (component* path sel trans (merge react-emit-opts (eval opts)))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Using Enlive to do selection and
;; attache base transforms
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn attach-transform [trans]
  (fn [node]
    (if (:trans node)
      (assoc node :trans `(fn [node#]
                           (cljs.core/-> node#
                               (~(:trans node))
                               (~trans))))
      (assoc node :trans trans))))


(defn resolve-enlive-var [sym]
  (ns-resolve 'net.cgrand.enlive-html sym))

(defn eval-selector [sel]
  (reduce
    (fn [sel-acc sel-frag]
      (conj sel-acc
          (cond
           (list? sel-frag) (apply (resolve-enlive-var (first sel-frag)) (eval-selector (rest sel-frag)))
           (or (vector? sel-frag)
               (map? sel-frag)
               (set? sel-frag)) (eval-selector sel-frag)
           (symbol? sel-frag) (resolve-enlive-var sel-frag)
            :else sel-frag)))
   (cond
    (vector? sel) []
    (set? sel) #{}
    (map? sel) {}
    :else []) sel))

(defn map-trans [node trans-lst]
  (reduce (fn [node [sel trans]]
            (at node (eval-selector sel) (attach-transform trans)))
          node
          trans-lst))


(defn resolve-resource-fn [resource {wrapper :resource-wrapper}]
  (cond
   (not (string? resource)) identity
   (keyword? wrapper) (resource-wrapper-fns wrapper)
   (symbol? wrapper) (resolve wrapper)
   :else parser/->MiniHtml))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main Structure of Compiler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defn parse-html
  "parses html with a pre-processing step provided by resource-fn
{'fraction_non_alphanumeric': 0.20987654320987653, 'fraction_numerical': 0.0016166960611405056, 'mean_word_length': 2.3704804358593363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '1309009', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2122, 'n_words': 540}