from bs4 import BeautifulSoup import json from copy import copy import markdown_renderer # markdown_renderer.py PATH_HTML_PROJECT_TEMPLATE = "./templates/project.html" PATH_PROJECT_INDEX = "./data/projects.json" PATH_HTML_BLOGPOST_TEMPLATE = "./templates/blogpost.html" PATH_BLOGPOST_INDEX = "./data/blogs.json" PATH_HTML_EVENT_TEMPLATE = "./templates/eventdetails.html" PATH_EVENT_INDEX = "./data/events.json" PATH_HTML_VIDEO_TEMPLATE = "./templates/video.html" PATH_VIDEO_INDEX = "./data/videos.json" HTML_PROJECT_TEMPLATE = """""" HTML_BLOGPOST_TEMPLATE = """""" HTML_EVENT_TEMPLATE = """""" HTML_VIDEO_TEMPLATE = """""" PROJECT_INDEX = {} BLOGPOST_INDEX = {} EVENT_INDEX = {} VIDEO_INDEX = {} # ## INITIALISATION ## # with open(PATH_HTML_PROJECT_TEMPLATE) as f: HTML_PROJECT_TEMPLATE = f.read() with open(PATH_HTML_BLOGPOST_TEMPLATE) as f: HTML_BLOGPOST_TEMPLATE = f.read() with open(PATH_HTML_EVENT_TEMPLATE) as f: HTML_EVENT_TEMPLATE = f.read() with open(PATH_HTML_VIDEO_TEMPLATE) as f: HTML_VIDEO_TEMPLATE = f.read() with open(PATH_PROJECT_INDEX) as f: PROJECT_INDEX = json.load(f) with open(PATH_BLOGPOST_INDEX) as f: BLOGPOST_INDEX = json.load(f) with open(PATH_EVENT_INDEX) as f: EVENT_INDEX = json.load(f) with open(PATH_VIDEO_INDEX) as f: VIDEO_INDEX = json.load(f) for idx in range(len(BLOGPOST_INDEX["posts"])): metadata_json = BLOGPOST_INDEX["posts"][idx] with open("." + metadata_json["path"], "r") as f: source = f.read() metadata, _ = markdown_renderer.md_to_html(source) BLOGPOST_INDEX["posts"][idx] = BLOGPOST_INDEX["posts"][idx] | metadata print(BLOGPOST_INDEX) # #### # def render_project_details(project_name: str) -> tuple: html = HTML_PROJECT_TEMPLATE source = "" if project_name not in PROJECT_INDEX: return (404, "") with open("." + PROJECT_INDEX[project_name]["path"], "r") as f: source = f.read() metadata, rendered_content = markdown_renderer.md_to_html(source) print(metadata, rendered_content) html = html.replace("{[{content}]}", rendered_content) if "logo" in metadata: html = html.replace("{[{img-src-icon-bottom-bar}]}", metadata["logo"]) soup = BeautifulSoup(html, 'html.parser') soup.title.string = soup.title.string.replace("{[{title}]}", metadata["title"]) soup.find("section", "banner-section")["id"] = metadata["bannerId"] for platform in ["win", "mac", "lin", "web"]: if platform not in metadata["platforms"]: soup.select_one(f"#compatible-{platform}").decompose() if "trailerPath" not in metadata: soup.select_one("#video-play-btn").decompose() soup.select_one("#video").decompose() else: soup.select_one("#video")["src"] = metadata["trailerPath"] for action in metadata["actions"]: new_btn = soup.new_tag("a") new_btn["class"] = "button button-inline" new_btn["href"] = action["url"] new_btn_icon = soup.new_tag("i") new_btn_icon["class"] = action["icon"] new_btn.append(new_btn_icon) new_btn.append(f' {action["text"]}') if action["mode"] == "mobile": new_btn["class"] += " mobile" if action["mode"] == "desktop": new_btn["class"] += " desktop" if action["bannerBar"]: soup.select_one("#banner-actions-bar").append(new_btn) if action["bottomBar"]: soup.select_one("#bottom-bar").insert(0, copy(new_btn)) return (200, str(soup)) def render_blogpost(blogpost_id: int) -> tuple: html = HTML_BLOGPOST_TEMPLATE source = "" if blogpost_id >= len(BLOGPOST_INDEX["posts"]): return (404, "") metadata = BLOGPOST_INDEX["posts"][blogpost_id] with open("." + metadata["path"], "r") as f: source = f.read() _, rendered_content = markdown_renderer.md_to_html(source, True) print(metadata, rendered_content) html = html.replace("{[{content}]}", rendered_content) soup = BeautifulSoup(html, "html.parser") soup.title.string = soup.title.string.replace("{[{title}]}", metadata["title"]) soup.select_one("#banner")["src"] = metadata["banner"] soup.select_one("#title").string = metadata["title"] soup.select_one("#description").string = metadata["description"] soup.select_one("#date").string = metadata["date"] soup.select_one("#author").string = metadata["author"] for tag in metadata["tags"]: new_tag = soup.new_tag("span") new_tag["class"] = "chip" new_tag.string = tag soup.select_one("#tag-container").append(new_tag) return (200, str(soup)) def blog_list() -> dict: return BLOGPOST_INDEX def render_video(video_name: str) -> tuple: html = HTML_VIDEO_TEMPLATE source = "" if video_name not in VIDEO_INDEX: return (404, "") with open("." + VIDEO_INDEX[video_name]["content_path"], "r") as f: source = f.read() metadata, rendered_content = markdown_renderer.md_to_html(source) metadata["path"] = VIDEO_INDEX[video_name]["path"] if "download_path" in VIDEO_INDEX[video_name]: metadata["download_path"] = VIDEO_INDEX[video_name]["download_path"] print(metadata, rendered_content) html = html.replace("{[{content}]}", rendered_content) html = html.replace("{[{video-title}]}", metadata["title"]) html = html.replace("{[{description}]}", metadata["description"]) html = html.replace("{[{date}]}", metadata["date"]) soup = BeautifulSoup(html, "html.parser") soup.title.string = soup.title.string.replace("{[{title}]}", metadata["title"]) if "youtube" in metadata["path"]: pass iframe = BeautifulSoup(f""" """, "html.parser") soup.select_one("#video-parent").append(iframe) soup.select_one("#video").decompose() soup.select_one("#video-controls").decompose() else: soup.select_one("#video")["src"] = metadata["path"] if metadata["default_copyright"]: soup.select_one("#video-license-parent").string = "©️ All rights reserved" else: soup.select_one("#video-license-info")["href"] = metadata["license_url"] soup.select_one("#video-license-info").insert(0, BeautifulSoup(metadata["license"], "html.parser")) if "download_path" in metadata and not metadata["default_copyright"]: soup.select_one("#video-download-btn")["href"] = metadata["download_path"] else: soup.select_one("#video-download-btn").decompose() return (200, str(soup)) def render_event(event_name: str) -> tuple: html = HTML_EVENT_TEMPLATE source = "" if event_name not in EVENT_INDEX: return (404, "") with open("." + EVENT_INDEX[event_name]["path"], "r") as f: source = f.read() metadata, rendered_content = markdown_renderer.md_to_html(source) print(metadata, rendered_content) html = html.replace("{[{content}]}", rendered_content) soup = BeautifulSoup(html, "html.parser") soup.title.string = soup.title.string.replace("{[{title}]}", metadata["title"]) return (200, str(soup))