diff --git a/src/generate_web.py b/src/generate_web.py index 47a42e7..66cd7f1 100644 --- a/src/generate_web.py +++ b/src/generate_web.py @@ -1,6 +1,7 @@ import os import random import re +import requests from datetime import datetime from os import path, makedirs from pprint import pprint @@ -15,7 +16,7 @@ from markdown import markdown from src.jinja_extensions.color_extension import ColorExtension -from src.web_helpers import load_projects, fix_readme_relative_images +from src.web_helpers import load_projects, fix_readme_relative_images, conv_markdown logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) @@ -121,9 +122,9 @@ def generate_repos_detail(self): print(f"Generating {repo.name} {i}/{repo_count}") readme_md = self.readme.get(repo.full_name, "No readme found") readme_fixed_images = fix_readme_relative_images(readme_md, repo.full_name, repo.default_branch) - readme_fixed_images = readme_fixed_images.replace(':\n- ', ':\n\n- ') # fix markdown lists - readme_fixed_images = readme_fixed_images.replace('- ', '- ● ') # add the dot before each element of the list - readme_html = markdown(readme_fixed_images, extensions=['fenced_code']) + readme_fixed_lists = readme_fixed_images.replace('\n- ', '\n@@ ').replace('\n* ', '\n@@ ').replace('\n - ', '\n @@ ').replace('\n * ', '\n @@ ').replace("@@", "- ●") # add the dot before each element of the list + + readme_html = conv_markdown(readme_fixed_lists) path_repo = self.paths.get("Repo").get("path").format(repo.name) self.render_page('repoDetail.html', path_repo, repo=repo, readme=readme_html, repo_contrib = self.contributors[repo.full_name]) @@ -139,8 +140,26 @@ def generate_projects(self, projects: list): pprint(projects) for project in projects: + for i, repo in enumerate(project["related_repos"]): + project["related_repos"][i]["name"] = repo["url"].split("/")[-1] # add "name" key to related_projects + + readme_url = project["readme"].replace("https://github.com/", "https://raw.githubusercontent.com/").replace("/blob", "") + readme_md = requests.get(readme_url).content.decode().strip() + if readme_md == "404: Not Found": + readme_md = "No readme found" + + + full_name = "/".join(project["readme"].split("/")[-5:-3]) + branch = project["readme"].split("/")[-2] + + readme_fixed_images = fix_readme_relative_images(readme_md, full_name, branch) + readme_fixed_lists = readme_fixed_images.replace('\n- ', '\n@@ ').replace('\n* ', '\n@@ ').replace('\n - ', '\n @@ ').replace('\n * ', '\n @@ ').replace("@@", "- ●") # add the dot before each element of the list + + readme_html = conv_markdown(readme_fixed_lists) + + path_project = self.paths.get("Project").get("path").format(project["url"]) - self.render_page('projectDetail.html', path_project, project=project) + self.render_page('projectDetail.html', path_project, project=project, readme=readme_html) diff --git a/src/web_helpers.py b/src/web_helpers.py index 8f6d1eb..7d67f54 100644 --- a/src/web_helpers.py +++ b/src/web_helpers.py @@ -1,6 +1,6 @@ import os from typing import IO - +import markdown import yaml def load_projects(project_dir: os.path) -> list: @@ -55,7 +55,7 @@ def replace_url(img_url): elif img_url.startswith("./"): new_url = f"{base_url}/{img_url[2:]}" elif img_url.startswith("https://github.com"): - new_url = base_url + img_url.split("/blob/master")[-1] + new_url = base_url + img_url.split(f"/blob/{default_branch}")[-1] elif img_url.startswith("http"): # The URL is already absolute, or it's a type of relative URL new_url = img_url @@ -78,3 +78,19 @@ def replace_url(img_url): # readme_md = re.sub(r'\[([^]]*)\]\(([^)]*)\)', link_replacer_md, readme_md) return readme_md + + +class TargetBlankExtension(markdown.Extension): + def extendMarkdown(self, md): + md.treeprocessors.register(TargetBlankTreeprocessor(md), 'target_blank', 15) + +class TargetBlankTreeprocessor(markdown.treeprocessors.Treeprocessor): + def run(self, root): + for element in root.iter(): + if element.tag == 'a': + element.set('target', '_blank') + +def conv_markdown(text): + md = markdown.Markdown(extensions=['fenced_code', 'markdown.extensions.extra', 'markdown.extensions.meta', TargetBlankExtension()]) + html = md.convert(text) + return html \ No newline at end of file diff --git a/templates/projectDetail.html b/templates/projectDetail.html index ea4a69e..d57e288 100644 --- a/templates/projectDetail.html +++ b/templates/projectDetail.html @@ -7,30 +7,49 @@ {% block content %} -
+{{ project.name }}
+{% if project.description %}{{ project.description + }}{% else + %}No description{% endif %}
+Visit project website
+Readme: #} +
Related repos:
+{{ repo.name }}
+{{ repo.description }}
+ + {% endfor %} + +