select function

42 views
Skip to first unread message

Li Junsong

unread,
Apr 6, 2016, 12:31:08 PM4/6/16
to Pollen
(select 'simple-tag doc) is affected by the transformation of tag function on simple-tag. I sometimes think `select` (or select-from-doc) function actually should apply on the original doc that tag functions haven't applied yet. That is, (select 'title doc) actually means get the contents of my semantic tag 'title. If my title later converts to h1, or h*, select won't be affected. This definitely complicates Pollen though.

Some tricks mentioned here https://github.com/mbutterick/pollen/issues/55 can solve this problem, but I couldn't help asking here.

Matthew Butterick

unread,
Apr 6, 2016, 2:45:42 PM4/6/16
to Li Junsong, Pollen

On Apr 6, 2016, at 9:31 AM, Li Junsong <ljs.da...@gmail.com> wrote:

> (select 'simple-tag doc) is affected by the transformation of tag function on simple-tag. I sometimes think `select` (or select-from-doc) function actually should apply on the original doc that tag functions haven't applied yet. That is, (select 'title doc) actually means get the contents of my semantic tag 'title. If my title later converts to h1, or h*, select won't be affected. This definitely complicates Pollen though.

It's a fair point. But when you say that you want "the original doc that tag functions haven't applied yet," that amounts to running the Pollen source file while pretending pollen.rkt doesn't exist. This would solve certain problems, but create others.

Another approach would be to divide your processing into semantic and non-semantic. Remember, when your Pollen source file gets rendered with a template, there are essentially two rounds of processing. The first round is when the source file itself is run, and the `doc` and `metas` exports are produced. The second round is when the template code is run, with the `doc` and `metas` available.

For instance, consider this toy example. When you run "test.html.pm", it will still have `simple-tag` in the `doc`, and `(select 'simple-tag doc)` will get you "My Title". But when you render the file in the project server, the template will send it through a decoder to convert `simple-tag` to `h1`.


;;;;;;;; "test.html.pm"

#lang pollen

◊simple-tag{My Title}

That's all.


;;;;;;;; "template.html.p"

<html>
◊(->html (remove-semantic-tags doc))
</html>


;;;;;;;; "pollen.rkt"

#lang racket/base
(require txexpr pollen/decode)
(provide (all-defined-out))

(define (convert-title tx)
(if (eq? (get-tag tx) 'simple-tag)
`(h1 ((class "title")) ,@(get-elements tx))
tx))

(define (remove-semantic-tags doc)
(decode-elements doc #:txexpr-proc convert-title))

Li Junsong

unread,
Apr 7, 2016, 12:20:49 PM4/7/16
to Pollen
Thanks for the example. Explicitly recognizing the two phases helps.
Reply all
Reply to author
Forward
0 new messages