s c h e m a t i c s : c o o k b o o k

/ WebHome / AuthorGuide / Cookbook.IncludingSourceCode

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Including Source Code in the Cookbook

Keywords and Inline Expressions

Keywords and other inline expressions can be highlighted and rendered in fixed font by surrounding them with "=" signs. For example, the notorious function name, call-with-current-continuation, was just embedded there using the following syntax:

=call-with-current-continuation=

Small Source Code Snippets

Small source code snippets can be formatted using the SyntaxHighlightingPlugin syntax, for example:

%begin scheme%
(define hello
  (lambda ()
    (display "Hello, web page")))
%end%

This results in the code being displayed as follows:

(define hello
  (lambda ()
    (display "Hello, web page")))

Including External Code

For any non-trivial code, and perhaps even for some trivial code, it's better to keep the code separately from the book text. Among other things, this will allow the code to be more easily run and tested within Scheme, as well as making it easier to package the code for distribution.

The current approach to including external code is that for each code sample, a topic should be created in the Scm web, for example Scm.Start1. Note that using a WikiName may be helpful, but it's not essential in this case. The raw source code should be stored in this topic - for now, simply cutting and pasting the source from a local copy should suffice, later we'll set up ways to manage this better.

Once the code is stored in a topic, it can be included in the book using syntax like this:

%begin scheme%
%INCLUDE{"Scm.Start1"}%
%end%

This results in the following:

(define (display-type x)
  (cond
   [(number? x) (display "It's a number!")]
   [(string? x) (display "It's a string!")]
   [else (display "It's something else!")]))

-- AntonVanStraaten - 31 Mar 2004

Comments

Is it possible to keep the original indenting? I'm a little annoyed with the indenting on StringRecipeDecodingBinaryMessages. Looking closer, it seems it the square brackets that are causing the problem (see the so-expression).

-- JensAxelSoegaard - 01 Apr 2004

Looks like BeautifierPlugin can't really handle Scheme properly, and fixing it looks like work (I looked at the Perl source; it's fairly primitive). Browsing the plugin list at twiki.org, I found and installed SyntaxHighlightingPlugin, which uses GNU enscript to do the highlighting. I've changed StringRecipeDecodingBinaryMessages to use this - check it out and see what you think. The tags you need look like this:

%begin scheme%
(let ([a 0])
      [b 1])
  (+ a b))
%end%

which produces this:

(let ([a 0])
      [b 1])
  (+ a b))

which improves on BeautifierPlugin's indentation:

(let ([a 0])
  [b 1])
(+ a b))

Long term, I'm a little concerned that shelling out to enscript to format every code snippet could be hard work for the server, but for now I think it's OK.

BTW, enscript generates font elements with hardcoded colors, and I'm not crazy about the colors. I have about a dozen lines of Perl lying around somewhere that translates this into CSS classes. When I get a chance I'll patch that in, at which point the source highlighting will be under the control of the skin (and will even support per-user customization).

Noel, you previously mentioned Moshi's Scheme code formatting abilities. Was that based on WebIt, or do you have some other code for that?

-- AntonVanStraaten - 01 Apr 2004

Moshi supports enscript. In addition BrentFulgham wrote some formatting code for the OriginalCookbook and I've brutually hacked it into my Moshi install (not in CVS yet). The code uses regexps and is in /doc/cookbook/fontify.ss

-- NoelWelsh - 02 Apr 2004

Anton, the enscript output is much better. Changing the colors of it much be relatively easy. If it turns out to be slow, we must figure out a way to cache the pages, so only new code snippets are passed to enscript.

Not that long ago I looked at the source of the source highlighter of the syntax checker tool, because I wanted to add a "Save highlighted code as HTML"-button. I was hoping to steal/reuse a function returning the code as syntax objects with color properties. However I had a hard time grasping the relation to the GUI as such, so I dropped the idea. It would however be cool to have the same syntax highlighting as in DrScheme. The code is in syncheck.ss.

-- JensAxelSoegaard - 02 Apr 2004

I've updated this topic to describe the new SyntaxHighlightingPlugin instead of BeautifierPlugin. I've also changed the converted cookbook pages to use this. The new plugin has had the side-effect of fixing the CookBook page so that it displays the whole (original) book, complete with source code.

I'm fine with enscript for now - I doubt we'll need to worry about performance for some time. As you say, caching could adddress that. Reusing PLT's Check Syntax sounds ambitious! Their new "dumb" syntax highlighter might be easier.

-- AntonVanStraaten - 03 Apr 2004

CookbookForm
TopicType: Recipe
ParentTopic: AuthorRecipes
TopicOrder: 050

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest