Skip to content

Commit

Permalink
Added the placeholder {git-commit-id} for the :source-uri.
Browse files Browse the repository at this point in the history
  • Loading branch information
Uwe Hubert committed Jan 15, 2018
1 parent ceb8512 commit 4b23ebd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,12 @@ to the function's source file in the documentation, you can set the

The URI is a template that may contain the following keys:

* `{filepath}` - the file path from the root of the repository
* `{basename}` - the basename of the file
* `{classpath}` - the relative path of the file within the source directory
* `{line}` - the line number of the source file
* `{version}` - the version of the project
* `{filepath}` - the file path from the root of the repository
* `{basename}` - the basename of the file
* `{classpath}` - the relative path of the file within the source directory
* `{line}` - the line number of the source file
* `{version}` - the version of the project
* `{git-commit-id}` - the Git commit id of the repository

You can also assign different URI templates to different paths of your
source tree. This is particularly useful for created source links from
Expand Down
48 changes: 37 additions & 11 deletions codox/src/codox/main.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns codox.main
"Main namespace for generating documentation"
(:use [codox.utils :only (add-source-paths)])
(:require [codox.reader.clojure :as clj]
(:require [clojure.string :as str]
[clojure.java.shell :as shell]
[codox.reader.clojure :as clj]
[codox.reader.plaintext :as text]))

(defn- writer [{:keys [writer]}]
Expand Down Expand Up @@ -91,17 +93,41 @@
(apply text/read-documents)
(sort-by :name))))

(defn- git-commit-id [dir]
(let [res (try
(shell/sh "git" "rev-parse" "HEAD"
:dir dir)
(catch Throwable t
{:exit -1
:err (.getMessage t)}))
exit (:exit res)
err (:err res)
err (if-not (str/blank? err)
err
(when-not (zero? exit)
(str "exit code " exit)))
out (if err
""
(-> res :out (str/trim)))]
(if err
(println "Warning: error while calling Git:" err)
(when (str/blank? out)
(println "Warning: retrieved empty Git commit id.")))
out))

(def defaults
{:language :clojure
:root-path (System/getProperty "user.dir")
:output-path "target/doc"
:source-paths ["src"]
:doc-paths ["doc"]
:doc-files :all
:namespaces :all
:exclude-vars #"^(map)?->\p{Upper}"
:metadata {}
:themes [:default]})
(let [root-path (System/getProperty "user.dir")]
{:language :clojure
:root-path root-path
:output-path "target/doc"
:source-paths ["src"]
:doc-paths ["doc"]
:doc-files :all
:namespaces :all
:exclude-vars #"^(map)?->\p{Upper}"
:metadata {}
:themes [:default]
:git-commit-id (git-commit-id root-path)}))

(defn generate-docs
"Generate documentation from source files."
Expand Down
13 changes: 7 additions & 6 deletions codox/src/codox/writer/html.clj
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,17 @@
(second (re-find #"/([^/]+?)$" path)))

(defn- var-source-uri
[{:keys [source-uri version]}
[{:keys [source-uri version git-commit-id]}
{:keys [path file line]}]
(let [path (util/uri-path path)
uri (if (map? source-uri) (get-source-uri source-uri path) source-uri)]
(-> uri
(str/replace "{filepath}" path)
(str/replace "{classpath}" (util/uri-path file))
(str/replace "{basename}" (uri-basename path))
(str/replace "{line}" (str line))
(str/replace "{version}" version))))
(str/replace "{filepath}" path)
(str/replace "{classpath}" (util/uri-path file))
(str/replace "{basename}" (uri-basename path))
(str/replace "{line}" (str line))
(str/replace "{version}" version)
(str/replace "{git-commit-id}" git-commit-id))))

(defn- split-ns [namespace]
(str/split (str namespace) #"\."))
Expand Down
2 changes: 1 addition & 1 deletion example/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
:doc-files ["doc/intro.md"
"doc/formatting.md"]
:source-uri
"https://github.com/weavejester/codox/blob/{version}/codox.example/{filepath}#L{basename}-{line}"
"https://github.com/weavejester/codox/blob/{git-commit-id}/example/{filepath}#L{basename}-{line}"
:html
{:transforms [[:head] [:prepend [:script "console.log('hello');"]]
[:head] [:append [:script "console.log('world');"]]
Expand Down

0 comments on commit 4b23ebd

Please sign in to comment.