任何编程语言的文学化编程工具
Literate programming tool for any language

原始链接: https://github.com/zyedidia/Literate

文学化编程,由Donald Knuth倡导,优先考虑代码对人类的可读性,而非机器的直接执行效率。Literate工具旨在促进这种方法,并力求改进和简化最初的CWEB系统。它支持多种语言的语法高亮显示、Markdown格式化,并生成具有良好可读性的注释代码。 Literate允许在解释性文本中嵌入代码片段,从而围绕程序逻辑构建清晰的叙述。它包含诸如TeX公式支持、代码段之间自动超链接以及将编译器错误报告回溯到原始文学化源代码等功能。安装说明适用于macOS(通过Homebrew)和其他系统(需要D语言工具)。它还提供了与Micro编辑器和Vim的集成。命令行工具`lit`提供了代码分离、HTML编织、输出目录指定和编译器错误报告等选项。

这篇 Hacker News 讨论围绕着“任何语言的文学化编程工具”的提交展开。评论者们强调了文学化编程 (LP) 的好处,尤其是在 shell 脚本中,它能够将文档和代码编织在一起,使代码更容易理解和维护。一位用户分享了使用 Literate Coffeescript 的积极经验,发现它在数年后极大地提高了代码的可读性。其他人讨论了使用 Emacs 和 org-mode 进行 LP 的优势,提到了代码的“分解 (tangle)”和“编织 (weave)”功能。LP 与现代 IDE、AI 编码代理和 Entangled 等双向同步工具的集成也受到了讨论,同时还有一些使用 LuaLaTeX、Haskell 和 MoonBit 的例子。一些用户认为传统的编程实践掩盖了其好处,并增加了编写重复代码的风险,而另一些用户则提倡注释“为什么”而不是“做什么”,以使上下文与当前目标保持同步。
相关文章

原文

What is Literate programming?

Literate programming is a style of programming invented by Donald Knuth, where the main idea is that a program's source code is made primarily to be read and understood by other people, and secondarily to be executed by the computer.

This frees the programmer from the structure of a program imposed by the computer and means that the programmer can develop programs in the order of the flow of their thoughts.

A Literate program generally consists of explanation of the code in a natural language such as English, interspersed with snippets of code to be executed. This means that Literate programs are very easy to understand and share, as all the code is well explained.


Literate is a tool for creating literate programs.

The goal of this project is to create a literate programming tool which keeps most, if not all of the features of Knuth and Levy's original CWEB system, but simplifies the system and adds even more features.

You can view the main website about Literate here including a manual on how to use Literate.

  • Supports any language including syntax highlighting and pretty printing in HTML
  • Markdown based -- very easy to read and write Literate source.
  • Reports syntax errors back from the compiler to the right line in the literate source
  • Generates readable and commented code in the target language (the generated code is usable by others)
  • Supports TeX equations with $ notation.
  • Literate source code is readable whether you are looking at the .lit file, or the generated HTML.
  • Highly customizable (you can add your own HTML or CSS)
  • Runs fast -- wc.lit compiled for me in 7ms for both code and HTML output
  • Automatically generates hyperlinks between code sections
  • Formatted output similar to CWEB
  • Supported by micro (by default)
  • Compatible with Vim (literate.vim)

Here is a trivial example of a literate program saved in the file hello.lit.

For a full example of a literate program, please see examples/wc.lit which is a literate implementation of the wc (word count) program found on Unix systems. You can find the compiled html here.

@title Hello world in C

@s Introduction

This is an example hello world C program.
We can define codeblocks with `---`

--- hello.c
@{Includes}

int main() {
    @{Print a string}
    return 0;
}
---

Now we can define the `Includes` codeblock:

--- Includes
#include <stdio.h>
---

Finally, our program needs to print "hello world"

--- Print a string
printf("hello world\n");
---

To compile this code simply run

$ lit hello.lit

Which generates hello.c and hello.html.

You can also find this program in examples/hello.lit.

On Mac you can use brew to build Literate from source:

$ brew tap zyedidia/literate
$ brew install --HEAD literate

For now, Literate is head only.


Literate is made with the D programming language so you must install dmd (D compiler) and dub (D package manager). Then you should download the zip or clone the repository and run the following commands:

You can find the binary in path/to/Literate/bin (you may want to add this to your path or move it to /usr/local/bin).

The micro editor has support for literate by default. Download it here.

You might also want to go install the Vim plugin (it has syntax highlighting of the embedded code, linting with Neomake, and jumping to codeblock definitions). I'm sorry that no other editors are supported -- I don't know how to make plugins for other editors.

Lit: Literate Programming System

Usage: lit [options] <inputs>

Options:
--help       -h         Show this help text
--tangle     -t         Only compile code files
--weave      -w         Only compile HTML files
--no-output  -no        Do not generate any output files
--out-dir    -odir DIR  Put the generated files in DIR
--compiler   -c         Report compiler errors (needs @compiler to be defined)
--linenums   -l    STR  Write line numbers prepended with STR to the output file
--md-compiler COMPILER  Use COMPILER as the markdown compiler instead of the built-in one
--version    -v         Show the version number and compiler information

For more information see the manual.

Literate is written in Literate D and you can find the source code in the lit directory. You can also read the source code compiled by Literate here. I am happy to accept pull requests, and if you find any bugs, please report them. Thanks!

联系我们 contact @ memedata.com