From 8d8aa53c5058a687596ec15fdf79eecc1be66466 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 28 Jun 2015 12:00:02 -0700 Subject: [PATCH] use submodule for settable values --- scribblings/world.scrbl | 18 ++++++++-------- test/data/override/directory-require.rkt | 26 +++++++++++++----------- world.rkt | 6 +++--- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/scribblings/world.scrbl b/scribblings/world.scrbl index 38d05d8..05ea927 100644 --- a/scribblings/world.scrbl +++ b/scribblings/world.scrbl @@ -32,26 +32,28 @@ A parameter that determines whether the @racket[world:directory-require] file is -@section{Values that can be overwritten with @racket[local:]} +@section{Settable values} -These values can be changed by overriding them in your @racket["directory-require.rkt"] source file. Use @racket[define] to make a variable with the same name as the one in @racket[pollen/world], but with the prefix @racket[local:] instead of @racket[world:]. Assign it whatever value you like. When Pollen runs, these definitions will supersede those in @racket[pollen/world]. +These values can be changed by overriding them in your @racket["directory-require.rkt"] source file. Within this file, create a submodule called @racket[config]. (More about @secref["submodules" #:doc '(lib "scribblings/guide/guide.scrbl")].) Then within this submodule, use @racket[define] to make variable with the same name as the one in @racket[pollen/world], but without the @racket[world:] prefix. Assign it whatever value you like. Repeat as needed. When Pollen runs, these definitions will supersede those in @racket[pollen/world]. -For instance, suppose you wanted the main export of every Pollen source file to be called @racket[van-halen] rather than @racket[doc], the extension of Pollen markup files to be @racket[rock] rather than @racket[pm], and the command character to be @litchar{🎸} instead of @litchar{◊}. Your @racket["directory-require.rkt"] would include these defintions: +For instance, suppose you wanted the main export of every Pollen source file to be called @racket[van-halen] rather than @racket[doc], the extension of Pollen markup files to be @racket[.rock] rather than @racket[.pm], and the command character to be @litchar{🎸} instead of @litchar{◊}. Your @racket["directory-require.rkt"] would look like this: @fileblock["directory-require.rkt" @codeblock{ #lang racket/base -(provide (all-defined-out)) -(define local:main-export 'van-halen) -(define local:markup-source-ext 'rock) -(define local:command-char #\🎸) + +(module config racket/base + (provide (all-defined-out)) + (define main-export 'van-halen) + (define markup-source-ext 'rock) + (define command-char #\🎸)) }] Though any of the values below can be overridden, it may not always be wise to do so. For instance, if you redefined @racket[world:fallback-template-prefix], you would simply break the fallback-template mechanism, because it would look for files that don't exist. But we don't live in a nanny state, so you are entrusted to say what you mean and accept the consequences. Of course, you can restore the defaults simply by deleting these defined values from @racket["directory-require.rkt"]. -These overridable values also come with a corresponding @racket[get-] function that will return the @racket[local:] value if it exists, otherwise the @racket[world:] value. In the example above, @racket[world:command-char] would be @litchar{◊} no matter what, but @racket[world:get-command-char] would return @litchar{🎸}. +These settable values also come with a corresponding @racket[get-] function that will return the @racket[config] value if it exists, otherwise the @racket[world:] value. In the example above, @racket[world:command-char] would be @litchar{◊} no matter what, but @racket[world:get-command-char] would return @litchar{🎸}. @defoverridable[default-port integer?]{ diff --git a/test/data/override/directory-require.rkt b/test/data/override/directory-require.rkt index 734b0f0..924b917 100644 --- a/test/data/override/directory-require.rkt +++ b/test/data/override/directory-require.rkt @@ -4,15 +4,17 @@ (define (root . xs) `(rootover ,@xs)) -(define local:pollen-version "42") - -(define local:preproc-source-ext 'ppover) -(define local:markup-source-ext 'pmover) -(define local:markdown-source-ext 'pmdover) -(define local:null-source-ext 'p) -(define local:pagetree-source-ext 'ptreeover) - -(define local:command-char #\∆) -(define local:main-export 'docover) -(define local:meta-export 'metasover) -(define local:meta-tag-name 'metaover) \ No newline at end of file +(module config racket/base + (provide (all-defined-out)) + (define pollen-version "42") + + (define preproc-source-ext 'ppover) + (define markup-source-ext 'pmover) + (define markdown-source-ext 'pmdover) + (define null-source-ext 'p) + (define pagetree-source-ext 'ptreeover) + + (define command-char #\∆) + (define main-export 'docover) + (define meta-export 'metasover) + (define meta-tag-name 'metaover)) \ No newline at end of file diff --git a/world.rkt b/world.rkt index 721323c..a5df93e 100644 --- a/world.rkt +++ b/world.rkt @@ -11,19 +11,19 @@ (build-path (current-project-root) directory-require)) ;; parameters should not be made settable. +(define-for-syntax config-submodule-name 'config) (define-syntax (define-settable stx) (syntax-case stx () [(_ name default-value) (with-syntax ([base-name (format-id stx "~a" #'name)] - [local:name (format-id stx "local:~a" #'name)] [get-name (format-id stx "get-~a" #'name)] + [config-submodule (format-id stx "~a" config-submodule-name)] [fail-thunk-name (format-id stx "fail-thunk-~a" #'name)] ) #'(begin (define base-name default-value) (define fail-thunk-name (λ _ base-name)) (define get-name (λ _ (with-handlers ([exn:fail? fail-thunk-name]) - (dynamic-require (get-path-to-override) 'local:name fail-thunk-name))))))])) - + (dynamic-require `(submod ,(get-path-to-override) config-submodule) 'base-name fail-thunk-name))))))])) (define-settable pollen-version "0.001")