Looking for advice on processing linebreaks #96

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

I've just been through the tutorials, and I've thoroughly enjoyed them and am excited to make stuff with Pollen and Racket. Kudos for all the hard work here!

I've used LaTeX in the past, and I really like the \\ command to indicate a linebreak, instead of Pollen's decode-linebreaks, because it helps to control line lengths in the source file while letting CSS reflow the final output. I have an implementation below, which leverages decode-elements on root, but it requires two passes because the string operations to trim the \\ happen before the txexpr element operations, so the '(br) substitution fails to trigger in one pass. I'm looking for advice, on everything from a better approach to the processing to better code style, as I'm new to Racket. Thanks for taking a look!

;;;; pollen.rkt
#lang racket/base

(require racket/string
         pollen/decode
         pollen/unstable/typography
         txexpr)

(provide root)

;; Only convert a newline to a linebreak if the preceding line ends with "\\".
(define (latex-linebreaker prev next)
  (if (and (string? prev) (regexp-match #rx"\\\\$" prev))
      '(br)
      " "))

;; All the smart features plus trim ending "\\".
(define smart-and-trim
  (compose1 (compose1 (compose1 smart-quotes smart-dashes)
                      smart-ellipses)
            (lambda (s) (string-trim s "\\\\"))))

;; Decode paragraphs plus LaTeX-style linebreaks.
(define (smart-paragraphs elements)
  (define (decode-latex-lines elements)
    (decode-linebreaks elements latex-linebreaker))
  (decode-paragraphs elements #:linebreak-proc decode-latex-lines))

(define (root . elements)
  (define processed-elems
    (decode-elements elements
                     #:txexpr-elements-proc smart-paragraphs))
  (txexpr 'root empty (decode-elements processed-elems
                                       #:string-proc smart-and-trim)))
I've just been through the tutorials, and I've thoroughly enjoyed them and am excited to make stuff with Pollen and Racket. Kudos for all the hard work here! I've used LaTeX in the past, and I really like the `\\` command to indicate a linebreak, instead of Pollen's `decode-linebreaks`, because it helps to control line lengths in the source file while letting CSS reflow the final output. I have an implementation below, which leverages `decode-elements` on `root`, but it requires two passes because the string operations to trim the `\\` happen before the txexpr element operations, so the `'(br)` substitution fails to trigger in one pass. I'm looking for advice, on everything from a better approach to the processing to better code style, as I'm new to Racket. Thanks for taking a look! ```racket ;;;; pollen.rkt #lang racket/base (require racket/string pollen/decode pollen/unstable/typography txexpr) (provide root) ;; Only convert a newline to a linebreak if the preceding line ends with "\\". (define (latex-linebreaker prev next) (if (and (string? prev) (regexp-match #rx"\\\\$" prev)) '(br) " ")) ;; All the smart features plus trim ending "\\". (define smart-and-trim (compose1 (compose1 (compose1 smart-quotes smart-dashes) smart-ellipses) (lambda (s) (string-trim s "\\\\")))) ;; Decode paragraphs plus LaTeX-style linebreaks. (define (smart-paragraphs elements) (define (decode-latex-lines elements) (decode-linebreaks elements latex-linebreaker)) (decode-paragraphs elements #:linebreak-proc decode-latex-lines)) (define (root . elements) (define processed-elems (decode-elements elements #:txexpr-elements-proc smart-paragraphs)) (txexpr 'root empty (decode-elements processed-elems #:string-proc smart-and-trim))) ```
mbutterick commented 4 years ago (Migrated from github.com)

There’s nothing wrong with nesting multiple calls to decode-elements. For instance, see the root tag function in the pollen-tfl sample code. On the contrary, it can often be easier to reason about these transformations by putting them into multiple passes.

There’s nothing wrong with nesting multiple calls to `decode-elements`. For instance, see [the `root` tag function](https://docs.racket-lang.org/pollen-tfl/_pollen_rkt_.html#%28def._%28%28lib._pollen-tfl%2Fpollen..rkt%29._root%29%29) in the `pollen-tfl` sample code. On the contrary, it can often be easier to reason about these transformations by putting them into multiple passes.
achuie commented 4 years ago (Migrated from github.com)

Ok cool, thanks for the feedback! Closing as there seems not to be any further discussion necessary.

Ok cool, thanks for the feedback! Closing as there seems not to be any further discussion necessary.
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#96
Loading…
There is no content yet.