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

Added check script for the icons and fix icons with fill or viewBox issues #460

Merged
merged 16 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change format of log and allow filehandler to return Path
  • Loading branch information
Thomas-Boi committed Jan 8, 2021
commit 4a072c81344cf507ea7869b3fdeb863f46db9554
20 changes: 12 additions & 8 deletions .github/scripts/build_assets/filehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ def is_not_in_icomoon_json(icon, icomoon_json) -> bool:


def get_svgs_paths(new_icons: List[dict], icons_folder_path: str,
icon_versions_only: bool=False) -> List[str]:
icon_versions_only: bool=False, as_str: bool=True) -> List[str] or List[Path]:
"""
Get all the suitable svgs file path listed in the devicon.json.
:param new_icons, a list containing the info on the new icons.
:param icons_folder_path, the path where the function can find the
listed folders.
:param icon_versions_only, whether to only get the svgs that can be
made into an icon.
:param: as_str, whether to add the path as a string or as a Path.
:return: a list of svg file paths that can be uploaded to Icomoon.
"""
file_paths = []
Expand All @@ -63,19 +64,20 @@ def get_svgs_paths(new_icons: List[dict], icons_folder_path: str,
raise ValueError(f"Invalid path. This is not a directory: {folder_path}.")

if icon_versions_only:
get_icon_svgs_paths(folder_path, icon_info, file_paths)
get_icon_svgs_paths(folder_path, icon_info, file_paths, as_str)
else:
get_all_svgs_paths(folder_path, icon_info, file_paths)
get_all_svgs_paths(folder_path, icon_info, file_paths, as_str)
return file_paths



def get_icon_svgs_paths(folder_path: Path, icon_info: dict, file_paths: List[str]):
def get_icon_svgs_paths(folder_path: Path, icon_info: dict,
file_paths: List[str], as_str: bool):
"""
Get only the svg file paths that can be made into an icon from the icon_info.
:param: folder_path, the folder where we can find the icons.
:param: icon_info, an icon object in the devicon.json.
:param: file_paths, an array containing all the file paths found.
:param: as_str, whether to add the path as a string or as a Path.
"""
aliases = icon_info["aliases"]

Expand All @@ -89,24 +91,26 @@ def get_icon_svgs_paths(folder_path: Path, icon_info: dict, file_paths: List[str
path = Path(folder_path, file_name)

if path.exists():
file_paths.append(str(path))
file_paths.append(str(path) if as_str else path)
else:
raise ValueError(f"This path doesn't exist: {path}")


def get_all_svgs_paths(folder_path: Path, icon_info: dict, file_paths: List[str]):
def get_all_svgs_paths(folder_path: Path, icon_info: dict,
file_paths: List[str], as_str: bool):
"""
Get all the svg file paths of an icon.
:param: folder_path, the folder where we can find the icons.
:param: icon_info, an icon object in the devicon.json.
:param: file_paths, an array containing all the file paths found.
:param: as_str, whether to add the path as a string or as a Path.
"""
for font_version in icon_info["versions"]["svg"]:
file_name = f"{icon_info['name']}-{font_version}.svg"
path = Path(folder_path, file_name)

if path.exists():
file_paths.append(str(path))
file_paths.append(str(path) if as_str else path)
else:
raise ValueError(f"This path doesn't exist: {path}")

Expand Down
27 changes: 16 additions & 11 deletions .github/scripts/check_svgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import xml.etree.ElementTree as et
import time
from pathlib import Path


# pycharm complains that build_assets is an unresolved ref
Expand All @@ -28,15 +29,15 @@ def main():
time.sleep(1) # do this so the logs stay clean
try:
# check the svgs
svgs = filehandler.get_svgs_paths(new_icons, args.icons_folder_path)
svgs = filehandler.get_svgs_paths(new_icons, args.icons_folder_path, as_str=False)
check_svgs(svgs)
print("All SVGs found were good.\nTask completed.")
except Exception as e:
github_env.set_env_var("ERR_MSGS", str(e))
sys.exit(str(e))


def check_svgs(svg_file_paths: List[str]):
def check_svgs(svg_file_paths: List[Path]):
"""
Check the width, height, viewBox and style of each svgs passed in.
The viewBox must be '0 0 128 128'.
Expand All @@ -51,35 +52,39 @@ def check_svgs(svg_file_paths: List[str]):
tree = et.parse(svg_path)
root = tree.getroot()
namespace = "{http://www.w3.org/2000/svg}"
err_msg = [f"{svg_path.name}:"]

if root.tag != f"{namespace}svg":
err_msgs.append(f"Root is '{root.tag}'. Root must be an 'svg' element: {svg_path}")
err_msg.append(f"-root is '{root.tag}'. Root must be an 'svg' element")

if root.get("viewBox") != "0 0 128 128":
err_msgs.append("<svg> 'viewBox' is not '0 0 128 128': " + svg_path)
err_msg.append("-'viewBox' is not '0 0 128 128'")

acceptable_size = [None, "128px", "128"]
if root.get("height") not in acceptable_size:
err_msgs.append("<svg> 'height' is present but is not '128' or '128px': " + svg_path)
err_msg.append("-'height' is present but is not '128' or '128px'")

if root.get("width") not in acceptable_size:
err_msgs.append("<svg> 'width' is present but is not '128' or '128px': " + svg_path)
err_msg.append("-'width' is present but is not '128' or '128px'")

if root.get("style") is not None and "enable-background" in root.get("style"):
err_msgs.append("<svg> uses 'enable-background' in its style. This is deprecated: " + svg_path)
err_msg.append("-uses 'enable-background' in its style. This is deprecated")

if root.get("x") is not None:
err_msgs.append("<svg> has an 'x' attribute, this is not needed: " + svg_path)
err_msg.append("-has an 'x' attribute, this is not needed")

if root.get("y") is not None:
err_msgs.append("<svg> has an 'y' attribute, this is not needed: " + svg_path)
err_msg.append("-has an 'y' attribute, this is not needed")

style = root.findtext(f".//{namespace}style")
if style != None and "fill" in style:
err_msgs.append("Found 'fill' in <style>. Use the 'fill' attribute instead: " + svg_path)
err_msg.append("-'fill' in style element. Use the 'fill' attribute instead")

if len(err_msg) > 1:
err_msgs.append("\n".join(err_msg))

if len(err_msgs) > 0:
raise Exception("\n" + "\n".join(err_msgs))
raise Exception("\n" + "\n\n".join(err_msgs))


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/check_svgs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ jobs:
I'm Devicons' SVG-Checker Bot and it seems we've ran into a problem. I'm supposed to check your svgs but I couldn't do my task due to an issue.

Here is what went wrong:
```
{0}
```

Please address these issues. When you update this PR, I will check your SVGs again.

Expand Down