12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- class SideBar:
- DIV: str = 'div'
- H1: str = 'h1'
- MORE: str = 'more'
- MORE_ITEMS_LENGTH = 3
- SHOULD_COMPRESS_HTML: bool = True
- def __init__(self,
- title: str,
- memu_items: [str],
- more: str = MORE,
- more_items_length: int = MORE_ITEMS_LENGTH,
- should_compress_html: bool = SHOULD_COMPRESS_HTML
- ) -> None:
- self.title = title
- self.more = more
- self.should_compress_html = should_compress_html
- self.memu_items = memu_items
- self.more_items_length = more_items_length
- def __len__(self):
- return len(self.memu_items)
- def __repr__(self):
- return f'SideBar: {len(self)} memu items'
- @classmethod
- def _header(cls, title: str) -> str:
- return cls._build_header(cls.H1, title)
- @classmethod
- def _body(cls, menu_items:[str], should_compress_html: bool) -> str:
- join_char = cls._get_split_char(should_compress_html)
- return join_char.join(
- list(cls._build_body(cls.DIV, menu_items))
- )
- @classmethod
- def _more(cls, more):
- return cls._build_more(cls.DIV, more)
- @staticmethod
- def _build_header(tag_name: str, title: str) -> str:
- return f'<{tag_name}>{title}</{tag_name}>'
- @staticmethod
- def _build_body(tag_name: str, menu_items: [str]) -> str:
- for menu_item in menu_items:
- yield f'<{tag_name}>{menu_item}</{tag_name}>'
- @staticmethod
- def _build_more(tag_name: str, text: str) -> str:
- return f'<{tag_name}>{text}</{tag_name}>'
- @staticmethod
- def _get_split_char(should_compress_html: bool) -> str:
- return '' if should_compress_html else '\n'
- def _is_few_items(self):
- return len(self) < self.more_items_length
- def build(self) -> str:
- header = self._header(self.title)
- body = self._body(self.memu_items,self.should_compress_html)
- footer = self._more(self.more) if self._is_few_items() else ''
- split_char = self._get_split_char(self.should_compress_html)
- html = split_char.join([header, body, footer])
- return html
- if __name__ == '__main__':
- side_bar = SideBar('DEMO SIDE BAR', ['item1', 'item2', 'item3', 'item4', 'item5'], should_compress_html=False, more_items_length=10)
- print(side_bar.build())
|