Print This Page

在HTML中嵌入D

News

Google
2008-03-03
Category: 更新(update)
Posted by: 刘策(yayv)
2007-04-24
Category: 更新(update)
Posted by: 刘策(yayv)
新设栏目 D语言高级教程 , 并转贴一篇 深入分析D语言接口与COM接口的关系 精华文章
2007-04-24
Category: 更新(update)
Posted by: 刘策(yayv)
新增2篇文章,均是对D语言数组进行探索的。一篇主要探索D语言数组长度修改时产生的影响, 另一篇主要探索D语言数组在多维的情况下如何初始化。

www.digitalmars.com [Home] [Search] [D]

在 HTML 中嵌入 D

The D compiler is designed to be able to extract and compile D code embedded within HTML files. This capability means that D code can be written to be displayed within a browser utilizing the full formatting and display capability of HTML.

For example, it is possible to make all uses of a class name actually be hyperlinks to where the class is defined. There's nothing new to learn for the person browsing the code, he just uses the normal features of an HTML browser. Strings can be displayed in green, comments in red, and keywords in boldface, for one possibility. It is even possible to embed pictures in the code, as normal HTML image tags.

Embedding D in HTML makes it possible to put the documentation for code and the code itself all together in one file. It is no longer necessary to relegate documentation in comments, to be extracted later by a tech writer. The code and the documentation for it can be maintained simultaneously, with no duplication of effort.

How it works is straightforward. If the source file to the compiler ends in .htm or .html, the code is assumed to be embedded in HTML. The source is then preprocessed by stripping all text outside of <code> and </code> tags. Then, all other HTML tags are stripped, and embedded character encodings are converted to ASCII. All newlines in the original HTML remain in their corresponding positions in the preprocessed text, so the debug line numbers remain consistent. The resulting text is then fed to the D compiler.

Here's an example of the D program "hello world" embedded in this very HTML file. This file can be compiled and run.

import std.c.stdio;

int main()
{
 printf("hello world\n");
 return 0;
}


Copyright (c) 1999-2001 by Digital Mars, All Rights Reserved