summaryrefslogtreecommitdiff
path: root/pages.lisp
blob: 5d2b41d21d63b8b3603432332571909d47dacfd1 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
;;;; pages.lisp -- html generation functions for dnd

(in-package :dnd)

(defmacro with-page ((&key title) &body body)
  `(with-html-string
     (:doctype)
     (:html
      (:head
       (:title ,title))
      (:body ,@body))))

(defun godess-shrine ()
  (with-page (:title "A Sacred Shrine")
    (:header
     (:h1 "Pray and become a hero..."))
    (:form :method "POST" :action "/godess-shrine"
	   (:label :for "NAME" "Enter the epithet by which the ages shall know thy hero:")
	   (:input :name "NAME")
	   (:button :type "submit" "Pray To The Goddess"))))

(defun doorkeeper (&key (message "Come ye player, Wot's yer name?"))
  (with-page (:title "Tavern Door")
    (:h1 message)
    (:form :method "POST" :action "/tavern-door"
	   (:label :for "NICK" "Wut's yer handle?:")
	   (:input :name "NICK")
	   (:button :type "submit" "Announce Thyself"))
    (:h2 "Eh? Ye need to announce thyeself?")
    (:a :href "/register" "Follow me...")))

(defun register ()
  (with-page (:title "Register Player")
    (:header
     (:h1 "Choose a Nickname Player"))
    (:form :method "POST" :action "/register"
           (:label  :for "NICK" "Choose a nickname. No spaces. Letters, Numbers, and -._")
           (:input :name "NICK" :placeholder "superbob")
           (:button :type "submit" "Register"))))

(defun tavern (player)
  (with-page (:title "A Bustling Tavern")
    (navbar)
    (:div
     :class "heroes-container"
     (:h2 "Heroes of rampant renown:")
     (hall-of-heroes))))

(defun navbar ()
  (with-html
      (:nav :class "navbar" :aria-label "Navigation"
	    (:div :class "logo" :aria-label "DND logo" "DND")
	    (:ul :class "nav-links" :aria-label "Nav links"
	         (:li (:a :href "/hero" :aria-label "Hero profile" "🧝"))
	         (:li (:a :href "/inventory" :aria-label "Inventory" "🎒"))
	         (:li (:a :href "/quests" :aria-label "Quests" "📜"))
	         (:li (:a :href "/tavern" :aria-label "Tavern" "🍺"))))))

(defun hall-of-heroes ()
  (with-html
      (:ul :class "hall-of-heroes"
           (dolist (hero (all-heroes))
	     (:li (hero-name hero)
                  "the"
                  (hero-class hero)
                  (hero-title hero))))))

(defgeneric render (element)
  (:documentation "Render an element as HTML"))

(defclass page ()
  ((title
    :reader title
    :initarg :title
    :documentation "Page title ")
   (main
    :reader main
    :initarg :main
    :initform ""
    :documentation "Main content for a page.")))

(defmethod render ((doc page))
  (with-html
    (:doctype)
    (:html
     (:head (:title (title doc)))
     (:body
      (main doc)))))

(defclass navable ()
  ((nav-links
    :reader nav-links
    :initarg :nav-links
    :type list))
  (:documentation "Mixin for a PAGE class with navigation."))

(defclass nav-link ()
  ((target :initarg :target)
   (text :initarg :text)
   (icon :initarg :icon :initform nil)
   (label :initarg :label :initform nil)))

(defmethod render ((element nav-link))
  (with-slots (target icon label text) element
    (with-html
      (:li (:a :href target :aria-label label icon text)))))

(defclass page-with-nav (page navable)
  ())

(defmethod render ((doc page-with-nav))
  (with-html
    (:doctype)
    (:html
     (:head (:title (title doc)))
     (:body
      (:nav (:ul (mapc #'render (nav-links doc))))
      (:main (main doc))))))

;; (let* ((link (make-instance
;; 	     'nav-link :target "/moo" :label "goo" :icon "🍺" :text "Tavern"))
;;        (foo (make-instance
;; 	     'page-with-nav :title "goober" :main "Content" :nav-links (list link link))))
;;   (render foo))