|
205 | 205 | (expand-paths {:paths-fn `my-paths}))
|
206 | 206 | #_(expand-paths {:paths ["notebooks/viewers**"]})
|
207 | 207 |
|
| 208 | +(def builtin-index |
| 209 | + (io/resource "nextjournal/clerk/index.clj")) |
| 210 | + |
208 | 211 | (defn process-build-opts [{:as opts :keys [paths index expand-paths?]}]
|
209 | 212 | (merge {:out-path default-out-path
|
210 | 213 | :bundle? false
|
|
223 | 226 | (assoc :index (first expanded-paths))
|
224 | 227 | (and (not index) (< 1 (count expanded-paths)) (every? (complement #{"index.clj"}) expanded-paths))
|
225 | 228 | (as-> opts
|
226 |
| - (let [index (io/resource "nextjournal/clerk/index.clj")] |
227 |
| - (-> opts (assoc :index index) (update :expanded-paths conj index))))))))) |
| 229 | + (-> opts (assoc :index builtin-index) (update :expanded-paths conj builtin-index)))))))) |
228 | 230 |
|
229 | 231 | #_(process-build-opts {:index 'book.clj :expand-paths? true})
|
230 | 232 | #_(process-build-opts {:paths ["notebooks/rule_30.clj"] :expand-paths? true})
|
231 | 233 | #_(process-build-opts {:paths ["notebooks/rule_30.clj"
|
232 | 234 | "notebooks/markdown.md"] :expand-paths? true})
|
233 | 235 |
|
234 |
| -(defn build-path->url [{:as opts :keys [bundle?]} docs] |
235 |
| - (into {} |
236 |
| - (map (comp (juxt identity #(cond-> (->> % (viewer/map-index opts) strip-index) (not bundle?) ->html-extension)) |
237 |
| - str :file)) |
238 |
| - docs)) |
239 |
| -#_(build-path->url {:bundle? false} [{:file "notebooks/foo.clj"} {:file "index.clj"}]) |
240 |
| -#_(build-path->url {:bundle? true} [{:file "notebooks/foo.clj"} {:file "index.clj"}]) |
241 |
| - |
242 | 236 | (defn build-static-app-opts [{:as opts :keys [bundle? out-path browse? index]} docs]
|
243 |
| - (let [path->doc (into {} (map (juxt (comp str :file) :viewer)) docs)] |
| 237 | + (let [path->doc (into {} (map (juxt (comp str fs/strip-ext strip-index (partial viewer/map-index opts) :file) :viewer)) docs)] |
244 | 238 | (assoc opts
|
245 | 239 | :bundle? bundle?
|
246 | 240 | :path->doc path->doc
|
247 |
| - :paths (vec (keys path->doc)) |
248 |
| - :path->url (build-path->url opts docs)))) |
| 241 | + :paths (vec (keys path->doc))))) |
249 | 242 |
|
250 | 243 | (defn ssr!
|
251 | 244 | "Shells out to node to generate server-side-rendered html."
|
|
268 | 261 |
|
269 | 262 | (defn cleanup [build-opts]
|
270 | 263 | (select-keys build-opts
|
271 |
| - [:bundle? :path->doc :path->url :current-path :resource->url :exclude-js? :index :html])) |
| 264 | + [:bundle? :path->doc :current-path :resource->url :exclude-js? :index :html])) |
272 | 265 |
|
273 | 266 | (defn write-static-app!
|
274 | 267 | [opts docs]
|
275 |
| - (let [{:as opts :keys [bundle? out-path browse? ssr?]} (process-build-opts opts) |
| 268 | + (let [{:keys [bundle? out-path browse? ssr?]} opts |
276 | 269 | index-html (str out-path fs/file-separator "index.html")
|
277 |
| - {:as static-app-opts :keys [path->url path->doc]} (build-static-app-opts (viewer/update-if opts :index str) docs)] |
278 |
| - (when-not (contains? (-> path->url vals set) "") |
279 |
| - (throw (ex-info "Index must have been processed at this point" {:opts opts :docs docs}))) |
| 270 | + {:as static-app-opts :keys [path->doc]} (build-static-app-opts opts docs)] |
| 271 | + (when-not (contains? (set (keys path->doc)) "") |
| 272 | + (throw (ex-info "Index must have been processed at this point" {:static-app-opts static-app-opts}))) |
280 | 273 | (when-not (fs/exists? (fs/parent index-html))
|
281 | 274 | (fs/create-dirs (fs/parent index-html)))
|
282 | 275 | (if bundle?
|
283 | 276 | (spit index-html (view/->html (cleanup static-app-opts)))
|
284 | 277 | (doseq [[path doc] path->doc]
|
285 |
| - (let [out-html (str out-path fs/file-separator (->> path (viewer/map-index opts) ->html-extension))] |
| 278 | + (let [out-html (fs/file out-path path "index.html")] |
286 | 279 | (fs/create-dirs (fs/parent out-html))
|
287 | 280 | (spit out-html (view/->html (-> static-app-opts
|
288 | 281 | (assoc :path->doc (hash-map path doc) :current-path path)
|
289 | 282 | (cond-> ssr? ssr!)
|
290 | 283 | cleanup))))))
|
291 | 284 | (when browse?
|
292 |
| - (browse/browse-url (-> index-html fs/absolutize .toString path-to-url-canonicalize))) |
| 285 | + (browse/browse-url (if-let [{:keys [port]} (and (= out-path "public/build") @webserver/!server)] |
| 286 | + (str "http://localhost:" port "/build/") |
| 287 | + (-> index-html fs/absolutize .toString path-to-url-canonicalize)))) |
293 | 288 | {:docs docs
|
294 | 289 | :index-html index-html
|
295 | 290 | :build-href (if (and @webserver/!server (= out-path default-out-path)) "/build/" index-html)}))
|
|
331 | 326 | (update opts :resource->url assoc "/css/viewer.css" url))))
|
332 | 327 |
|
333 | 328 | (defn doc-url
|
334 |
| - ([opts doc file path] (doc-url opts doc file path nil)) |
335 |
| - ([{:as opts :keys [bundle?]} docs file path fragment] |
336 |
| - (let [url (get (build-path->url (viewer/update-if opts :index str) docs) path)] |
337 |
| - (if bundle? |
338 |
| - (str "#/" url) |
339 |
| - (str (viewer/relative-root-prefix-from (viewer/map-index opts file)) |
340 |
| - url (when fragment (str "#" fragment))))))) |
| 329 | + ([opts file path] (doc-url opts file path nil)) |
| 330 | + ([opts file path fragment] |
| 331 | + (if (:bundle? opts) |
| 332 | + (cond-> (str "#/" path) |
| 333 | + fragment (str ":" fragment)) |
| 334 | + (str (viewer/relative-root-prefix-from (viewer/map-index opts file)) path |
| 335 | + (when fragment (str "#" fragment)))))) |
341 | 336 |
|
342 | 337 | (defn read-opts-from-deps-edn! []
|
343 | 338 | (if (fs/exists? "deps.edn")
|
|
395 | 390 | (try
|
396 | 391 | (binding [*ns* *ns*
|
397 | 392 | *build-opts* opts
|
398 |
| - viewer/doc-url (partial doc-url opts state file)] |
| 393 | + viewer/doc-url (partial doc-url opts file)] |
399 | 394 | (let [doc (eval/eval-analyzed-doc doc)]
|
400 |
| - (assoc doc :viewer (view/doc->viewer (assoc opts :static-build? true |
401 |
| - :nav-path (str file)) doc)))) |
| 395 | + (assoc doc :viewer (view/doc->viewer (assoc opts |
| 396 | + :static-build? true |
| 397 | + :nav-path (if (instance? java.net.URL file) |
| 398 | + (str "'" (:ns doc)) |
| 399 | + (str file))) |
| 400 | + doc)))) |
402 | 401 | (catch Exception e
|
403 | 402 | {:error e})))]
|
404 | 403 | (report-fn (merge {:stage :built :duration duration :idx idx}
|
|
422 | 421 |
|
423 | 422 | (comment
|
424 | 423 | (build-static-app! {:paths clerk-docs :bundle? true})
|
425 |
| - (build-static-app! {:paths ["notebooks/index.clj" "notebooks/rule_30.clj" "notebooks/viewer_api.md"] :index "notebooks/index.clj"}) |
426 |
| - (build-static-app! {:paths ["index.clj" "notebooks/rule_30.clj" "notebooks/markdown.md"] :bundle? false :browse? false}) |
| 424 | + (build-static-app! {:paths ["notebooks/editor.clj"] :browse? true}) |
| 425 | + (build-static-app! {:paths ["CHANGELOG.md" "notebooks/editor.clj"] :browse? true}) |
| 426 | + (build-static-app! {:paths ["index.clj" "notebooks/links.md" "notebooks/rule_30.clj" "notebooks/markdown.md"] :bundle? true :browse? true}) |
| 427 | + (build-static-app! {:paths ["notebooks/links.md" "notebooks/rule_30.clj" "notebooks/markdown.md"] :bundle? true :browse? true}) |
| 428 | + (build-static-app! {:paths ["index.clj" "notebooks/rule_30.clj" "notebooks/markdown.md"] :bundle? false :browse? true}) |
427 | 429 | (build-static-app! {:paths ["notebooks/viewers/**"]})
|
428 | 430 | (build-static-app! {:index "notebooks/rule_30.clj" :git/sha "bd85a3de12d34a0622eb5b94d82c9e73b95412d1" :git/url "https://github.com/nextjournal/clerk"})
|
429 | 431 | (reset! config/!resource->url @config/!asset-map)
|
|
455 | 457 | :bundle? true
|
456 | 458 | :git/sha "d60f5417"
|
457 | 459 | :git/url "https://github.com/nextjournal/clerk"}))
|
| 460 | + |
0 commit comments