From 3ceb57d169c1c5f97a4b76c7e591bf96de8f8118 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Tue, 16 Jul 2024 20:03:40 -0700 Subject: [PATCH 1/8] initial idea for grid and example support --- notebooks/index.clj | 7 + src/scicloj/clay/v2/notebook.clj | 42 +++++- src/scicloj/clay/v2/prepare.clj | 19 +++ test/index_generated_test.clj | 220 ++++++++++++++++--------------- 4 files changed, 180 insertions(+), 108 deletions(-) diff --git a/notebooks/index.clj b/notebooks/index.clj index e55480bc..e88c269a 100644 --- a/notebooks/index.clj +++ b/notebooks/index.clj @@ -632,6 +632,13 @@ nested-structure-1 tc/dataset (kind/dataset {:dataset/print-range 6})) +;; ### Example and grid + +^:kind/example (inc 1) + +^:kind/grid +[1 2 3 4 5 6] + ;; ### Tables ;; The `:kind/table` kind can be handy for an interactive table view. `:kind/table` understands many structures which can be rendered as a table. diff --git a/src/scicloj/clay/v2/notebook.clj b/src/scicloj/clay/v2/notebook.clj index fe203b2c..130908c2 100644 --- a/src/scicloj/clay/v2/notebook.clj +++ b/src/scicloj/clay/v2/notebook.clj @@ -58,11 +58,51 @@ (string/join "\n")) item/md)) +(defn code-by-value [{:as note :keys [code]}] + (let [source-item (item/source-clojure code) + source-hiccup (:hiccup source-item) + source-md (:md source-item) + value-items (-> note + (select-keys [:value :code :form + :base-target-path + :full-target-path + :kindly/options + :format]) + (update :value deref-if-needed) + prepare/prepare-or-pprint) + ;; TODO: should join them if there are more than one. + value-item (first value-items) + value-hiccup (:hiccup value-item) + value-md (:md value-item)] + [{:hiccup (when (and value-hiccup source-hiccup) + [:div.grid + [:div.g-col-6 source-hiccup] + [:div.g-col-6 value-hiccup]]) + :md (when (and value-md source-md) + (str (str/join \newline + ["::: {.grid}" + "::: {.g-col-6}" + source-md + ":::" + "::: {.g-col-6}" + value-md + ":::" + ":::"]) + \newline)) + :deps (:deps value-item)}])) + (defn note-to-items [{:as note :keys [comment? code form value kind]} {:keys [hide-code hide-nils hide-vars]}] - (if (and comment? code) + (cond + (and comment? code) [(comment->item code)] + + ;; TODO: should handle multiple examples at a time. + (= kind :kind/example) + (code-by-value note) + + :else (concat ;; code [(when-not (or hide-code diff --git a/src/scicloj/clay/v2/prepare.clj b/src/scicloj/clay/v2/prepare.clj index 2bb81248..243014ee 100644 --- a/src/scicloj/clay/v2/prepare.clj +++ b/src/scicloj/clay/v2/prepare.clj @@ -482,3 +482,22 @@ (add-preparer-from-value-fn! :kind/highcharts #'item/highcharts) + +(defn grid [{:as context :keys [value]}] + (let [prepared-parts (->> value + (mapcat (fn [subvalue] + (-> context + (dissoc :form) + (update :kindly/options :dissoc :element/max-height) + (assoc :value subvalue) + prepare-or-pprint))))] + {:hiccup (->> (map (fn [part] + ;; TODO: width should be an option + [:div.g-col-6 (item->hiccup part context)]) + prepared-parts) + (into [:div.grid])) + :md (map item->md prepared-parts) + :deps (->> (mapcat :deps prepared-parts) + (distinct))})) + +(add-preparer! :kind/grid #'grid) diff --git a/test/index_generated_test.clj b/test/index_generated_test.clj index 80f57d44..d461ddb7 100644 --- a/test/index_generated_test.clj +++ b/test/index_generated_test.clj @@ -600,90 +600,96 @@ (kind/dataset #:dataset{:print-range 6}))) -(def var108 nil) +(def var108 (inc 1)) + + +(def var109 [1 2 3 4 5 6]) + + +(def var110 nil) (def - var109 + var111 (kind/table {:column-names [:preferred-language :age], :row-vectors people-as-vectors})) -(def var110 nil) +(def var112 nil) -(def var111 (kind/table {:row-vectors (take 5 people-as-vectors)})) +(def var113 (kind/table {:row-vectors (take 5 people-as-vectors)})) -(def var112 nil) +(def var114 nil) -(def var113 (kind/table {:row-maps (take 5 people-as-maps)})) +(def var115 (kind/table {:row-maps (take 5 people-as-maps)})) -(def var114 nil) +(def var116 nil) (def - var115 + var117 (kind/table {:column-names [:preferred-language], :row-maps (take 5 people-as-maps)})) -(def var116 nil) +(def var118 nil) -(def var117 (kind/table (take 5 people-as-vectors))) +(def var119 (kind/table (take 5 people-as-vectors))) -(def var118 (kind/table (take 5 people-as-maps))) +(def var120 (kind/table (take 5 people-as-maps))) -(def var119 nil) +(def var121 nil) -(def var120 (kind/table {:x (range 6), :y [:A :B :C :A :B :C]})) +(def var122 (kind/table {:x (range 6), :y [:A :B :C :A :B :C]})) -(def var121 nil) +(def var123 nil) -(def var122 (def people-as-dataset (tc/dataset people-as-maps))) +(def var124 (def people-as-dataset (tc/dataset people-as-maps))) -(def var123 (-> people-as-dataset kind/table)) +(def var125 (-> people-as-dataset kind/table)) -(def var124 nil) +(def var126 nil) (def - var125 + var127 (-> people-as-dataset (kind/table #:element{:max-height "300px"}))) -(def var126 nil) +(def var128 nil) (def - var127 + var129 (-> people-as-maps tc/dataset (kind/table {:use-datatables true}))) (def - var128 + var130 (-> people-as-dataset (kind/table {:use-datatables true, :datatables {:scrollY 300}}))) -(def var129 nil) +(def var131 nil) (def - var130 + var132 (-> (toydata/iris-ds) (noj.stats/linear-regression-model @@ -692,11 +698,11 @@ ml/thaw-model)) -(def var131 nil) +(def var133 nil) (def - var132 + var134 (defn vega-lite-point-plot [data] @@ -712,7 +718,7 @@ (def - var133 + var135 (defn random-data [n] @@ -724,21 +730,21 @@ (def - var134 + var136 (defn random-vega-lite-plot [n] (-> n random-data vega-lite-point-plot))) -(def var135 (random-vega-lite-plot 9)) +(def var137 (random-vega-lite-plot 9)) -(def var136 nil) +(def var138 nil) (def - var137 + var139 (-> {:data {:values "x,y\n1,1\n2,4\n3,9\n-1,1\n-2,4\n-3,9", @@ -750,11 +756,11 @@ kind/vega-lite)) -(def var138 nil) +(def var140 nil) (def - var139 + var141 (def cytoscape-example {:elements @@ -781,21 +787,21 @@ :layout {:name "preset", :padding 5}})) -(def var140 (kind/cytoscape cytoscape-example)) +(def var142 (kind/cytoscape cytoscape-example)) (def - var141 + var143 (-> cytoscape-example (kind/cytoscape #:element{:style {:width "100px", :height "100px"}}))) -(def var142 nil) +(def var144 nil) (def - var143 + var145 (def echarts-example {:title {:text "Echarts Example"}, @@ -807,21 +813,21 @@ :series [{:name "sales", :type "bar", :data [5 20 36 10 10 20]}]})) -(def var144 (kind/echarts echarts-example)) +(def var146 (kind/echarts echarts-example)) (def - var145 + var147 (-> echarts-example (kind/echarts #:element{:style {:width "500px", :height "200px"}}))) -(def var146 nil) +(def var148 nil) (def - var147 + var149 (def plotly-example {:data @@ -836,21 +842,21 @@ :layout {:title "Plotly example"}})) -(def var148 (kind/plotly plotly-example)) +(def var150 (kind/plotly plotly-example)) (def - var149 + var151 (-> plotly-example (kind/plotly #:element{:style {:width "300px", :height "300px"}}))) -(def var150 nil) +(def var152 nil) (def - var151 + var153 (kind/highcharts {:title {:text "Line chart"}, :subtitle {:text "By Job Category"}, @@ -925,56 +931,56 @@ :verticalAlign "bottom"}}}]}})) -(def var152 nil) +(def var154 nil) (def - var153 + var155 (kind/observable "\n//| panel: input\nviewof bill_length_min = Inputs.range(\n [32, 50],\n {value: 35, step: 1, label: 'Bill length (min):'}\n )\nviewof islands = Inputs.checkbox(\n ['Torgersen', 'Biscoe', 'Dream'],\n { value: ['Torgersen', 'Biscoe'],\n label: 'Islands:'\n }\n )\n\nPlot.rectY(filtered,\n Plot.binX(\n {y: 'count'},\n {x: 'body_mass_g', fill: 'species', thresholds: 20}\n ))\n .plot({\n facet: {\n data: filtered,\n x: 'sex',\n y: 'species',\n marginRight: 80\n },\n marks: [\n Plot.frame(),\n ]\n }\n )\nInputs.table(filtered)\npenguins = FileAttachment('notebooks/datasets/palmer-penguins.csv').csv({ typed: true })\nfiltered = penguins.filter(function(penguin) {\n return bill_length_min < penguin.bill_length_mm &&\n islands.includes(penguin.island);\n })\n")) -(def var154 nil) +(def var156 nil) (def - var155 + var157 (kind/observable "athletes = FileAttachment('notebooks/datasets/athletes.csv').csv({typed: true})")) -(def var156 (kind/observable "athletes")) +(def var158 (kind/observable "athletes")) -(def var157 (kind/observable "Inputs.table(athletes)")) +(def var159 (kind/observable "Inputs.table(athletes)")) (def - var158 + var160 (kind/observable "\nPlot.plot({\n grid: true,\n facet: {\n data: athletes,\n y: 'sex'\n },\n marks: [\n Plot.rectY(\n athletes,\n Plot.binX({y: 'count'}, {x: 'weight', fill: 'sex'})\n ),\n Plot.ruleY([0])\n ]\n})\n")) (def - var159 + var161 (kind/observable "population = FileAttachment('notebooks/datasets/population.json').json()")) -(def var160 (kind/observable "population")) +(def var162 (kind/observable "population")) (def - var161 + var163 (kind/observable " import { chart } with { population as data } from '@d3/zoomable-sunburst'\n chart")) -(def var162 nil) +(def var164 nil) (def - var163 + var165 (kind/reagent ['(fn [] @@ -999,11 +1005,11 @@ #:html{:deps [:leaflet]})) -(def var164 nil) +(def var166 nil) (def - var165 + var167 (let [letter-frequencies [{:letter "A", :frequency 0.08167} @@ -1070,11 +1076,11 @@ #:html{:deps [:d3]}))) -(def var166 nil) +(def var168 nil) (def - var167 + var169 (defn ->ggplotly-spec [{:keys [layers labels]}] @@ -1270,14 +1276,14 @@ :jsHooks []})))) -(def var168 (require '[tech.v3.datatype.functional :as fun])) +(def var170 (require '[tech.v3.datatype.functional :as fun])) -(def var169 nil) +(def var171 nil) (def - var170 + var172 (let [n 100 @@ -1301,11 +1307,11 @@ :labels {:x "wt", :y "mpg"}}))) -(def var171 nil) +(def var173 nil) (def - var172 + var174 (kind/reagent ['(fn [{:keys [data-pdb]}] @@ -1320,16 +1326,16 @@ #:html{:deps [:three-d-mol]})) -(def var173 nil) +(def var175 nil) (def - var174 + var176 (defonce pdb-2POR (slurp "https://files.rcsb.org/download/2POR.pdb"))) (def - var175 + var177 (kind/reagent ['(fn [{:keys [pdb-data]}] @@ -1359,40 +1365,40 @@ #:html{:deps [:three-d-mol]})) -(def var176 nil) +(def var178 nil) -(def var177 (kind/video {:youtube-id "DAQnvAgBma8"})) +(def var179 (kind/video {:youtube-id "DAQnvAgBma8"})) (def - var178 + var180 (kind/video {:youtube-id "DAQnvAgBma8", :allowfullscreen false})) (def - var179 + var181 (kind/video {:youtube-id "DAQnvAgBma8", :iframe-width 480, :iframe-height 270})) (def - var180 + var182 (kind/video {:youtube-id "DAQnvAgBma8", :embed-options {:mute 1, :controls 0}})) -(def var181 nil) +(def var183 nil) -(def var182 (kind/portal {:x (range 3)})) +(def var184 (kind/portal {:x (range 3)})) -(def var183 nil) +(def var185 nil) (def - var184 + var186 (kind/portal [(kind/hiccup [:img @@ -1408,16 +1414,16 @@ (def - var185 + var187 (kind/portal [(kind/hiccup [:big [:big "a plot"]]) (random-vega-lite-plot 9)])) -(def var186 nil) +(def var188 nil) (def - var187 + var189 (kind/hiccup [:div {:style {:background "#f5f3ff", :border "solid"}} @@ -1458,11 +1464,11 @@ (vec (range 40))])])) -(def var188 nil) +(def var190 nil) (def - var189 + var191 (kind/table {:column-names [(kind/hiccup @@ -1479,7 +1485,7 @@ (def - var190 + var192 (kind/table {:column-names ["size" "square"], :row-vectors @@ -1495,26 +1501,26 @@ {:use-datatables true})) -(def var191 nil) +(def var193 nil) (def - var192 + var194 {:plot (random-vega-lite-plot 9), :dataset (tc/dataset {:x (range 3), :y (repeatedly 3 rand)})}) (def - var193 + var195 [(random-vega-lite-plot 9) (tc/dataset {:x (range 3), :y (repeatedly 3 rand)})]) -(def var194 nil) +(def var196 nil) (def - var195 + var197 (->> ["purple" "darkgreen" "brown"] (mapcat @@ -1528,44 +1534,44 @@ kind/fragment)) -(def var196 (->> (range 3) kind/fragment)) +(def var198 (->> (range 3) kind/fragment)) -(def var197 nil) +(def var199 nil) -(def var198 (kind/fn [+ 1 2])) +(def var200 (kind/fn [+ 1 2])) (def - var199 + var201 (kind/fn {:kindly/f (fn [{:keys [x y]}] (+ x y)), :x 1, :y 2})) (def - var200 + var202 (kind/fn [tc/dataset {:x (range 3), :y (repeatedly 3 rand)}])) (def - var201 + var203 (kind/fn {:kindly/f tc/dataset, :x (range 3), :y (repeatedly 3 rand)})) -(def var202 nil) +(def var204 nil) -(def var203 (delay (Thread/sleep 500) (+ 1 2))) +(def var205 (delay (Thread/sleep 500) (+ 1 2))) -(def var204 nil) +(def var206 nil) -(def var205 (kind/hiccup [:img {:src "notebooks/images/Clay.svg.png"}])) +(def var207 (kind/hiccup [:img {:src "notebooks/images/Clay.svg.png"}])) (def - var206 + var208 (kind/vega-lite {:data {:url "notebooks/datasets/iris.csv"}, :mark "rule", @@ -1580,29 +1586,29 @@ :background "floralwhite"})) -(def var207 nil) +(def var209 nil) -(def var208 (+ 1 2)) +(def var210 (+ 1 2)) -(deftest test209 (is (> var208 2.9))) +(deftest test211 (is (> var210 2.9))) -(deftest test210 (is (> var208 2.9))) +(deftest test212 (is (> var210 2.9))) -(deftest test211 (is (> var208 2.9))) +(deftest test213 (is (> var210 2.9))) -(def var212 nil) +(def var214 nil) (def - var213 + var215 (kindly/hide-code (kind/code "(kind/test-last [> 2.9])\n\n^kind/test-last\n[> 2.9]\n\n(kindly/check > 2.9)"))) -(def var214 nil) +(def var216 nil) From 74ef48debb59ddb517ca3d8225ea39d28bc972cd Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Mon, 22 Jul 2024 14:45:09 -0700 Subject: [PATCH 2/8] use kindly option to show side-by-side --- src/scicloj/clay/v2/notebook.clj | 132 +++++++++++++++---------------- 1 file changed, 64 insertions(+), 68 deletions(-) diff --git a/src/scicloj/clay/v2/notebook.clj b/src/scicloj/clay/v2/notebook.clj index 130908c2..8b8ed73e 100644 --- a/src/scicloj/clay/v2/notebook.clj +++ b/src/scicloj/clay/v2/notebook.clj @@ -58,80 +58,76 @@ (string/join "\n")) item/md)) -(defn code-by-value [{:as note :keys [code]}] - (let [source-item (item/source-clojure code) - source-hiccup (:hiccup source-item) - source-md (:md source-item) - value-items (-> note - (select-keys [:value :code :form - :base-target-path - :full-target-path - :kindly/options - :format]) - (update :value deref-if-needed) - prepare/prepare-or-pprint) - ;; TODO: should join them if there are more than one. - value-item (first value-items) - value-hiccup (:hiccup value-item) - value-md (:md value-item)] - [{:hiccup (when (and value-hiccup source-hiccup) +(defn hide-code? [{:as note :keys [code form value kind]} {:as opts :keys [hide-code]}] + (or hide-code + (-> form meta :kindly/hide-code) + (-> form meta :kindly/hide-code?) ; legacy convention + (-> value meta :kindly/hide-code) + (-> value meta :kindly/hide-code?) ; legacy convention + (when kind + (some-> note + :kindly/options + :kinds-that-hide-code + kind)) + (nil? code))) + +(defn hide-value? [{:as note :keys [form value]} {:as opts :keys [hide-nils hide-vars]}] + (or (and (sequential? form) + (-> form first hidden-form-starters)) + (-> note :form meta :kind/hidden) + (and hide-nils (nil? value)) + (and hide-vars (var? value)))) + +(defn side-by-side-items [code-item value-items] + (let [code-hiccup (:hiccup code-item) + code-md (:md code-item)] + [{:hiccup (when (every? :hiccup value-items) [:div.grid - [:div.g-col-6 source-hiccup] - [:div.g-col-6 value-hiccup]]) - :md (when (and value-md source-md) - (str (str/join \newline - ["::: {.grid}" - "::: {.g-col-6}" - source-md - ":::" - "::: {.g-col-6}" - value-md - ":::" - ":::"]) + [:div.g-col-6 code-hiccup] + (into [:div.g-col-6] (map :hiccup value-items))]) + :md (when (every? :md value-items) + (str (string/join \newline + (-> ["::: {.grid}" "::: {.g-col-6}" code-md ":::"] + (into (mapcat (fn [{:keys [md]}] + ["::: {.g-col-6}" md ":::"]) + value-items)) + (conj ":::"))) \newline)) - :deps (:deps value-item)}])) + :deps (set (mapcat :deps value-items))}])) -(defn note-to-items [{:as note - :keys [comment? code form value kind]} - {:keys [hide-code hide-nils hide-vars]}] - (cond - (and comment? code) +(defn note-to-items [{:as note + :keys [comment? code]} + {:as opts + :keys [code-and-value]}] + (if (and comment? code) [(comment->item code)] + (let [code-item (when-not (hide-code? note opts) + (item/source-clojure code)) + value-items (when-not (hide-value? note opts) + (-> note + (select-keys [:value :code :form + :base-target-path + :full-target-path + :kindly/options + :format]) + (update :value deref-if-needed) + prepare/prepare-or-pprint))] + (cond (and (not code-item) (empty? value-items)) + [] - ;; TODO: should handle multiple examples at a time. - (= kind :kind/example) - (code-by-value note) + (not code-item) + value-items - :else - (concat - ;; code - [(when-not (or hide-code - (-> form meta :kindly/hide-code) - (-> form meta :kindly/hide-code?) ; legacy convention - (-> value meta :kindly/hide-code) - (-> value meta :kindly/hide-code?) ; legacy convention - (when kind - (some-> note - :kindly/options - :kinds-that-hide-code - kind)) - (nil? code)) - (item/source-clojure code))] - ;; value - (when-not (or - (and (sequential? form) - (-> form first hidden-form-starters)) - (-> note :form meta :kind/hidden) - (and hide-nils (nil? value)) - (and hide-vars (var? value))) - (-> note - (select-keys [:value :code :form - :base-target-path - :full-target-path - :kindly/options - :format]) - (update :value deref-if-needed) - prepare/prepare-or-pprint))))) + (empty? value-items) + [code-item] + + (and (= code-and-value :horizontal) + (or (every? :md value-items) + (every? :hiccup value-items))) + (side-by-side-items code-item value-items) + + :else + (into [code-item] value-items))))) (defn add-info-line [items {:keys [full-source-path hide-info-line]}] (if hide-info-line From 783d3e11dcada130ad71c5cc5621561e1599cfb4 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Mon, 22 Jul 2024 14:47:34 -0700 Subject: [PATCH 3/8] remove grid example until finalized --- notebooks/index.clj | 7 ------- 1 file changed, 7 deletions(-) diff --git a/notebooks/index.clj b/notebooks/index.clj index e88c269a..e55480bc 100644 --- a/notebooks/index.clj +++ b/notebooks/index.clj @@ -632,13 +632,6 @@ nested-structure-1 tc/dataset (kind/dataset {:dataset/print-range 6})) -;; ### Example and grid - -^:kind/example (inc 1) - -^:kind/grid -[1 2 3 4 5 6] - ;; ### Tables ;; The `:kind/table` kind can be handy for an interactive table view. `:kind/table` understands many structures which can be rendered as a table. From 33e80813cbed686e6b0fba68c2218578da27b126 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Mon, 22 Jul 2024 19:48:59 -0700 Subject: [PATCH 4/8] fix side-by-side souce code formatting --- resources/styles/main.css | 2 + src/scicloj/clay/v2/notebook.clj | 138 +++++++++++++++---------------- src/scicloj/clay/v2/prepare.clj | 7 +- 3 files changed, 73 insertions(+), 74 deletions(-) diff --git a/resources/styles/main.css b/resources/styles/main.css index 86eaa04d..63c3f12c 100644 --- a/resources/styles/main.css +++ b/resources/styles/main.css @@ -1 +1,3 @@ .clay-limit-image-width .clay-image {max-width: 100%} +.clay-side-by-side .sourceCode {margin: 0} +.clay-side-by-side {margin: 1em 0} diff --git a/src/scicloj/clay/v2/notebook.clj b/src/scicloj/clay/v2/notebook.clj index 272ad4c8..8b593ed0 100644 --- a/src/scicloj/clay/v2/notebook.clj +++ b/src/scicloj/clay/v2/notebook.clj @@ -1,15 +1,15 @@ (ns scicloj.clay.v2.notebook (:require - [clojure.string :as string] - [scicloj.clay.v2.item :as item] - [scicloj.clay.v2.util.path :as path] - [scicloj.clay.v2.item :as item] - [scicloj.clay.v2.prepare :as prepare] - [scicloj.clay.v2.read :as read] - [scicloj.clay.v2.config :as config] - [scicloj.clay.v2.util.merge :as merge] - [scicloj.kindly.v4.kind :as kind] - [scicloj.kindly-advice.v1.api :as kindly-advice])) + [clojure.string :as string] + [scicloj.clay.v2.item :as item] + [scicloj.clay.v2.util.path :as path] + [scicloj.clay.v2.item :as item] + [scicloj.clay.v2.prepare :as prepare] + [scicloj.clay.v2.read :as read] + [scicloj.clay.v2.config :as config] + [scicloj.clay.v2.util.merge :as merge] + [scicloj.kindly.v4.kind :as kind] + [scicloj.kindly-advice.v1.api :as kindly-advice])) (defn deref-if-needed [v] (if (delay? v) @@ -25,45 +25,45 @@ (defn info-line [absolute-file-path] (let [relative-file-path (path/path-relative-to-repo - absolute-file-path)] + absolute-file-path)] (item/info-line {:path relative-file-path - :url (some-> (config/config) - :remote-repo - (path/file-git-url relative-file-path))}))) + :url (some-> (config/config) + :remote-repo + (path/file-git-url relative-file-path))}))) -(defn complete [{:as note +(defn complete [{:as note :keys [comment? code form value]}] (-> (if (or value comment?) note (assoc - note - :value (cond form (-> form - eval - deref-if-needed) - code (-> code - read-string - eval - deref-if-needed)))) + note + :value (cond form (-> form + eval + deref-if-needed) + code (-> code + read-string + eval + deref-if-needed)))) (cond-> (not comment?) - kindly-advice/advise))) + kindly-advice/advise))) (defn comment->item [comment] (-> comment (string/split #"\n") (->> (map #(-> % (string/replace - #"^;+\s?" "") + #"^;+\s?" "") (string/replace - #"^#" "\n#"))) + #"^#" "\n#"))) (string/join "\n")) item/md)) (defn hide-code? [{:as note :keys [code form value kind]} {:as opts :keys [hide-code]}] (or hide-code (-> form meta :kindly/hide-code) - (-> form meta :kindly/hide-code?) ; legacy convention + (-> form meta :kindly/hide-code?) ; legacy convention (-> value meta :kindly/hide-code) - (-> value meta :kindly/hide-code?) ; legacy convention + (-> value meta :kindly/hide-code?) ; legacy convention (when kind (some-> note :kindly/options @@ -78,21 +78,21 @@ (and hide-nils (nil? value)) (and hide-vars (var? value)))) -(defn side-by-side-items [code-item value-items] - (let [code-hiccup (:hiccup code-item) - code-md (:md code-item)] - [{:hiccup (when (every? :hiccup value-items) - [:div.grid - [:div.g-col-6 code-hiccup] - (into [:div.g-col-6] (map :hiccup value-items))]) - :md (when (every? :md value-items) - (str (string/join \newline - (-> ["::: {.grid}" "::: {.g-col-6}" code-md ":::"] - (into (mapcat (fn [{:keys [md]}] - ["::: {.g-col-6}" md ":::"]) - value-items)) - (conj ":::"))) - \newline)) +(defn side-by-side-items [{:as spec :keys [format]} code-item value-items] + ;; markdown grids are not structurally nested, but hiccup grids are + (if (= :quarto (first format)) + `[{:md "::: {.grid .clay-side-by-side}"} + {:md "::: {.g-col-6}"} + ~code-item + {:md ":::"} + {:md "::: {.g-col-6}"} + ~@value-items + {:md ":::"} + {:md ":::"}] + [{:hiccup [:div.grid + [:div.g-col-6 (:hiccup code-item)] + (->> (map #(prepare/item->hiccup % spec) value-items) + (into [:div.g-col-6]))] :deps (set (mapcat :deps value-items))}])) (defn note-to-items [{:as note @@ -121,10 +121,8 @@ (empty? value-items) [code-item] - (and (= code-and-value :horizontal) - (or (every? :md value-items) - (every? :hiccup value-items))) - (side-by-side-items code-item value-items) + (and (= code-and-value :horizontal)) + (side-by-side-items opts code-item value-items) :else (into [code-item] value-items))))) @@ -135,9 +133,9 @@ (let [il (info-line full-source-path)] (concat #_[il item/separator] - items - [item/separator - il])))) + items + [item/separator + il])))) (defn ->var-name [i] (symbol (str "var" i))) @@ -195,7 +193,7 @@ (defn items-and-test-forms - ([{:as options + ([{:as options :keys [full-source-path hide-info-line hide-code hide-nils hide-vars @@ -207,14 +205,14 @@ format]}] (let [code (some-> full-source-path slurp) - notes (cond - single-value (conj (when code - [{:form (read/read-ns-form code)}]) - {:value single-value}) - single-form (conj (when code + notes (cond + single-value (conj (when code [{:form (read/read-ns-form code)}]) - {:form single-form}) - :else (read/->safe-notes code))] + {:value single-value}) + single-form (conj (when code + [{:form (read/read-ns-form code)}]) + {:form single-form}) + :else (read/->safe-notes code))] (-> (->> notes (reduce (fn [{:as aggregation :keys [i items @@ -226,11 +224,11 @@ new-items (when-not test-note (-> complete-note (merge/deep-merge - (-> options - (select-keys [:base-target-path - :full-target-path - :kindly/options - :format]))) + (-> options + (select-keys [:base-target-path + :full-target-path + :kindly/options + :format]))) (note-to-items options))) test-form (if test-note ;; a deftest form @@ -245,17 +243,17 @@ (def-form (->var-name i) form)))] - {:i (inc i) - :items (concat items new-items) - :test-forms (conj test-forms test-form) + {:i (inc i) + :items (concat items new-items) + :test-forms (conj test-forms test-form) :last-nontest-i (if (or (:comment? complete-note) test-note) last-nontest-i i)})) ;; initial value - {:i 0 - :items [] - :test-forms [] + {:i 0 + :items [] + :test-forms [] :last-nontest-i nil})) (update :items ;; final processing of items @@ -279,4 +277,4 @@ (-> "notebooks/scratch.clj" (notebook-items {:full-target-path "docs/scratch.html" - :single-form '(+ 1 2)}))) + :single-form '(+ 1 2)}))) diff --git a/src/scicloj/clay/v2/prepare.clj b/src/scicloj/clay/v2/prepare.clj index 243014ee..91ffbabf 100644 --- a/src/scicloj/clay/v2/prepare.clj +++ b/src/scicloj/clay/v2/prepare.clj @@ -55,7 +55,7 @@ script item-class inside-a-table]} - {:as context + {:as spec :keys [format kind]}] (-> (or hiccup (some->> html @@ -92,9 +92,8 @@ (update :hiccup conj script) item->md) (-> (or md - (format "\n```{=html}\n%s\n```\n" - (or html - (some-> hiccup hiccup/html)))))) + (or html + (some-> hiccup hiccup/html))))) (cond-> item-class (#(format "::: {.%s}\n%s\n:::\n" item-class %))))) From 3757b5350dbcda423e300a48536d80fb381064f0 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Tue, 23 Jul 2024 11:31:07 -0700 Subject: [PATCH 5/8] revert whitespace and html raw block --- src/scicloj/clay/v2/notebook.clj | 85 +++++++++++++++----------------- src/scicloj/clay/v2/prepare.clj | 5 +- 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/scicloj/clay/v2/notebook.clj b/src/scicloj/clay/v2/notebook.clj index 8b593ed0..e59f235a 100644 --- a/src/scicloj/clay/v2/notebook.clj +++ b/src/scicloj/clay/v2/notebook.clj @@ -1,15 +1,15 @@ (ns scicloj.clay.v2.notebook (:require - [clojure.string :as string] - [scicloj.clay.v2.item :as item] - [scicloj.clay.v2.util.path :as path] - [scicloj.clay.v2.item :as item] - [scicloj.clay.v2.prepare :as prepare] - [scicloj.clay.v2.read :as read] - [scicloj.clay.v2.config :as config] - [scicloj.clay.v2.util.merge :as merge] - [scicloj.kindly.v4.kind :as kind] - [scicloj.kindly-advice.v1.api :as kindly-advice])) + [clojure.string :as string] + [scicloj.clay.v2.item :as item] + [scicloj.clay.v2.util.path :as path] + [scicloj.clay.v2.item :as item] + [scicloj.clay.v2.prepare :as prepare] + [scicloj.clay.v2.read :as read] + [scicloj.clay.v2.config :as config] + [scicloj.clay.v2.util.merge :as merge] + [scicloj.kindly.v4.kind :as kind] + [scicloj.kindly-advice.v1.api :as kindly-advice])) (defn deref-if-needed [v] (if (delay? v) @@ -25,36 +25,36 @@ (defn info-line [absolute-file-path] (let [relative-file-path (path/path-relative-to-repo - absolute-file-path)] + absolute-file-path)] (item/info-line {:path relative-file-path - :url (some-> (config/config) - :remote-repo - (path/file-git-url relative-file-path))}))) + :url (some-> (config/config) + :remote-repo + (path/file-git-url relative-file-path))}))) -(defn complete [{:as note +(defn complete [{:as note :keys [comment? code form value]}] (-> (if (or value comment?) note (assoc - note - :value (cond form (-> form - eval - deref-if-needed) - code (-> code - read-string - eval - deref-if-needed)))) + note + :value (cond form (-> form + eval + deref-if-needed) + code (-> code + read-string + eval + deref-if-needed)))) (cond-> (not comment?) - kindly-advice/advise))) + kindly-advice/advise))) (defn comment->item [comment] (-> comment (string/split #"\n") (->> (map #(-> % (string/replace - #"^;+\s?" "") + #"^;+\s?" "") (string/replace - #"^#" "\n#"))) + #"^#" "\n#"))) (string/join "\n")) item/md)) @@ -131,11 +131,8 @@ (if hide-info-line items (let [il (info-line full-source-path)] - (concat #_[il - item/separator] - items - [item/separator - il])))) + (concat items + [item/separator il])))) (defn ->var-name [i] (symbol (str "var" i))) @@ -193,7 +190,7 @@ (defn items-and-test-forms - ([{:as options + ([{:as options :keys [full-source-path hide-info-line hide-code hide-nils hide-vars @@ -224,11 +221,11 @@ new-items (when-not test-note (-> complete-note (merge/deep-merge - (-> options - (select-keys [:base-target-path - :full-target-path - :kindly/options - :format]))) + (-> options + (select-keys [:base-target-path + :full-target-path + :kindly/options + :format]))) (note-to-items options))) test-form (if test-note ;; a deftest form @@ -243,17 +240,17 @@ (def-form (->var-name i) form)))] - {:i (inc i) - :items (concat items new-items) - :test-forms (conj test-forms test-form) + {:i (inc i) + :items (concat items new-items) + :test-forms (conj test-forms test-form) :last-nontest-i (if (or (:comment? complete-note) test-note) last-nontest-i i)})) ;; initial value - {:i 0 - :items [] - :test-forms [] + {:i 0 + :items [] + :test-forms [] :last-nontest-i nil})) (update :items ;; final processing of items @@ -277,4 +274,4 @@ (-> "notebooks/scratch.clj" (notebook-items {:full-target-path "docs/scratch.html" - :single-form '(+ 1 2)}))) + :single-form '(+ 1 2)}))) diff --git a/src/scicloj/clay/v2/prepare.clj b/src/scicloj/clay/v2/prepare.clj index 91ffbabf..366bb14c 100644 --- a/src/scicloj/clay/v2/prepare.clj +++ b/src/scicloj/clay/v2/prepare.clj @@ -92,8 +92,9 @@ (update :hiccup conj script) item->md) (-> (or md - (or html - (some-> hiccup hiccup/html))))) + (format "\n```{=html}\n%s\n```\n" + (or html + (some-> hiccup hiccup/html)))))) (cond-> item-class (#(format "::: {.%s}\n%s\n:::\n" item-class %))))) From afda9946f99c9d86586c5e0e5e56206e5f0cdc69 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Tue, 23 Jul 2024 11:33:55 -0700 Subject: [PATCH 6/8] revert whitespace change --- src/scicloj/clay/v2/notebook.clj | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/scicloj/clay/v2/notebook.clj b/src/scicloj/clay/v2/notebook.clj index e59f235a..d3467215 100644 --- a/src/scicloj/clay/v2/notebook.clj +++ b/src/scicloj/clay/v2/notebook.clj @@ -202,14 +202,14 @@ format]}] (let [code (some-> full-source-path slurp) - notes (cond - single-value (conj (when code + notes (cond + single-value (conj (when code + [{:form (read/read-ns-form code)}]) + {:value single-value}) + single-form (conj (when code [{:form (read/read-ns-form code)}]) - {:value single-value}) - single-form (conj (when code - [{:form (read/read-ns-form code)}]) - {:form single-form}) - :else (read/->safe-notes code))] + {:form single-form}) + :else (read/->safe-notes code))] (-> (->> notes (reduce (fn [{:as aggregation :keys [i items From a255ac44a687c8385366139c92b664bd3af53e44 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Tue, 23 Jul 2024 19:57:40 -0700 Subject: [PATCH 7/8] remove grid, still thinking about it --- src/scicloj/clay/v2/prepare.clj | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/scicloj/clay/v2/prepare.clj b/src/scicloj/clay/v2/prepare.clj index 366bb14c..806f749d 100644 --- a/src/scicloj/clay/v2/prepare.clj +++ b/src/scicloj/clay/v2/prepare.clj @@ -482,22 +482,3 @@ (add-preparer-from-value-fn! :kind/highcharts #'item/highcharts) - -(defn grid [{:as context :keys [value]}] - (let [prepared-parts (->> value - (mapcat (fn [subvalue] - (-> context - (dissoc :form) - (update :kindly/options :dissoc :element/max-height) - (assoc :value subvalue) - prepare-or-pprint))))] - {:hiccup (->> (map (fn [part] - ;; TODO: width should be an option - [:div.g-col-6 (item->hiccup part context)]) - prepared-parts) - (into [:div.grid])) - :md (map item->md prepared-parts) - :deps (->> (mapcat :deps prepared-parts) - (distinct))})) - -(add-preparer! :kind/grid #'grid) From 7433a0c87868a14eb208c10db72084229a7fa156 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Tue, 23 Jul 2024 20:00:33 -0700 Subject: [PATCH 8/8] revert unnecessary alias name --- src/scicloj/clay/v2/prepare.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scicloj/clay/v2/prepare.clj b/src/scicloj/clay/v2/prepare.clj index 806f749d..2bb81248 100644 --- a/src/scicloj/clay/v2/prepare.clj +++ b/src/scicloj/clay/v2/prepare.clj @@ -55,7 +55,7 @@ script item-class inside-a-table]} - {:as spec + {:as context :keys [format kind]}] (-> (or hiccup (some->> html