Browse code

Refactor some common elements of spell and item panels

Joseph Weston authored on 05/12/2021 03:37:34
Showing 1 changed files
... ...
@@ -68,6 +68,14 @@
68 68
         [:section.license-notice
69 69
          (spaced (x :name) "is" (l :content-kind) "subject to the" (l :license-name))])))
70 70
 
71
+(defn- description
72
+    [x]
73
+    (some->> x :desc (map #(vector :p %)) (into [:section.description])))
74
+
75
+(defn- heading
76
+    [x]
77
+    [:h1 (x :name)])
78
+
71 79
 (defn- pounds [w]
72 80
     (if (= w 1)
73 81
       (spaced w "lb")
... ...
@@ -81,6 +89,12 @@
81 89
     (when (x :speed)
82 90
         (spaced (-> x :speed :quantity) (-> x :speed :unit))))
83 91
 
92
+(defn- weight [x]
93
+    (when (x :weight)
94
+     (spaced
95
+      (-> x :weight pounds)
96
+      (some->> x :quantity (str "for ") parenthesize))))
97
+
84 98
 (defn license-panel [license]
85 99
     [:article {:class (styles/license-panel)}
86 100
      [:h1 (license :license-name)]
... ...
@@ -90,15 +104,15 @@
90 104
 
91 105
 (defn spell-panel [spell]
92 106
       [:article {:class (styles/spell-panel)}
93
-       [:h1 (spell :name)]
107
+       (heading spell)
94 108
        [:div
95 109
         [:span.kind
96 110
          (spaced
97 111
           (-> spell :level ordinal)
98 112
           "level"
99 113
           (-> spell :school :name string/lower-case)
100
-          "spell")]
101
-        (property-list
114
+          "spell")]]
115
+       (property-list
102 116
         ; Refactor these so that we get the "name" from the keyword by replacing
103 117
         ; '-' and '_' and capitalizing
104 118
          :Damage
... ...
@@ -117,8 +131,8 @@
117 131
          :Duration
118 132
            (spaced
119 133
             (-> spell :duration string/lower-case)
120
-            (when (spell :concentration) "(requires concentration)")))]
121
-       [:section.description (->> spell :desc (map #(vector :p %)))]
134
+            (when (spell :concentration) "(requires concentration)")))
135
+       (description spell)
122 136
        (let [at-higher-levels (some-> spell :higher_level join-lines)]
123 137
         (when at-higher-levels
124 138
            [:section.higher-level
... ...
@@ -128,7 +142,7 @@
128 142
 
129 143
 (defn equipment-panel [item]
130 144
     [:article {:class (styles/equipment-panel)}
131
-     [:h1 (item :name)]
145
+     (heading item)
132 146
      [:span.kind
133 147
       (spaced
134 148
        (-> item :equipment_category :name)
... ...
@@ -139,24 +153,20 @@
139 153
       :Speed
140 154
         (speed item)
141 155
       :Weight
142
-        (when (item :weight)
143
-         (spaced
144
-          (-> item :weight pounds)
145
-          (some->> item :quantity (str "for ") parenthesize)))
156
+        (weight item)
146 157
       :Cost
147 158
         (spaced
148 159
          (cost item)
149 160
          (some->> item :quantity (str "for ") parenthesize)))
150
-     (when (item :desc)
151
-         [:section.description (->> item :desc (map #(vector :p %)))])
161
+     (description item)
152 162
      (license-notice item)])
153 163
 
154 164
 (defn weapon-panel [weapon]
155 165
  (let [weapon-kind (-> weapon :weapon_range string/lower-case)
156 166
        weapon-category (-> weapon :weapon_category string/lower-case)]
157 167
   [:article {:class (styles/weapon-panel)}
158
-    [:h1 (weapon :name)]
159
-    [:span.kind (string/join " " [weapon-category weapon-kind "weapon"])]
168
+    (heading weapon)
169
+    [:span.kind (spaced weapon-category weapon-kind "weapon")]
160 170
     (property-list
161 171
      ; 'two-handed-damage' is defined for *single-handed* weapons with the "versatile"
162 172
      ; property. Actual two-handed weapons have 'damage' only.
... ...
@@ -171,9 +181,9 @@
171 181
        (when (= weapon-kind "ranged")
172 182
          (gstr/format "%d' (%d')" (-> weapon :range :normal) (-> weapon :range :long)))
173 183
      :Properties
174
-       (comma-separated (->> weapon :properties (map :name) (map string/lower-case)))
184
+       (some->> weapon :properties (map :name) (map string/lower-case) comma-separated)
175 185
      :Weight
176
-       (-> weapon :weight pounds)
186
+       (weight weapon)
177 187
      :Cost
178 188
        (cost weapon))
179 189
     (license-notice weapon)]))