Idiomatic Pollen?
#55
Closed
opened 9 years ago by malcolmstill
·
3 comments
Loading…
Reference in New Issue
There is no content yet.
Delete Branch '%!s(<nil>)'
Deleting a branch is permanent. It CANNOT be undone. Continue?
I've just started playing around with Pollen and I'm trying to implement a static blog. In posts I want the option of including a table of contents.
I have some code that seems to work but it feels like a hack. Is this idiomatic Pollen?
In
directory-require.rkt
I have the following code:If I want to include a table of contents I call
◊make-toc{}
at the top of the markup file and any call to◊section[<level>]{<text>}
overwrites meta to include all of the appropriate HTML up to that point for the ToC at the top of the page:In
template.html
:Is there a more idiomatic way of doing this. I initially tried to hold a txepxr containing the ToC in meta but it can only include symbols/strings (hence having to use
->html
insection
)? Is there another way of passing data between a.pm
and a template other than meta?I wouldn’t hold myself out as the arbiter between “hack” and “idiom.” Everyone should write programs however they want ;)
Yes. You can extract anything from the
doc
part of a.pm
file usingselect-from-doc
. Thus, when setting up a template, you don’t need to usedoc
as a single indivisible thing. You can instead use it as a container for other things.Along those lines, here’s how I’d approach this problem.
The source file needs to do three things: a) provide the content of the document, b) provide a summary of the document's headings for an optional TOC, and c) toggle the TOC with a
meta
setting. So I would changedoc
to have two parts: abody
tag that contains the main content, and atoc-entries
tag that has the TOC entries.The source file will look the same:
directory-require.rkt
will be different. In this case, the TOC entries are generated whether or not the TOC has been requested in the source file. (Side benefit: if you’re working in DrRacket, it’s easier to see what’s going on when you run the file.) Notice that the TOC-gathering logic is now separated from thesection
function:Finally, rather than using
doc
directly, the template will now look like this, usingselect-from-doc
to extracttoc-entries
for the TOC, andbody
for the main content. Thetoc
inmetas
is used to determine whether to show or hide the TOC entries:Excellent reply, thanks!
PS. Check out the
gregor
package for handling date / time fields.