style: ruff formatting

This commit is contained in:
Meinard Francisco 2025-09-01 17:23:03 +08:00
parent 6f6197064d
commit 0be37ae042

View file

@ -18,10 +18,13 @@ logger = logging.getLogger(__name__)
@dataclass(frozen=True)
class Config:
"""Configuration constants for the FMHY bookmark generator."""
site_base_url: str = "https://fmhy.net/"
reddit_base_url: str = "https://www.reddit.com/r/FREEMEDIAHECKYEAH/wiki/"
base64_rentry_url: str = "https://rentry.co/FMHYBase64/raw"
github_raw_base: str = "https://raw.githubusercontent.com/fmhy/edit/refs/heads/main/docs/"
github_raw_base: str = (
"https://raw.githubusercontent.com/fmhy/edit/refs/heads/main/docs/"
)
folder_name: str = "FMHY"
decode_base64: bool = True
@ -29,6 +32,7 @@ class Config:
@dataclass
class WikiSection:
"""Represents a wiki section to be processed."""
filename: str
icon: str
url_key: str
@ -73,7 +77,7 @@ def add_hierarchy_prefix(
if subsubcat:
curr_subsubcat = clean_category_name(subsubcat)
elif any(char.isalpha() for char in line): # Content line
prefix = f'{{"{ section_name.replace(".md", "") }", "{ curr_subcat }", "{ curr_subsubcat }"}}'
prefix = f'{{"{section_name.replace(".md", "")}", "{curr_subcat}", "{curr_subsubcat}"}}'
content = line[2:] if line.startswith("* ") else line
modified_lines.append(prefix + content)
@ -93,7 +97,7 @@ def decode_base64_content(input_string: str) -> str:
"""Decode base64 content within backticks."""
if not CONFIG.decode_base64:
return input_string
def base64_decode(match):
encoded_data = match.group(0)[1:-1] # Remove backticks
decoded_bytes = base64.b64decode(fix_base64_padding(encoded_data))
@ -107,25 +111,29 @@ def process_base64_sections(base64_page: str) -> List[str]:
"""Process base64 page sections."""
sections = base64_page.split("***")
formatted_sections = []
for section in sections:
# Clean up section formatting
clean_section = (section.strip()
.replace("#### ", "")
.replace("\n\n", " - ")
.replace("\n", ", "))
clean_section = (
section.strip()
.replace("#### ", "")
.replace("\n\n", " - ")
.replace("\n", ", ")
)
# Remove empty lines
lines = [line for line in clean_section.split("\n") if line.strip()]
clean_section = "\n".join(lines)
# Decode base64 if enabled
clean_section = decode_base64_content(clean_section)
# Add base64 prefix
formatted_section = "[🔑Base64](https://rentry.co/FMHYBase64) ► " + clean_section
formatted_section = (
"[🔑Base64](https://rentry.co/FMHYBase64) ► " + clean_section
)
formatted_sections.append(formatted_section)
return formatted_sections
@ -206,30 +214,32 @@ def filter_starred_content(content: str) -> str:
)
def parse_bookmark_line(line: str, starred_only: bool = False) -> Tuple[str, str, str, List[Tuple[str, str]]]:
def parse_bookmark_line(
line: str, starred_only: bool = False
) -> Tuple[str, str, str, List[Tuple[str, str]]]:
"""Parse a line to extract hierarchy and bookmarks."""
url_pattern = re.compile(r"\[([^\]]+)\]\((https?://[^\)]+)\)")
hierarchy_pattern = re.compile(r'^\{"([^"]+)", "([^"]+)", "([^"]+)"\}')
hierarchy_match = hierarchy_pattern.match(line)
if not hierarchy_match:
return "", "", "", []
level1, level2, level3 = hierarchy_match.groups()
matches = url_pattern.findall(line)
if starred_only:
matches = matches[:1] # Only first match for starred content
# Extract description (text after last URL)
last_paren = line.rfind(")")
description = (
line[last_paren + 1:].replace("**", "").strip() if last_paren != -1 else ""
line[last_paren + 1 :].replace("**", "").strip() if last_paren != -1 else ""
)
if not description:
description = "- " + (level3 if level3 != "/" else level2 if level2 else level1)
bookmarks = [(f"{title} {description}".strip(), url) for title, url in matches]
return level1, level2, level3, bookmarks
@ -240,19 +250,23 @@ def generate_bookmark_html(bookmarks_dict: Dict, indent: int = 1) -> str:
for key, value in bookmarks_dict.items():
html += " " * indent + f"<DT><H3>{key}</H3>\n"
html += " " * indent + "<DL><p>\n"
if isinstance(value, dict):
html += generate_bookmark_html(value, indent + 1)
else:
for title, url in value:
html += (" " * (indent + 1) +
f'<DT><A HREF="{url}" ADD_DATE="0">{title}</A>\n')
html += (
" " * (indent + 1)
+ f'<DT><A HREF="{url}" ADD_DATE="0">{title}</A>\n'
)
html += " " * indent + "</DL><p>\n"
return html
def create_html_bookmarks(content: str, output_file: str, starred_only: bool = False) -> None:
def create_html_bookmarks(
content: str, output_file: str, starred_only: bool = False
) -> None:
"""Create HTML bookmark file from processed content."""
bookmarks: Dict[str, Dict[str, Dict[str, List[Tuple[str, str]]]]] = {}
@ -260,28 +274,26 @@ def create_html_bookmarks(content: str, output_file: str, starred_only: bool = F
level1, level2, level3, bookmark_list = parse_bookmark_line(line, starred_only)
if not level1: # Skip lines that don't match hierarchy pattern
continue
# Initialize nested structure
bookmarks.setdefault(level1, {}).setdefault(level2, {}).setdefault(level3, [])
bookmarks[level1][level2][level3].extend(bookmark_list)
# Generate HTML
html_content = (
'<!DOCTYPE NETSCAPE-Bookmark-file-1>\n'
"<!DOCTYPE NETSCAPE-Bookmark-file-1>\n"
'<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n'
'<TITLE>Bookmarks</TITLE>\n'
'<H1>Bookmarks</H1>\n'
'<DL><p>\n'
f' <DT><H3>{CONFIG.folder_name}</H3>\n'
' <DL><p>\n'
+ generate_bookmark_html(bookmarks) +
' </DL><p>\n'
'</DL><p>\n'
"<TITLE>Bookmarks</TITLE>\n"
"<H1>Bookmarks</H1>\n"
"<DL><p>\n"
f" <DT><H3>{CONFIG.folder_name}</H3>\n"
" <DL><p>\n" + generate_bookmark_html(bookmarks) + " </DL><p>\n"
"</DL><p>\n"
)
with open(output_file, "w", encoding="utf-8") as f:
f.write(html_content)
logger.info("Created bookmark file: %s", output_file)
@ -290,13 +302,15 @@ def main() -> None:
logger.info("Collecting wiki content...")
all_content = collect_all_wiki_content()
full_content = "\n".join(all_content)
# Generate both bookmark files
create_html_bookmarks(full_content, "fmhy_in_bookmarks.html")
starred_content = filter_starred_content(full_content)
create_html_bookmarks(starred_content, "fmhy_in_bookmarks_starred_only.html", starred_only=True)
create_html_bookmarks(
starred_content, "fmhy_in_bookmarks_starred_only.html", starred_only=True
)
logger.info("Bookmark generation complete!")