feat: add docs update

pull/760/head
Wen Junhua 2023-12-25 23:13:32 +09:00
parent 3a998b4598
commit eb527de54b
1 changed files with 59 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import logging
import requests import requests
import os import os
from typing import Tuple from typing import Tuple, List
DEFAULT_FONTAWESOME_VERSION = "v6.5.1" DEFAULT_FONTAWESOME_VERSION = "v6.5.1"
@ -14,13 +14,17 @@ DEFAULT_BASE_URL = (
% DEFAULT_FONTAWESOME_VERSION % DEFAULT_FONTAWESOME_VERSION
+ "%s.svg" + "%s.svg"
) )
DEFAULT_BASE_PATH = os.path.join(os.getcwd(), "..")
DEFAULT_ICON_PATH = os.path.join( DEFAULT_ICON_PATH = os.path.join(
os.getcwd(), DEFAULT_BASE_PATH,
"..",
"assets", "assets",
"icons", "icons",
) )
DEFAULT_SVG_ATTR = {"fill": "currentColor"} DEFAULT_SVG_ATTR = {"fill": "currentColor"}
DEFAULT_ICON_DOCS = os.path.join(
DEFAULT_BASE_PATH, "exampleSite", "content", "samples", "icons"
)
DEFAULT_TABLE_DELIMITER = "| -------------------- | --------------------------------- |"
def download_icon(download_url: str) -> None: def download_icon(download_url: str) -> None:
@ -39,9 +43,56 @@ def update_svg_to_theme(svg: bytes) -> bytes:
svg_elem[k] = v svg_elem[k] = v
# Remove comments # Remove comments
return re.sub(r'<!.*?->','', str(soup)) return re.sub(r"<!.*?->", "", str(soup))
def update_docs(icon_name: str) -> None:
"""Update icon to docs"""
files = get_folder_md(DEFAULT_ICON_DOCS)
for file in files:
# Parse Table
logging.debug(f"reading {file}")
with open(file) as f:
file_data = f.read()
table_fmt, table = parse_table(file_data)
table.append(str(table[0]).replace("amazon", icon_name))
# Write Doc
print(table_fmt)
with open(file, "w") as f:
f.write(
"\n".join(
(
table_fmt,
DEFAULT_TABLE_DELIMITER,
"\n".join(sorted(table)),
)
)
)
def get_folder_md(dir_path: str) -> List[str]:
return list(
map(
lambda path: os.path.join(dir_path, path),
filter(lambda x: x.endswith(".md"), os.listdir(dir_path)),
)
)
def parse_table(table_str: str) -> Tuple[str, List[str]]:
"""Parse the table to a list of (article_fmt, (icon_name, icon_shortcode))"""
headers, table = table_str.split(DEFAULT_TABLE_DELIMITER)
return headers.strip(), list(
filter(
lambda x: len(x.strip()) > 0,
map(
lambda x: x.strip(),
table.split("\n"),
),
)
)
def save_file(name: str, svg: str) -> None: def save_file(name: str, svg: str) -> None:
file_name = f"{name}.svg" file_name = f"{name}.svg"
@ -125,3 +176,7 @@ if __name__ == "__main__":
save_file(icon_name, final_svg) save_file(icon_name, final_svg)
# Write to docs (TODO) # Write to docs (TODO)
logging.debug("updating")
update_docs(icon_name)
logging.info("done!")