# -*- coding: utf-8 -*- import os import re import requests from lektor.pluginsystem import Plugin from markupsafe import Markup class IconPlugin(Plugin): name = 'Icon Plugin' description = u'Embed SVG icons in your templates.' repos = { 'mdi': 'https://raw.githubusercontent.com/Templarian/MaterialDesign/master/svg/%s.svg', 'fab': 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/svgs/brands/%s.svg', 'fas': 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/svgs/solid/%s.svg', 'fa': 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/svgs/regular/%s.svg', 'far': 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/svgs/regular/%s.svg', 'twbs': 'https://raw.githubusercontent.com/twbs/icons/main/icons/%s.svg', } def on_setup_env(self, **extra): self.env.jinja_env.globals.update( icon=self.icon, inline_svg=self.inline_svg ) def icon(self, name, repo='mdi'): path = self.get_config().get('path', 'assets/icons/') full_path = os.path.join(path, repo, name + '.svg') if (os.path.exists(full_path)): return self.inline_svg(full_path) # download and save icon down_path = self.repos[repo] % name response = requests.get(down_path) if response.status_code == 200: os.makedirs(os.path.dirname(full_path), exist_ok=True) # fix attributes and add class to svg svg = response.text svg = re.sub('', '', svg) svg = re.sub('(height|width|xmlns:xlink|id)=".*?"', '', svg) svg = re.sub('