aboutsummaryrefslogtreecommitdiff
path: root/lazybones-documentation.lisp
blob: 48e55b227b6b3296687f85f7a22016284045c7ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
;;;; lazybones-documentation.lisp -- documenting APP instances

(in-package  :lazybones)

(defun sorted-endpoints (endpoints)
  (sort (copy-seq endpoints) #'string< :key #'endpoint-route))

(defun generate-app-documentation (app)
  "For now, generates a single Markdown string that documents each endpoint in APP."
  (symbol-macrolet ((newline (progn  (princ #\newline)(princ #\newline))))
    (with-slots
          (title
           version
           endpoints
           (default-authorizer authorizer)
           default-content-type
           description
           definitions)
        app
      (with-output-to-string (*standard-output*) 
        (princ "# ") (princ title) (princ " - ") (princ "v") (princ version) 
        newline
        (princ description)
        newline
        (princ "## Endpoints")
        (dolist (ep (sorted-endpoints endpoints))
          (with-slots (method content-type route authorizer params endpoint-documentation) ep
            newline 
            (princ "### ") (princ method) (princ " ") (princ (make-route-presentable route))
            (terpri)
            (princ "*")
            (princ (if content-type content-type default-content-type ))
            (princ "*")
            (when authorizer 
              newline
              (princ "Authorization Required: ")
              newline
              (cond ((function-or-function-name-p authorizer)
                     (princ (ensure-blockquote (documentation authorizer 'function))))
                    ((function-or-function-name-p default-authorizer)
                     (princ (ensure-blockquote (documentation default-authorizer 'function)))))
              newline)
            (a:when-let (vars (endpoint-route-vars ep)) 
              newline
              (princ "Route Variables: ") newline
              (dolist (var vars)
                (princ "- ") (princ var)
                (a:when-let (val-parser (route-var-value-parser ep var))
                  (princ ": ") (princ (strip-newlines (documentation val-parser 'function))))
                (princ #\newline)))
            (when params
              newline
              (princ "Documented Query Parameters: ") newline
              (loop for (var parser) in params
                    do (princ "- ") (princ (string-downcase (symbol-name var)))
                       (princ ": ") (princ (strip-newlines (documentation parser 'function)))
                       (princ #\newline)))
            newline
            (princ endpoint-documentation)))
        newline
        (when (plusp (hash-table-count definitions)) 
          (princ "## Definitions") newline
          (loop for name being the hash-key of definitions
                for (node-id . text) being the hash-value of definitions
                do (format *standard-output*
                           "<h3 id='~a'>~a</h3>"
                           node-id name)
                   (princ #\newline) (princ #\newline)
                   (princ text)
                   (princ #\newline) (princ #\newline)))))))

(defun ensure-blockquote (string)
  (concatenate 'string "> "
               (str:replace-all
                '(#\newline)
                "
> "
                string)))

(defun strip-newlines (string)
  (str:replace-all '(#\newline) "" string))

(defun function-or-function-name-p (thing)
  (or (functionp thing)
      (and (symbolp thing) (fboundp thing))))

(defun endpoint-route-vars (ep)
  "return a list of route variables for endpoint EP"
  (mapcar 'first (remove-if-not #'consp  (endpoint-dispatch-pattern ep))))

(defun route-var-value-parser (ep var)
  (second (assoc var (remove-if-not #'consp (endpoint-dispatch-pattern ep)))))

(defun make-route-presentable (routestring)
  (ppcre:regex-replace-all " [a-z0-9A-Z\-]+:" routestring ":"))