[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.:

foo.ss

(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:

foo.scrbl

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.

Thanks,
Noel


PS: Code I tried that doesn't work:


(module util mzscheme

  (require-for-syntax
   (planet "port.ss" ("schematics" "port.plt" 1)))

  (require
   (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))])
         (with-syntax
             ([(e0 e1 ...)
               (with-input-from-file file-name
                 (lambda ()
                   (port->list
                    (lambda (p) (read-syntax file-name p))
                    (current-input-port))))])
           (syntax
            (begin
              (schemeblock e0 e1 ...)))))]))

  )


Posted on the users mailing list.