Newbie: decode-paragraphs breaks LaTeX paragraphs #8

Closed
opened 5 years ago by theramiyer · 6 comments
theramiyer commented 5 years ago (Migrated from github.com)

Hi, everyone. This is my third day with Pollen. Perhaps I'm jumping the gun before learning to use Racket, but I fear hitting writer's block by the time I learn the language. I want to start using Pollen, though, and I fully intend to learn it.

Meanwhile, I want my book output in rudimentary HTML (for Kindle) plus LaTeX. I have created the LaTeX template, and Kindle is not sophisticated enough for layouts; a basic CSS would do (Amazon would rather I submit plain HTML).

Here is a sample of how I'm writing my poly.pm files:

#lang pollen

◊chapter{One}

"TOM!"

No answer.

"TOM!"

No answer.

Here is my pollen.rkt

#lang racket/base

(provide author title)
(define title "The Adventures of Tom Sawyer")
(define author "Mark Twain")

(require racket/date txexpr pollen/setup)
(provide (all-defined-out))

(module setup racket/base
  (define poly-targets '(html tex)))

(define (chapter . elements)
  (case (current-poly-target)
    [(tex) (apply string-append `("\\chapter{" ,@elements "}"))]
    [else (txexpr 'h1 empty elements)]))

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

This renders the HTML as (which is fantastic):

<html>
<head>
<meta charset="UTF-8">
<title>One | Adventures of Tom Sawyer by Mark Twain</title>
<link rel="stylesheet" type="text/css" media="all" href="/assets/css/main.css" />
</head>
<body>
<h1>The Edge</h1>
<p>“TOM!”</p>
<p>No answer.</p>
<p>“TOM!”</p>
<p>No answer.</p>
</body>
</html>

But renders my LaTeX as:

\chapter{One} “TOM!” No answer. “TOM!” No answer.

One way of handling this would be to use ◊p{"TOM!"}◊p{No answer.}, and omit decode-paragraphs, but that is going to make writing worse for me.

Is there a way I can conditionally use decode-paragraphs only for HTML output? I terribly messed up the syntax in my attempts. How do I write this condition? LaTeX handles --- and ..., but renders 'word' as ’word’; LaTeX uses `word' for quotes. It accepts ‘word’, though; doesn't interfere with smart-quotes.

The bigger goal is to know how to use Pollen to process basic HTML and basic LaTeX without having to resort to Markdown, but get all of the capabilities of Markdown (with a parser like Kramdown), ... to …; --- to —, 'word' to ‘word’, etc.

Hi, everyone. This is my third day with Pollen. Perhaps I'm jumping the gun before learning to use Racket, but I fear hitting writer's block by the time I learn the language. I want to start using Pollen, though, and I fully intend to learn it. Meanwhile, I want my book output in rudimentary HTML (for Kindle) plus LaTeX. I have created the LaTeX template, and Kindle is not sophisticated enough for layouts; a basic CSS would do (Amazon would rather I submit plain HTML). Here is a sample of how I'm writing my `poly.pm` files: ```pollen #lang pollen ◊chapter{One} "TOM!" No answer. "TOM!" No answer. ``` Here is my `pollen.rkt` ```racket #lang racket/base (provide author title) (define title "The Adventures of Tom Sawyer") (define author "Mark Twain") (require racket/date txexpr pollen/setup) (provide (all-defined-out)) (module setup racket/base (define poly-targets '(html tex))) (define (chapter . elements) (case (current-poly-target) [(tex) (apply string-append `("\\chapter{" ,@elements "}"))] [else (txexpr 'h1 empty elements)])) (require pollen/decode txexpr) (define (root . elements) (txexpr 'root empty (decode-elements elements #:txexpr-elements-proc decode-paragraphs #:string-proc (compose1 smart-quotes smart-dashes)))) ``` This renders the HTML as (which is fantastic): ```html <html> <head> <meta charset="UTF-8"> <title>One | Adventures of Tom Sawyer by Mark Twain</title> <link rel="stylesheet" type="text/css" media="all" href="/assets/css/main.css" /> </head> <body> <h1>The Edge</h1> <p>“TOM!”</p> <p>No answer.</p> <p>“TOM!”</p> <p>No answer.</p> </body> </html> ``` But renders my LaTeX as: ```latex \chapter{One} “TOM!” No answer. “TOM!” No answer. ``` One way of handling this would be to use `◊p{"TOM!"}◊p{No answer.}`, and omit `decode-paragraphs`, but that is going to make writing worse for me. Is there a way I can conditionally use `decode-paragraphs` only for HTML output? I terribly messed up the syntax in my attempts. How do I write this condition? LaTeX handles `---` and `...`, but renders `'word'` as `’word’`; LaTeX uses `` `word' `` for quotes. It accepts `‘word’`, though; doesn't interfere with `smart-quotes`. The bigger goal is to know how to use Pollen to process basic HTML and basic LaTeX without having to resort to Markdown, but get all of the capabilities of Markdown (with a parser like Kramdown), \.\.\. to …; \-\-\- to —, 'word' to ‘word’, etc.
sorawee commented 5 years ago (Migrated from github.com)

I think you are supposed to name the document so that it ends with .poly.pm (e.g., foo.poly.pm). Not sure what will happen if you have just poly.pm.

I believe you need (provide (all-defined-out)) in the submodule setup. Otherwise, the tex rendering won't work (how did you get tex rendering working?!?)

You have not shown us the content of template.tex.p. One potential solution to your problem is to change this file.

Is there a way I can conditionally use decode-paragraphs only for HTML output?

Yes: use this root

(require racket/function)

(define (root . elements)
  (txexpr 'root empty
          (decode-elements
           elements
           #:txexpr-elements-proc
           (if (eq? (current-poly-target) 'tex)
               identity
               decode-paragraphs)
           #:string-proc
           (compose1 smart-quotes smart-dashes))))
I think you are supposed to name the document so that it ends with `.poly.pm` (e.g., `foo.poly.pm`). Not sure what will happen if you have just `poly.pm`. I believe you need `(provide (all-defined-out))` in the submodule `setup`. Otherwise, the `tex` rendering won't work (how did you get `tex` rendering working?!?) You have not shown us the content of `template.tex.p`. One potential solution to your problem is to change this file. > Is there a way I can conditionally use `decode-paragraphs` only for HTML output? Yes: use this `root` ``` (require racket/function) (define (root . elements) (txexpr 'root empty (decode-elements elements #:txexpr-elements-proc (if (eq? (current-poly-target) 'tex) identity decode-paragraphs) #:string-proc (compose1 smart-quotes smart-dashes)))) ```
mbutterick commented 5 years ago (Migrated from github.com)

Is there a way I can conditionally use decode-paragraphs only for HTML output?

@sorawee is correct that you can do this, but I doubt you want to — detect-paragraphs does other tidying too, plus you want consistent results between formats (that is, you want a guarantee that paragraph breaks all fall in the same places, which is only possible if they're detected by the same processing steps)

I’m no LaTeX expert but I think you’d want to find a way to convert the p tags to blank lines in your LaTeX rendering template (that is, the last possible moment, and then you get the convenience of handling everything as X-expressions up till then)

> Is there a way I can conditionally use decode-paragraphs only for HTML output? @sorawee is correct that you *can* do this, but I doubt you want to — `detect-paragraphs` does other tidying too, plus you want consistent results between formats (that is, you want a guarantee that paragraph breaks all fall in the same places, which is only possible if they're detected by the same processing steps) I’m no LaTeX expert but I think you’d want to find a way to convert the `p` tags to blank lines in your LaTeX rendering template (that is, the last possible moment, and then you get the convenience of handling everything as X-expressions up till then)
sorawee commented 5 years ago (Migrated from github.com)

Yup, I think that changing template.tex.p is a cleaner solution as well.

Yup, I think that changing `template.tex.p` is a cleaner solution as well.
theramiyer commented 5 years ago (Migrated from github.com)

I'm sorry, I didn't post the contents of template.tex.p. I was so caught up in thinking paragraph-decode was interfering, I did not think about my TeX template. Yes, changing this makes more sense, now that it's pointed out. Thank you! Here are the contents.

◊(local-require racket/list)
◊(apply string-append (filter string? (flatten doc)))

And I think I inadvertently removed (provide (all-defined-out)) from the submodule setup. Fixed that.

I'm sorry, I didn't post the contents of `template.tex.p`. I was so caught up in thinking paragraph-decode was interfering, I did not think about my TeX template. Yes, changing this makes more sense, now that it's pointed out. Thank you! Here are the contents. ```pollen ◊(local-require racket/list) ◊(apply string-append (filter string? (flatten doc))) ``` And I think I inadvertently removed `(provide (all-defined-out))` from the submodule setup. Fixed that.
sorawee commented 5 years ago (Migrated from github.com)

Actually, there's another solution similar to "conditionally use decode-paragraphs only for HTML output", but behave much better: change your root to:

(define (the-paragraph-wrapper xs) (txexpr 'p '() (append xs (list "\n\n"))))

(define (root . elements)
  (txexpr 'root empty
          (decode-elements
           elements
           #:txexpr-elements-proc
           (if (eq? (current-poly-target) 'tex)
               (λ (elements) (decode-paragraphs elements the-paragraph-wrapper))
               decode-paragraphs)
           #:string-proc
           (compose1 smart-quotes smart-dashes))))

with this, decode-paragraphs is always called, but will also add extras \n\n for tex documents.

Actually, there's another solution similar to "conditionally use `decode-paragraphs` only for HTML output", but behave much better: change your `root` to: ``` (define (the-paragraph-wrapper xs) (txexpr 'p '() (append xs (list "\n\n")))) (define (root . elements) (txexpr 'root empty (decode-elements elements #:txexpr-elements-proc (if (eq? (current-poly-target) 'tex) (λ (elements) (decode-paragraphs elements the-paragraph-wrapper)) decode-paragraphs) #:string-proc (compose1 smart-quotes smart-dashes)))) ``` with this, `decode-paragraphs` is always called, but will also add extras `\n\n` for tex documents.
theramiyer commented 5 years ago (Migrated from github.com)

That worked, thank you!

Sorry about the delay in response; no Internet for the last two days. 😄

That worked, thank you! Sorry about the delay in response; no Internet for the last two days. 😄
This repo is archived. You cannot comment on issues.
Loading…
There is no content yet.