import crawdad class HelloWorldDoc (crawdad.HtmlDocument): def mainBody (self): return self.P ("Hello world.")
Subclass HelloWorldDoc inherits lots of behavior from
HtmlDocument.
Crawdad's HtmlDocument includes a model for a well formed
HTML document: a DOCTYPE
definition followed by an <HTML>
entity, which contains a <HEAD>
with at least a <TITLE>
and a <BODY>
.
Subclasses like HelloWorldDoc can override methods like
to define custom content.
In this example, HelloWorldDoc redefines mainBody
to include custom content--a <P>
(paragraph)
containing the words ``Hello world.''
The inherited P method creates an HtmlElement
object initialized to generate a <P>
with the contents ``Hello
world.''
To generate the HTML document and print it to standard output, create an instance of HelloWorldDoc and call its printHtml method.
>>> doc = HelloWorldDoc () >>> doc.printHtml () <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML><HEAD><TITLE>Untitled document</TITLE> </HEAD> <BODY><P>Hello world.</P> </BODY> </HTML>
The resulting HTML document is structurally correct.
It includes a document type definition; HTML
,
HEAD
, and BODY
tags; and a default title.
All tags are correctly rendered, nested, and closed.