> On May 27, 2019, at 5:24 PM, Joel McCracken <mccracken.j...@gmail.com> wrote:
> 
> The point is I was hoping to avoid having to rebuild the entire links table 
> every time I process another file.


Here's a perhaps simpler way to do it that you could customize further.

The Pollen sources define metas for each target that they want to host (you 
would sprinkle these through the file to taste).

In "pollen.rkt", `generate-link-dests!` makes a table associating paths and 
targets. `resolve-alias` calculates a URL using this table. If there are 
duplicate targets, or no targets, it throws an error.

If this approach is too slow, you could have `generate-link-dests!` write its 
table to a ".rktd" file (with `write-to-file`) and then `resolve-alias` could 
load it in with `file->value`. In your render script, you would trigger 
`generate-link-dests!` once before the render to freshen the index.

The trick here is that `metas` is made available directly from a Pollen source, 
but also through a submodule called `metas`. You can read the `metas` without 
triggering an evaluation of the rest of the file (which in turn would trigger 
"pollen.rkt", so you'd have a circularity problem).
 

;;; foo.html.pm
#lang pollen

◊(define-meta one #t)
◊(define-meta two #t)
◊(define-meta three #t)

◊link['four]{link to bar}


;;; bar.html.pm
#lang pollen

◊(define-meta three #t)
◊(define-meta four #t)
◊(define-meta five #t)

◊link['one]{link to foo}


;;; pollen.rkt

#lang racket
(require racket/path sugar/coerce pollen/core pollen/file)
(provide link)

(define (generate-link-dests!)
  (define link-table (make-hash))
  (for ([f (in-directory)]
        #:when (path-has-extension? f #"pm")
        [(k _) (in-hash (dynamic-require `(submod ,(path->string f) metas) 
'metas))]
        #:unless (eq? k 'here-path))
    (define key (->symbol (path-replace-extension (->output-path 
(find-relative-path (current-directory) f)) #"")))
    (hash-update! link-table key (λ (val) (cons k val)) null))
  link-table)

(define (resolve-alias alias)
  (define link-table (generate-link-dests!))
  (match (for*/list ([k (in-hash-keys link-table)]
                     [v (in-list (hash-ref link-table k))]
                     #:when (eq? alias v))
           (cons k v))
    [(list (cons k v)) (format "~a.html#~a" k v)]
    [(list) (error 'alias-not-found)]
    [_ (error 'alias-ambiguous)]))

(define (link name . xs)
  `(a ((href ,(resolve-alias name))) ,@xs))

(module+ test
  (require rackunit)
  (check-equal? (resolve-alias 'one) "foo.html#one")
  (check-equal? (resolve-alias 'five) "bar.html#five")
  (check-exn exn:fail? (λ () (resolve-alias 'three)))
  (check-exn exn:fail? (λ () (resolve-alias 'nowhere))))


-- 
You received this message because you are subscribed to the Google Groups 
"Pollen" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pollenpub+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pollenpub/5A3337F1-C98E-409F-AE55-C4CB1D9061B1%40mbtype.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to