mbutterick / pollen-users

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

Accessing `metas` from Racket #103

Closed casouri closed 3 years ago

casouri commented 3 years ago

The tutorial mentions using (require "pollen-source.rkt") to gain access to metas outside Pollen. What does this "pollen-source.rkt" refer to? Currently in my pollen.rkt there is

(require
 pollen/decode
 pollen/file
 pollen/tag
 pollen/core
 txexpr)

which doesn't work. Apparently, none of them provide metas. What should I do?

Background:

I'm trying to write a little helper that turns the filename of the pollen source into the title of the page. For example, it turns "day-12.html.pm" into "Day 12". This is how I implemented it:

(define (title)
  (let* ([path (select-from-metas 'here-path metas)]
         [filename (file-name-from-path path)]
         [day (list-ref (regexp-match #rx"^day-(.+?)\\." filename) 1)])
    (format "Day ~a" day)))

The problem is when I wrote my function in pollen.rkt, Racket complains that metas is not defined.

My complete pollen.rkt:

#lang racket

(provide title jpns link image root)

(require
 pollen/decode
 pollen/file
 pollen/tag
 pollen/core
 txexpr)

(define (title)
  (let* ([path (select-from-metas 'here-path metas)]
         [filename (file-name-from-path path)]
         [day (list-ref (regexp-match #rx"^day-(.+?)\\." filename) 1)])
    (format "Day ~a" day)))

(define (jpns text)
  (txexpr 'span '((class "jpns")) (list text)))

(define (link url . tx-elements)
  (let* ([tx-elements (if (empty? tx-elements)
                          (list (string-normalize-nfc url))
                          tx-elements)]
         [link-tx (txexpr 'a empty tx-elements)]
         [link-tx (attr-set link-tx 'href url)])
    link-tx))

(define (image src)
  (attr-set* '(img) 'src src))

(define (root . elements)
  (txexpr 'root empty
          (decode-elements
           elements
           #:txexpr-elements-proc decode-paragraphs)))
mbutterick commented 3 years ago

What does this "pollen-source.rkt" refer to?

It’s a placeholder for the actual name. So if your source is "day-12.html.pm", then (require "day-12.html.pm").

Racket complains that metas is not defined.

If you want to use metas in a tag function, you probably want to use the (current-metas) parameter, which holds the metas of the Pollen source currently being evaluated (or #false if none is available)

casouri commented 3 years ago

Thanks! (current-metas) is what I'm looking for. It would be nice if the tutorial can mention it in 12.3.1.5 Retrieving metas.