From 556b633bcfcfd6ced4208b92006ae3897f9e6bbc Mon Sep 17 00:00:00 2001 From: dm3 Date: Fri, 29 Jul 2011 03:04:42 +0800 Subject: [PATCH] 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). --- src/marginalia/core.clj | 18 +++------- src/marginalia/html.clj | 59 ++++++++++++++++++++++++------- test/marginalia/test/multidoc.clj | 11 +++--- 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/src/marginalia/core.clj b/src/marginalia/core.clj index c59e3ca6..191b9e6e 100644 --- a/src/marginalia/core.clj +++ b/src/marginalia/core.clj @@ -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)] @@ -168,25 +168,17 @@ ;; ## Ouput Generation -(defn index-html - [props files] - "") - -(defn single-page-html - [file] - (str "" file "")) - (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))))) diff --git a/src/marginalia/html.clj b/src/marginalia/html.clj index bb94be20..bbabd3df 100644 --- a/src/marginalia/html.clj +++ b/src/marginalia/html.clj @@ -26,7 +26,7 @@ (defn slurp-resource "Stolen from leiningen" [resource-name] - (try + (try (-> (.getContextClassLoader (Thread/currentThread)) (.getResourceAsStream resource-name) (java.io.InputStreamReader.) @@ -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!\" -> `\"

header!

\"` @@ -87,7 +87,7 @@ outlined above. ex. (docs-to-html [{:doc-text \"# hello world!\"} {:docstring-text \"I'm a docstring!}]) - + -> `\"

hello world!


\"` " [docs] @@ -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 @@ -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"} " "]])) @@ -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 @@ -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))) diff --git a/test/marginalia/test/multidoc.clj b/test/marginalia/test/multidoc.clj index 5566c4f5..00be4e98 100644 --- a/test/marginalia/test/multidoc.clj +++ b/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"]] @@ -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)))