#!/usr/bin/env python3 import sys class page_builder(object): LAYOUT_AUTO = 0 LAYOUT_ONE_ITEM = 1 LAYOUT_FOUR_ITEMS = 2 LAYOUT_MANY_ITEMS = 3 LAYOUT_TEXT_ONE_COLUMN = 4 LAYOUT_TEXT_TWO_COLUMNS = 5 def __init__(self): self.title = None self.style = "" self.layout = page_builder.LAYOUT_AUTO self.items = [] self.debug_info = None self.custom_html = None def set_layout(self, layout: int): self.layout = layout return self def set_title(self, title: str): self.title = title return self def set_style(self, style: str): self.style = style return self def add_item(self, item: str): self.items.append(item) return self def set_debug_info(self, debug_info: bool): self.debug_info = debug_info return self def __pick_layout(self) -> None: if len(self.items) == 1: self.layout = page_builder.LAYOUT_ONE_ITEM elif len(self.items) <= 4: self.layout = page_builder.LAYOUT_FOUR_ITEMS else: self.layout = page_builder.LAYOUT_MANY_ITEMS def __render_custom_html(self, f) -> None: if self.custom_html is not None: f.write(self.custom_html) def __render_header(self, f) -> None: if self.title is not None: f.write("

%s

\n" % self.title) f.write("
\n\n\n") if self.style is not None: f.write(self.style) def __render_footer(self, f) -> None: f.write("\n
\n") def render_html(self, f) -> None: if self.layout == page_builder.LAYOUT_AUTO or self.layout is None: self.__pick_layout() self.__render_custom_html(f) self.__render_header(f) items_per_row = None if self.layout == page_builder.LAYOUT_ONE_ITEM: items_per_row = 1 elif self.layout == page_builder.LAYOUT_FOUR_ITEMS: items_per_row = 2 elif self.layout == page_builder.LAYOUT_MANY_ITEMS: items_per_row = 3 elif self.layout == page_builder.LAYOUT_TEXT_TWO_COLUMNS: items_per_row = 1 elif self.layout == page_builder.LAYOUT_TEXT_ONE_COLUMN: items_per_row = 1 else: print("Error, unknown layout type: %d" % self.layout) return count = 0 self.items.sort(key=len, reverse=True) for item in self.items: f.write('\n%s\n\n' % item) count += 1 if count % items_per_row == 0: f.write("\n\n") self.__render_footer(f) def set_custom_html(self, html): self.custom_html = html # x = page_builder() # x.set_title("title").add_item("item1").add_item("item2").add_item("item3").render_html(sys.stdout)