Re: [pollen] A few questions about pollen capabilities

2019-05-27 Thread Matthew Butterick


> On May 27, 2019, at 5:24 PM, Joel McCracken  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.


Re: [pollen] A few questions about pollen capabilities

2019-05-27 Thread Matthew Butterick



> On May 27, 2019, at 3:20 PM, Joel McCracken  wrote:
> 
>  I suppose I might be able to do that, but I don't think it will solve my 
> overall problem. `pollen.rkt` gets loaded/computed repeatedly, once per page, 
> so it would lose context between pages. I would then have to re-compute it, 
> and/or read/write it to disk, which is what I am doing right now, which I 
> would like to avoid if possible.

I'm still not clear why you need to introduce global state. Didn't you say the 
alias names are embedded in the source files as `meta` values? So why not just 
have "pollen.rkt" generate the alias table every time it runs? (You can read 
the `metas` of a Pollen source without rendering the file, or even the `doc`, 
for that matter.)


-- 
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/57E3126B-962E-491C-ADE5-FDF95501EACA%40mbtype.com.
For more options, visit https://groups.google.com/d/optout.


Re: [pollen] A few questions about pollen capabilities

2019-05-27 Thread Joel Dueck
Why not just construct the links "blind"? For example

(require net/uri-codec)

(define (section name . elements)
  `(h2 [[id ,(uri-encode name)]] ,@elements))

(define (xref-link file id . elements)
  `(a [[href ,(format "~a.html#~a" file id)]] ,@elements))

(The uri-encode is just there to handle any spaces or other must-escape 
characters in the section name)

In this scheme the section tag just produces an  with an id attribute. 
It doesn't need to know anything about what file it's on.

