用NoWEB进行文学编程

WEB不是Web,它是进行文学编程的工具。 所谓文学编程, 即在文档同时编程, 程序实际上是嵌入在文章里面的。 这种方式为高德纳首先提出,并和TeX紧密结合。 首先有CWEB工具是为c语言设计的web程序, 后又有为fortran设计的web。
在pyhton中有leo既是IDE又是文章编程工具, 但是对ide我一向推崇emacs, 故也不可用。
找到三个语言无关的web,fuweb, nuweb, noweb.
fuweb比较简单, 但是好像不太标准, nuweb好像缺乏更新。 比较之下noweb最好。
在windows下有编译好的noweb不过要配合cygwin使用。 将需要的程序拷到对应目录,刷新TeX目录。 
在noweb的src中有example,可以玩玩, 在cygwin环境下,用noweave输出tex 用notangle输出程序。 我自己写了一点,非常nice。

noweb is a literate programming tool, created in 1989–1999 by Norman Ramsey,[1] and designed to be simple, easily extensible and language independent.

As in WEB and CWEB, the main components of noweb are two programs: "notangle", which extracts 'machine' source code from the source texts, and "noweave", which produces nicely-formatted printable documentation.

noweb supports TeXLaTeXHTML, and troff back ends and works with any programming language. Besides simplicity this is the main advantage over WEB, which needs different versions to support programming languages other than Pascal. (Thus the necessity of CWEB, which supports C and similar languages.)

Noweb's input[edit]

A noweb input text contains program source code interleaved with documentation. It consists of so-called chunks that are either documentation chunks or code chunks.

A documentation chunk begins with a line that starts with an at sign (@) followed by a space or newline. A documentation chunk has no name. Documentation chunks normally contain LaTeX, but noweb is also used with HTML, plain TeX, and with troff.

Code chunks are named. A code chunk begins with

<<chunk name>>=

on a line by itself. The double left angle bracket (<<) must be in the first column.

Each chunk is terminated by the beginning of another chunk. If the first line in the file does not mark the beginning of a chunk, it is assumed to be the first line of a documentation chunk.

Code chunks aren't treated specially by noweb's tools—they may be placed in any order and, when needed, they are just concatenated. Chunk references in code are dereferenced and the whole requested source code is extracted.

Example of a simple noweb program[edit]

This is an example of a "hello world" program with documentation:

\section{Hello world}
 
Today I awoke and decided to write
some code, so I started to write Hello World in \textsf C.
 
<<hello.c>>=
/*
  <<license>>
*/
#include <stdio.h>
 
int main(int argc, char *argv[]) {
  printf("Hello World!\n");
  return 0;
}
@
\noindent \ldots then I did the same in PHP.
 
<<hello.php>>=
<?php
  /*
  <<license>>
  */
  echo "Hello world!\n";
?>
@
\section{License}
Later the same day some lawyer reminded me about licenses.
So, here it is:
 
<<license>>=
This work is placed in the public domain.

Assuming that the above code is placed in a file named 'hello.nw', the command to extract the human-readable document in HTML format is:

noweave -filter l2h -index -html hello.nw | htmltoc > hello.html

... and in LaTeX format:

noweave -index -latex hello.nw > hello.tex

To extract machine source code:

notangle -Rhello.c hello.nw > hello.c
notangle -Rhello.php hello.nw > hello.php

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值