PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/joodoweb/view/tutorial/reference.hiccup.clj

http://github.com/slagyr/joodoweb
Clojure | 182 lines | 158 code | 24 blank | 0 comment | 2 complexity | c1f21d65eae6b9234dfc9d0aeb3c4f61 MD5 | raw file
  1. [:h3 "All Done"]
  2. [:p "That's all there is to creating our sample application. We hope this was enough to get you familiar with the Joodo framework. There are a lot of features that we didn't cover. To get more familair with other parts of Joodo, review our " [:a {:href "/docs/index"} "API Documentation"] " section. If you have any ideas for how this site or Joodo can be better, join our " [:a {:href "/community"} "community"] " and let us know."]
  3. [:p "Just for your reference, here are all of the files that we outlined throughout this tutorial:"]
  4. [:h4 "src/sample_app/view/layout.hiccup.clj"]
  5. [:pre {:class "brush: clojure"}
  6. "(doctype :html5)
  7. [:html
  8. [:head
  9. [:meta {:http-equiv \"Content-Type\" :content \"text/html\" :charset \"iso-8859-1\"}]
  10. [:title \"sample_app\"]
  11. (include-css \"/stylesheets/sample_app.css\")
  12. (include-js \"/javascript/sample_app.js\")]
  13. [:body
  14. [:img {:src \"/images/logo.png\"}]
  15. (eval (:template-body joodo.views/*view-context*))
  16. ]]"]
  17. [:h4 "public/images/logo.png"]
  18. [:img {:src "/images/logo.png"}]
  19. [:h4 "spec/sample_app/view/test_posts/20111215_test-post.hiccup.clj"]
  20. [:pre {:class "brush: clojure"}
  21. "[:h1 \"Test Post\"]
  22. [:p \"This is my test post.\"]"]
  23. [:h4 "spec/sample_app/controller/post_controller_spec.clj"]
  24. [:pre {:class "brush: clojure"}
  25. "(ns sample_app.controller.post-controller-spec
  26. (:use
  27. [speclj.core
  28. :only (describe around it should= run-specs)]
  29. [joodo.spec-helpers.controller
  30. :only (do-get rendered-template rendered-context with-mock-rendering with-routes)]
  31. [sample_app.controller.post-controller]))
  32. (describe \"post-controller\"
  33. (with-mock-rendering)
  34. (with-routes post-controller)
  35. (around [it]
  36. (binding [blog-post-directory (clojure.java.io/file (str
  37. (. (java.io.File. \".\") getCanonicalPath)
  38. \"/spec/sample_app/view/test_posts\"))]
  39. (it)))
  40. (it \"directs to the not_found page if the blog post isn't specified\"
  41. (let [result (do-get \"/post\")]
  42. (should= 404 (:status result))
  43. (should= \"not_found\" @rendered-template)
  44. (should= \"You must specify a blog post.\" (:error @rendered-context))))
  45. (it \"directs to the blog post if the post exists\"
  46. (let [result (do-get \"/post/20111215_test-post\")]
  47. (should= 200 (:status result))
  48. (should= \"posts/20111215_test-post\" @rendered-template)))
  49. (it \"directs to the not_found page if the post doesn't exist\"
  50. (let [result (do-get \"/post/20111215_invalid\")]
  51. (should= 404 (:status result))
  52. (should= \"not_found\" @rendered-template)
  53. (should= \"Blog post does not exist.\" (:error @rendered-context))))
  54. )
  55. (run-specs)"]
  56. [:h4 "src/sample_app/controller/post_controller.clj"]
  57. [:pre {:class "brush: clojure"}
  58. "(ns sample_app.controller.post-controller
  59. (:use
  60. [compojure.core :only (GET context defroutes)]
  61. [joodo.views :only (render-template)]))
  62. (def ^{:private true} current-path
  63. (. (java.io.File. \".\") getCanonicalPath))
  64. (def blog-post-directory
  65. (clojure.java.io/file (str current-path \"/src/sample_app/view/posts\")))
  66. (defn blog-post-filenames []
  67. (map
  68. #(.getName %)
  69. (remove
  70. #(.isDirectory %)
  71. (file-seq blog-post-directory))))
  72. (defn- blog-post-exists? [post-route]
  73. (some #(= % (str post-route \".hiccup.clj\")) (blog-post-filenames)))
  74. (defn- render-not-found [error-msg]
  75. {:status 404
  76. :headers {}
  77. :body (render-template \"not_found\" :error error-msg)})
  78. (defn- render-post [post-route]
  79. (if (blog-post-exists? post-route)
  80. (render-template (str \"posts/\" post-route))
  81. (render-not-found \"Blog post does not exist.\")))
  82. (defroutes post-controller
  83. (GET \"/post\" [] (render-not-found \"You must specify a blog post.\"))
  84. (context \"/post\" []
  85. (GET \"/:post-route\" [post-route] (render-post post-route))))"]
  86. [:h4 "spec/sample_app/view/view_helpers_spec.clj"]
  87. [:pre {:class "brush: clojure"}
  88. "(ns sample_app.view.view-helpers-spec
  89. (:use
  90. [speclj.core :only (describe it should= run-specs around with)]
  91. [joodo.datetime :only (parse-datetime)]
  92. [sample_app.view.view-helpers]))
  93. (describe \"view_helpers\"
  94. (with test-post-1 \"20111215_test-post-1.hiccup.clj\")
  95. (with test-post-2 \"20111216_test-post-2.hiccup.clj\")
  96. (with test-post-3 \"20111217_test-post-3.hiccup.clj\")
  97. (it \"gets the title of a post\"
  98. (should= \"test post 1\" (get-post-name @test-post-1)))
  99. (it \"gets the date of a post\"
  100. (should=
  101. (parse-datetime :dense \"20111215000000\")
  102. (get-post-date @test-post-1)))
  103. (it \"orders posts from most recent to oldest\"
  104. (should= [@test-post-3 @test-post-2 @test-post-1]
  105. (order-posts [@test-post-2 @test-post-1 @test-post-3])))
  106. (it \"gets the route for a specified post\"
  107. (should= \"/post/20111215_test-post-1\"
  108. (get-post-route @test-post-1)))
  109. )
  110. (run-specs)"]
  111. [:h4 "src/sample_app/view/view_helpers.clj"]
  112. [:pre {:class "brush: clojure"}
  113. "(ns sample_app.view.view-helpers
  114. \"Put helper functions for views in this namespace.\"
  115. (:use
  116. [joodo.views :only (render-partial *view-context*)]
  117. [joodo.string :only (gsub)]
  118. [joodo.datetime :only (parse-datetime)]
  119. [hiccup.page-helpers]
  120. [hiccup.form-helpers]
  121. [sample_app.controller.post-controller :only (blog-post-filenames)]
  122. [clojure.string :as string :only (split)]))
  123. (defn- post-parts [post-file-name]
  124. (string/split post-file-name #\"(\\.)|(_)\"))
  125. (defn get-post-name [post-file-name]
  126. (gsub
  127. (second (post-parts post-file-name))
  128. #\"-\"
  129. (fn [_] \" \")))
  130. (defn get-post-date [post-file-name]
  131. (parse-datetime :dense
  132. (str (first (post-parts post-file-name)) \"000000\")))
  133. (defn order-posts [post-file-names]
  134. (sort-by
  135. #(Integer/parseInt (first (post-parts %)))
  136. >
  137. post-file-names))
  138. (defn get-post-route [post-file-name]
  139. (let [this-posts-parts (post-parts post-file-name)
  140. date (first this-posts-parts)
  141. title (second this-posts-parts)]
  142. (str \"/post/\" date \"_\" title)))"]
  143. [:h4 "src/sample_app/view/index.hiccup.clj"]
  144. [:pre {:class "brush: clojure"}
  145. "[:h1 \"Posts\"]
  146. (for [current-post-filename (order-posts (blog-post-filenames))]
  147. (list
  148. [:a {:href (get-post-route current-post-filename)}
  149. (get-post-name current-post-filename)]
  150. [:span \" - \" (.toString (get-post-date current-post-filename))]
  151. [:br]))"]
  152. [:h4 "src/sample_app/view/not_found.hiccup.clj"]
  153. [:pre {:class "brush: clojure"}
  154. "[:h1 \"Not Found\"]
  155. (if-let [error (:error *view-context*)]
  156. [:p error])"]
  157. [:br][:br]
  158. [:a {:href "/tutorial/views"} "<-- Previous Step"]