Skip to content

Commit

Permalink
spinneret-tags(:nsection): Make smarter when nested/local.
Browse files Browse the repository at this point in the history
This binds the spinneret::*html-path* (the variable representing the nesting of
the HTML tags so far) to a value matching the required level of headings, thus
making all the :h*/:nsection inside :nsection to be consistent with the level of
the :nsection. In other words,

(spinneret:with-html-string
 (:nsection
  :title "hello" :level 4
  (:nsection :title "what")))

returns

<section id=hello>
 <details open>
  <summary>
   <h4 style=\"display: inline\">hello <a href=#hello>#</a></h4>
  </summary>
  <section id=what>
   <details open>
    <summary>
     <h5 style=\"display: inline\">what <a href=#what>#</a></h5>
    </summary>
   </details>
  </section>
 </details>
</section>

instead of the previous version, where the inner heading was <h2>.
  • Loading branch information
aartaka committed Nov 18, 2022
1 parent 6d976d2 commit e81027e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions source/spinneret-tags.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,17 @@ by default"
(remf attrs :level)
(remf attrs :open-p)
(remf attrs :id)
`(:section
:id ,id
(:details
:open ,open-p
(:summary (,(if level
(alexandria:make-keyword (format nil "H~d" level))
:h*)
:style "display: inline" ,@attrs ,title
" " (:a :href ,(uiop:strcat "#" id) "#")))
,@body)))
`(let ((spinneret::*html-path* (append
spinneret::*html-path*
(make-list ,(if level
`(1- (- ,level (spinneret::heading-depth)))
0)
:initial-element :section))))
(:section
:id ,id
(:details
:open ,open-p
(:summary (:h* :style "display: inline"
,@attrs ,title
" " (:a :href ,(uiop:strcat "#" id) "#")))
,@body))))

2 comments on commit e81027e

@aadcg
Copy link
Member

@aadcg aadcg commented on e81027e Nov 18, 2022

Choose a reason for hiding this comment

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

Suggestion: abstracting setting *html-path* to

(append
 spinneret::*html-path*
 (make-list ,(if level
                 `(1- (- ,level (spinneret::heading-depth)))
               0)
            :initial-element :section))

in a function would make it more readable. Then you could expand on what it does in its docstring (in other words, moving the commit explanation to the function's docstring).

@aartaka
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've chosen to add an explanation commentary, because I'm too lazy to reason about function/macro interactions at the moment: 3ae2229.

Please sign in to comment.