11#!/usr/bin/python
22
3- class element (object ):
4- tag = 'html'
3+ class Element (object ):
54
5+ #tag = 'html'
66 indent = ' '
7-
87 def __init__ (self , content = None , ** attributes ):
9-
8+
109 self .content = []
10+
11+ self .attributes = attributes
1112
1213 if content is not None :
14+
1315 self .content .append (content )
14- self .attributes = attributes
15- def append (self , content ):
16+ def append (self , content ):
1617
1718 self .content .append (content )
1819
1920 def render_tag (self , current_ind ):
2021
2122 attrs = "" .join ([' {}={}' .format (key ,val ) for key , val in self .attributes .items ()])
2223
23- tag_str = "{}<{}{}>" ) .format (current_ind ,self .tag , attrs )
24+ tag_str = "{}<{}{}>" .format (current_ind ,self .tag ,attrs )
2425
2526 return tag_str
2627
@@ -29,72 +30,104 @@ def render(self, file_out, current_ind=""):
2930 file_out .write (self .render_tag (current_ind ))
3031 file_out .write ('\n ' )
3132
32- for c in self .content :
33+ for con in self .content :
3334 try :
3435 file_out .write (current_ind + self .indent + con + '\n ' )
36+
3537 except TypeError :
3638 con .render (file_out , current_ind + self .indent )
3739
38- file_out .write ("{}<{}> + \n " .format (current_ind , self .tag ))
39-
40-
41- class Html (element ):
40+ file_out .write ("{}</{}> \n " .format (current_ind , self .tag ))
4241
42+ class Html (Element ):
4343 tag = 'html'
44+ def render (self , file_out ,current_ind = "" ):
4445
45- class Body (element ):
46+ file_out .write ("<!DOCTYPE html>\n " )
47+ Element .render (self ,file_out , current_ind = current_ind )
4648
49+ class Body (Element ):
4750 tag = 'body'
48-
49- class P (element ):
50-
51+
52+ class P (Element ):
5153 tag = 'p'
5254
53- class Head (element ):
54-
55+ class Head (Element ):
5556 tag = 'head'
56-
57- class OneLineTag (element ):
58-
57+
58+ class OneLineTag (Element ):
59+
5960 def render (self , file_out , current_ind = "" ):
6061
6162 file_out .write (self .render_tag (current_ind ))
62-
63- for c in self .content :
64- file_out .write (con )
6563
66- file_out .write ("</{}>\n " ).format (self .tag ))
64+ for con in self .content :
65+ file_out .write (con )
66+
67+ file_out .write ("</{}> \n " .format (self .tag ))
68+
69+ class Title (OneLineTag ):
6770
68- class SelfClosingTag (OneLineTag ):
71+ tag = 'title'
72+
73+ class SelfClosingTag (Element ):
6974
75+ def render_tag (self , current_ind ):
76+
77+ attrs = "" .join ([' {}={}' .format (key ,val ) for key , val in self .attributes .items ()])
78+
79+ tag_str = "{}<{}{} />" .format (current_ind ,self .tag ,attrs )
80+
81+ return tag_str
82+
7083 def render (self , file_out , current_ind = "" ):
84+
7185 file_out .write (self .render_tag (current_ind ))
72- file_out .write (" />\n " )
7386
87+ for con in self .content :
88+ try :
89+ file_out .write (current_ind + self .indent + con )
7490
75- class Hr (SelfClosingTag ):
91+ except TypeError :
92+ con .render (file_out , current_ind + self .indent )
7693
94+ #file_out.write("{}</{}> \n".format(current_ind, self.tag))
95+ file_out .write ("\n " )
7796
7897
79- class title (OneLineTag ):
98+ class Hr (SelfClosingTag ):
99+ tag = 'hr'
80100
81- tag = 'title'
101+ class Br (SelfClosingTag ):
102+ tag = 'br'
82103
83104class A (OneLineTag ):
84105 tag = 'a'
85- def __init__ (self , link , content ):
86- OneLineTag .___init__ (self , content = None , ** attributes )
106+
107+ def __init__ (self , link , content = None , ** attributes ):
108+
109+ OneLineTag .__init__ (self , content , ** attributes )
87110 self .attributes ["href" ] = link
88-
111+
112+ class Ul (Element ):
113+
114+ tag = 'ul'
115+
116+ class Li (Element ):
117+
118+ tag = 'li'
119+
89120class H (OneLineTag ):
90121
91122 tag = 'h'
92-
123+
93124 def __init__ (self , level , content = None , ** attributes ):
125+
126+ OneLineTag .__init__ (self , content , ** attributes )
94127 self .tag = 'h{}' .format (level )
95128
96- class Ul (element ):
97- tag = 'ul'
129+ class Meta (SelfClosingTag ):
98130
99- class li (element ):
100- tag = 'li'
131+ tag = 'meta'
132+
133+
0 commit comments