from enum import Enum from typing import Optional import bs4 from PFERD.utils import soupify _link_template_plain = "{{link}}" _link_template_fancy = """ ILIAS - Link: {{name}}
{{description}}
""".strip() # noqa: E501 line too long _link_template_internet_shortcut = """ [InternetShortcut] URL={{link}} """.strip() _learning_module_template = """ {{name}} {{body}} """ def learning_module_template(body: bs4.Tag, name: str, prev: Optional[str], next: Optional[str]) -> str: # Seems to be comments, ignore those. for elem in body.select(".il-copg-mob-fullscreen-modal"): elem.decompose() nav_template = """ """ if prev and body.select_one(".ilc_page_lnav_LeftNavigation"): text = body.select_one(".ilc_page_lnav_LeftNavigation").getText().strip() left = f'{text}' else: left = "" if next and body.select_one(".ilc_page_rnav_RightNavigation"): text = body.select_one(".ilc_page_rnav_RightNavigation").getText().strip() right = f'{text}' else: right = "" if top_nav := body.select_one(".ilc_page_tnav_TopNavigation"): top_nav.replace_with( soupify(nav_template.replace("{{left}}", left).replace("{{right}}", right).encode()) ) if bot_nav := body.select_one(".ilc_page_bnav_BottomNavigation"): bot_nav.replace_with(soupify(nav_template.replace( "{{left}}", left).replace("{{right}}", right).encode()) ) body = body.prettify() return _learning_module_template.replace("{{body}}", body).replace("{{name}}", name) class Links(Enum): IGNORE = "ignore" PLAINTEXT = "plaintext" FANCY = "fancy" INTERNET_SHORTCUT = "internet-shortcut" def template(self) -> Optional[str]: if self == Links.FANCY: return _link_template_fancy elif self == Links.PLAINTEXT: return _link_template_plain elif self == Links.INTERNET_SHORTCUT: return _link_template_internet_shortcut elif self == Links.IGNORE: return None raise ValueError("Missing switch case") def extension(self) -> Optional[str]: if self == Links.FANCY: return ".html" elif self == Links.PLAINTEXT: return ".txt" elif self == Links.INTERNET_SHORTCUT: return ".url" elif self == Links.IGNORE: return None raise ValueError("Missing switch case") @staticmethod def from_string(string: str) -> "Links": try: return Links(string) except ValueError: raise ValueError("must be one of 'ignore', 'plaintext'," " 'html', 'internet-shortcut'")