|
| 1 | +# Week 7 - Lab #1: HTML Renderer Exercise |
| 2 | +# Date: Friday, February 19, 2016 |
| 3 | +# Student: Chi Kin Ho |
| 4 | + |
| 5 | +# !/usr/bin/env python |
| 6 | + |
| 7 | +""" |
| 8 | +Python class example. |
| 9 | +""" |
| 10 | + |
| 11 | +# The start of it all: |
| 12 | +# Fill it all in here. |
| 13 | + |
| 14 | + |
| 15 | +class Element(object): |
| 16 | + |
| 17 | + tag = 'html' # class attribute |
| 18 | + level = 1 # indentation level |
| 19 | + |
| 20 | + def __init__(self, content=None, **kwargs): |
| 21 | + # Initialize the Element object with the given content. |
| 22 | + if content is None: |
| 23 | + self.content = list() |
| 24 | + else: |
| 25 | + self.content = [content] |
| 26 | + |
| 27 | + if kwargs is not None: |
| 28 | + if 'style' in kwargs: |
| 29 | + self.style = kwargs['style'] |
| 30 | + else: |
| 31 | + self.style = None |
| 32 | + |
| 33 | + if 'id' in kwargs: |
| 34 | + self.id = kwargs['id'] |
| 35 | + else: |
| 36 | + self.id = None |
| 37 | + |
| 38 | + def append(self, new_content): |
| 39 | + # Append the new_content to the end of the existing content. |
| 40 | + self.content.append(new_content) |
| 41 | + |
| 42 | + def render(self, file_out, ind=""): |
| 43 | + |
| 44 | + file_out.write(ind * Element.level + '<' + self.tag) |
| 45 | + |
| 46 | + if self.style is not None: |
| 47 | + file_out.write(' ' + 'style="' + self.style + '"') |
| 48 | + if self.id is not None: |
| 49 | + file_out.write(' ' + 'id="' + self.id + '"') |
| 50 | + |
| 51 | + file_out.write('>\n') |
| 52 | + |
| 53 | + for content in self.content: |
| 54 | + if isinstance(content, str): # Stop the recursion when content is of string. |
| 55 | + file_out.write(ind * (Element.level + 1) + content + '\n') |
| 56 | + else: # Recursively call render() to print the tag element. |
| 57 | + Element.level += 1 |
| 58 | + content.render(file_out, ind) |
| 59 | + Element.level -= 1 |
| 60 | + |
| 61 | + file_out.write(ind * Element.level + '</' + self.tag + '>') |
| 62 | + |
| 63 | + if self.tag != 'html': |
| 64 | + file_out.write('\n') |
| 65 | + |
| 66 | + |
| 67 | +class Html(Element): |
| 68 | + |
| 69 | + tag = 'html' # Override the Element's class attribute with html |
| 70 | + |
| 71 | + def render(self, file_out, ind=""): |
| 72 | + |
| 73 | + file_out.write('<!DOCTYPE html>' + '\n') |
| 74 | + Element.render(self, file_out, ind) |
| 75 | + |
| 76 | + |
| 77 | +class Body(Element): |
| 78 | + |
| 79 | + tag = 'body' # Override the Element's class attribute with body |
| 80 | + |
| 81 | + |
| 82 | +class P(Element): |
| 83 | + |
| 84 | + tag = 'p' # Override the Element's class attribute with p |
| 85 | + |
| 86 | + |
| 87 | +class Head(Element): |
| 88 | + |
| 89 | + tag = 'head' # Override the Element's class attribute with head |
| 90 | + |
| 91 | + |
| 92 | +class OneLineTag(Element): |
| 93 | + |
| 94 | + def render(self, file_out, ind=""): |
| 95 | + |
| 96 | + file_out.write(ind * Element.level + '<' + self.tag + '>') |
| 97 | + file_out.write(' ' + self.content[0] + ' ') |
| 98 | + file_out.write('</' + self.tag + '>' + '\n') |
| 99 | + |
| 100 | + |
| 101 | +class Title(OneLineTag): |
| 102 | + |
| 103 | + tag = 'title' # Override the OneLineTag's class attribute with title |
| 104 | + |
| 105 | + |
| 106 | +class SelfClosingTag(Element): |
| 107 | + |
| 108 | + def render(self, file_out, ind=""): |
| 109 | + |
| 110 | + file_out.write(ind * Element.level + '<' + self.tag + ' />' + '\n') |
| 111 | + |
| 112 | + |
| 113 | +class Hr(SelfClosingTag): |
| 114 | + |
| 115 | + tag = 'hr' # Override the SelfClosingTag's class attribute with hr |
| 116 | + |
| 117 | + |
| 118 | +class Br(SelfClosingTag): |
| 119 | + |
| 120 | + tag = 'br' # Override the SelfClosingTag's class attribute with br |
| 121 | + |
| 122 | + |
| 123 | +class Meta(SelfClosingTag): |
| 124 | + |
| 125 | + tag = 'meta' # Override the SelfClosingTag's class attribute with meta |
| 126 | + |
| 127 | + def __init__(self, **kwargs): |
| 128 | + |
| 129 | + Element.__init__(self, None) |
| 130 | + if 'charset' in kwargs: |
| 131 | + self.charset = kwargs['charset'] |
| 132 | + |
| 133 | + def render(self, file_out, ind=""): |
| 134 | + |
| 135 | + file_out.write(ind * Element.level + '<' + self.tag + ' charset="' + self.charset + '" />' + '\n') |
| 136 | + |
| 137 | + |
| 138 | +class A(Element): |
| 139 | + |
| 140 | + tag = 'a' # Override the Element's class attribute with a |
| 141 | + |
| 142 | + def __init__(self, link, content=None): |
| 143 | + |
| 144 | + Element.__init__(self, content) |
| 145 | + self.link = link # Store the hyperlink of the anchor tag. |
| 146 | + |
| 147 | + def render(self, file_out, ind=""): |
| 148 | + |
| 149 | + file_out.write(ind * Element.level + '<' + self.tag + ' href="' + self.link + '">') |
| 150 | + |
| 151 | + for content in self.content: |
| 152 | + if isinstance(content, str): # Stop the recursion when content is of string. |
| 153 | + file_out.write(' ' + content + ' ') |
| 154 | + else: # Recursively call render() to print the tag element. |
| 155 | + Element.level += 1 |
| 156 | + content.render(file_out, ind) |
| 157 | + Element.level -= 1 |
| 158 | + |
| 159 | + file_out.write('</' + self.tag + '>\n') |
| 160 | + |
| 161 | + |
| 162 | +class Ul(Element): |
| 163 | + |
| 164 | + tag = 'ul' # Override the Element's class attribute with ul |
| 165 | + |
| 166 | + |
| 167 | +class Li(Element): |
| 168 | + |
| 169 | + tag = 'li' # Override the Element's class attribute with li |
| 170 | + |
| 171 | + |
| 172 | +class H(OneLineTag): |
| 173 | + |
| 174 | + tag = 'h' # Override the OneLineTag's class attribute with h2 |
| 175 | + |
| 176 | + def __init__(self, size, content=None, **kwargs): |
| 177 | + |
| 178 | + self.tag += str(size) # Add the size of the header to the tag name |
| 179 | + Element.__init__(self, content, **kwargs) |
| 180 | + |
0 commit comments