Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Docstring-able defpartial #94

Merged
merged 5 commits into from

2 participants

@pandeiro

I found myself wanting to be able to document certain template functions; this patch uses clojure.tools.macro/name-with-attributes to parse and associate optional docstrings/metadata.

src/noir/core.clj
@@ -100,11 +101,14 @@
(swap! noir-routes assoc ~(keyword fn-name) (~action ~url {params# :params} (~fn-name params#))))))
(defmacro defpartial
- "Create a function that returns html using hiccup. The function is callable with the given name."
- [fname params & body]
- `(defn ~fname ~params
- (html
- ~@body)))
+ "Create a function that returns html using hiccup. The function is callable with the given name. Can optionally include a docstring or metadata map, like a normal function declaration."
+ [fname & args]
+ (let [[fname args] (macro/name-with-attributes fname args)
+ params (first args)
@Raynes Owner
Raynes added a note

I would replace this line and the next with destructuring:

(let [[fname args] ..
      [params &  body] args]
  ..)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Raynes Raynes commented on the diff
src/noir/core.clj
@@ -2,7 +2,8 @@
"Functions to work with partials and pages."
(:use hiccup.core
compojure.core)
- (:require [clojure.string :as string]))
+ (:require [clojure.string :as string]
+ [clojure.tools.macro :as macro]))
@Raynes Owner
Raynes added a note

We need to add an explicit dependency on clojure.tools.macro. It isn't in the project.clj, and I don't even know where it is coming from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Raynes Raynes merged commit eb36d99 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 7 deletions.
  1. +2 −1  project.clj
  2. +9 −6 src/noir/core.clj
View
3  project.clj
@@ -10,4 +10,5 @@
[ring "1.0.2"]
[hiccup "1.0.0-beta1"]
[clj-stacktrace "0.2.4"]
- [org.mindrot/jbcrypt "0.3m"]])
+ [org.mindrot/jbcrypt "0.3m"]
+ [org.clojure/tools.macro "0.1.1"]])
View
15 src/noir/core.clj
@@ -2,7 +2,8 @@
"Functions to work with partials and pages."
(:use hiccup.core
compojure.core)
- (:require [clojure.string :as string]))
+ (:require [clojure.string :as string]
+ [clojure.tools.macro :as macro]))
@Raynes Owner
Raynes added a note

We need to add an explicit dependency on clojure.tools.macro. It isn't in the project.clj, and I don't even know where it is coming from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
(defonce noir-routes (atom {}))
(defonce route-funcs (atom {}))
@@ -100,11 +101,13 @@
(swap! noir-routes assoc ~(keyword fn-name) (~action ~url {params# :params} (~fn-name params#))))))
(defmacro defpartial
- "Create a function that returns html using hiccup. The function is callable with the given name."
- [fname params & body]
- `(defn ~fname ~params
- (html
- ~@body)))
+ "Create a function that returns html using hiccup. The function is callable with the given name. Can optionally include a docstring or metadata map, like a normal function declaration."
+ [fname & args]
+ (let [[fname args] (macro/name-with-attributes fname args)
+ [params & body] args]
+ `(defn ~fname ~params
+ (html
+ ~@body))))
(defn ^{:skip-wiki true} route-arguments
"returns the list of route arguments in a route"
Something went wrong with that request. Please try again.