Add docstrings; Fix names

This commit is contained in:
Marcus Kammer 2024-08-08 21:29:38 +02:00
parent 7f9b14b3c7
commit 2673da3566
Signed by: marcuskammer
GPG key ID: C374817BE285268F

View file

@ -5,11 +5,26 @@
(in-package :ml-sbt/tbl) (in-package :ml-sbt/tbl)
(defun format-cell-content (content) (defun format-cell-content (content)
"Format cell content for HTML table display.
If CONTENT is a number, format it to 9 decimal places.
Otherwise, return CONTENT as-is (Spinneret will handle escaping).
- CONTENT: The cell content (can be a number or any other type)
Returns:
- If CONTENT is a number, a string representation with 9 decimal places.
- Otherwise, the original CONTENT."
(if (numberp content) (if (numberp content)
(format nil "~,9f" content) (format nil "~,9f" content)
content)) content))
(defun render-tbl-header (headers) (defun render-tbl-header (headers)
"Render the header of an HTML table using Spinneret.
- HEADERS: A list of header cell contents
Example:
(render-tbl-header '(\"Name\" \"Age\" \"City\"))"
(spinneret:with-html (spinneret:with-html
(:thead (:thead
(:tr (:tr
@ -17,15 +32,31 @@
do (:th :scope "col" (format-cell-content header))))))) do (:th :scope "col" (format-cell-content header)))))))
(defun render-tbl-body (rows) (defun render-tbl-body (rows)
"Render the body of an HTML table using Spinneret.
- ROWS: A list of rows, where each row is a list of cell contents
Example:
(render-tbl-body '((\"John\" 30 \"New York\") (\"Jane\" 25 \"San Francisco\")))"
(spinneret:with-html (spinneret:with-html
(:tbody (:tbody
(loop for row in rows (loop for row in rows
do (:tr do (:tr
(loop for cell in row (loop for cell in row
do (:td (escape-cell-content cell)))))))) do (:td (format-cell-content cell))))))))
(defun render-tbl (headers rows &key (class "table")) (defun render-tbl (headers rows &key (class "table"))
"Render a complete HTML table using Spinneret.
- HEADERS: A list of header cell contents
- ROWS: A list of rows, where each row is a list of cell contents
- CLASS: CSS class for the table (default: \"table\")
Example:
(render-tbl '(\"Name\" \"Age\" \"City\")
'((\"John\" 30 \"New York\") (\"Jane\" 25 \"San Francisco\"))
:class \"table table-striped\")"
(spinneret:with-html (spinneret:with-html
(:table :class class (:table :class class
(render-table-header headers) (render-tbl-header headers)
(render-table-body rows)))) (render-tbl-body rows))))