Skip to content

Commit

Permalink
Fix browse in build-static-app! on windows (#39)
Browse files Browse the repository at this point in the history
Fixes #38.

Co-authored-by: ikappaki <ikappaki@users.noreply.github.com>
Co-authored-by: Martin Kavalar <mk@katercalling.com>
  • Loading branch information
3 people committed Dec 21, 2021
1 parent 68f6545 commit 415a5eb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
org.clojure/java.classpath {:mvn/version "1.0.0"}
org.clojure/tools.analyzer.jvm {:mvn/version "1.1.0"}
org.clojure/tools.deps.alpha {:mvn/version "0.11.905"}
babashka/fs {:mvn/version "0.0.5"}
babashka/fs {:mvn/version "0.1.2"}
borkdude/edamame {:mvn/version "0.0.11"}
weavejester/dependency {:mvn/version "0.2.1"}

Expand Down Expand Up @@ -49,5 +49,5 @@
io.github.nextjournal/cas {:git/url "git@github.com:nextjournal/cas"
:git/sha "5e8079b720e347b9466db9c2282ce79a125a011c"}
rewrite-clj/rewrite-clj {:mvn/version "1.0.699-alpha"}
babashka/fs {:mvn/version "0.0.5"}}
babashka/fs {:mvn/version "0.1.2"}}
:ns-default build}}}
14 changes: 11 additions & 3 deletions src/nextjournal/clerk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,16 @@

#_(->html-extension "hello.clj")


(defn- path-to-url-canonicalize
"Canonicalizes the system specific path separators in `PATH` (e.g. `\\`
on MS-Windows) to URL-compatible forward slashes."
[path]
(str/replace path fs/file-separator "/"))

(defn build-static-app!
"Builds a static html app of the notebooks. Takes an options map with keys:
"Builds a static html app of the notebooks and opens the app in the
default browser. Takes an options map with keys:
- `:paths` a vector of relative paths to notebooks to include in the build
- `:bundle?` builds a single page app versus a folder with an html page for each notebook (defaults to `true`)
Expand All @@ -321,7 +329,7 @@
"
[{:as opts :keys [paths out-path live-js? bundle? browse?]
:or {paths clerk-docs
out-path "public/build"
out-path (str "public" fs/file-separator "build")
live-js? view/live-js?
bundle? true
browse? true}}]
Expand All @@ -342,7 +350,7 @@
(when browse?
(if (and live-js? (str/starts-with? out-path "public/"))
(browse/browse-url (str "http://localhost:7778/" (str/replace out-path "public/" "")))
(browse/browse-url index-html)))))
(browse/browse-url (-> index-html fs/absolutize .toString path-to-url-canonicalize))))))

#_(build-static-app! {:paths ["index.clj" "notebooks/rule_30.clj" "notebooks/markdown.md"] :bundle? true})
#_(build-static-app! {:paths ["index.clj" "notebooks/rule_30.clj" "notebooks/markdown.md"] :bundle? false :path-prefix "build/"})
Expand Down
24 changes: 24 additions & 0 deletions test/nextjournal/clerk_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns nextjournal.clerk-test
(:require [babashka.fs :as fs]
[clojure.java.browse :as browse]
[clojure.string :as str]
[clojure.test :refer :all]
[nextjournal.clerk :as clerk])
(:import (java.io File)))

(deftest url-canonicalize
(testing "canonicalization of file components into url components"
(let [dice (str/join (File/separator) ["notebooks" "dice.clj"])]
(is (= (#'clerk/path-to-url-canonicalize dice) (str/replace dice (File/separator) "/"))))))

(deftest static-app
(let [url* (volatile! nil)]
(with-redefs [clojure.java.browse/browse-url (fn [path]
(vreset! url* path))]
(testing "browser receives canonical url in this system arch"
(fs/with-temp-dir [temp {}]
(let [expected (-> (str/join (java.io.File/separator) [(.toString temp) "index.html"])
(str/replace (java.io.File/separator) "/"))]
(clerk/build-static-app! {:paths ["notebooks/hello.clj"]
:out-path temp})
(is (= expected @url*))))))))

0 comments on commit 415a5eb

Please sign in to comment.