<commit_before>(ns com.lemondronor.leaflet-gorilla
  ""
  (:require [clojure.data.json :as json]
            [clojure.java.io :as io]
            [gorilla-renderable.core :as render]
            [selmer.parser :as selmer]))

(set! *warn-on-reflection* true)


(defn- uuid [] (str (java.util.UUID/randomUUID)))


(defn transpose-coord [[lat lon]]
  [lon lat])


(defn multipoint-feature [coords]
  {
   "type" "Feature"
   "geometry" {
               "type" "MultiPoint"
               "coordinates" (map transpose-coord coords)
               }
   })


(defn linestring-feature [coords]
  {
   "type" "Feature"
   "geometry" {
               "type" "LineString"
               "coordinates" (map transpose-coord coords)
               }
   })


(defn polygon-feature [coords-arrays]
  {
   "type" "Feature"
   "geometry" {
               "type" "Polygon"
               "coordinates" (map #(map transpose-coord %) coords-arrays)
               }
   })


(defn geojson-feature [geodesc]
  (let [type-desig (first geodesc)
        coords (second geodesc)]
    (case type-desig
      :points (multipoint-feature coords)
      :line (linestring-feature coords)
      :polygon (polygon-feature coords))))


(defn geojson [& geometries]
  (json/write-str
   {"features"
    (map geojson-feature geometries)
    }))


(defrecord LeafletView [points opts])

(defn leaflet [points & opts]
  (LeafletView. points (apply hash-map opts)))


(def default-options
  {:width 400
   :height 400
   :leaflet-js-url "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"
   :leaflet-css-url "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css"
   :tile-layer-url "http://{s}.tile.osm.org/{z}/{x}/{y}.png"
   :color "steelblue"
   :opacity 1.0})


(defonce js-tag-id (uuid))
(defonce css-tag-id (uuid))


(def content-template
  "<div>
<div id='{{map-id}}' style='height: {{height}}px; width: {{width}}px;'></div>
<script type='text/javascript'>
$(function () {
  if (!document.getElementById('{{css-tag-id}}')) {
    $('<link>')
      .attr('rel', 'stylesheet')
      .attr('href', '{{leaflet-css-url}}')
      .attr('id', '{{css-tag-id}}')
      .appendTo('head');
  }
  if (!document.getElementById('{{js-tag-id}}')) {
    $('<script>')
      .attr('src', '{{leaflet-js-url}}')
      .attr('id', '{{js-tag-id}}')
      .appendTo('head');
  }
  setTimeout(function() {
    var map = L.map('{{map-id}}')
    L.tileLayer('{{tile-layer-url}}')
        .addTo(map);
    var geoJson = L.geoJson(
      {{geojson}},
      {style: {'color': '{{color}}',
               'opacity': {{opacity}}}});
    geoJson.addTo(map);
    map.fitBounds(geoJson.getBounds());
  },
  3000);
});
</script>
</div>")


(extend-type LeafletView
  render/Renderable
  (render [self]
    (let [values (merge default-options
                        (:opts self)
                        {:js-tag-id js-tag-id
                         :css-tag-id css-tag-id
                         :map-id (uuid)
                         :geojson [:safe (geojson (:points self))]})
          html (selmer/render content-template values)]<commit_msg>Tried to improve script insertion & running.<commit_after>(ns com.lemondronor.leaflet-gorilla
  ""
  (:require [clojure.data.json :as json]
            [clojure.java.io :as io]
            [gorilla-renderable.core :as render]
            [selmer.parser :as selmer]))

(set! *warn-on-reflection* true)


(defn- uuid [] (str (java.util.UUID/randomUUID)))


(defn transpose-coord [[lat lon]]
  [lon lat])


(defn multipoint-feature [coords]
  {
   "type" "Feature"
   "geometry" {
               "type" "MultiPoint"
               "coordinates" (map transpose-coord coords)
               }
   })


(defn linestring-feature [coords]
  {
   "type" "Feature"
   "geometry" {
               "type" "LineString"
               "coordinates" (map transpose-coord coords)
               }
   })


(defn polygon-feature [coords-arrays]
  {
   "type" "Feature"
   "geometry" {
               "type" "Polygon"
               "coordinates" (map #(map transpose-coord %) coords-arrays)
               }
   })


(defn geojson-feature [geodesc]
  (let [type-desig (first geodesc)
        coords (second geodesc)]
    (case type-desig
      :points (multipoint-feature coords)
      :line (linestring-feature coords)
      :polygon (polygon-feature coords))))


(defn geojson [& geometries]
  (json/write-str
   {"features"
    (map geojson-feature geometries)
    }))


(defrecord LeafletView [points opts])

(defn leaflet [points & opts]
  (LeafletView. points (apply hash-map opts)))


(def default-options
  {:width 400
   :height 400
   :leaflet-js-url "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"
   :leaflet-css-url "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css"
   :tile-layer-url "http://{s}.tile.osm.org/{z}/{x}/{y}.png"
   :color "steelblue"
   :opacity 1.0})


(defonce js-tag-id (uuid))
(defonce css-tag-id (uuid))


(def content-template
  "<div>
<div id='{{map-id}}' style='height: {{height}}px; width: {{width}}px;'></div>
<script type='text/javascript'>
$(function () {
  var createMap = function() {
    var map = L.map('{{map-id}}')
    L.tileLayer('{{tile-layer-url}}')
        .addTo(map);
    var geoJson = L.geoJson(
      {{geojson}},
      {style: {'color': '{{color}}',
               'opacity': {{opacity}}}});
    geoJson.addTo(map);
    map.fitBounds(geoJson.getBounds());
  };
  if (!document.getElementById('{{css-tag-id}}')) {
    $('<link>')
      .attr('rel', 'stylesheet')
      .attr('href', '{{leaflet-css-url}}')
      .attr('id', '{{css-tag-id}}')
      .appendTo('head');
  }
  if (!document.getElementById('{{js-tag-id}}')) {
    var jsTag = $('<script>');
    jsTag.appendTo('head');
    jsTag.attr('onload',
               function() {setTimeout(createMap, 100)});
    jsTag.attr('id', '{{js-tag-id}}');
    jsTag.attr('src', '{{leaflet-js-url}}')
    console.log('woo');
    } else {
    createMap();
  }
});
</script>
</div>")


(extend-type LeafletView
  render/Renderable
  (render [self]
    (let [values (merge default-options
                        (:opts self)
                        {:js-tag-id js-tag-id
                         :css-tag-id css-tag-id
                         :map-id (uuid)
                         :geojson [:safe (geojson (:points self))]})
          html (selmer/render content-template values)]
{'fraction_non_alphanumeric': 0.18557025834908633, 'fraction_numerical': 0.005513547574039068, 'mean_word_length': 2.3433385992627698, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '3298808', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2215, 'n_words': 424}