Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python code improvements #6533

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/overrides/hooks/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def on_page_markdown(markdown: str, *, page: Page, config: MkDocsConfig, files):
# -----------------------------------------------------------------------------

# Map ISO 639-1 (languages) to ISO 3166 (countries)
countries = dict({
countries = {
"af": "za",
"az": "az",
"ar": "ae",
Expand Down Expand Up @@ -191,4 +191,4 @@ def on_page_markdown(markdown: str, *, page: Page, config: MkDocsConfig, files):
"zh": "cn",
"zh-Hant": "cn",
"zh-TW": "tw"
})
}
2 changes: 1 addition & 1 deletion src/plugins/privacy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import os

from mkdocs.config.base import Config
from mkdocs.config.config_options import Deprecated, DictOfItems, Type
from mkdocs.config.config_options import DictOfItems, Type

# -----------------------------------------------------------------------------
# Classes
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/privacy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def on_config(self, config):

# Initialize collections of external assets
self.assets = Files([])
self.assets_expr_map = dict({
self.assets_expr_map = {
".css": r"url\((\s*http?[^)]+)\)",
".js": r"[\"'](http[^\"']+\.(?:css|js(?:on)?))[\"']",
**self.config.assets_expr_map
})
}

# Process external style sheets and scripts (run latest) - run this after
# all other plugins, so they can add additional assets
Expand Down Expand Up @@ -537,7 +537,7 @@ def _save_to_file(self, path: str, content: str | bytes):
log = logging.getLogger("mkdocs.material.privacy")

# Expected file extensions
extensions = dict({
extensions = {
"application/javascript": ".js",
"image/avif": ".avif",
"image/gif": ".gif",
Expand All @@ -547,4 +547,4 @@ def _save_to_file(self, path: str, content: str | bytes):
"image/webp": ".webp",
"text/javascript": ".js",
"text/css": ".css"
})
}
60 changes: 29 additions & 31 deletions src/plugins/search/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,15 @@ def handle_starttag(self, tag, attrs):
return

# Render opening tag if kept
if not self.skip.intersection(self.context):
if tag in self.keep:
if not self.skip.intersection(self.context) and tag in self.keep:

# Check whether we're inside the section title
data = self.section.text
if self.section.el in self.context:
data = self.section.title
# Check whether we're inside the section title
data = self.section.text
if self.section.el in self.context:
data = self.section.title

# Append to section title or text
data.append(f"<{tag}>")
# Append to section title or text
data.append(f"<{tag}>")

# Called at the end of every HTML tag
def handle_endtag(self, tag):
Expand Down Expand Up @@ -488,29 +487,28 @@ def handle_endtag(self, tag):
return

# Render closing tag if kept
if not self.skip.intersection(self.context):
if tag in self.keep:

# Check whether we're inside the section title
data = self.section.text
if self.section.el in self.context:
data = self.section.title

# Search for corresponding opening tag
index = data.index(f"<{tag}>")
for i in range(index + 1, len(data)):
if not data[i].isspace():
index = len(data)
break

# Remove element if empty (or only whitespace)
if len(data) > index:
while len(data) > index:
data.pop()

# Append to section title or text
else:
data.append(f"</{tag}>")
if not self.skip.intersection(self.context) and tag in self.keep:

# Check whether we're inside the section title
data = self.section.text
if self.section.el in self.context:
data = self.section.title

# Search for corresponding opening tag
index = data.index(f"<{tag}>")
for i in range(index + 1, len(data)):
if not data[i].isspace():
index = len(data)
break

# Remove element if empty (or only whitespace)
if len(data) > index:
while len(data) > index:
data.pop()

# Append to section title or text
else:
data.append(f"</{tag}>")

# Called for the text contents of each tag
def handle_data(self, data):
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/social/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def _load_font_from_google(self, name):
log.addFilter(DuplicateFilter())

# Color palette
colors = dict({
colors = {
"red": { "fill": "#ef5552", "text": "#ffffff" },
"pink": { "fill": "#e92063", "text": "#ffffff" },
"purple": { "fill": "#ab47bd", "text": "#ffffff" },
Expand All @@ -513,4 +513,4 @@ def _load_font_from_google(self, name):
"blue-grey": { "fill": "#546d78", "text": "#ffffff" },
"black": { "fill": "#000000", "text": "#ffffff" },
"white": { "fill": "#ffffff", "text": "#000000" }
})
}
3 changes: 0 additions & 3 deletions src/plugins/tags/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

from functools import partial
from markdown.extensions.toc import slugify
from mkdocs.config.config_options import Optional, Type
from mkdocs.config.base import Config

from . import casefold

# -----------------------------------------------------------------------------
# Classes
Expand Down