use submodule for settable values

pull/84/head
Matthew Butterick 10 years ago
parent 333a13da99
commit 8d8aa53c50

@ -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" @fileblock["directory-require.rkt"
@codeblock{ @codeblock{
#lang racket/base #lang racket/base
(provide (all-defined-out))
(define local:main-export 'van-halen) (module config racket/base
(define local:markup-source-ext 'rock) (provide (all-defined-out))
(define local:command-char #\🎸) (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. 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"]. 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?]{ @defoverridable[default-port integer?]{

@ -4,15 +4,17 @@
(define (root . xs) (define (root . xs)
`(rootover ,@xs)) `(rootover ,@xs))
(define local:pollen-version "42") (module config racket/base
(provide (all-defined-out))
(define pollen-version "42")
(define local:preproc-source-ext 'ppover) (define preproc-source-ext 'ppover)
(define local:markup-source-ext 'pmover) (define markup-source-ext 'pmover)
(define local:markdown-source-ext 'pmdover) (define markdown-source-ext 'pmdover)
(define local:null-source-ext 'p) (define null-source-ext 'p)
(define local:pagetree-source-ext 'ptreeover) (define pagetree-source-ext 'ptreeover)
(define local:command-char #\∆) (define command-char #\∆)
(define local:main-export 'docover) (define main-export 'docover)
(define local:meta-export 'metasover) (define meta-export 'metasover)
(define local:meta-tag-name 'metaover) (define meta-tag-name 'metaover))

@ -11,19 +11,19 @@
(build-path (current-project-root) directory-require)) (build-path (current-project-root) directory-require))
;; parameters should not be made settable. ;; parameters should not be made settable.
(define-for-syntax config-submodule-name 'config)
(define-syntax (define-settable stx) (define-syntax (define-settable stx)
(syntax-case stx () (syntax-case stx ()
[(_ name default-value) [(_ name default-value)
(with-syntax ([base-name (format-id stx "~a" #'name)] (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)] [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)] ) [fail-thunk-name (format-id stx "fail-thunk-~a" #'name)] )
#'(begin #'(begin
(define base-name default-value) (define base-name default-value)
(define fail-thunk-name (λ _ base-name)) (define fail-thunk-name (λ _ base-name))
(define get-name (λ _ (with-handlers ([exn:fail? fail-thunk-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") (define-settable pollen-version "0.001")

Loading…
Cancel
Save