From 962747d6ec255a7bd4da8162f6a1707365fd4591 Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Mon, 5 Dec 2022 18:57:36 +0100 Subject: [PATCH 1/7] Implement closing-paren reassignment for string leaf nodes --- notebooks/paren_soup.clj | 5 +++++ resources/viewer-js-hash | 2 +- src/nextjournal/clerk/render.cljs | 9 ++++----- src/nextjournal/clerk/viewer.cljc | 8 ++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/notebooks/paren_soup.clj b/notebooks/paren_soup.clj index 75486ea4a..44fdb2693 100644 --- a/notebooks/paren_soup.clj +++ b/notebooks/paren_soup.clj @@ -30,6 +30,11 @@ {1 [2] 3 [4]} +[1 "a\nb"] + +[[1 "a\nb"]] + +{:a 1 :b [[3 4 "a\nb"]]} #_(nextjournal.clerk/build-static-app! {:paths ["notebooks/paren_soup.clj"]}) #_(nextjournal.clerk/show! "notebooks/paren_soup.clj") diff --git a/resources/viewer-js-hash b/resources/viewer-js-hash index 91584fd85..5fe4bfebd 100644 --- a/resources/viewer-js-hash +++ b/resources/viewer-js-hash @@ -1 +1 @@ -8GnZqWmiQN3odN9v6Rdb9ccfy2T \ No newline at end of file +igmw9fgpMozqgNVchnN9JTmxX51 \ No newline at end of file diff --git a/src/nextjournal/clerk/render.cljs b/src/nextjournal/clerk/render.cljs index 0db73c39b..0ad1e49e4 100644 --- a/src/nextjournal/clerk/render.cljs +++ b/src/nextjournal/clerk/render.cljs @@ -415,15 +415,14 @@ (defn render-quoted-string [s {:as opts :keys [path viewer !expanded-at] :or {path []}}] (let [{:keys [closing-paren]} viewer] - [:span.cmt-string.inspected-value.inline-flex - [:span + [:span.inspected-value.inline-flex + [:span.cmt-string (if (some #(and (string? %) (str/includes? % "\n")) (if (string? s) [s] s)) [expand-button !expanded-at "\"" path] [:span "\""])] [:div - (viewer/->value (render-string s opts)) - "\"" - closing-paren]])) + [:span.cmt-string (viewer/->value (render-string s opts)) "\""] + (when (list? closing-paren) (into [:<>] closing-paren))]])) (defn render-number [num] [:span.cmt-number.inspected-value diff --git a/src/nextjournal/clerk/viewer.cljc b/src/nextjournal/clerk/viewer.cljc index 94981ccb9..8b51c1328 100644 --- a/src/nextjournal/clerk/viewer.cljc +++ b/src/nextjournal/clerk/viewer.cljc @@ -1408,11 +1408,19 @@ viewer (->viewer node) closing (:closing-paren viewer) non-leaf? (and (vector? value) (wrapped-value? (first value))) + ;; TODO: check this works with resolved aliases + expandable-string-node? (every-pred wrapped-value? + (comp string? ->value) + (comp #(str/includes? % "\n") ->value) + (comp #{'nextjournal.clerk.render/render-quoted-string} :form :render-fn ->viewer)) defer-closing? (and non-leaf? (or (-> value last :nextjournal/viewer :closing-paren) ;; the last element can carry parens + (-> value last expandable-string-node?) ;; the last element is an expandable string (and (= :map-entry (-> value last :nextjournal/viewer :name)) ;; the last element is a map entry whose value can carry parens (-> value last :nextjournal/value last :nextjournal/viewer :closing-paren))))] (cond-> (cond + (and (expandable-string-node? node) (seq closing-parens)) + (update node :nextjournal/viewer assoc :closing-paren closing-parens) (not closing) node defer-closing? (update node :nextjournal/viewer dissoc :closing-paren) :else (update-in node [:nextjournal/viewer :closing-paren] cons closing-parens)) From 8d8737018b3f3dbd69f987953ea1364b64db062b Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Mon, 5 Dec 2022 19:14:25 +0100 Subject: [PATCH 2/7] It does --- resources/viewer-js-hash | 2 +- src/nextjournal/clerk/viewer.cljc | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/resources/viewer-js-hash b/resources/viewer-js-hash index 5fe4bfebd..abc914cd4 100644 --- a/resources/viewer-js-hash +++ b/resources/viewer-js-hash @@ -1 +1 @@ -igmw9fgpMozqgNVchnN9JTmxX51 \ No newline at end of file +XxeRDCxoab3pEpCuR8gECKWKEri \ No newline at end of file diff --git a/src/nextjournal/clerk/viewer.cljc b/src/nextjournal/clerk/viewer.cljc index 8b51c1328..f5cfb5079 100644 --- a/src/nextjournal/clerk/viewer.cljc +++ b/src/nextjournal/clerk/viewer.cljc @@ -1408,7 +1408,6 @@ viewer (->viewer node) closing (:closing-paren viewer) non-leaf? (and (vector? value) (wrapped-value? (first value))) - ;; TODO: check this works with resolved aliases expandable-string-node? (every-pred wrapped-value? (comp string? ->value) (comp #(str/includes? % "\n") ->value) From 061b74f5dacb631fad9b0a8d20a513b5b526c313 Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Mon, 5 Dec 2022 19:20:52 +0100 Subject: [PATCH 3/7] Test paginated strings --- notebooks/paren_soup.clj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/notebooks/paren_soup.clj b/notebooks/paren_soup.clj index 44fdb2693..77b5b2390 100644 --- a/notebooks/paren_soup.clj +++ b/notebooks/paren_soup.clj @@ -36,5 +36,7 @@ {:a 1 :b [[3 4 "a\nb"]]} +{:a 1 :b [[3 4 (apply str (repeat 81 "x"))]]} + #_(nextjournal.clerk/build-static-app! {:paths ["notebooks/paren_soup.clj"]}) #_(nextjournal.clerk/show! "notebooks/paren_soup.clj") From bcd89975b2041bcba3f8ea154183d30ec7966d8d Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Mon, 5 Dec 2022 19:23:59 +0100 Subject: [PATCH 4/7] Fix paginated example --- notebooks/paren_soup.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebooks/paren_soup.clj b/notebooks/paren_soup.clj index 77b5b2390..dd7e37885 100644 --- a/notebooks/paren_soup.clj +++ b/notebooks/paren_soup.clj @@ -36,7 +36,7 @@ {:a 1 :b [[3 4 "a\nb"]]} -{:a 1 :b [[3 4 (apply str (repeat 81 "x"))]]} +{:a 1 :b [[3 4 (apply str (cons "a\n" (repeat 80 "b")))]]} #_(nextjournal.clerk/build-static-app! {:paths ["notebooks/paren_soup.clj"]}) #_(nextjournal.clerk/show! "notebooks/paren_soup.clj") From aabc84b5abd31f2e68964fed61755c6056114dba Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Mon, 5 Dec 2022 19:36:27 +0100 Subject: [PATCH 5/7] Fix paginated case --- resources/viewer-js-hash | 2 +- src/nextjournal/clerk/viewer.cljc | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/resources/viewer-js-hash b/resources/viewer-js-hash index abc914cd4..c6dcc6a71 100644 --- a/resources/viewer-js-hash +++ b/resources/viewer-js-hash @@ -1 +1 @@ -XxeRDCxoab3pEpCuR8gECKWKEri \ No newline at end of file +43HFQ6sTpjhEhNXEGhPjhF2hZYy7 \ No newline at end of file diff --git a/src/nextjournal/clerk/viewer.cljc b/src/nextjournal/clerk/viewer.cljc index f5cfb5079..93ccebc18 100644 --- a/src/nextjournal/clerk/viewer.cljc +++ b/src/nextjournal/clerk/viewer.cljc @@ -1408,17 +1408,15 @@ viewer (->viewer node) closing (:closing-paren viewer) non-leaf? (and (vector? value) (wrapped-value? (first value))) - expandable-string-node? (every-pred wrapped-value? - (comp string? ->value) - (comp #(str/includes? % "\n") ->value) - (comp #{'nextjournal.clerk.render/render-quoted-string} :form :render-fn ->viewer)) + string-node? (every-pred wrapped-value? + (comp #{'nextjournal.clerk.render/render-quoted-string} :form :render-fn ->viewer)) defer-closing? (and non-leaf? (or (-> value last :nextjournal/viewer :closing-paren) ;; the last element can carry parens - (-> value last expandable-string-node?) ;; the last element is an expandable string + (-> value last string-node?) ;; the last element is a string (and (= :map-entry (-> value last :nextjournal/viewer :name)) ;; the last element is a map entry whose value can carry parens (-> value last :nextjournal/value last :nextjournal/viewer :closing-paren))))] (cond-> (cond - (and (expandable-string-node? node) (seq closing-parens)) + (and (string-node? node) (seq closing-parens)) (update node :nextjournal/viewer assoc :closing-paren closing-parens) (not closing) node defer-closing? (update node :nextjournal/viewer dissoc :closing-paren) From ff5b3e5262c50a006421751f1dac6ab8a94ba41f Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Mon, 5 Dec 2022 19:49:50 +0100 Subject: [PATCH 6/7] Fix the map-entry case --- notebooks/paren_soup.clj | 2 ++ resources/viewer-js-hash | 2 +- src/nextjournal/clerk/viewer.cljc | 10 ++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/notebooks/paren_soup.clj b/notebooks/paren_soup.clj index dd7e37885..2403fde28 100644 --- a/notebooks/paren_soup.clj +++ b/notebooks/paren_soup.clj @@ -30,6 +30,8 @@ {1 [2] 3 [4]} +{:a "a\nb"} + [1 "a\nb"] [[1 "a\nb"]] diff --git a/resources/viewer-js-hash b/resources/viewer-js-hash index c6dcc6a71..5552c60b2 100644 --- a/resources/viewer-js-hash +++ b/resources/viewer-js-hash @@ -1 +1 @@ -43HFQ6sTpjhEhNXEGhPjhF2hZYy7 \ No newline at end of file +2zLuLDN3GCA7iZauSDRsmcD8emye \ No newline at end of file diff --git a/src/nextjournal/clerk/viewer.cljc b/src/nextjournal/clerk/viewer.cljc index 93ccebc18..45e43c375 100644 --- a/src/nextjournal/clerk/viewer.cljc +++ b/src/nextjournal/clerk/viewer.cljc @@ -1408,13 +1408,15 @@ viewer (->viewer node) closing (:closing-paren viewer) non-leaf? (and (vector? value) (wrapped-value? (first value))) - string-node? (every-pred wrapped-value? - (comp #{'nextjournal.clerk.render/render-quoted-string} :form :render-fn ->viewer)) + string-node? (comp #{'nextjournal.clerk.render/render-quoted-string} :form :render-fn ->viewer) + has-closing-paren? (comp :closing-paren ->viewer) defer-closing? (and non-leaf? - (or (-> value last :nextjournal/viewer :closing-paren) ;; the last element can carry parens + (or (-> value last has-closing-paren?) ;; the last element can carry parens (-> value last string-node?) ;; the last element is a string (and (= :map-entry (-> value last :nextjournal/viewer :name)) ;; the last element is a map entry whose value can carry parens - (-> value last :nextjournal/value last :nextjournal/viewer :closing-paren))))] + (-> value last :nextjournal/value last (as-> v + (or (has-closing-paren? v) + (string-node? v)))))))] (cond-> (cond (and (string-node? node) (seq closing-parens)) (update node :nextjournal/viewer assoc :closing-paren closing-parens) From 0a9aa1a57cfef4a04b7ecfa758bb79ba165f7532 Mon Sep 17 00:00:00 2001 From: Martin Kavalar Date: Mon, 5 Dec 2022 21:17:53 +0100 Subject: [PATCH 7/7] Simplify dropping special case for string viewer --- notebooks/paren_soup.clj | 2 +- resources/viewer-js-hash | 2 +- src/nextjournal/clerk/render.cljs | 10 +++++----- src/nextjournal/clerk/viewer.cljc | 17 +++++++---------- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/notebooks/paren_soup.clj b/notebooks/paren_soup.clj index 2403fde28..20f7e1c2f 100644 --- a/notebooks/paren_soup.clj +++ b/notebooks/paren_soup.clj @@ -36,7 +36,7 @@ [[1 "a\nb"]] -{:a 1 :b [[3 4 "a\nb"]]} +{:a 1 :b [[3 4 "aaaa\nbbb\nccc\ndd\ne"]]} {:a 1 :b [[3 4 (apply str (cons "a\n" (repeat 80 "b")))]]} diff --git a/resources/viewer-js-hash b/resources/viewer-js-hash index 5552c60b2..b29244da8 100644 --- a/resources/viewer-js-hash +++ b/resources/viewer-js-hash @@ -1 +1 @@ -2zLuLDN3GCA7iZauSDRsmcD8emye \ No newline at end of file +3DEHoEomGh3QJLoJ4WjRdcbuf36v \ No newline at end of file diff --git a/src/nextjournal/clerk/render.cljs b/src/nextjournal/clerk/render.cljs index 0ad1e49e4..0259896a0 100644 --- a/src/nextjournal/clerk/render.cljs +++ b/src/nextjournal/clerk/render.cljs @@ -414,15 +414,15 @@ (if (string? s) [s] s)))) (defn render-quoted-string [s {:as opts :keys [path viewer !expanded-at] :or {path []}}] - (let [{:keys [closing-paren]} viewer] + (let [{:keys [opening-paren closing-paren]} viewer] [:span.inspected-value.inline-flex [:span.cmt-string (if (some #(and (string? %) (str/includes? % "\n")) (if (string? s) [s] s)) - [expand-button !expanded-at "\"" path] - [:span "\""])] + [expand-button !expanded-at opening-paren path] + [:span opening-paren])] [:div - [:span.cmt-string (viewer/->value (render-string s opts)) "\""] - (when (list? closing-paren) (into [:<>] closing-paren))]])) + [:span.cmt-string (viewer/->value (render-string s opts)) (first closing-paren)] + (when (list? closing-paren) (into [:<>] (rest closing-paren)))]])) (defn render-number [num] [:span.cmt-number.inspected-value diff --git a/src/nextjournal/clerk/viewer.cljc b/src/nextjournal/clerk/viewer.cljc index 45e43c375..c0d71841c 100644 --- a/src/nextjournal/clerk/viewer.cljc +++ b/src/nextjournal/clerk/viewer.cljc @@ -659,7 +659,11 @@ {:pred char? :render-fn '(fn [c] [:span.cmt-string.inspected-value "\\" c])}) (def string-viewer - {:pred string? :render-fn 'nextjournal.clerk.render/render-quoted-string :page-size 80}) + {:pred string? + :render-fn 'nextjournal.clerk.render/render-quoted-string + :opening-paren "\"" + :closing-paren "\"" + :page-size 80}) (def number-viewer {:pred number? :render-fn 'nextjournal.clerk.render/render-number}) @@ -1408,18 +1412,11 @@ viewer (->viewer node) closing (:closing-paren viewer) non-leaf? (and (vector? value) (wrapped-value? (first value))) - string-node? (comp #{'nextjournal.clerk.render/render-quoted-string} :form :render-fn ->viewer) - has-closing-paren? (comp :closing-paren ->viewer) defer-closing? (and non-leaf? - (or (-> value last has-closing-paren?) ;; the last element can carry parens - (-> value last string-node?) ;; the last element is a string + (or (-> value last :nextjournal/viewer :closing-paren) ;; the last element can carry parens (and (= :map-entry (-> value last :nextjournal/viewer :name)) ;; the last element is a map entry whose value can carry parens - (-> value last :nextjournal/value last (as-> v - (or (has-closing-paren? v) - (string-node? v)))))))] + (-> value last :nextjournal/value last :nextjournal/viewer :closing-paren))))] (cond-> (cond - (and (string-node? node) (seq closing-parens)) - (update node :nextjournal/viewer assoc :closing-paren closing-parens) (not closing) node defer-closing? (update node :nextjournal/viewer dissoc :closing-paren) :else (update-in node [:nextjournal/viewer :closing-paren] cons closing-parens))