In your doc you would do:

   ◊xref-link['my-post "Introduction"]{See the introduction to my post}

Which results in 

See the introduction to my post

-- 
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/b387d046-f140-401f-9134-3e26b7423811%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [pollen] A few questions about pollen capabilities

2019-05-10 Thread Joel McCracken
Interesting, thank you. Will requiring the pollen file also have it output 
the compiled file to the filesystem?

On Friday, May 10, 2019 at 11:30:22 AM UTC-4, Matthew Butterick wrote:
>
> You can also `(require "a-pollen-file.html.pm")` just like any Racket 
> module. Every Pollen source exports `doc`, which is an S-expression 
> representing the content of the document. 
>
> On May 10, 2019, at 8:12 AM, Joel McCracken  > wrote:
>
> The thing that worries me the most is the `racket a-pollen-file.html.pm`, 
> that seems like not the best way to try to treat a page as data. But I will 
> see how far I can go!
>
>
>

-- 
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/d3250231-7a65-4667-b35f-224b28b03075%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [pollen] A few questions about pollen capabilities

2019-05-10 Thread Matthew Butterick
You can also `(require "a-pollen-file.html.pm")` just like any Racket module. 
Every Pollen source exports `doc`, which is an S-expression representing the 
content of the document. 

> On May 10, 2019, at 8:12 AM, Joel McCracken  wrote:
> 
> The thing that worries me the most is the `racket a-pollen-file.html.pm`, 
> that seems like not the best way to try to treat a page as data. But I will 
> see how far I can go!

-- 
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/66769605-3724-4D3B-80B9-A5CCD2FE0D7D%40mbtype.com.
For more options, visit https://groups.google.com/d/optout.


Re: [pollen] A few questions about pollen capabilities

2019-05-10 Thread Joel McCracken
Thank you, I think you answered all my issues. I will try it out. 

The thing that worries me the most is the `racket a-pollen-file.html.pm`, 
that seems like not the best way to try to treat a page as data. But I will 
see how far I can go!

On Friday, May 10, 2019 at 4:44:04 AM UTC-4, Sorawee Porncharoenwase wrote:
>
> I want to be able to define aliases for pages, and be able to do like 
>> `@link[the-alias]`. I noticed that i could use metas to implement this, so 
>> that's fine.
>>
>> I want to have  a CLI command that outputs all the aliases and the files 
>> they refer to, as a way to remember the aliases. Ideally, I will eventually 
>> have emacs run this command to get suggestions while I program. 
>>
>> In general, I know I will want to implement new CLI commands of varous 
>> kinds, especially where I output information based upon post metadata. Is 
>> that something I could do without much difficulty?
>>
>
> This should be straightforward. You simply need to implement a simple 
> Racket program that does this.
>  
>
>> Not necessary, but another thing I was thinking about doing is 
>> implementing bi-directional links, that is, adding a section on some pages 
>> that says "what links here". Is this doable?
>>
>
> Doable, though might not be easy. I did something similar but only for 
> sections within a page. The trick is that you should not directly compile 
> into the final output, but instead compile to an intermediate structure. 
> Meanwhile, collect the links. Then, add another compilation pass that 
> consumes the intermediate structure and the linking data and produces the 
> final output. The fact that you want to do this across pages is going to 
> add some difficulty compared to mine, but it should be doable.
>  
>
>> When I write something, sometimes I want to declare the file to be a 
>> draft, and not have it appear in the final output, and/or have "published 
>> on" data available. I was thinking it would be handy if I could do 
>> something like:
>>
>> ◊(define-publish-status 'draft)
>>
>> and only have draft posts be rendered in development.
>>
>
> Take a look at 
> https://docs.racket-lang.org/pollen/Setup.html#%28def._%28%28lib._pollen%2Fsetup..rkt%29._setup~3aomitted-path~3f%29%29
>  
>
>> Is it possible to read a pollen file and get the sexpr representation, or 
>> something like it?
>>
>
> If you `racket a-pollen-file.html.pm`, you will get the sexpr 
> representation.
>

-- 
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/9d176c8a-e626-4141-8f6b-e070e01e088f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [pollen] A few questions about pollen capabilities

2019-05-10 Thread Sorawee Porncharoenwase
>
> I want to be able to define aliases for pages, and be able to do like
> `@link[the-alias]`. I noticed that i could use metas to implement this, so
> that's fine.
>
> I want to have  a CLI command that outputs all the aliases and the files
> they refer to, as a way to remember the aliases. Ideally, I will eventually
> have emacs run this command to get suggestions while I program.
>
> In general, I know I will want to implement new CLI commands of varous
> kinds, especially where I output information based upon post metadata. Is
> that something I could do without much difficulty?
>

This should be straightforward. You simply need to implement a simple
Racket program that does this.


> Not necessary, but another thing I was thinking about doing is
> implementing bi-directional links, that is, adding a section on some pages
> that says "what links here". Is this doable?
>

Doable, though might not be easy. I did something similar but only for
sections within a page. The trick is that you should not directly compile
into the final output, but instead compile to an intermediate structure.
Meanwhile, collect the links. Then, add another compilation pass that
consumes the intermediate structure and the linking data and produces the
final output. The fact that you want to do this across pages is going to
add some difficulty compared to mine, but it should be doable.


> When I write something, sometimes I want to declare the file to be a
> draft, and not have it appear in the final output, and/or have "published
> on" data available. I was thinking it would be handy if I could do
> something like:
>
> ◊(define-publish-status 'draft)
>
> and only have draft posts be rendered in development.
>

Take a look at
https://docs.racket-lang.org/pollen/Setup.html#%28def._%28%28lib._pollen%2Fsetup..rkt%29._setup~3aomitted-path~3f%29%29


> Is it possible to read a pollen file and get the sexpr representation, or
> something like it?
>

If you `racket a-pollen-file.html.pm`, you will get the sexpr
representation.

-- 
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/CADcuegtGKTcq_vjtzDhmK7GY0%2Bnbjyzu1avO9pX_DsWUw8j%3D%2BA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.