How to hook a decode procedure as xexpr postprocessing #11

Closed
opened 11 years ago by amirouche · 7 comments
amirouche commented 11 years ago (Migrated from github.com)

Héllo,

In the documentation, it's written that it's possible to hook a decode function to an xexpr. I did not find how. The documentation says it needs to be updated.

I want to use to do some typographic corrections, just like it's suggested in the decode documentation (replacing " with « in french for instance, or using dashes to create item lists à la markdown or just create p tags for paragraphs...

I have another problem, I use a template.html file with ◊->html{◊doc} but it embeds a root node, see example at http://hypermove.fr/2014/why-nerfed.html#doc the div#doc child is a root node.

My project is simple, the only "extra" feature I've written will browse a directory for blog posts and include them in the index page cf. index-year

Thanks.

Héllo, In the documentation, it's written that it's possible to hook a decode function to an xexpr. I did not find how. [The documentation says it needs to be updated](http://mbutterick.github.io/pollen/doc/Decode.html). I want to use to do some typographic corrections, just like it's suggested in the decode documentation (replacing `"` with `«` in french for instance, or using dashes to create item lists à la markdown or just create p tags for paragraphs... I have another problem, I use a template.html file with `◊->html{◊doc}` but it embeds a `root` node, see example at http://hypermove.fr/2014/why-nerfed.html#doc the `div#doc` child is a `root` node. My project is simple, the only "extra" feature I've written will browse a directory for blog posts and include them in the index page cf. [`index-year`](https://github.com/amirouche/racket-now/blob/master/amirouche/project-require.rkt#L41) Thanks.
mbutterick commented 11 years ago (Migrated from github.com)

I've updated the documentation with an example of how I use decode.

You should put decode (and other functions you write) in your "project-require.rkt" file (see Quick Tour for an explanation of this file)

Replacing French quotes: yes, it would work like smart-quotes, mais vous devez le traduire en français, bien sûr;

Detecting paragraphs: already part of the decode module, you can use that;

Using dashes to create lists: yes, but I wouldn't put this in the main decode function. I would create a special tag for the list (e.g., amirouche-list) so in the markup it appears as ◊amirouche-list{ ... }. Then, in "project-require.rkt", you add a function (define (amirouche-list . items) ... ). This function will automatically be applied to every ◊amirouche-list{...}. (You can use another decode within this function, if you like).

I've updated [the documentation](http://mbutterick.github.io/pollen/doc/Decode.html) with an example of how I use `decode`. You should put `decode` (and other functions you write) in your "project-require.rkt" file (see [Quick Tour](http://mbutterick.github.io/pollen/doc/quick-tour.html) for an explanation of this file) **Replacing French quotes**: yes, it would work like `smart-quotes`, mais vous devez le traduire en français, bien sûr; **Detecting paragraphs**: already part of the `decode` module, you can use that; **Using dashes to create lists**: yes, but I wouldn't put this in the main `decode` function. I would create a special tag for the list (e.g., `amirouche-list`) so in the markup it appears as `◊amirouche-list{ ... }`. Then, in "project-require.rkt", you add a function `(define (amirouche-list . items) ... )`. This function will automatically be applied to every `◊amirouche-list{...}`. (You can use another `decode` _within_ this function, if you like).
mbutterick commented 11 years ago (Migrated from github.com)

Yes, you are correct that ◊->html{◊doc} will embed a root node. That's just the default name for the main node of the data structure inside doc. You have two choices:

  1. ignore it (it will just be treated as an inline tag by web browsers);

  2. rename it in your main decode function. So rather than this:

(define (root . items)
  (decode (make-txexpr 'root '() items) 
  ... )

Try something like this:

(define (root . items)
  (decode (make-txexpr 'div '((id "doc")) items) 
  ... )
Yes, you are correct that `◊->html{◊doc}` will embed a `root` node. That's just the default name for the main node of the data structure inside `doc`. You have two choices: 1) ignore it (it will just be treated as an inline tag by web browsers); 2) rename it in your main `decode` function. So rather than this: ``` racket (define (root . items) (decode (make-txexpr 'root '() items) ... ) ``` Try something like this: ``` racket (define (root . items) (decode (make-txexpr 'div '((id "doc")) items) ... ) ```
mbutterick commented 11 years ago (Migrated from github.com)

The problem with my suggestion (2) is that it pushes template logic down into your code, which you may not want to do.

A better idea: I just pushed a fix for the ->html function that allows you to change the tag, or splice it into your page by passing keyword arguments. See the documentation for details.

So instead of writing ◊->html{◊doc}
You can rename the tag and id by doing this: ◊->html[#:tag 'div #:attrs '((id "doc"))]{◊doc}
Or strip the outer tag by doing this ◊->html[#:splice #t]{◊doc}

The problem with my suggestion (2) is that it pushes template logic down into your code, which you may not want to do. A better idea: I just pushed a fix for the `->html` function that allows you to change the tag, or splice it into your page by passing keyword arguments. See [the documentation](http://mbutterick.github.io/pollen/doc/Template.html) for details. So instead of writing `◊->html{◊doc}` You can rename the tag and id by doing this: `◊->html[#:tag 'div #:attrs '((id "doc"))]{◊doc}` Or strip the outer tag by doing this `◊->html[#:splice #t]{◊doc}`
amirouche commented 11 years ago (Migrated from github.com)

I can't get paragraph displaying correctly. Everyline break is translated to a html br. I failed so far to rework pollen sources, can you help?

I can't get paragraph displaying correctly. Everyline break is translated to a html br. I failed so far to rework pollen sources, can you help?
mbutterick commented 11 years ago (Migrated from github.com)

Can you show me your decode function?

Can you show me your `decode` function?
mbutterick commented 11 years ago (Migrated from github.com)

Here's a simple working example.

sample.html.pm

#lang pollen

Paragraph 1

Paragraph 2

project-require.rkt

#lang racket/base
(require pollen/decode)
(provide root)

(define (root . elements)
  (decode `(root ,@elements) #:txexpr-elements-proc detect-paragraphs))

Output from sample.html.pm in DrRacket:

'(root (p "Paragraph 1") (p "Paragraph 2"))
Here's a simple working example. **sample.html.pm** ``` racket #lang pollen Paragraph 1 Paragraph 2 ``` **project-require.rkt** ``` racket #lang racket/base (require pollen/decode) (provide root) (define (root . elements) (decode `(root ,@elements) #:txexpr-elements-proc detect-paragraphs)) ``` Output from sample.html.pm in DrRacket: ``` racket '(root (p "Paragraph 1") (p "Paragraph 2")) ```
amirouche commented 10 years ago (Migrated from github.com)

Thanks for the help, maybe I put it to good use later. I created something "similar" to Pollen in python for the time being https://github.com/amirouche/azoufzouf

Thanks for the help, maybe I put it to good use later. I created something "similar" to Pollen in python for the time being https://github.com/amirouche/azoufzouf
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
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#11
Loading…
There is no content yet.