<commit_before>(ns app.http.export-svg
  (:require
   [cuerdas.core :as str]
   [clojure.walk :as walk]
   [app.browser :as bwr]
   [app.config :as cfg]
   [lambdaisland.glogi :as log]
   [cljs.spec.alpha :as s]
   [promesa.core :as p]
   [app.common.exceptions :as exc :include-macros true]
   [app.common.data :as d]
   [app.common.pages :as cp]
   [app.common.spec :as us]
   ["xml-js" :as xml]
   ["child_process" :as chp]
   ["os" :as os]
   ["path" :as path]
   ["fs" :as fs])
  (:import
   goog.Uri))

(def default-svgo-plugins
  #js [#js {:convertStyleToAttrs false}])

(defn- create-tmpdir!
  [prefix]
  (p/create
   (fn [resolve reject]
     (fs/mkdtemp (path/join (os/tmpdir) prefix)
                 (fn [err dir]
                   (if err
                     (reject err)
                     (resolve dir)))))))

(defn- write-file!
  [fpath content]
  (p/create
   (fn [resolve reject]
     (fs/writeFile fpath content (fn [err]
                                   (if err
                                     (reject err)
                                     (resolve nil)))))))
(defn- read-file
  [fpath]
  (p/create
   (fn [resolve reject]
     (fs/readFile fpath (fn [err content]
                          (if err
                            (reject err)
                            (resolve content)))))))

(defn- run-cmd!
  [cmd]
  (p/create
   (fn [resolve reject]
     (log/info :fn :run-cmd :cmd cmd)
     (chp/exec cmd #js {:encoding "buffer"}
               (fn [error stdout stderr]
                 ;; (log/info :fn :run-cmd :stdout stdout)
                 (if error
                   (reject error)
                   (resolve stdout)))))))

(defn- rmdir!
  [path]
  (p/create
   (fn [resolve reject]
     (fs/rmdir path #js {:recursive true}
               (fn [err]
                 (if err
                   (reject err)
                   (resolve nil)))))))

(defn- parse-xml
  [data]
  (js->clj (xml/xml2js data)))

(defn- encode-xml
  [data]
  (xml/js2xml (clj->js data)))

(defn- render-object
  [browser {:keys [page-id object-id token scale suffix type]}]
  (letfn [(render-in-page [page {:keys [uri cookie] :as rctx}]
            (p/do!
             (bwr/emulate! page {:viewport [1920 1080]
                                 :scale 4})
             (bwr/set-cookie! page cookie)
             (bwr/navigate! page uri)
             (bwr/eval! page (js* "() => document.body.style.background = 'transparent'"))
             page))<commit_msg>:bug: Fix single text shape rendering.<commit_after>(ns app.http.export-svg
  (:require
   [cuerdas.core :as str]
   [clojure.walk :as walk]
   [app.browser :as bwr]
   [app.config :as cfg]
   [lambdaisland.glogi :as log]
   [cljs.spec.alpha :as s]
   [promesa.core :as p]
   [app.common.exceptions :as exc :include-macros true]
   [app.common.data :as d]
   [app.common.pages :as cp]
   [app.common.spec :as us]
   ["xml-js" :as xml]
   ["child_process" :as chp]
   ["os" :as os]
   ["path" :as path]
   ["fs" :as fs])
  (:import
   goog.Uri))

(def default-svgo-plugins
  #js [#js {:convertStyleToAttrs false}])

(defn- create-tmpdir!
  [prefix]
  (p/create
   (fn [resolve reject]
     (fs/mkdtemp (path/join (os/tmpdir) prefix)
                 (fn [err dir]
                   (if err
                     (reject err)
                     (resolve dir)))))))

(defn- write-file!
  [fpath content]
  (p/create
   (fn [resolve reject]
     (fs/writeFile fpath content (fn [err]
                                   (if err
                                     (reject err)
                                     (resolve nil)))))))
(defn- read-file
  [fpath]
  (p/create
   (fn [resolve reject]
     (fs/readFile fpath (fn [err content]
                          (if err
                            (reject err)
                            (resolve content)))))))

(defn- run-cmd!
  [cmd]
  (p/create
   (fn [resolve reject]
     (log/info :fn :run-cmd :cmd cmd)
     (chp/exec cmd #js {:encoding "buffer"}
               (fn [error stdout stderr]
                 ;; (log/info :fn :run-cmd :stdout stdout)
                 (if error
                   (reject error)
                   (resolve stdout)))))))

(defn- rmdir!
  [path]
  (p/create
   (fn [resolve reject]
     (fs/rmdir path #js {:recursive true}
               (fn [err]
                 (if err
                   (reject err)
                   (resolve nil)))))))

(defn- parse-xml
  [data]
  (js->clj (xml/xml2js data)))

(defn- encode-xml
  [data]
  (xml/js2xml (clj->js data)))

(defn- render-object
  [browser {:keys [page-id object-id token scale suffix type]}]
  (letfn [(render-in-page [page {:keys [uri cookie] :as rctx}]
            (p/do!
             (bwr/emulate! page {:viewport [1920 1080]
                                 :scale 4})
             (bwr/set-cookie! page cookie)
             (bwr/navigate! page uri)
             ;; (bwr/eval! page (js* "() => document.body.style.background = 'transparent'"))
             page))
{'fraction_non_alphanumeric': 0.14597517020424508, 'fraction_numerical': 0.004405286343612335, 'mean_word_length': 1.5562947799385876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4807375', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1657, 'n_words': 444}