Skip to content

Commit

Permalink
qxqx
Browse files Browse the repository at this point in the history
  • Loading branch information
eyurtsev committed Dec 21, 2024
1 parent 9e31b82 commit 6e8557b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
62 changes: 60 additions & 2 deletions docs/_scripts/notebook_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import re
from typing import Any, Dict

import markdown as markdown_pkg
from mkdocs.structure.files import Files, File
from mkdocs.structure.pages import Page
from mkdocs.config import Config

from notebook_convert import convert_notebook

Expand Down Expand Up @@ -103,15 +105,71 @@ def replace_highlight_comments(match: re.Match) -> str:
# Replace all code blocks in the markdown
markdown = code_block_pattern.sub(replace_highlight_comments, markdown)
return markdown
def collapsable_directive(markdown: str, config) -> str:
"""Define a custom directive that allows for collapsable code blocks in markdown.

Check failure on line 109 in docs/_scripts/notebook_hooks.py

View workflow job for this annotation

GitHub Actions / (Check for spelling errors)

collapsable ==> collapsible
This directive is similar to the `???` built-in directive, but it also allows
attaching a link to the collapsable section.

Check failure on line 112 in docs/_scripts/notebook_hooks.py

View workflow job for this annotation

GitHub Actions / (Check for spelling errors)

collapsable ==> collapsible
def on_page_markdown(markdown: str, page: Page, **kwargs: Dict[str, Any]):
The format is
::: "title" | "link"
markdown content
:::
or
::: "title"
markdown content
:::
Returns:
str: The markdown string with collapsable sections formatted.

Check failure on line 127 in docs/_scripts/notebook_hooks.py

View workflow job for this annotation

GitHub Actions / (Check for spelling errors)

collapsable ==> collapsible
"""
import re

# Regex pattern to match the custom collapsable directive with optional link using named groups

Check failure on line 131 in docs/_scripts/notebook_hooks.py

View workflow job for this annotation

GitHub Actions / (Check for spelling errors)

collapsable ==> collapsible
pattern = re.compile(
r':::\s*"(?P<title>[^"]+)"(?:\s*\|\s*"(?P<link>[^"]+)")?\s*\n(?P<content>.*?)\n:::',
re.DOTALL
)

# Function to replace matched patterns with the appropriate HTML or markdown
def replace(match: re.Match) -> str:
title: str = match.group('title')
link: str = match.group('link')
content: str = match.group('content')
# Use the Markdown instance with the extensions already registered with mkdocs
md = markdown_pkg.Markdown(extensions=config['markdown_extensions'])
html_content = md.convert(content)
if link:
return (
f'<details class="example">\n'
f' <summary>{title} <a href="{link}" class="title-example-link">Full Example</a></summary>\n'
f' {html_content}\n'
f' <p>For more details, please see the <a href="{link}" class="see-full-example">full example</a>.</p>'
f'</details>'
)
else:
return (
f'<details>\n'
f' <summary>{title}</summary>\n'
f' {html_content}\n'
f'</details>'
)

# Replace all occurrences in the markdown
markdown = pattern.sub(replace, markdown)
return markdown

def on_page_markdown(markdown: str, config: Config, page: Page, **kwargs: Dict[str, Any]):
if DISABLED:
return markdown
if page.file.src_path.endswith(".ipynb"):
logger.info("Processing Jupyter notebook: %s", page.file.src_path)
markdown = convert_notebook(page.file.abs_src_path)
# Apply highlight comments to code blocks
markdown = _highlight_code_blocks(markdown)

# Apply collapsable directive

Check failure on line 173 in docs/_scripts/notebook_hooks.py

View workflow job for this annotation

GitHub Actions / (Check for spelling errors)

collapsable ==> collapsible
markdown = collapsable_directive(markdown, config)
return markdown
12 changes: 12 additions & 0 deletions docs/docs/static/css/extra.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.title-example-link {
padding: 2px 3px; /* Comfortable padding */
text-decoration: none;
border-radius: 2px; /* Material design standard border radius */
display: inline-block;
float: right; /* Aligns the element to the right side of its container */
}

.title-example-link:hover {
color: #673ab7; /* Darker purple text on hover for better interaction feedback */
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15); /* Enhanced shadow on hover for depth */
}
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ markdown_extensions:
- github-callouts
hooks:
- _scripts/notebook_hooks.py
extra_css: [static/css/custom.css]
extra:
social:
- icon: fontawesome/brands/js
Expand Down

0 comments on commit 6e8557b

Please sign in to comment.