Skip to content

Commit

Permalink
Updated after code review.
Browse files Browse the repository at this point in the history
- Use ensure-list from uiop to reduce number of utility functions.
- Fixed documentation string.
  • Loading branch information
woudshoo committed Jan 22, 2024
1 parent b7903f5 commit ab5a71f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
24 changes: 1 addition & 23 deletions src/utils/list.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
#:ninsert
#:find-all
#:remove-keyword-parameter
#:remove-keyword-parameters
#:safe-first
#:safe-rest))
#:remove-keyword-parameters))
(in-package #:reblocks/utils/list)


Expand All @@ -31,26 +29,6 @@
(setf end length))
(subseq sequence start end)))

(defun safe-first (item)
"Returns first element of ITEM.
There are two cases:
- ITEM is a list, in which case it returns (first ITEM)
- ITEM is not a list, in which case it retuns ITEM.
The effect is the same as (first (alexandria:ensure-list item))"
(typecase item
(list (first item))
(t item)))

(defun safe-rest (item)
"Returns rest of ITEM
There are two cases:
- ITEM is a list, in which case it returns (rest ITEM)
- ITEM is not a list, in whch case it returns nil.
The effect is the same as (rest (alexandria:ensure-list item))"
(typecase item
(list (rest item))
(t nil)))

;; TODO: replace with alexandria:alist-plist
(defun alist->plist (alist)
"Converts an alist to plist."
Expand Down
21 changes: 12 additions & 9 deletions src/widget.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,29 @@ inherits from REBLOCKS/WIDGET:WIDGET if no DIRECT-SUPERCLASSES are provided."
You can use any other templating engine, just ensure
it writes output to REBLOCKS/HTML:*STREAM*
Outer DIV wrapper will be added automaticall. It will
have CSS tags returned by GET-CSS-CLASSES."))
Outer DIV wrapper will be added automaticall, see GET-HTML-TAG.
It will have CSS tags returned by GET-CSS-CLASSES."))


(defgeneric get-html-tag (widget)
(:documentation "This method determines the enclosing tag of the widget.
The return value should either be a keyword like :div, which will be the enclosing tag,
or a list of the form (:tag . attributes), where :tag is the enclosing tag (like :div)
and attributes is a property list, where the keys are keywords, corresponding to the attribute name
and the values are the values of the attribute.
The return value should either be a keyword like :div,
which will be the enclosing tag, or a list of the form (:tag . attributes),
where :tag is the enclosing tag (like :div) and attributes is a property list.
The attributes property list has keywords for keys, corresponding to
the attribute name and the values are the values of the attribute.
For example:
- :div -- generates a <div ...> WIDGET CONTENT </div>
- (:div :display \"flex\") -- generates (<div ... :display \"flex\">WIDGET CONTENT</div>
- :div -- generates <div ...> widget content </div>
- (:div :display \"flex\") -- generates (<div ... :display \"flex\">widget content</div>
NOTE on attributes, in the attribute list the following attributes can
not be specified, they will be ignored:
- :class -- Use the get-css-classes method to specify these
- :class -- Use the GET-CSS-CLASSES method to specify these
- :id -- This is the value of the dom-id slot of the widget,
normally automatically managed by reblocks.
Expand Down
23 changes: 12 additions & 11 deletions src/widgets/render-methods.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
#:with-html)
(:import-from #:reblocks/widgets/dom
#:dom-id)
(:import-from #:reblocks/utils/list
#:safe-first
#:safe-rest))
(:import-from #:uiop
#:ensure-list))
(in-package #:reblocks/widgets/render-methods)


Expand All @@ -44,13 +43,15 @@
(push-dependencies widget-dependencies))

(register-widget widget)

(with-html
(:tag
:name (safe-first (get-html-tag widget))
:class (get-css-classes-as-string widget)
:id (dom-id widget)
:attrs (safe-rest (get-html-tag widget))
(call-next-method))))

(destructuring-bind (tag-name . attributes)
(ensure-list (get-html-tag widget))
(with-html
(:tag
:name tag-name
:class (get-css-classes-as-string widget)
:id (dom-id widget)
:attrs attributes
(call-next-method)))))


0 comments on commit ab5a71f

Please sign in to comment.