@@ -21,8 +21,11 @@ def __init__(self):
2121
2222class Head (Tag ):
2323
24- def __init__ (self ):
24+ def __init__ (self , title = None ):
2525 super ().__init__ ('head' , '' )
26+ if title :
27+ self ._title_tag = Tag ('title' , title )
28+ self .content = str (self ._title_tag )
2629
2730
2831class Body (Tag ):
@@ -44,10 +47,15 @@ def display(self, file=None):
4447
4548class HtmlDoc (object ):
4649
47- def __init__ (self ):
48- self ._doc_type = DocType ()
49- self ._head = Head ()
50- self ._body = Body ()
50+ # def __init__(self, title=None):
51+ # self._doc_type = DocType()
52+ # self._head = Head(title)
53+ # self._body = Body()
54+
55+ def __init__ (self , doc_type , head , body ):
56+ self ._doc_type = doc_type
57+ self ._head = head
58+ self ._body = body
5159
5260 def add_tag (self , name , contents ):
5361 self ._body .add_tag (name , contents )
@@ -61,10 +69,24 @@ def display(self, file=None):
6169
6270
6371if __name__ == '__main__' :
64- my_page = HtmlDoc ()
65- my_page .add_tag ('h1' , 'Main heading' )
66- my_page .add_tag ('h2' , 'sub-heading' )
67- my_page .add_tag ('p' , 'This is a paragraph that will appear on the page' )
68-
69- with open ('test.html' , 'w' ) as test_doc :
70- my_page .display (file = test_doc )
72+ # my_page = HtmlDoc('Demo HTML Doc')
73+ # my_page.add_tag('h1', 'Main heading')
74+ # my_page.add_tag('h2', 'sub-heading')
75+ # my_page.add_tag('p', 'This is a paragraph that will appear on the page')
76+ #
77+ # with open('test.html', 'w') as test_doc:
78+ # my_page.display(file=test_doc)
79+
80+ new_body = Body ()
81+ new_body .add_tag ('h1' , 'Aggregation' )
82+ new_body .add_tag ('p' , "Unlike <strong>composition</strong>, aggregation uses existing instances"
83+ " of objects to build up another object." )
84+ new_body .add_tag ('p' , "The composed object doesn't actually own the objects that it's composed of"
85+ " - if it's destroyed, those objects continue to exist." )
86+
87+ new_docType = DocType ()
88+ new_header = Head ('Aggregation document' )
89+ my_page = HtmlDoc (new_docType , new_header , new_body )
90+
91+ with open ('test3.html' , 'w' ) as test_doc :
92+ my_page .display (file = test_doc )
0 commit comments