@@ -14,15 +14,15 @@ class Element(object):
1414 #class attributes
1515 tag = "element tag"
1616 indent = " "
17- print ("hi" )
17+ print ("hi from class element " )
1818 #content = []
1919
2020
2121 def __init__ (self , content = None , ** attributes ): ###!!!could i have added the link attribute here??
22+
2223 #store content
2324 self .content = []
2425
25-
2626 # add an attributes dictionary
2727 self .attributes = attributes
2828
@@ -49,7 +49,7 @@ def append(self, new_content):
4949 def render_tag (self , current_ind ):
5050 # tag and then content for each class
5151 attrs = "" .join ([' {} ="{}"' .format (key ,val ) for key , val in self .attributes .items ()])
52- # print("this is self.attributes from render_tag: ", self.attributes)
52+ print ("this is self.attributes from render_tag: " , self .attributes )
5353 #indentation + tag + content
5454 tag_str = "{}<{}{}>" .format (current_ind , self .__str__ (), attrs )
5555 #print("from render_tag: current_ind is '{}' and tag strg is '{}'".format(current_ind, tag_str))
@@ -99,6 +99,10 @@ class P (Element):
9999class Html (Element ):
100100 tag = "html"
101101 print ("subclass tag = : " ,tag )
102+
103+ def render (self , file_out , current_ind = "" ):
104+ file_out .write ("<!DOCTYPE html>\n " )
105+ super ().render (file_out ,current_ind = current_ind )
102106 pass
103107
104108class Head (Element ):
@@ -164,15 +168,65 @@ class Br (SelfClosingTag):
164168 tag = "br"
165169 pass
166170
167- class A (Element ):
171+ class A (OneLineTag ):
168172 tag = "a"
169173
174+ def __init__ (self , link , content = None , ** attributes ):
175+ #self.attributes = "href="
176+ #contentstr = "href=" + '"'+content+'"> ' + link
177+
178+
179+ Element .__init__ (self ,content , ** attributes ) #!!! not quite right - needs the format: <a href="http://google.com"> link </a>
180+ self .attributes ["href" ] = link #class guided answer - THANK you!!
181+ #print("a's link is:", self.link)
182+ print ("a's content **** is:" , self .content )
183+ print ("a's kwrgs extract of href** is:" , self .attributes ["href" ])
184+
185+
186+
187+ def render (self , file_out , current_ind = " default indent" ):
188+ print ("entering a's rendering func " )
189+ tag_str = '{}<{}{}"{}"> ' .format (current_ind , self .__str__ (),"href=" , self .attributes .get ("href" ))
190+ file_out .write (tag_str )
191+
192+
193+ for con in self .content :
194+ try :
195+ #render it
196+ print ("\t --> con in A is: " , con )
197+ file_out .write (con ) #was: stuff_str.render(file_out)
198+ except TypeError as e :
199+ print ("hit a snag: " , e , "con is: " , con )
200+ con .render (file_out , current_ind + self .indent ) # was: .write(str(stuff_str)) ### watch it if it is self.tag
201+
202+ #write out closing tag
203+ #was:
204+ #stop at the closing html tag
205+ #end_tag = "</{}>".format(self.tag)
206+ #add that tag to the end of the file object
207+ #file_out.write(end_tag)
208+ file_out .write (" </{}>\n " .format (self .tag ))
170209
171210
172- def __init__ (self , content = None , link = "link" ,** attributes ):
173- self .attributes = "href="
174- contentstr = "href=" + '"' + content + '">' + link
175- super ().__init__ (contentstr ,** attributes ) #!!! not quite right - needs the format: <a href="http://google.com"> link </a>
211+
212+ #to over write the default __str__
213+ def __str__ (self ):
214+ return self .ToString ()
215+
216+ def ToString (self ):
217+ return "a "
218+
219+
220+ # def render_tag(self, current_ind):
221+ # # tag and then content for each class
222+ # attrs = "".join([' {} ="{}"'.format(key,val) for key, val in self.attributes.items()])
223+ # #print("this is self.attributes from render_tag: ", self.attributes)
224+ # #indentation + tag + content
225+ # tag_str = "{}<{}{}".format(current_ind, self.__str__(), attrs) #removed the extra ">" from this tag render
226+ # #print("from render_tag: current_ind is '{}' and tag strg is '{}'".format(current_ind, tag_str))
227+ # print("a's tag string is",tag_str," and self.string is:", self.__str__())
228+ # return tag_str
229+
176230
177231 #self.link = link
178232 pass
@@ -192,3 +246,12 @@ def __init__(self,header,content = None,**attributes):
192246 self .header = switchdict [int (header )]
193247 self .tag = self .header
194248 super ().__init__ (content ,** attributes )
249+
250+ class Meta (SelfClosingTag ):
251+ tag = "meta"
252+
253+ def __init__ (self ,content = None , ** attributes ):
254+ #dfault for charset
255+ if "charset" not in attributes :
256+ attributes ["charset" ] = "UTF-8"
257+ SelfClosingTag .__init__ (self ,content ,** attributes )
0 commit comments