From f8240fd02ecee45120addd6748c7df98b3b91879 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 19 May 2020 14:15:57 -0700 Subject: [PATCH] make it possible to use `require` in templates --- pollen/private/render-helper.rkt | 3 ++- pollen/private/ts.rktd | 2 +- pollen/scribblings/tutorial-fourth.scrbl | 10 +++++----- pollen/scribblings/tutorial-second.scrbl | 2 +- pollen/test/data/poly/template.txt | 2 +- pollen/test/data/rerequire/template.txt | 2 +- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pollen/private/render-helper.rkt b/pollen/private/render-helper.rkt index af94d25..34c467e 100644 --- a/pollen/private/render-helper.rkt +++ b/pollen/private/render-helper.rkt @@ -47,5 +47,6 @@ (or (select-from-metas (setup:here-path-key SOURCE-PATH-STRING) metas) 'unknown))) (if (bytes? doc) ; if main export is binary, just pass it through doc - (include-template #:command-char COMMAND-CHAR (file TEMPLATE-PATH-STRING))))) + (splicing-let-syntax ([require (make-rename-transformer #'local-require)]) + (include-template #:command-char COMMAND-CHAR (file TEMPLATE-PATH-STRING)))))) (provide result)))))])) \ No newline at end of file diff --git a/pollen/private/ts.rktd b/pollen/private/ts.rktd index 8865b95..3eac2e6 100644 --- a/pollen/private/ts.rktd +++ b/pollen/private/ts.rktd @@ -1 +1 @@ -1589922948 +1589922957 diff --git a/pollen/scribblings/tutorial-fourth.scrbl b/pollen/scribblings/tutorial-fourth.scrbl index 5f751ed..c8012e8 100644 --- a/pollen/scribblings/tutorial-fourth.scrbl +++ b/pollen/scribblings/tutorial-fourth.scrbl @@ -199,11 +199,11 @@ The goal of this whole endeavor was to derive multiple output files from one sou @seclink["Templates" #:tag-prefixes '("tutorial-2")] should be familiar to you by now. As usual, the name of the template is @tt{template} plus the relevant file extension, so in this case @filepath{template.txt.p}. Add the file as follows: @fileblock["template.txt.p" @codeblock|{ -◊(local-require racket/list) +◊(require racket/list) ◊(apply string-append (filter string? (flatten doc))) }|] -What we're doing here is converting the X-expression to text in a smarter way. Because we're in a template, we use @racket[local-require] (rather than plain @racket[require]) to bring in @racketmodname[racket/list] so we can use the @racket[flatten] function. +What we're doing here is converting the X-expression to text in a smarter way. We use @racket[require] to bring in @racketmodname[racket/list] so we can use the @racket[flatten] function. To understand what the next line does, just read it from the inside out: ``Take the @racket[doc] export from the source file (which is an X-expression), @racket[flatten] it into a list, @racket[filter] with @racket[string?] (creating a list that's only strings) and @racket[apply] the @racket[string-append] function to these, resulting in one big string.'' Which is exactly what we need for a plain-text file. @@ -304,7 +304,7 @@ Then a @filepath{template.ltx.p}: @fileblock["template.ltx.p" @codeblock|{ \documentclass[a4paper,12pt]{letter} \begin{document} -◊(local-require racket/list) +◊(require racket/list) ◊(apply string-append (filter string? (flatten doc))) \end{document} }|] @@ -366,7 +366,7 @@ The template, not as easy: @fileblock["template.pdf.p" @codeblock|{ -◊(local-require racket/file racket/system) +◊(require racket/file racket/system) ◊(define latex-source ◊string-append{ \documentclass[a4paper,12pt]{letter} \begin{document} @@ -391,7 +391,7 @@ First, we use @filepath{template.pdf.p} rather than @filepath{template.pdf} for A quick narrative of the rest: @codeblock|{ -◊(local-require racket/file racket/system) +◊(require racket/file racket/system) }| We need @racketmodname[racket/file] for @racket[display-to-file] and @racket[file->bytes]; we need @racketmodname[racket/system] for @racket[system] (to use the command line). diff --git a/pollen/scribblings/tutorial-second.scrbl b/pollen/scribblings/tutorial-second.scrbl index 2396266..b22094a 100644 --- a/pollen/scribblings/tutorial-second.scrbl +++ b/pollen/scribblings/tutorial-second.scrbl @@ -298,7 +298,7 @@ And two major differences: -@margin-note{``So a template is also a Pollen source file?'' Not quite. More accurately, it's a fragment of Pollen source that is completed by adding the X-expression that comes out of one of your source files. Because of this, there are a few extra limitations on the code you can put in a template, though with easy workarounds (for instance, you can't use @racket[require] in a template, but you can use @racket[local-require], which accomplishes the same thing).} +@margin-note{``So a template is also a Pollen source file?'' Not quite. More accurately, it's a fragment of Pollen source that is completed by adding the X-expression that comes out of one of your source files. Because of this, there are a few extra limitations on the code you can put in a template, though with easy workarounds.} To see how this works, let's return to the source file we started in the last section: diff --git a/pollen/test/data/poly/template.txt b/pollen/test/data/poly/template.txt index a221b8a..63d1eee 100644 --- a/pollen/test/data/poly/template.txt +++ b/pollen/test/data/poly/template.txt @@ -1,2 +1,2 @@ -◊(local-require racket/list) +◊(require racket/list) ◊(apply string-append (filter string? (flatten doc))) \ No newline at end of file diff --git a/pollen/test/data/rerequire/template.txt b/pollen/test/data/rerequire/template.txt index a221b8a..63d1eee 100644 --- a/pollen/test/data/rerequire/template.txt +++ b/pollen/test/data/rerequire/template.txt @@ -1,2 +1,2 @@ -◊(local-require racket/list) +◊(require racket/list) ◊(apply string-append (filter string? (flatten doc))) \ No newline at end of file