diff --git a/mkdocs_juvix/common/preprocesors/links.py b/mkdocs_juvix/common/preprocesors/links.py index cadbf2b..7a6081d 100644 --- a/mkdocs_juvix/common/preprocesors/links.py +++ b/mkdocs_juvix/common/preprocesors/links.py @@ -4,8 +4,8 @@ from typing import Any, List, Optional, Tuple from urllib.parse import urljoin -from colorama import Fore, Style import numpy as np # type: ignore +from colorama import Fore, Style from fuzzywuzzy import fuzz # type: ignore from markdown.preprocessors import Preprocessor # type: ignore from ncls import NCLS # type: ignore @@ -163,6 +163,7 @@ def process_wikilink(config, full_text, match, md_filepath) -> Optional[WikiLink return link + class WLPreprocessor(Preprocessor): absolute_path: Optional[Path] = None relative_path: Optional[Path] = None @@ -188,10 +189,11 @@ def _run(self, content: str) -> str: and self.url is None ): raise ValueError("No absolute path, relative path, or URL provided") - + # Find all code blocks, HTML comments, and script tags in a single pass ignore_blocks = re.compile( - r"((`{1,3})(?:[\s\S]*?)(\2)||)", re.DOTALL + r"((`{1,3})(?:[\s\S]*?)(\2)||)", + re.DOTALL, ) intervals = [] try: @@ -219,20 +221,28 @@ def _run(self, content: str) -> str: if intervals_where_not_to_look and not list( intervals_where_not_to_look.find_overlap(start, end) ): - log.debug(f"{Fore.YELLOW}Processing wikilink: {m.group(0)}{Style.RESET_ALL}") + log.debug( + f"{Fore.YELLOW}Processing wikilink: {m.group(0)}{Style.RESET_ALL}" + ) link: Optional[WikiLink] = process_wikilink( self.config, content, m, self.absolute_path ) - replacement = (start, end, link.markdown()) if link is not None else None + replacement = ( + (start, end, link.markdown()) if link is not None else None + ) if replacement is not None: replacements.append(replacement) - log.debug(f"{Fore.YELLOW}Processed replacement: {replacement}{Style.RESET_ALL}") + log.debug( + f"{Fore.YELLOW}Processed replacement: {replacement}{Style.RESET_ALL}" + ) else: log.debug( f"{Fore.YELLOW}Link was not processed: {m.group(0)}{Style.RESET_ALL}" ) else: - log.debug(f"{Fore.YELLOW}Skipping wikilink: {m.group(0)}{Style.RESET_ALL}") + log.debug( + f"{Fore.YELLOW}Skipping wikilink: {m.group(0)}{Style.RESET_ALL}" + ) for start, end, new_text in reversed(replacements): content = content[:start] + new_text + content[end:] return content diff --git a/mkdocs_juvix/images.py b/mkdocs_juvix/images.py index f828d76..b81202d 100644 --- a/mkdocs_juvix/images.py +++ b/mkdocs_juvix/images.py @@ -47,7 +47,7 @@ def process_images( env: ENV, md: Optional[str], md_filepath: Optional[Path] = None ) -> Optional[str]: log.debug(f"{Fore.CYAN}Starting process_images function{Style.RESET_ALL}") - + def create_ignore_tree(text: str) -> Optional[Any]: """Create NCLS tree of regions to ignore (code blocks, comments, divs)""" log.debug(f"{Fore.CYAN}Creating ignore tree for text regions{Style.RESET_ALL}") @@ -58,7 +58,9 @@ def create_ignore_tree(text: str) -> Optional[Any]: if intervals: starts, ends, ids = map(np.array, zip(*intervals)) - log.debug(f"{Fore.CYAN}Ignore tree created with {len(intervals)} intervals{Style.RESET_ALL}") + log.debug( + f"{Fore.CYAN}Ignore tree created with {len(intervals)} intervals{Style.RESET_ALL}" + ) return NCLS(starts, ends, ids) log.debug(f"{Fore.CYAN}No intervals found for ignore tree{Style.RESET_ALL}") return None @@ -66,7 +68,9 @@ def create_ignore_tree(text: str) -> Optional[Any]: def should_process_match(tree: Optional[Any], start: int, end: int) -> bool: """Check if match should be processed based on ignore regions""" result = not tree or not list(tree.find_overlap(start, end)) - log.debug(f"{Fore.CYAN}Match processing check: {result} for range ({start}, {end}){Style.RESET_ALL}") + log.debug( + f"{Fore.CYAN}Match processing check: {result} for range ({start}, {end}){Style.RESET_ALL}" + ) return result def process_image_url(new_url, match: re.Match, html: bool = False) -> str: @@ -77,18 +81,24 @@ def process_image_url(new_url, match: re.Match, html: bool = False) -> str: if html: img_rest = match.group("rest") or " List[Tuple[int, int, str]]: """Find all image references that need to be replaced""" - log.debug(f"{Fore.CYAN}Finding replacements for image references{Style.RESET_ALL}") + log.debug( + f"{Fore.CYAN}Finding replacements for image references{Style.RESET_ALL}" + ) replacements = [] if html: @@ -111,20 +121,27 @@ def find_replacements( and not url.is_absolute() and url.parent == Path(".") ): - log.debug(f"{Fore.YELLOW}Processing image URL: {url}{Style.RESET_ALL}") - log.debug(f"{Fore.YELLOW}env.SITE_URL: {env.SITE_URL}{Style.RESET_ALL}") - log.debug(f"{Fore.YELLOW}env.IMAGES_PATH: {env.IMAGES_PATH}{Style.RESET_ALL}") + log.debug( + f"{Fore.YELLOW}Processing image URL: {url}{Style.RESET_ALL}" + ) + log.debug( + f"{Fore.YELLOW}env.SITE_URL: {env.SITE_URL}{Style.RESET_ALL}" + ) + log.debug( + f"{Fore.YELLOW}env.IMAGES_PATH: {env.IMAGES_PATH}{Style.RESET_ALL}" + ) log.debug(f"{Fore.YELLOW}url.name: {url.name}{Style.RESET_ALL}") - log.debug(f"{Fore.YELLOW}env.DOCS_ABSPATH: {env.DOCS_ABSPATH}{Style.RESET_ALL}") + log.debug( + f"{Fore.YELLOW}env.DOCS_ABSPATH: {env.DOCS_ABSPATH}{Style.RESET_ALL}" + ) _image_url = env.IMAGES_PATH / url.name - if _image_url.exists() and _image_url.is_relative_to(env.DOCS_ABSPATH): + if _image_url.exists() and _image_url.is_relative_to( + env.DOCS_ABSPATH + ): _image_url = _image_url.relative_to(env.DOCS_ABSPATH) log.debug(f"{Fore.YELLOW}_image_url: {_image_url}{Style.RESET_ALL}") - image_url = urljoin( - env.SITE_URL or "/" , - _image_url.as_posix() - ) + image_url = urljoin(env.SITE_URL or "/", _image_url.as_posix()) log.debug(f"{Fore.YELLOW}image_url: {image_url}{Style.RESET_ALL}") new_text = process_image_url( @@ -138,9 +155,13 @@ def find_replacements( if md is None: if md_filepath is None: - log.debug(f"{Fore.CYAN}No markdown content or filepath provided{Style.RESET_ALL}") + log.debug( + f"{Fore.CYAN}No markdown content or filepath provided{Style.RESET_ALL}" + ) return None - log.debug(f"{Fore.CYAN}Reading markdown content from file: {md_filepath}{Style.RESET_ALL}") + log.debug( + f"{Fore.CYAN}Reading markdown content from file: {md_filepath}{Style.RESET_ALL}" + ) markdown_text = Path(md_filepath).read_text() else: log.debug(f"{Fore.CYAN}Using provided markdown content{Style.RESET_ALL}") @@ -151,13 +172,15 @@ def find_replacements( log.debug(f"{Fore.CYAN}Ignore tree: {ignore_tree}{Style.RESET_ALL}") replacements = find_replacements(markdown_text, ignore_tree, html=False) for start, end, new_text in replacements: - log.debug(f"{Fore.CYAN}Replacement: {new_text}{Style.RESET_ALL}") + log.debug(f"{Fore.CYAN}Replacement: {new_text}{Style.RESET_ALL}") log.debug(f"{Fore.CYAN}Found {len(replacements)} replacements{Style.RESET_ALL}") for start, end, new_url in reversed(replacements): markdown_text = markdown_text[:start] + new_url + markdown_text[end:] if "