Inserting html comment with pollen command #126

Open
opened 2 years ago by jaybonthius · 4 comments
jaybonthius commented 2 years ago (Migrated from github.com)

I want to insert an html comment <!-- like this --> with a pollen command, but I'm not sure how I would define a tag function, since a comment doesn't take the same form as other tags.

EDIT: I'm realizing that a tag is probably not the way to accomplish this. But how would I insert a < or > without getting turned into &lt; or &gt;?

(define html-comment `(!-- "this is a comment"))

From this pollen markup:

this is a paragraph ◊html-comment

I want to get this:

<p>
  this is a paragraph
  <!-- this is a comment -->
</p>

but I'm getting this:

<p>
  this is a paragraph
  <!-->this is a comment</!-->
</p>

which the browser isn't sure how to interpret, and turns into this:

<p>
  this is a paragraph
  <!---->
  this is a comment
  <!--!---->
</p>
I want to insert an html comment `<!-- like this -->` with a pollen command, but I'm not sure how I would define a tag function, since a comment doesn't take the same form as other tags. EDIT: I'm realizing that a tag is probably not the way to accomplish this. But how would I insert a `<` or `>` without getting turned into `&lt;` or `&gt;`? ```racket (define html-comment `(!-- "this is a comment")) ``` From this pollen markup: ``` this is a paragraph ◊html-comment ``` I want to get this: ```html <p> this is a paragraph <!-- this is a comment --> </p> ``` but I'm getting this: ```html <p> this is a paragraph <!-->this is a comment</!--> </p> ``` which the browser isn't sure how to interpret, and turns into this: ```html <p> this is a paragraph <!----> this is a comment <!--!----> </p> ```
sorawee commented 2 years ago (Migrated from github.com)

The documentation of ->html suggests that it consumes an arbitrary xexpr?. The grammar shows that a misc, particularly a comment? from the xml module is considered an xexpr?. So ideally this should work:

#lang pollen

◊(require xml)

◊(comment "abc")

Unfortunately, it's not, and I personally consider this a Pollen bug.

That being said, I'm curious why do you want to generate HTML comments? They won't be displayed to users anyway. And if you want to write a comment in the source, wouldn't it be better to do so in the Pollen source rather than HTML source? Pollen does support a comment syntax already:

#lang pollen

◊;abc

def
The documentation of [`->html`](https://docs.racket-lang.org/pollen/Template.html#%28def._%28%28lib._pollen%2Ftemplate%2Fhtml..rkt%29._-~3ehtml%29%29) suggests that it consumes an arbitrary [`xexpr?`](https://docs.racket-lang.org/xml/index.html#%28def._%28%28lib._xml%2Fprivate%2Fxexpr-core..rkt%29._xexpr~3f%29%29). The grammar shows that a _misc_, particularly a [`comment?`](https://docs.racket-lang.org/xml/index.html#%28def._%28%28lib._xml%2Fmain..rkt%29._comment%29%29) from the `xml` module is considered an `xexpr?`. So ideally this should work: ``` #lang pollen ◊(require xml) ◊(comment "abc") ``` Unfortunately, it's not, and I personally consider this a Pollen bug. That being said, I'm curious why do you want to generate HTML comments? They won't be displayed to users anyway. And if you want to write a comment in the source, wouldn't it be better to do so in the Pollen source rather than HTML source? Pollen does support a comment syntax already: ``` #lang pollen ◊;abc def ```
mbutterick commented 2 years ago (Migrated from github.com)

I just pushed an update to the underlying txexpr library that should fix your case:

#lang pollen/mode racket
(require pollen/tag pollen/template/html rackunit)

(define html-comment '(@ "<!-- this is a comment -->"))
(define root (default-tag-function 'root))
(define doc ◊root{this is a paragraph ◊html-comment})
(check-equal? (->html doc) "<root>this is a paragraph <!-- this is a comment --></root>")

Keep in mind that the comment string, like a CDATA string, has to be separated from adjacent string elements. (Ordinarily, adjacent strings will be concatenated in the output.) In this case I’ve used the splicing tag @ to do this without adding an extra tag to the generated HTML.

I just pushed an update to the underlying `txexpr` library that should fix your case: ```racket #lang pollen/mode racket (require pollen/tag pollen/template/html rackunit) (define html-comment '(@ "<!-- this is a comment -->")) (define root (default-tag-function 'root)) (define doc ◊root{this is a paragraph ◊html-comment}) (check-equal? (->html doc) "<root>this is a paragraph <!-- this is a comment --></root>") ``` Keep in mind that the comment string, like a `CDATA` string, has to be separated from adjacent string elements. (Ordinarily, adjacent strings will be concatenated in the output.) In this case I’ve used the splicing tag `@` to do this without adding an extra tag to the generated HTML.
jaybonthius commented 2 years ago (Migrated from github.com)

Whoops, didn't mean to close the issue.

@sorawee I am using an HTML presentation framework (reveal.js) to make a web-based slideshow. Weirdly enough, some attributes can be controlled through HTML comments. (https://revealjs.com/markdown/#element-attributes).

@mbutterick Amazing! Thank you!

Whoops, didn't mean to close the issue. @sorawee I am using an HTML presentation framework (`reveal.js`) to make a web-based slideshow. Weirdly enough, some attributes can be controlled through HTML comments. (https://revealjs.com/markdown/#element-attributes). @mbutterick Amazing! Thank you!
mbutterick commented 2 years ago (Migrated from github.com)

See also https://github.com/applied-science/talks/tree/master/mxnet

(example of using Pollen to orchestrate reveal.js)

See also https://github.com/applied-science/talks/tree/master/mxnet (example of using Pollen to orchestrate reveal.js)
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#126
Loading…
There is no content yet.