Skip to content

Commit 96ad3df

Browse files
📝 Add docstrings to dev-from-macmini
Docstrings generation was requested by @myoshi2891. * #308 (comment) The following files were modified: * `generate_index.py`
1 parent 1bfd8b8 commit 96ad3df

1 file changed

Lines changed: 34 additions & 11 deletions

File tree

generate_index.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import html
55
import urllib.parse
66
import shutil
7+
import typing
78
from collections import defaultdict
89
from typing import List, Tuple, Dict
910

@@ -77,15 +78,31 @@ def copy_vendor_files(self, output_dir: str) -> None:
7778

7879
def rewrite_html_content(self, content: str) -> str:
7980
"""
80-
Rewrite CDN asset URLs in HTML to local /vendor/ paths.
81-
82-
Replaces known external CDN links (React, Babel, Tailwind, PrismJS, FontAwesome, etc.) with corresponding /vendor/... paths and removes `integrity` and `crossorigin` attributes from `<link>` and `<script>` tags that reference those local vendor files.
83-
81+
Replace known CDN asset URLs in the HTML with local `/vendor/` paths and remove `integrity`/`crossorigin` attributes from tags that reference `/vendor/`.
82+
8483
Parameters:
8584
content (str): HTML document content to rewrite.
85+
86+
Returns:
87+
str: HTML content with matching CDN URLs substituted by local `/vendor/` URLs and `integrity`/`crossorigin` attributes removed from tags that reference `/vendor/`.
88+
"""
89+
"""
90+
Remove `integrity` and `crossorigin` attributes from a matched `<link>` or `<script>` tag when it references a `/vendor/` path.
91+
92+
Parameters:
93+
match (typing.Match[str]): A regex match whose matched text is the full HTML tag.
94+
95+
Returns:
96+
str: The tag text with `integrity` and `crossorigin` attributes removed if the tag contains `/vendor/`, otherwise the original tag text.
97+
"""
98+
"""
99+
Remove `integrity` and `crossorigin` attributes from a matched <link> or <script> tag when it references a `/vendor/` path.
100+
101+
Parameters:
102+
match (typing.Match[str]): A regex match whose matched text is the full HTML tag.
86103
87104
Returns:
88-
str: The HTML content with matching CDN URLs substituted by local vendor URLs and SRI/crossorigin attributes stripped for vendored assets.
105+
str: The tag text with `integrity` and `crossorigin` attributes removed if the tag contains `/vendor/`, otherwise the original tag text.
89106
"""
90107
replacements = [
91108
# React
@@ -100,25 +117,31 @@ def rewrite_html_content(self, content: str) -> str:
100117
(r'https://cdn\.tailwindcss\.com(?:@[^/]+)?', '/vendor/tailwindcss/script.js'),
101118
# PrismJS
102119
(r'https://cdnjs\.cloudflare\.com/ajax/libs/prism/[^/]+/themes/prism\.min\.css', '/vendor/prismjs/themes/prism.css'),
103-
(r'https://cdnjs\.cloudflare\.com/ajax/libs/prism/[^/]+/plugins/line-numbers/prism-line-numbers\.min\.css', '/vendor/prismjs/plugins/line-numbers/prism-line-numbers.css'),
104-
(r'https://cdnjs\.cloudflare\.com/ajax/libs/prism/[^/]+/plugins/toolbar/prism-toolbar\.min\.css', '/vendor/prismjs/plugins/toolbar/prism-toolbar.css'),
120+
(r'https://cdnjs\.cloudflare\.com/ajax/libs/prism/[^/]+/plugins/([a-zA-Z0-9_-]+)/prism-\1\.min\.css', r'/vendor/prismjs/plugins/\1/prism-\1.css'),
105121
# FontAwesome
106122
(r'https://cdnjs\.cloudflare\.com/ajax/libs/font-awesome/[^/]+/css/all\.min\.css', '/vendor/fontawesome/css/all.min.css'),
123+
# jsDelivr generic patterns for Prism JS and CSS (often used interchangeably)
124+
(r'https://cdn\.jsdelivr\.net/npm/prismjs(?:@[^/]+)?/prism\.min\.js', '/vendor/prismjs/prism.js'),
125+
(r'https://cdn\.jsdelivr\.net/npm/prismjs(?:@[^/]+)?/components/prism-core\.min\.js', '/vendor/prismjs/prism.js'),
126+
(r'https://cdn\.jsdelivr\.net/npm/prismjs(?:@[^/]+)?/components/prism-([a-zA-Z0-9_-]+)\.min\.js', r'/vendor/prismjs/components/prism-\1.js'),
127+
(r'https://cdn\.jsdelivr\.net/npm/prismjs(?:@[^/]+)?/plugins/([a-zA-Z0-9_-]+)/prism-\1\.min\.js', r'/vendor/prismjs/plugins/\1/prism-\1.js'),
128+
(r'https://cdn\.jsdelivr\.net/npm/prismjs(?:@[^/]+)?/plugins/([a-zA-Z0-9_-]+)/prism-\1\.min\.css', r'/vendor/prismjs/plugins/\1/prism-\1.css'),
129+
(r'https://cdn\.jsdelivr\.net/npm/prismjs(?:@[^/]+)?/themes/prism(?:-[a-zA-Z0-9_-]+)?\.min\.css', '/vendor/prismjs/themes/prism.css'),
107130
]
108131

109132
for pattern_str, new in replacements:
110133
content = re.sub(pattern_str, new, content)
111134

112135
# Strip integrity and crossorigin attributes from tags referencing local /vendor/ files
113-
def strip_sri(match):
136+
def strip_sri(match: typing.Match[str]) -> str:
114137
"""
115-
Remove Subresource Integrity (`integrity`) and `crossorigin` attributes from an HTML <link> or <script> tag if the tag references a `/vendor/` path.
138+
Remove `integrity` and `crossorigin` attributes from an HTML `<link>` or `<script>` tag that references a `/vendor/` path.
116139
117140
Parameters:
118-
match (re.Match): A regex match object whose matched text is the full HTML tag.
141+
match (typing.Match[str]): Regex match whose matched text is the full HTML tag to process.
119142
120143
Returns:
121-
str: The original tag text with `integrity` and `crossorigin` attributes removed when the tag contains `/vendor/`; otherwise the original tag text unchanged.
144+
str: The tag text with `integrity` and `crossorigin` attributes removed if the tag contains `/vendor/`, otherwise the original tag text.
122145
"""
123146
tag_text = match.group(0)
124147
if '/vendor/' in tag_text:

0 commit comments

Comments
 (0)