[plt-scheme] Including example code from a file in Scribble

From: Noel Welsh (noelwelsh at gmail.com)
Date: Sun Aug 5 10:57:16 EDT 2007

Hi all,

I have some code in a file.  E.g.:


(define foo 42)

Say I'm writing a document in Scribble, and I want to include the code
from the file as Scheme formatted text:


The answer to life can be calculated in Scheme using the following expression:

...  [include and typeset code from foo.ss]

How do I fill in the dots?  I see plenty of forms in Scribble for
typesetting literal code but none for reading it from a file.


PS: Code I tried that doesn't work:

(module util mzscheme

   (planet "port.ss" ("schematics" "port.plt" 1)))

   (only (lib "manual.ss" "scribble") schemeblock))

  (provide include-schemeblock)

  (define-syntax (include-schemeblock stx)
    (syntax-case stx ()
      [(include-schemeblock file-name)
       (let ([file-name (syntax-object->datum (syntax file-name))])
             ([(e0 e1 ...)
               (with-input-from-file file-name
                 (lambda ()
                    (lambda (p) (read-syntax file-name p))
              (schemeblock e0 e1 ...)))))]))


Posted on the users mailing list.