6 class page_builder(object):
11 LAYOUT_TEXT_ONE_COLUMN = 4
12 LAYOUT_TEXT_TWO_COLUMNS = 5
17 self.layout = page_builder.LAYOUT_AUTO
19 self.debug_info = None
20 self.custom_html = None
22 def set_layout(self, layout: int):
26 def set_title(self, title: str):
30 def set_style(self, style: str):
34 def add_item(self, item: str):
35 self.items.append(item)
38 def set_debug_info(self, debug_info: bool):
39 self.debug_info = debug_info
42 def __pick_layout(self) -> None:
43 if len(self.items) == 1:
44 self.layout = page_builder.LAYOUT_ONE_ITEM
45 elif len(self.items) <= 4:
46 self.layout = page_builder.LAYOUT_FOUR_ITEMS
48 self.layout = page_builder.LAYOUT_MANY_ITEMS
50 def __render_custom_html(self, f) -> None:
51 if self.custom_html is not None:
52 f.write(self.custom_html)
54 def __render_header(self, f) -> None:
55 if self.title is not None:
56 f.write("<H1>%s</H1>\n" % self.title)
57 f.write("<HR>\n<TABLE WIDTH=99% BORDER=0>\n<TR>\n")
58 if self.style is not None:
61 def __render_footer(self, f) -> None:
62 f.write("</TR>\n</TABLE>\n")
64 def render_html(self, f) -> None:
65 if self.layout == page_builder.LAYOUT_AUTO or self.layout is None:
67 self.__render_custom_html(f)
68 self.__render_header(f)
72 if self.layout == page_builder.LAYOUT_ONE_ITEM:
75 elif self.layout == page_builder.LAYOUT_FOUR_ITEMS:
78 elif self.layout == page_builder.LAYOUT_MANY_ITEMS:
81 elif self.layout == page_builder.LAYOUT_TEXT_TWO_COLUMNS:
84 elif self.layout == page_builder.LAYOUT_TEXT_ONE_COLUMN:
88 print("Error, unknown layout type: %d" % self.layout)
92 self.items.sort(key=len, reverse=True)
93 for item in self.items:
94 f.write('<TD WIDTH=50%% STYLE="padding: 10px;">\n%s\n</TD>\n' % item)
96 if count % items_per_row == 0:
97 f.write("</TR>\n<TR>\n")
98 self.__render_footer(f)
100 def set_custom_html(self, html):
101 self.custom_html = html
105 # x.set_title("title").add_item("item1").add_item("item2").add_item("item3").render_html(sys.stdout)