Skip to content

Commit

Permalink
Add support for advanced :base-language option
Browse files Browse the repository at this point in the history
Author: @ptaoussanis

This is an advanced option to help prevent any broken doc links
when upgrading a project's docs from single language to dual language.

In this case, :base-language can be set to the previous (single)
language for which doc links may already exist in the wild.

In detail, if cross-platform project then:

  {:base-language nil}            => ".clj", ".cljs" file extensions ; Default
  {:base-language :clojure}       => nil,    ".cljs" file extensions
  {:base-language :clojurescript} => ".clj", nil     file extensions

For example:

  Library Foo previously used {:language :clojure} (either because
  it was Clojure only, or because of limitations in Codox).

  Various links to Foo's Codox documentation now exist in the wild.

  Foo's authors want to change to cross-platform, but don't
  want to break pre-existing links in the wild.

  In this case, Foo's authors can use the following opts:
    {:language #{:clojure :clojurescript}
     :base-language :clojure}

  This will produce files like the following:
    com.foolib.html      ; For Clojure       platform
    com.foolib.cljs.html ; For ClojureScript platform

  Any pre-existing links will successfully point to the same
  (Clojure) docs they did previously.
  • Loading branch information
ptaoussanis committed Jul 6, 2023
1 parent f88c2d9 commit f91fdf4
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions codox/src/codox/writer/html.clj
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,26 @@
(markdown-to-html doc project ns))])

(defn- language-info
[language]
(when language
(case language
:clojure {:ext "clj", :filename-suffix ".clj", :name "Clojure"}
:clojurescript {:ext "cljs", :filename-suffix ".cljs", :name "ClojureScript"}
(ex-info (str "Unexpected language: `" language "`")
{:language language}))))
([language]
(when language
(case language
:clojure {:ext "clj", :filename-suffix ".clj", :name "Clojure"}
:clojurescript {:ext "cljs", :filename-suffix ".cljs", :name "ClojureScript"}
(ex-info (str "Unexpected language: `" language "`")
{:language language}))))

([language base-language]
(when-let [info (language-info language)]
(if (= language base-language)
(assoc info :filename-suffix "")
info))))

(defn- index-filename [language]
(str "index" (:filename-suffix (language-info language)) ".html"))

(defn- ns-filename [namespace]
(str (:name namespace)
(:filename-suffix (language-info (:language namespace)))
(:filename-suffix (language-info (:language namespace) (:base-language namespace)))
".html"))

(comment
Expand Down Expand Up @@ -517,8 +523,11 @@
(.mkdirs (io/file output-dir dir))))

(defn- cross-platform-namespaces
[namespaces language]
(map #(assoc % :language language)
[namespaces language base-language]
(map
#(assoc %
:language language
:base-language base-language)
(get namespaces language)))

(defn- write-index [output-dir project]
Expand All @@ -527,7 +536,7 @@
(when cross-platform?
;; Write an index file for each language
(doseq [language (:languages project)]
(let [namespaces (cross-platform-namespaces namespaces language)
(let [namespaces (cross-platform-namespaces namespaces language (:base-language project))
project
(assoc project
:namespaces namespaces
Expand All @@ -550,7 +559,7 @@
(if cross-platform?
;; Write namespace files for each language
(doseq [language (:languages project)]
(let [namespaces (cross-platform-namespaces namespaces language)]
(let [namespaces (cross-platform-namespaces namespaces language (:base-language project))]
(doseq [namespace namespaces]
(let [project (assoc project
:namespaces namespaces
Expand Down

0 comments on commit f91fdf4

Please sign in to comment.