Skip to content

Commit

Permalink
docs: fix relative links in github pages
Browse files Browse the repository at this point in the history
  • Loading branch information
percevalw committed Sep 15, 2023
1 parent a1fb579 commit a5959d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
31 changes: 14 additions & 17 deletions docs/scripts/autorefs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
this plugin searches for references of the form `[identifier][]` or `[title][identifier]` that were not resolved,
and fixes them using the previously stored identifier-URL mapping.
"""

import contextlib
import functools
import logging
import os
import re
from html import escape, unescape
from typing import Any, Callable, Dict, List, Match, Optional, Sequence, Tuple, Union
from urllib.parse import urlsplit
from xml.etree.ElementTree import Element

import pathspec
from markdown import Markdown
from markdown.extensions import Extension
from markdown.inlinepatterns import REFERENCE_RE, ReferenceInlineProcessor
Expand Down Expand Up @@ -278,35 +277,32 @@ def __init__(self) -> None:
def priority_patterns(self):
if self._priority_patterns is None:
self._priority_patterns = [
pathspec.patterns.GitWildMatchPattern(pat)
for pat in self.config.get("priority")
os.path.join("/", pat) for pat in self.config.get("priority")
]
return self._priority_patterns

def register_anchor(self, page: str, identifier: str):
def register_anchor(self, url: str, identifier: str):
"""Register that an anchor corresponding to an identifier was encountered when rendering the page.
Arguments:
page: The relative URL of the current page. Examples: `'foo/bar/'`, `'foo/index.html'`
url: The relative URL of the current page. Examples: `'foo/bar/'`, `'foo/index.html'`
identifier: The HTML anchor (without '#') as a string.
"""

if identifier in self._url_map:
old_url = os.path.join("/", self._url_map[identifier])
rev_patterns = list(enumerate(self.priority_patterns))[::-1]
old_priority_idx = next(
(
i
for i, pat in rev_patterns
if pat.match_file(self._url_map[identifier])
),
(i for i, pat in rev_patterns if re.match(pat, old_url)),
len(rev_patterns),
)
new_priority_idx = next(
(i for i, pat in rev_patterns if pat.match_file(page)),
(i for i, pat in rev_patterns if re.match(pat, url)),
len(rev_patterns),
)
if new_priority_idx >= old_priority_idx:
return
self._url_map[identifier] = f"{page}#{identifier}"
self._url_map[identifier] = f"{url}#{identifier}"

def register_url(self, identifier: str, url: str):
"""Register that the identifier should be turned into a link to this URL.
Expand Down Expand Up @@ -418,10 +414,10 @@ def on_page_content(
f"{__name__}: Mapping identifiers to URLs for page {page.file.src_path}"
)
for item in page.toc.items:
self.map_urls(page.url, item)
self.map_urls(page, item)
return html

def map_urls(self, base_url: str, anchor: AnchorLink) -> None:
def map_urls(self, page: Page, anchor: AnchorLink) -> None:
"""Recurse on every anchor to map its ID to its absolute URL.
This method populates `self.url_map` by side-effect.
Expand All @@ -430,9 +426,10 @@ def map_urls(self, base_url: str, anchor: AnchorLink) -> None:
base_url: The base URL to use as a prefix for each anchor's relative URL.
anchor: The anchor to process and to recurse on.
"""
self.register_anchor(base_url, anchor.id)
abs_url = os.path.join("/", page.file.url)
self.register_anchor(abs_url, anchor.id)
for child in anchor.children:
self.map_urls(base_url, child)
self.map_urls(page, child)

def on_post_page(
self, output: str, page: Page, **kwargs
Expand Down
16 changes: 15 additions & 1 deletion docs/scripts/griffe_ext.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import ast
import importlib
import inspect
import logging
import sys
from typing import Union

import astunparse
from griffe import Extension, Object, ObjectNode, get_logger
from griffe import Extension, Object, ObjectNode
from griffe.docstrings.dataclasses import DocstringSectionParameters
from griffe.expressions import Expr
from griffe.logger import patch_loggers


def get_logger(name):
new_logger = logging.getLogger(name)
new_logger.setLevel("ERROR")
return new_logger


patch_loggers(get_logger)

logger = get_logger(__name__)

Expand Down Expand Up @@ -98,5 +110,7 @@ def on_instance(self, node: Union[ast.AST, ObjectNode], obj: Object) -> None:
for param in param_section.value:
if param.name in defaults:
param.default = str(defaults[param.name])
if isinstance(param.default, Expr):
continue
if param.default is not None and len(param.default) > 50:
param.default = param.default[: 50 - 3] + "..."
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ plugins:
- search
- autorefs:
priority:
- '*'
- .*
- reference
- mkdocstrings:
enable_inventory: true
Expand Down

0 comments on commit a5959d3

Please sign in to comment.