Skip to content

CommunityMaterialDesignProvider

Base classes

Name Children Inherits
FontProvider
pycons.font_providers.base
Base class for font providers.

⋔ Inheritance diagram

graph TD
  249777827["providers.CommunityMaterialDesignProvider"]
  2987344157["base.FontProvider"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  2987344157 --> 249777827
  925645236 --> 2987344157
  2751680898 --> 925645236

🛈 DocStrings

Bases: FontProvider

Provider for Material Design icons.

Source code in src/pycons/font_providers/providers.py
class CommunityMaterialDesignProvider(FontProvider):
    """Provider for Material Design icons."""

    NAME: ClassVar[str] = "material"
    PREFIX: ClassVar[str] = "mdi"
    DISPLAY_NAME: ClassVar[str] = "Material Design Icons"

    VERSION_URL = "https://api.github.com/repos/Templarian/MaterialDesign-Webfont/tags"
    BASE_URL = "https://raw.githubusercontent.com/Templarian/MaterialDesign-Webfont/master"
    CSS_PATTERN = r'\.mdi-([^:]+):before\s*{\s*content:\s*"(.+)"\s*}'

    async def get_latest_version(self) -> str:
        """Get latest version from GitHub tags."""
        data = await fetch_url(self.VERSION_URL, use_cache=self.use_cache)
        tags = load_json(data, return_type=list)
        # Tags API returns list of tags, first one is most recent
        return tags[0]["name"].lstrip("v")

    def get_download_urls(self, version: str) -> tuple[str, str]:
        return (
            f"{self.BASE_URL}/fonts/materialdesignicons-webfont.ttf",
            f"{self.BASE_URL}/css/materialdesignicons.css",
        )

    def process_mapping(self, mapping_data: bytes) -> dict[str, str]:
        """Process Material Design Icons CSS with a custom parser."""
        content = mapping_data.decode("utf-8")
        icons = {}

        # Parse the CSS using the correct double-colon syntax
        current_icon = None
        for line in content.splitlines():
            if ".mdi-" in line and "::before" in line:
                # Extract the icon name
                current_icon = line.split(".mdi-")[1].split("::before")[0]
            elif current_icon and "content:" in line:
                # Extract the Unicode value - format is: content: "\F01C9";
                if "\\F" in line.upper() or "\\f" in line:
                    unicode_value = (
                        line
                        .split("content:")[1]
                        .split('"\\')[1]  # Get part after the backslash
                        .split('"')[0]  # Get part before the closing quote
                    )
                    icons[current_icon] = f"0x{unicode_value.lower()}"
                current_icon = None

        return icons

get_latest_version async

get_latest_version() -> str

Get latest version from GitHub tags.

Source code in src/pycons/font_providers/providers.py
async def get_latest_version(self) -> str:
    """Get latest version from GitHub tags."""
    data = await fetch_url(self.VERSION_URL, use_cache=self.use_cache)
    tags = load_json(data, return_type=list)
    # Tags API returns list of tags, first one is most recent
    return tags[0]["name"].lstrip("v")

process_mapping

process_mapping(mapping_data: bytes) -> dict[str, str]

Process Material Design Icons CSS with a custom parser.

Source code in src/pycons/font_providers/providers.py
def process_mapping(self, mapping_data: bytes) -> dict[str, str]:
    """Process Material Design Icons CSS with a custom parser."""
    content = mapping_data.decode("utf-8")
    icons = {}

    # Parse the CSS using the correct double-colon syntax
    current_icon = None
    for line in content.splitlines():
        if ".mdi-" in line and "::before" in line:
            # Extract the icon name
            current_icon = line.split(".mdi-")[1].split("::before")[0]
        elif current_icon and "content:" in line:
            # Extract the Unicode value - format is: content: "\F01C9";
            if "\\F" in line.upper() or "\\f" in line:
                unicode_value = (
                    line
                    .split("content:")[1]
                    .split('"\\')[1]  # Get part after the backslash
                    .split('"')[0]  # Get part before the closing quote
                )
                icons[current_icon] = f"0x{unicode_value.lower()}"
            current_icon = None

    return icons

:fa-brands-github: Show source on GitHub