Skip to content

Commit

Permalink
Run pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaprieto committed Nov 26, 2024
1 parent 46bc7d2 commit 0e9179a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 29 deletions.
24 changes: 17 additions & 7 deletions mkdocs_juvix/common/preprocesors/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)|<!--[\s\S]*?-->|<script>[\s\S]*?</script>)", re.DOTALL
r"((`{1,3})(?:[\s\S]*?)(\2)|<!--[\s\S]*?-->|<script>[\s\S]*?</script>)",
re.DOTALL,
)
intervals = []
try:
Expand Down Expand Up @@ -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
Expand Down
61 changes: 42 additions & 19 deletions mkdocs_juvix/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -58,15 +58,19 @@ 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

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:
Expand All @@ -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 "<img"
log.debug(f"{Fore.CYAN}Processing HTML image URL: {new_url}{Style.RESET_ALL}")
log.debug(
f"{Fore.CYAN}Processing HTML image URL: {new_url}{Style.RESET_ALL}"
)
return f'{img_rest} src="{new_url}"'

caption = match.group("caption") or ""
log.debug(f"{Fore.CYAN}Processing Markdown image URL: {new_url}{Style.RESET_ALL}")
log.debug(
f"{Fore.CYAN}Processing Markdown image URL: {new_url}{Style.RESET_ALL}"
)
return f"![{caption}]({new_url})"

def find_replacements(
text: str, ignore_tree: Optional[Any], html: bool = False
) -> 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:
Expand All @@ -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(
Expand All @@ -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}")
Expand 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 "<img" in markdown_text:
log.debug(f"{Fore.CYAN}Processing HTML image tags in markdown content{Style.RESET_ALL}")
log.debug(
f"{Fore.CYAN}Processing HTML image tags in markdown content{Style.RESET_ALL}"
)
ignore_tree = create_ignore_tree(markdown_text)
replacements = find_replacements(markdown_text, ignore_tree, html=True)
for start, end, new_url in reversed(replacements):
Expand Down
1 change: 1 addition & 0 deletions mkdocs_juvix/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def extendMarkdown(self, md): # noqa: N802

TOKEN_LIST_WIKILINKS: str = "<!-- list_wikilinks -->"


class WikilinksPlugin:
env: Optional[ENV] = None

Expand Down
9 changes: 6 additions & 3 deletions mkdocs_juvix/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, TypeVar
from urllib.parse import urljoin
from rich.console import Console # type: ignore
from rich.markdown import Markdown # type: ignore

import pathspec
import questionary
import yaml # type:ignore
Expand All @@ -21,6 +20,8 @@
from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import Files
from mkdocs.structure.pages import Page
from rich.console import Console # type: ignore
from rich.markdown import Markdown # type: ignore
from semver import Version
from tqdm import tqdm as sync_tqdm # type: ignore
from tqdm.asyncio import tqdm as async_tqdm # type: ignore
Expand Down Expand Up @@ -1128,7 +1129,9 @@ def generate_images(
)
return None

log.info(f"{Fore.MAGENTA}Generating images for {self.relative_filepath}{Style.RESET_ALL}")
log.info(
f"{Fore.MAGENTA}Generating images for {self.relative_filepath}{Style.RESET_ALL}"
)
_output = None
_markdown_output = self.cache_filepath.read_text()
metadata = parse_front_matter(_markdown_output) or {}
Expand Down
1 change: 1 addition & 0 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

FIXTURES_PATH = SRC_PATH / "fixtures"


def version_from_toml():
toml_path = SRC_PATH.parent / "pyproject.toml"
if toml_path.exists():
Expand Down

0 comments on commit 0e9179a

Please sign in to comment.