Skip to content

Commit

Permalink
authorize any parameter in the favicon dict (#24)
Browse files Browse the repository at this point in the history
* get is already defaulting to None

* accept any type of parameters

* add " " around parameter string

Co-authored-by: Timo Cornelius Metzger <[email protected]>
  • Loading branch information
12rambau and tcmetzger authored Jan 18, 2023
1 parent eb17311 commit 260a285
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions sphinx_favicon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,33 @@ def generate_meta(favicon: Dict[str, str]) -> str:
favicon: Favicon data
Returns:
Favicon meta tag
Favicon link or meta tag
"""
rel = favicon.get("rel", "icon")
href = favicon["href"]
meta = f' <link rel="{rel}" href="{href}"'

# Read "sizes" from config. Omit sizes if not provided.
sizes = favicon.get("sizes", None)
if sizes:
meta += f' sizes="{sizes}"'

# Set MIME type. Detect MIME type if not provided. Omit "type=" if not detectable
favicon_type = favicon.get("type", None)
favicon_file_ending = href.split(".")[-1]
if favicon_type:
meta += f' type="{favicon_type}"'
elif favicon_file_ending in SUPPORTED_MIME_TYPES.keys():
favicon_type = SUPPORTED_MIME_TYPES[favicon_file_ending]
meta += f' type="{favicon_type}"'

meta += ">"

return meta
# get the tag of the output
tag = "link"

# prepare all the tag parameters and leave them in the favicon dict

# to raise an error if not set
favicon["href"]

# default to "icon"
favicon.setdefault("rel", "icon")

# set the type. if type is not set try to guess it from the file extention
type_ = favicon.get("type")
if not type_:
extention = favicon["href"].split(".")[-1]
if extention in SUPPORTED_MIME_TYPES.keys():
type_ = SUPPORTED_MIME_TYPES[extention]
if type_ is not None:
favicon["type"] = type_

# build the html element
parameters = [f'{k}="{v}"' for k, v in favicon.items()]
html_element = f" <{tag} {' '.join(parameters)}>"
print(html_element)
return html_element


def _static_to_href(pathto: Callable, favicon: Dict[str, str]) -> Dict[str, str]:
Expand Down

0 comments on commit 260a285

Please sign in to comment.