Skip to content

Commit

Permalink
Generate content for $namespace.html and toc.html
Browse files Browse the repository at this point in the history
`multidoc!` generates a `toc.html` file containing the toc (with links)
and project info, and a bunch of `$namespace.html` files (one for each
.clj file).
  • Loading branch information
dm3 authored and fogus committed Sep 7, 2011
1 parent b76df81 commit 556b633
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
18 changes: 5 additions & 13 deletions src/marginalia/core.clj
Expand Up @@ -35,7 +35,7 @@
(:require [clojure.java.io :as io]
[clojure.string :as str])
(:use [marginalia
[html :only (uberdoc-html)]
[html :only (uberdoc-html index-html single-page-html)]
[parser :only (parse-file)]]
[clojure.contrib
[find-namespaces :only (read-file-ns-decl)]
Expand Down Expand Up @@ -168,25 +168,17 @@

;; ## Ouput Generation

(defn index-html
[props files]
"<html></html>")

(defn single-page-html
[file]
(str "<html><body>" file "</body></html>"))

(defn filename-contents
[output-dir parsed-file]
[props output-dir all-files parsed-file]
{:name (io/file output-dir (str (:ns parsed-file) ".html"))
:contents (single-page-html parsed-file)})
:contents (single-page-html props parsed-file all-files)})

(defn multidoc!
[output-dir files-to-analyze props]
(let [parsed-files (map path-to-doc files-to-analyze)
index (index-html props parsed-files)
pages (map #(filename-contents output-dir %) parsed-files)]
(doseq [f (conj pages {:name (io/file output-dir "index.html")
pages (map #(filename-contents props output-dir parsed-files %) parsed-files)]
(doseq [f (conj pages {:name (io/file output-dir "toc.html")
:contents index})]
(spit (:name f) (:contents f)))))

Expand Down
59 changes: 47 additions & 12 deletions src/marginalia/html.clj
Expand Up @@ -26,7 +26,7 @@
(defn slurp-resource
"Stolen from leiningen"
[resource-name]
(try
(try
(-> (.getContextClassLoader (Thread/currentThread))
(.getResourceAsStream resource-name)
(java.io.InputStreamReader.)
Expand Down Expand Up @@ -62,7 +62,7 @@
;; Markdown processor.
(def mdp (com.petebevin.markdown.MarkdownProcessor.))

(defn md
(defn md
"Markdown string to html converter. Translates strings like:
\"# header!\" -> `\"<h1>header!</h1>\"`
Expand All @@ -87,7 +87,7 @@
outlined above.
ex. (docs-to-html [{:doc-text \"# hello world!\"} {:docstring-text \"I'm a docstring!}])
-> `\"<h1>hello world!</h1><br />\"`
"
[docs]
Expand Down Expand Up @@ -145,7 +145,7 @@
(defn opt-resources-html
"Generate script and link tags for optional external javascript and css."
[project-info]
(let [options (:marginalia project-info)
(let [options (:marginalia project-info)
javascript (:javascript options)
css (:css options)]
(html (concat
Expand Down Expand Up @@ -175,14 +175,31 @@
[:br]
"(this space intentionally left almost blank)"]]))

(defn toc-html [docs]
(defn link-to-namespace
"Creates an 'a' tag pointing to the `namespace-name`, either as an anchor (if
`anchor?` is true) or as a link to a separate `$namespace-name.html` file.
If `attrs` aren't empty, they are added to the resulting tag."
[namespace-name anchor? & attrs]
[:a (into {:href (if anchor?
(str "#" namespace-name)
(str namespace-name ".html"))}
attrs)
namespace-name])

(defn link-to-toc
"This is a hack, as in the case when `anchor?` is false, the link will contain
a reference to `toc.html` which might not even exist."
[anchor?]
(link-to-namespace "toc" anchor? {:class "toc-link"}))

(defn toc-html [props docs]
(html
[:tr
[:td {:class "docs"}
[:div {:class "toc"}
[:a {:name "toc"} [:h3 "namespaces"]]
[:ul
(map #(vector :li [:a {:href (str "#" (:ns %))} (:ns %)])
(map #(vector :li (link-to-namespace (:ns %) (:uberdoc? props)))
docs)]]]
[:td {:class "codes"} "&nbsp;"]]))

Expand All @@ -194,16 +211,15 @@
(:ns %))
docs)]])

(defn groups-html [doc]
(html
(defn groups-html [props doc]
(html
[:tr
[:td {:class "docs"}
[:div {:class "docs-header"}
[:a {:class "anchor" :name (:ns doc) :href (str "#" (:ns doc))}
[:h1 {:class "project-name"}
(:ns doc)]
[:a {:href "#toc" :class "toc-link"}
"toc"]]]]
(link-to-toc (:uberdoc? props))]]]
[:td {:class "codes"}]]
(map section-to-html (:groups doc))
[:tr
Expand Down Expand Up @@ -388,7 +404,26 @@
project-metadata
(opt-resources-html project-metadata)
(header-html project-metadata)
(toc-html docs)
(toc-html {:uberdoc? true} docs)
(floating-toc-html docs)
(map groups-html docs)))
(map #(groups-html (:uberdoc? true) %) docs)))

(defn index-html
[project-metadata docs]
(page-template
project-metadata
(opt-resources-html project-metadata)
(header-html project-metadata)
(toc-html {:uberdoc? false} docs)
"" ;; no floating toc
"")) ;; no contents

(defn single-page-html
[project-metadata doc all-docs]
(page-template
project-metadata
(opt-resources-html project-metadata)
"" ;; no header
"" ;; no toc
(floating-toc-html all-docs)
(groups-html (:uberdoc? false) doc)))
11 changes: 6 additions & 5 deletions test/marginalia/test/multidoc.clj
@@ -1,17 +1,18 @@
(ns marginalia.test.multidoc
(:require marginalia.core)
(:use clojure.test)
(:use [clojure.java.io :only (file delete-file)]))
(:use [clojure.java.io :only (file)])
(:use [clojure.contrib.java-utils :only (delete-file-recursively)]))

(def multi-page-project (file "test_projects" "multi_page"))
(def test-project-src (file multi-page-project "src"))
(def test-project-target (file multi-page-project "docs"))

(use-fixtures :each (fn [f]
(delete-file test-project-target true)
(delete-file-recursively test-project-target true)
(.mkdirs test-project-target)
(f)
(delete-file test-project-target true)))
(f)))
;;(delete-file-recursively test-project-target true)))

(def test-metadata {
:dependencies [["some/dep" "0.0.1"]]
Expand All @@ -22,7 +23,7 @@
})

(defn run-marginalia [source-dir output-dir]
(binding [marginalia.html/*resources* "resources"]
(binding [marginalia.html/*resources* ""]
(marginalia.core/multidoc! output-dir
(marginalia.core/find-clojure-file-paths source-dir)
test-metadata)))
Expand Down

0 comments on commit 556b633

Please sign in to comment.