mbutterick / pollen-users

please use https://forums.matthewbutterick.com/c/typesetting/ instead
https://forums.matthewbutterick.com/c/typesetting/
52 stars 0 forks source link

not insert <br> for single linebreak #41

Closed adamfeuer closed 4 years ago

adamfeuer commented 4 years ago

Hi,

Is there a way to get pollen to not insert <br> tags for single linebreaks? That way I can write text with linebreaks in my editor, but use CSS to flow text when it gets rendered to html.

Right now if I enter this:

◊p{First sentence.
Second sentence.
Third sentence.}

I get this output:

<p>First sentence<br>
Second sentence.<br>
Third sentence.</p>

But I want this:

<p>First sentence Second sentence. Third sentence.</p>
otherjoel commented 4 years ago

Yes, there is a way to do this; in fact, the desired behavior you describe is the default behavior unless you write code to change it. Are you by chance using decode-paragraphs [edit: or decode-linebreaks] in your pollen.rkt or other required modules?

adamfeuer commented 4 years ago

@otherjoel Yes, here's an excerpt from my pollen.rkt:

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

I'm a pollen and racket newbie, so I'm not sure what all that does. I put that together after reading the pollen tutorials...

adamfeuer commented 4 years ago

@otherjoel Ah, I see. The root tag function is decoding everything when it shouldn't. If I take out the decode-paragraphs pollen now does what I want.

When is the right time to add decode-paragraphs? Where is it usually used?

mbutterick commented 4 years ago

By default, decode-paragraphs will also invoke the default decode-linebreaks for single newlines. If you don’t want that behavior, you can supply a #:linebreak-proc procedure that will just leave them alone. For instance:

#lang racket
(require pollen/decode rackunit)

(check-equal?
 (decode-paragraphs '("para1" "\n\n" "para2 line1" "\n" "para2 line2"))
 '((p "para1") (p "para2 line1" (br) "para2 line2")))

(check-equal?
 (decode-paragraphs '("para1" "\n\n" "para2 line1" "\n" "para2 line2")
                    #:linebreak-proc (λ (x) x))
 '((p "para1") (p "para2 line1" "\n" "para2 line2")))

You could also replace the linebreaks with a single space " ". Note, however, that you wouldn’t want to delete them entirely, because the resulting HTML would be rendered without any space between lines (try it and see).

adamfeuer commented 4 years ago

@otherjoel @mbutterick Thank you both!