Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not indent leaf tag. #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
:*html-no-indent-tags*
:*html-empty-tags*
:*html-empty-tag-aware-p*
:*short-leaf-content-length*
:conc
:convert-attributes
:convert-tag-to-string-list
Expand Down
3 changes: 3 additions & 0 deletions specials.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ in *HTML-EMPTY-TAGS* as <tag/> \(XHTML mode) or <tag> \(SGML
mode and HTML5 mode). For all other tags, it will always generate
<tag></tag>.")

(defvar *short-leaf-content-length* 72
"Below this threshold leaf tags won't be indented.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring is unclear in that it doesn't specify units. I'd much prefer something like "Leaf tags shorter than this many characters won't be indented."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, old stuff! Yes fine by me. Having units is better. I'm also ok with your other comments… but now I should find out how to modify the patch for this PR :-|


(defconstant +newline+ (make-string 1 :initial-element #\Newline)
"Used for indentation.")

Expand Down
62 changes: 34 additions & 28 deletions who.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,34 @@ XHTML and :HTML5 for HTML5 (HTML syntax)."
"Returns a string list corresponding to the `HTML' \(in CL-WHO
syntax) in SEXP. Uses the generic function CONVERT-TO-STRING-LIST
internally. Utility function used by TREE-TO-TEMPLATE."
(let (tag attr-list body)
(cond
((keywordp sexp)
(setq tag sexp))
((atom (first sexp))
(setq tag (first sexp))
;; collect attribute/value pairs into ATTR-LIST and tag body (if
;; any) into BODY
(loop for rest on (cdr sexp) by #'cddr
if (keywordp (first rest))
collect (cons (first rest) (second rest)) into attr
else
do (progn (setq attr-list attr)
(setq body rest)
(return))
finally (setq attr-list attr)))
((listp (first sexp))
(setq tag (first (first sexp)))
(loop for rest on (cdr (first sexp)) by #'cddr
if (keywordp (first rest))
collect (cons (first rest) (second rest)) into attr
finally (setq attr-list attr))
(setq body (cdr sexp))))
(convert-tag-to-string-list tag attr-list body body-fn)))
(flet ((short-leaf-p (s)
(and (loop for e in (rest s) never (and (listp e) (keywordp (first e))))
(< (loop for e in (rest s) when (stringp e) sum (length e))
*short-leaf-content-length*))))
(let (tag attr-list body)
(cond
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorrect indentation of cond clauses

((keywordp sexp)
(setq tag sexp))
((atom (first sexp))
(setq tag (first sexp))
;; collect attribute/value pairs into ATTR-LIST and tag body (if
;; any) into BODY
(loop for rest on (cdr sexp) by #'cddr
if (keywordp (first rest))
collect (cons (first rest) (second rest)) into attr
else
do (progn (setq attr-list attr)
(setq body rest)
(return))
finally (setq attr-list attr)))
((listp (first sexp))
(setq tag (first (first sexp)))
(loop for rest on (cdr (first sexp)) by #'cddr
if (keywordp (first rest))
collect (cons (first rest) (second rest)) into attr
finally (setq attr-list attr))
(setq body (cdr sexp))))
(convert-tag-to-string-list tag attr-list body body-fn (short-leaf-p sexp)))))

(defun convert-attributes (attr-list)
"Helper function for CONVERT-TAG-TO-STRING-LIST which converts the
Expand Down Expand Up @@ -132,15 +136,17 @@ forms."
,=var=
*attribute-quote-char*)))))))

(defgeneric convert-tag-to-string-list (tag attr-list body body-fn)
(defgeneric convert-tag-to-string-list (tag attr-list body body-fn leaf-p)
(:documentation "Used by PROCESS-TAG to convert `HTML' into a list
of strings. TAG is a keyword symbol naming the outer tag, ATTR-LIST
is an alist of its attributes \(the car is the attribute's name as a
keyword, the cdr is its value), BODY is the tag's body, and BODY-FN is
a function which should be applied to BODY. The function must return
a list of strings or Lisp forms."))
a list of strings or Lisp forms. SHORT-LEAF-P is t when the TAG is a
leaf and its content is shorter than *SHORT-LEAF-CONTENT-LENGTH* then
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, specifying that this is in characters would be useful. Also, the grammar here irks me.

"[...] is t when TAG is a leaf and its content is shorter than SHORT-LEAF-CONTENT-LENGTH characters. Short leaves are not indented."

it is not indented."))

(defmethod convert-tag-to-string-list (tag attr-list body body-fn)
(defmethod convert-tag-to-string-list (tag attr-list body body-fn short-leaf-p)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awful lot of required params. Could some of these be &key args?

"The standard method which is not specialized. The idea is that you
can use EQL specializers on the first argument."
(declare (optimize speed space))
Expand All @@ -164,7 +170,7 @@ can use EQL specializers on the first argument."
;; now hand over the tag's body to TREE-TO-TEMPLATE
(let ((*indent* body-indent))
(funcall body-fn body))
(when body-indent
(when (and body-indent (not short-leaf-p))
;; indentation
(list +newline+ (n-spaces *indent*)))
;; closing tag
Expand Down