Generate content for $namespace.html and toc.html

`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).
This commit is contained in:
dm3 2011-07-29 03:04:42 +08:00 committed by fogus
parent b76df81683
commit 556b633bcf
3 changed files with 58 additions and 30 deletions

View file

@ -35,7 +35,7 @@
(:require [clojure.java.io :as io] (:require [clojure.java.io :as io]
[clojure.string :as str]) [clojure.string :as str])
(:use [marginalia (:use [marginalia
[html :only (uberdoc-html)] [html :only (uberdoc-html index-html single-page-html)]
[parser :only (parse-file)]] [parser :only (parse-file)]]
[clojure.contrib [clojure.contrib
[find-namespaces :only (read-file-ns-decl)] [find-namespaces :only (read-file-ns-decl)]
@ -168,25 +168,17 @@
;; ## Ouput Generation ;; ## Ouput Generation
(defn index-html
[props files]
"<html></html>")
(defn single-page-html
[file]
(str "<html><body>" file "</body></html>"))
(defn filename-contents (defn filename-contents
[output-dir parsed-file] [props output-dir all-files parsed-file]
{:name (io/file output-dir (str (:ns parsed-file) ".html")) {: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! (defn multidoc!
[output-dir files-to-analyze props] [output-dir files-to-analyze props]
(let [parsed-files (map path-to-doc files-to-analyze) (let [parsed-files (map path-to-doc files-to-analyze)
index (index-html props parsed-files) index (index-html props parsed-files)
pages (map #(filename-contents output-dir %) parsed-files)] pages (map #(filename-contents props output-dir parsed-files %) parsed-files)]
(doseq [f (conj pages {:name (io/file output-dir "index.html") (doseq [f (conj pages {:name (io/file output-dir "toc.html")
:contents index})] :contents index})]
(spit (:name f) (:contents f))))) (spit (:name f) (:contents f)))))

View file

@ -26,7 +26,7 @@
(defn slurp-resource (defn slurp-resource
"Stolen from leiningen" "Stolen from leiningen"
[resource-name] [resource-name]
(try (try
(-> (.getContextClassLoader (Thread/currentThread)) (-> (.getContextClassLoader (Thread/currentThread))
(.getResourceAsStream resource-name) (.getResourceAsStream resource-name)
(java.io.InputStreamReader.) (java.io.InputStreamReader.)
@ -62,7 +62,7 @@
;; Markdown processor. ;; Markdown processor.
(def mdp (com.petebevin.markdown.MarkdownProcessor.)) (def mdp (com.petebevin.markdown.MarkdownProcessor.))
(defn md (defn md
"Markdown string to html converter. Translates strings like: "Markdown string to html converter. Translates strings like:
\"# header!\" -> `\"<h1>header!</h1>\"` \"# header!\" -> `\"<h1>header!</h1>\"`
@ -87,7 +87,7 @@
outlined above. outlined above.
ex. (docs-to-html [{:doc-text \"# hello world!\"} {:docstring-text \"I'm a docstring!}]) ex. (docs-to-html [{:doc-text \"# hello world!\"} {:docstring-text \"I'm a docstring!}])
-> `\"<h1>hello world!</h1><br />\"` -> `\"<h1>hello world!</h1><br />\"`
" "
[docs] [docs]
@ -145,7 +145,7 @@
(defn opt-resources-html (defn opt-resources-html
"Generate script and link tags for optional external javascript and css." "Generate script and link tags for optional external javascript and css."
[project-info] [project-info]
(let [options (:marginalia project-info) (let [options (:marginalia project-info)
javascript (:javascript options) javascript (:javascript options)
css (:css options)] css (:css options)]
(html (concat (html (concat
@ -175,14 +175,31 @@
[:br] [:br]
"(this space intentionally left almost blank)"]])) "(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 (html
[:tr [:tr
[:td {:class "docs"} [:td {:class "docs"}
[:div {:class "toc"} [:div {:class "toc"}
[:a {:name "toc"} [:h3 "namespaces"]] [:a {:name "toc"} [:h3 "namespaces"]]
[:ul [:ul
(map #(vector :li [:a {:href (str "#" (:ns %))} (:ns %)]) (map #(vector :li (link-to-namespace (:ns %) (:uberdoc? props)))
docs)]]] docs)]]]
[:td {:class "codes"} "&nbsp;"]])) [:td {:class "codes"} "&nbsp;"]]))
@ -194,16 +211,15 @@
(:ns %)) (:ns %))
docs)]]) docs)]])
(defn groups-html [doc] (defn groups-html [props doc]
(html (html
[:tr [:tr
[:td {:class "docs"} [:td {:class "docs"}
[:div {:class "docs-header"} [:div {:class "docs-header"}
[:a {:class "anchor" :name (:ns doc) :href (str "#" (:ns doc))} [:a {:class "anchor" :name (:ns doc) :href (str "#" (:ns doc))}
[:h1 {:class "project-name"} [:h1 {:class "project-name"}
(:ns doc)] (:ns doc)]
[:a {:href "#toc" :class "toc-link"} (link-to-toc (:uberdoc? props))]]]
"toc"]]]]
[:td {:class "codes"}]] [:td {:class "codes"}]]
(map section-to-html (:groups doc)) (map section-to-html (:groups doc))
[:tr [:tr
@ -388,7 +404,26 @@
project-metadata project-metadata
(opt-resources-html project-metadata) (opt-resources-html project-metadata)
(header-html project-metadata) (header-html project-metadata)
(toc-html docs) (toc-html {:uberdoc? true} docs)
(floating-toc-html 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)))

View file

@ -1,17 +1,18 @@
(ns marginalia.test.multidoc (ns marginalia.test.multidoc
(:require marginalia.core) (:require marginalia.core)
(:use clojure.test) (: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 multi-page-project (file "test_projects" "multi_page"))
(def test-project-src (file multi-page-project "src")) (def test-project-src (file multi-page-project "src"))
(def test-project-target (file multi-page-project "docs")) (def test-project-target (file multi-page-project "docs"))
(use-fixtures :each (fn [f] (use-fixtures :each (fn [f]
(delete-file test-project-target true) (delete-file-recursively test-project-target true)
(.mkdirs test-project-target) (.mkdirs test-project-target)
(f) (f)))
(delete-file test-project-target true))) ;;(delete-file-recursively test-project-target true)))
(def test-metadata { (def test-metadata {
:dependencies [["some/dep" "0.0.1"]] :dependencies [["some/dep" "0.0.1"]]
@ -22,7 +23,7 @@
}) })
(defn run-marginalia [source-dir output-dir] (defn run-marginalia [source-dir output-dir]
(binding [marginalia.html/*resources* "resources"] (binding [marginalia.html/*resources* ""]
(marginalia.core/multidoc! output-dir (marginalia.core/multidoc! output-dir
(marginalia.core/find-clojure-file-paths source-dir) (marginalia.core/find-clojure-file-paths source-dir)
test-metadata))) test-metadata)))