Scribble hacks: insert module code
get-module-content for showing module implementation
Normally Scribble, the Racket’s documentation tool, does not provide a way to see how a function/object/module is actually defined in the code. Just recently I found a good-enough way to insert the whole content of the module we are documenting with Scribble.
Here’s my implementation:
1 2 3 4 5 6 7 8 9 10 11 |
(define (get-module-content module-name) (~> module-name string->symbol (module-path-index-join #false) module-path-index-resolve resolved-module-path-name file->lines (filter (lambda (s) (not (regexp-match-exact? #rx"^;.*" s))) _) (string-join "\n") string-trim)) |
The input is a module name in the form that you would use in absolute require form, except that it is a string, not a bare syntax.
get-module-content will reach out to Your Racket package database and find a module that you gave. It will read it and remove all comments. It returns a Racket string.
The string? returned by get-module-content can later be used to either extract some more info but it can be effectively used to insert the full module implementation block.
get-module-content usage inside Scribble
In Scribble I insert the module source code like so:
1 2 3 |
The source code of this module: @codeblock[@get-module-content{mypkg/utils/path}] |