[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Looking for "literate programming" tools for Scheme/LISP

A while ago, I prepared some really simple tools for processing files
that were both documents and source code.  The basic idea is to use a
verbatim environment (with *no* interesting processing) for code, but
under a different name.  Consequently, it will work for any programming
language, but will never do any fancy formatting.

The idea is this:  You write a ".ptex" file.  Two "projections" are
defined for such files: one converts a ".ptex" file to a ".tex" file;
the other converts it to a ".lsp" (or whateverP file.  Two new
environments are defined:

   program  --  converted directly to verbatim for ".tex";
                contents outout as-is for ".lsp".

   program-only -- like program, but discarded for ".tex"

All that's required are two AWK scripts and some entries in a

-------------------- Makefile entries --------------------

.SUFFIXES: .ptex .tex .lisp .lsp

.ptex.tex: ; awk -f totex < $*.ptex > $*.tex

.ptex.lsp: ; awk -f tocode < $*.ptex > $*.lsp

.ptex.lisp: ; awk -f tocode < $*.ptex > $*.lisp

-------------------- totex script --------------------
BEGIN { state="copy" }

state == "copy" && /^ *\\begin{program}/       {print "\\begin{verbatim}"; break}
state == "copy" && /^ *\\end{program}/         {print "\\end{verbatim}"; break}
state == "copy" && /^ *\\begin{program-only}/  {state="ignore"; break}
state == "copy"                                {print; break}

/^ *\\end{program-only}/                       {state = "copy"; break}
-------------------- tocode script --------------------
BEGIN { state="ignore" }

state == "copy" && /^ *\\end{program}/         {state = "ignore"; printf "\n"; break}
state == "copy" && /^ *\\end{program-only}/    {state = "ignore"; printf "\n"; break}
state == "copy"                                {print; break}

/^ *\\begin{program-only}/                     {state = "copy"; break}
/^ *\\begin{program}/                          {state = "copy"; break}
-------------------- end --------------------

Simple, but can be effective.

Jeff Dalton,                      JANET: J.Dalton@uk.ac.ed             
AI Applications Institute,        ARPA:  J.Dalton%uk.ac.ed@nsfnet-relay.ac.uk
Edinburgh University.             UUCP:  ...!ukc!ed.ac.uk!J.Dalton