Skip to content

Commit 4f1052a

Browse files
committed
do not remove newlines in :pre :samp :code when generating orgx
1 parent b52915f commit 4f1052a

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/net/coruscation/cerulean/orgx/orgx.clj

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,27 @@
9595
(loop [loc (zip/vector-zip hiccup)]
9696
(if (zip/end? loc)
9797
(zip/node loc)
98-
(recur (zip/next
99-
(if (and (string? (zip/node loc))
100-
(= (str/trim-newline (zip/node loc))
101-
""))
102-
(zip/remove loc)
103-
loc))))))
98+
(let [node (zip/node loc)]
99+
(cond
100+
;; do not handle verbatim blocks
101+
(and (vector? node)
102+
(or (= (first node)
103+
:pre)
104+
(= (first node)
105+
:node)
106+
(= (first node)
107+
:samp)))
108+
(if (zip/right loc)
109+
(recur (zip/right loc))
110+
(zip/root loc))
111+
112+
(and (string? node)
113+
(= (str/trim-newline node)
114+
""))
115+
(recur (zip/next (zip/remove loc)))
116+
117+
:else
118+
(recur (zip/next loc)))))))
104119

105120
(defn from-html [html]
106121
(let [results (->> (hickory.core/parse-fragment html)

test/net/coruscation/cerulean/orgx/orgx_test.clj

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
[clojure.java.io :as io]
55
[clojure.test :refer [deftest is testing]]
66
[clojure.tools.logging :as log]
7+
[hickory.core :as hc]
78
[kaocha.classpath :as kaocha.classpath]
89
[net.coruscation.cerulean.config :refer [join-workspace-path]]
9-
[net.coruscation.cerulean.orgx.orgx :as sut]
10+
[net.coruscation.cerulean.orgx.orgx :as sut :refer [remove-extra-newline]]
1011
[net.coruscation.cerulean.server.utils :refer [add-to-classpath path-join]]
1112
[net.coruscation.cerulean.test-constants :refer [resources-dir]]
1213
[net.coruscation.cerulean.test-utils :refer [with-temp-workspace]]))
1314

1415
(defonce test-resources-dir (path-join resources-dir "orgx-test"))
1516

17+
(defn html->hiccup [htmlstr]
18+
(->> (hc/parse-fragment htmlstr)
19+
(mapv hc/as-hiccup)))
20+
1621
(deftest from-html-test
1722
(testing
1823
(let [[[ele] toplevels] (sut/from-html "<pre class=\"orgx\"> ($ :button {:on-click (fn [&amp; _] (js/alert &quot;clicked!&quot;))} &quot;Click Me&quot;) </pre>")]
@@ -23,8 +28,8 @@
2328
(deftest toplevesl-test
2429
(testing
2530
(let [[_ toplevels] (sut/from-html "<pre class=\"orgx\" data-toplevel> (defui demo []) </pre> ")]
26-
(is (= (str toplevels)
27-
(str '((defui demo []))))))))
31+
(is (= (str '((defui demo [])))
32+
(str toplevels))))))
2833

2934
(deftest load-orgx-test
3035
(testing
@@ -42,3 +47,14 @@
4247
(require (symbol "orgx.demo") :reload)
4348
(is (= 10 (var-get (intern (find-ns (symbol "orgx.demo"))
4449
'a)))))))
50+
51+
(deftest remove-extra-newline-test
52+
(is (= [[:p {} [:b {} "hello"]]]
53+
(remove-extra-newline (html->hiccup "<p>
54+
<b>hello</b>
55+
</p>"))))
56+
(is (= [[:pre {} "\n" [:b {} "hello"] "\n"]]
57+
(remove-extra-newline (html->hiccup "<pre>
58+
59+
<b>hello</b>
60+
</pre>")))))

0 commit comments

Comments
 (0)