Ignoring certain elements in decode-paragraphs #22

Open
opened 5 years ago by zachmandeville · 3 comments
zachmandeville commented 5 years ago (Migrated from github.com)

Hello!

I am making a pollen site that includes a form, which is organized into a series of fieldsets.

I would like to write the form like so:

 ◊fieldset[]{
 ◊legend{About You}
 ◊text-input[#:for "Name"]{}
 ◊text-input[#:for "Email"]{}
 ◊text-input[#:for "Emergency Contact"]{Who should we call (name and number)? }}

(the text input will make a label and input based on #:for, with any text in the curly braces added as helper text)

The issue I'm having is that, when this renders, everything inside of fieldset gets wrapped in a <p> tag, and so the legend is not added correctly and it loses semantic clarity. I can remove the linebreaks in my page and solve the issue, but then the pollen file is hard to read.

I have decode-paragraphs added in the style given in the pollen tutorial, and love it for the other pages in this site:

 (define (root . elements)
-     (txexpr 'root empty (decode-elements elements
                                           #:txexpr-elements-proc decode-paragraphs
                                           #:string-proc (compose1 smart-quotes smart-dashes))))

I think it is causing the <p> tag wrapping on the form, though.

Is it possible to have it pass over elements like fieldset, so I can get both the pollen file and html output I want?

Thank you!

Hello! I am making a pollen site that includes a form, which is organized into a series of [fieldsets](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset). I would like to write the form like so: ``` ◊fieldset[]{ ◊legend{About You} ◊text-input[#:for "Name"]{} ◊text-input[#:for "Email"]{} ◊text-input[#:for "Emergency Contact"]{Who should we call (name and number)? }} ``` (the text input will make a label and input based on #:for, with any text in the curly braces added as helper text) The issue I'm having is that, when this renders, everything inside of fieldset gets wrapped in a `<p>` tag, and so the legend is not added correctly and it loses semantic clarity. I can remove the linebreaks in my page and solve the issue, but then the pollen file is hard to read. I have `decode-paragraphs` added in the style given in the pollen tutorial, and love it for the other pages in this site: ``` (define (root . elements) - (txexpr 'root empty (decode-elements elements #:txexpr-elements-proc decode-paragraphs #:string-proc (compose1 smart-quotes smart-dashes)))) ``` I think it is causing the `<p>` tag wrapping on the form, though. Is it possible to have it pass over elements like `fieldset`, so I can get both the pollen file and html output I want? Thank you!
zachmandeville commented 5 years ago (Migrated from github.com)

alternately: is there a recommended way to write a fieldset tag function so it's not wrapping things in <p> tags? If I could do it with a tag function, I'd want a fieldset structured like so:

 ◊fieldset[#:legend "About You"]{
 ◊text-input[#:for "Name"]{}
 ◊text-input[#:for "Email"]{}
 ◊text-input[#:for "Emergency Contact"]{Who should we call (name and number)? }}

Currently, I have my functions defined as so:

(define  (get-attr attributes keyword)
  ;; given a list of attributes, and a keyword of specific attribute
  ;; return the value of this attribute.
  (define attribute (car (filter (λ (x) (eq? (car x) keyword)) attributes)))
  (list-ref attribute 1))

(define-tag-function (text-input attrs note)
;; create three sibling elements: label, p.form-note, input[type=text].
;; keyword for is used for the label text and input id.
;; all other keywords fed as attributes to input.
;; If note given, it is used for a <p> tag, otherwise <p> is not created.
  (define label (get-attr attrs 'for))
  (define id (string-downcase(string-replace label " " "-")))
  (define attributes (filter (λ (x) (not(eq? (car x) 'for))) attrs))
  `(@
    (label ((for ,id)) ,label)
    ,(when (not (null? note)) `(p ((class "form-note")),@note))
    (input ((type "text") (id ,id) ,@attributes))))

(define-tag-function (fieldset attrs elems)
;; creates fieldset element, with label keyword used to generate label element.
;; should be able to handle any input tags included in the elems.
  (define legend (get-attr attrs 'legend))
  `(fieldset (legend ,legend) ,@elems))

For the fieldset, if I don't include the @elems it generates the proper html:

<fieldset>
  <label>about me</label>
</fieldset>

if I do include ,@elems it wraps everything in fieldset in a p tag, including that explicitly defined legend:

<fieldset>
  <p>
     <legend>about me</legend>
     <label for="name">name</label>
     <input type='text' id='name'>
  </p>
</fieldset>

There must be something I'm doing wrong, but I'm baffled.

Thank you!

alternately: is there a recommended way to write a fieldset tag function so it's not wrapping things in `<p>` tags? If I could do it with a tag function, I'd want a fieldset structured like so: ```web ◊fieldset[#:legend "About You"]{ ◊text-input[#:for "Name"]{} ◊text-input[#:for "Email"]{} ◊text-input[#:for "Emergency Contact"]{Who should we call (name and number)? }} ``` Currently, I have my functions defined as so: ```racket (define (get-attr attributes keyword) ;; given a list of attributes, and a keyword of specific attribute ;; return the value of this attribute. (define attribute (car (filter (λ (x) (eq? (car x) keyword)) attributes))) (list-ref attribute 1)) (define-tag-function (text-input attrs note) ;; create three sibling elements: label, p.form-note, input[type=text]. ;; keyword for is used for the label text and input id. ;; all other keywords fed as attributes to input. ;; If note given, it is used for a <p> tag, otherwise <p> is not created. (define label (get-attr attrs 'for)) (define id (string-downcase(string-replace label " " "-"))) (define attributes (filter (λ (x) (not(eq? (car x) 'for))) attrs)) `(@ (label ((for ,id)) ,label) ,(when (not (null? note)) `(p ((class "form-note")),@note)) (input ((type "text") (id ,id) ,@attributes)))) (define-tag-function (fieldset attrs elems) ;; creates fieldset element, with label keyword used to generate label element. ;; should be able to handle any input tags included in the elems. (define legend (get-attr attrs 'legend)) `(fieldset (legend ,legend) ,@elems)) ``` For the fieldset, if I don't include the @elems it generates the proper html: ```html <fieldset> <label>about me</label> </fieldset> ``` if I do include `,@elems` it wraps everything in fieldset in a p tag, including that explicitly defined legend: ```html <fieldset> <p> <legend>about me</legend> <label for="name">name</label> <input type='text' id='name'> </p> </fieldset> ``` There must be something I'm doing wrong, but I'm baffled. Thank you!
mbutterick commented 5 years ago (Migrated from github.com)

Is it possible to have it pass over elements like fieldset, so I can get both the pollen file and html output I want?

Try the #:exclude-tags option of decode-elements.

Also keep in mind that by default, Pollen treats fieldset as a block tag, so it gets some extra semantics that an ordinary tag function would not.

> Is it possible to have it pass over elements like fieldset, so I can get both the pollen file and html output I want? Try the `#:exclude-tags` option of [`decode-elements`](https://docs.racket-lang.org/pollen/Decode.html?q=block-txexpr%3F#%28def._%28%28lib._pollen%2Fdecode..rkt%29._decode-elements%29%29). Also keep in mind that by default, Pollen treats `fieldset` [as a block tag](https://docs.racket-lang.org/pollen/Decode.html?q=block-txexpr%3F#%28def._%28%28lib._pollen%2Fdecode..rkt%29._block-txexpr~3f%29%29), so it gets some extra semantics that an ordinary tag function would not.
zachmandeville commented 5 years ago (Migrated from github.com)

Ah, this worked perfectly. Thank you!

Ah, this worked perfectly. Thank you!
This repo is archived. You cannot comment on issues.
No Milestone
No project
No Assignees
1 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen-users#22
Loading…
There is no content yet.