Re: DRY way to insert source code into an article?
"Donald Roby" <[email protected]>
| Newsgroups | gmane.comp.programming.language-of-the-year |
|---|---|
| Message-ID | <[email protected]> |
--- In [email protected], "John Hughes" <john.hughes@...> wrote: > > > > > Hi all, > > I'm starting to write articles about programming and I'd like to > include source code snippets into my article. My strategy was to get > the source code examples working, then annotate sections of code which > should be imported into the body of the article. The goal here was to > avoid copy-paste problems of putting incorrect or stale source code > examples into the document. > > How do other pragmatists approach this problem? > > > > > . > > I generally use a very simple home-grown tool generating LaTeX or HTML (I like to tweak!). Whether I put LaTeX in the comments in my source code, or source code in my LaTeX for extraction by my tool, depends on the kind of document I'm writing. > > Something I've found very useful when doing this with Haskell is to build in the ability to run examples while rendering the document. When I include an example in the text, it's automatically compiled and run, and the output pasted back into the document, every time the document is generated. I've found many, many mistakes because the example output was actually an error message! > > Haskell's "calculator"-style interface makes this particularly easy to do, but I'm sure you could adapt the idea to Java. > > John Hughes > Haskell (at least in some versions) has some built-in support for literate programming, and many public examples are written in this style. See http://www.haskell.org/haskellwiki/Literate_programming for some explanation of what's commonly available. You'd still have to do some work to get it to html though. Since TeX has been discussed as a tool that can be used for part of this process,it's also worth noting that TeX itself is written in a literate programming system called WEB created by Donald Knuth, which essentially is preprocessed into TeX for documentation and into a "normal" programming language (originally Pascal, but I think there are other options now) for execution. http://tex.loria.fr/litte/knuthweb.pdf contains a description of this system and a smallish example. Knuth's WEB had no html support as it predates the other web. ;-) But there are also tools for converting TeX to html. It might take a bit of work to get something like this together for or in Java, but it certainly sounds like an interesting idea. -- Don Roby <[email protected]>