From 8baa377c19a9c588d51404f9d3147d046c0e257b Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Fri, 17 Jan 2025 17:51:40 +0000 Subject: [PATCH] feat: add tag listings page --- src/app.py | 5 ++++- src/lib/generate_index_file.py | 2 +- src/lib/generate_tag_file.py | 37 ++++++++++++++++++++++++++++++++ src/lib/transfer_files.py | 1 - src/lib/transform_links.py | 39 +++++++++++++++------------------- 5 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 src/lib/generate_tag_file.py diff --git a/src/app.py b/src/app.py index 6f4dda2..2d1ed6f 100644 --- a/src/app.py +++ b/src/app.py @@ -1,8 +1,10 @@ import subprocess + from lib.create_target_dir import create_target_dir +from lib.generate_index_file import generate_index_file +from lib.generate_tag_file import generate_tag_file from lib.transfer_files import transfer_files from lib.transform_links import transform_links -from lib.generate_index_file import generate_index_file SOURCE = "/home/thomas/repos/eolas" TARGET = "/home/thomas/repos/eolas/neuron" @@ -15,6 +17,7 @@ def main(): transfer_files(f"{TARGET}/{build_id}", SOURCE) transform_links(f"{TARGET}/{build_id}") generate_index_file(f"{TARGET}/{build_id}", build_id, SOURCE) + generate_tag_file(f"{TARGET}/{build_id}") subprocess.run( [ "node", diff --git a/src/lib/generate_index_file.py b/src/lib/generate_index_file.py index 709ed0a..47ffd26 100644 --- a/src/lib/generate_index_file.py +++ b/src/lib/generate_index_file.py @@ -52,6 +52,6 @@ def generate_index_file(target_dir, unique_dir_name, source_dir): except Exception as e: print( colored( - f"ERROR Error occurred when transferring files: {str(e)}", "light_red" + f"ERROR Error occurred when generating index file: {str(e)}", "light_red" ) ) diff --git a/src/lib/generate_tag_file.py b/src/lib/generate_tag_file.py new file mode 100644 index 0000000..5bc35b0 --- /dev/null +++ b/src/lib/generate_tag_file.py @@ -0,0 +1,37 @@ +import json +import subprocess + +from termcolor import colored + + +def invoke_eolas_db(): + process = subprocess.run( + ["eolas-db", "export-tags"], + capture_output=True, + text=True, + ) + return json.loads(process.stdout) + + +def generate_tag_file(target_dir): + try: + print(colored("INFO Creating tag file...", "light_blue")) + tag_file = f"{target_dir}/tags.md" + tag_index = invoke_eolas_db() + print(tag_index["time"]) + with open(tag_file, "a") as file: + for tag in tag_index: + file.write(f"[{tag}](./tags#{tag}), ") + file.write("\n\n") + for tag in tag_index: + file.write(f"### {tag} \n\n") + for entry in tag_index[tag]: + file.write(f"- [[{entry}]] \n") + except Exception as e: + print( + colored( + f"ERROR Error occurred when creating tag file: {str(e)}", "light_red" + ) + ) + + # check with open syntax diff --git a/src/lib/transfer_files.py b/src/lib/transfer_files.py index 65e847d..4908147 100644 --- a/src/lib/transfer_files.py +++ b/src/lib/transfer_files.py @@ -1,4 +1,3 @@ -import glob import shutil from termcolor import colored diff --git a/src/lib/transform_links.py b/src/lib/transform_links.py index cf0a044..151daa2 100644 --- a/src/lib/transform_links.py +++ b/src/lib/transform_links.py @@ -5,27 +5,22 @@ from termcolor import colored def process_line(line): - image_rgx = r"!\[.*?\]\((.*?)\)" - internal_link_rgx = r"\[.*?\]\((.*?)\)" - img_links = re.findall(image_rgx, line) - internal_links = re.findall(internal_link_rgx, line) - - if img_links: - for img_link in img_links: - stripped_img_ref = re.search(r"[^/\\]+$", img_link) - if stripped_img_ref: - stripped_img_ref = stripped_img_ref.group() - new_img_ref = f"static/{stripped_img_ref}" - line = line.replace(f"({img_link})", f"({new_img_ref})") - - if internal_links: - for internal_link in internal_links: - if internal_link.endswith('.md') and ('/' in internal_link): - stripped_path = (re.search(r"[^/\\]+$", internal_link)) - if stripped_path: - stripped_path = stripped_path.group() - line = line.replace(f"({internal_link})", f"({stripped_path})") - return line + link_rgx = r"\[.*?\]\((.*?)\)" + links = re.findall(link_rgx, line) + + if links: + for link in links: + stripped_path = re.search(r"[^/\\]+$", link) + if stripped_path: + stripped_path = stripped_path.group() + # Handle internal links + if ".md" in stripped_path: + line = line.replace(f"({link})", f"({stripped_path})") + # Handle image links + else: + new_img_path = f"static/{stripped_path}" + line = line.replace(f"({link})", f"({new_img_path})") + return line def transform_links(target_dir): @@ -39,7 +34,7 @@ def transform_links(target_dir): new_lines = [] for line in lines: new_lines.append(process_line(line)) - if len(new_lines): + if len(new_lines): with open(file_path, "w") as f: f.writelines(new_lines) print(