On this page:
context
8.3.1 HTML Rendering
camp-doc->html-xexpr
8.3.2 Collection Retrieval
page-link
page-link-doc
get-collection
8.3.3 Taxonomy Functions
get-taxonomy-terms
get-taxonomy-pages
8.3.4 Page Navigation
prev
next
8.3.5 Page Filtering
filter-pages
8.3.6 Date Formatting
~t
~d
9.1

8.3 Rendering and Context🔗ℹ

 (require camp) package: camp-lib

A render context is a table containing information about a page and its context within the current site build. It is provided render functions and provides navigation helpers and metadata.

hash-view

(hash-view context (slug
    url
    collection
    taxonomies))
  slug : string?
  url : string?
  collection : string?
  taxonomies : hash?
A hash-view representing a render context. The context is passed to render functions and provides access to taxonomy data.

The url field contains the canonical URL for the current page. The taxonomies field maps taxonomy keys to lists of normalized values for the current page. Use the prev and next functions with a context to navigate between pages.

8.3.1 HTML Rendering🔗ℹ

procedure

(camp-doc->html-xexpr doc [fallback])  (listof any/c)

  doc : document?
  fallback : (or/c (-> symbol? list? list? (or/c any/c #f)) #f)
   = #f
Renders a Punct document to HTML x-expressions with cross-references resolved. This is the primary function for converting document bodies in render functions.

For #lang camp/page documents, calls the body thunk to produce the content. For Punct documents, renders the document content with:

  • term elements resolved to hyperlinks

  • term-definition elements rendered as <dfn> with anchors

  • page-ref elements resolved to page links

Returns a list of x-expressions (the body content without an outer wrapper).

The optional fallback procedure handles custom elements. It receives the tag name, attributes, and rendered child elements. Return an x-expression to override rendering, or #f to use the default (passthrough) behavior.

(define (my-fallback tag attrs elems)
  (if (eq? tag 'callout)
      `(aside ((class "callout")) ,@elems)
      #f))
 
(define body (camp-doc->html-xexpr doc my-fallback))

8.3.2 Collection Retrieval🔗ℹ

struct

(struct page-link (url title metas)
    #:extra-constructor-name make-page-link
    #:transparent)
  url : string?
  title : string?
  metas : hash?
A lightweight reference to a page, used for navigation and collection retrieval. Contains the page’s URL, title, and full metadata hash.

procedure

(page-link-doc pl)  document?

  pl : page-link?
Retrieves the full Punct document for a page-link. This allows paginated pages to display full post content rather than just titles and metadata.

Must be called within a build context (i.e., when current-site-info is set).

(paginate "blog" #:per-page 5
  (λ (items pagination)
    `(main
      ,@(for/list ([p items])
          `(article
            (h2 ,(page-link-title p))
            ,(camp-doc->html-xexpr (page-link-doc p))))
      ,(pagination-nav pagination))))

procedure

(get-collection name    
  [#:limit limit    
  #:full-docs? full-docs?])  list?
  name : string?
  limit : (or/c #f exact-positive-integer?) = #f
  full-docs? : boolean? = #f
Retrieves pages from a named collection. Returns a list of page-link structs by default, or full Punct documents if full-docs? is #t. The optional limit restricts the number of results returned.

8.3.3 Taxonomy Functions🔗ℹ

procedure

(get-taxonomy-terms collection-name    
  taxonomy-key)  (listof string?)
  collection-name : string?
  taxonomy-key : string?
Returns all distinct values for a taxonomy within a collection, ordered by first appearance in the collection’s sort order.

procedure

(get-taxonomy-pages collection-name 
  taxonomy-key 
  [term]) 
  (or/c (listof page-link?) hash?)
  collection-name : string?
  taxonomy-key : string?
  term : (or/c string? #f) = #f
With two arguments, returns a hash mapping each taxonomy term to its list of page-links. With three arguments (including a specific term), returns the list of page-links for that term.

8.3.4 Page Navigation🔗ℹ

procedure

(prev ctxt [taxonomy-key term])  (or/c page-link? #f)

  ctxt : context?
  taxonomy-key : string? = #f
  term : string? = #f
Returns the previous page relative to the current page, or #f if there is no previous page.

With only ctxt, navigates within the current page’s collection in sort order.

With taxonomy-key, navigates within the pages sharing the current page’s first value for that taxonomy. For example, if the current page has tags: emacs, lisp, then (prev ctxt "tags") navigates within pages tagged "emacs".

With both taxonomy-key and term, navigates within pages having that specific taxonomy term.

(prev ctxt)                  ; previous in collection
(prev ctxt "tags")           ; previous in first tag
(prev ctxt "tags" "emacs")   ; previous in "emacs" tag

procedure

(next ctxt [taxonomy-key term])  (or/c page-link? #f)

  ctxt : context?
  taxonomy-key : string? = #f
  term : string? = #f
Returns the next page relative to the current page, or #f if there is no next page.

Calling conventions are the same as prev: with only ctxt, navigates within the collection; with taxonomy-key, navigates within pages sharing the current page’s first value for that taxonomy; with both taxonomy-key and term, navigates within pages having that specific term.

(next ctxt)                  ; next in collection
(next ctxt "tags")           ; next in first tag
(next ctxt "tags" "emacs")   ; next in "emacs" tag

8.3.5 Page Filtering🔗ℹ

procedure

(filter-pages pages    
  [#:date-from date-from    
  #:date-to date-to    
  #:date-key date-key    
  #:taxonomies taxonomies])  (listof page-link?)
  pages : (listof page-link?)
  date-from : (or/c date-provider? #f) = #f
  date-to : (or/c date-provider? #f) = #f
  date-key : symbol? = 'date
  taxonomies : hash? = #hasheq()
Filters a list of page-links by date range and/or taxonomy values. All filters are combined with AND logic—pages must pass all specified filters to be included.

  • #:date-from and #:date-to: Inclusive date bounds. Pages without a date (or with unparseable dates) are excluded when date filtering is active.

  • #:date-key: The metadata key to use for date comparison (default: 'date).

  • #:taxonomies: A hash where keys are taxonomy names (strings or symbols) and values are either a single string (exact match) or a list of strings (match any).

This function is useful for building custom page sets or for book parts that filter collections:

(require gregor)  ; for date constructor
 
;; Get 2024 posts tagged "featured"
(filter-pages (get-collection "blog")
              #:date-from (date 2024 1 1)
              #:date-to (date 2024 12 31)
              #:taxonomies (hasheq 'tags "featured"))
 
;; Get posts in any of these categories
(filter-pages posts
              #:taxonomies (hasheq 'category '("tech" "science")))

Taxonomy values in page metadata can be comma-separated strings or lists; both formats are normalized before matching.

8.3.6 Date Formatting🔗ℹ

procedure

(~t t pattern)  string?

  t : date-provider?
  pattern : string?
Formats a date using CLDR (Unicode Common Locale Data Repository) patterns. This function is re-exported from the gregor library.

Common pattern elements:

Pattern

  

Output

  

Example

yyyy

  

4-digit year

  

2025

yy

  

2-digit year

  

25

MMMM

  

Full month name

  

January

MMM

  

Abbreviated month

  

Jan

MM

  

2-digit month

  

01

M

  

1 or 2-digit month

  

1

dd

  

2-digit day

  

05

d

  

1 or 2-digit day

  

5

EEEE

  

Full weekday name

  

Wednesday

EEE

  

Abbreviated weekday

  

Wed

Patterns can be combined with literal text:
(~t my-date "MMMM d, yyyy")    ; "January 15, 2025"
(~t my-date "d MMM yyyy")      ; "15 Jan 2025"
(~t my-date "yyyy-MM-dd")      ; "2025-01-15"
(~t my-date "EEE, MMM d")      ; "Wed, Jan 15"

See the CLDR documentation for the complete list of pattern symbols.

procedure

(~d pattern v)  string?

  pattern : string?
  v : (or/c string? date-provider?)
Formats a date using CLDR patterns, automatically converting ISO 8601 date strings. This is the recommended function for formatting dates from page metadata, since Punct documents provide dates as strings. The pattern comes first, matching Racket’s format convention.

;; In a render function:
(define date (meta-ref doc 'date))  ; might be "2025-01-15" or a date object
 
(~d "d MMM yyyy" date)      ; "15 Jan 2025" - works either way
(~d "yyyy-MM-dd" date)      ; "2025-01-15" - for datetime attributes

If v is already a date-provider?, it is passed directly to ~t. If v is a string, it is first parsed as an ISO 8601 date.