Skip to content

Commit

Permalink
chore: change os.path to Pathlib
Browse files Browse the repository at this point in the history
It makes sense and remove a lot of hacks
  • Loading branch information
keyboard-slayer committed Sep 25, 2024
1 parent 529f6f1 commit c566a49
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions meta/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

ensure((0, 7, 0))

import http.server
import sys
import os
from pathlib import Path
Expand All @@ -15,25 +14,25 @@
CAT = "ᓚ₍ ^. .^₎"
DEFAULT_STYLE_PATH = __file__.replace("__init__.py", "default.css")

SITE_DIR = os.path.join(const.META_DIR, "site")
SITE_BUILD_DIR = os.path.join(const.BUILD_DIR, "site")
SITE_DIR = Path(const.META_DIR) / "site"
SITE_BUILD_DIR = Path(const.BUILD_DIR) / "site"


# MARK: Model ------------------------------------------------------------------


def readFile(path: str) -> str:
with open(path, "r", encoding="utf8") as f:
def readFile(path: Path) -> str:
with path.open("r", encoding="utf8") as f:
return f.read()


def writeFile(path: str, content: str):
with open(path, "w", encoding="utf8") as f:
def writeFile(path: Path, content: str):
with path.open("w", encoding="utf8") as f:
f.write(content)


def fixupLinks(html: str, path: str) -> str:
rootPath = os.path.relpath(".", os.path.dirname(path))
def fixupLinks(html: str, path: Path) -> str:
rootPath = os.path.relpath(".", path.parent)
return (
html.replace('.md"', '.html"')
.replace(".md#", ".html#")
Expand All @@ -57,8 +56,8 @@ class Site(DataClassJsonMixin):

@staticmethod
def load() -> "Site":
siteFile = os.path.join(SITE_DIR, "site.json")
siteJson = jexpr.include(Path(siteFile))
siteFile = SITE_DIR / "site.json"
siteJson = jexpr.include(siteFile)
site = Site.from_dict(siteJson)
site.path = siteFile
return site
Expand All @@ -72,36 +71,35 @@ def renderFavicon(self) -> str:
def renderHeader(self) -> str:
return f'<a href="/"><h1>{self.header or self.title}</h1></a>'

def renderAll(self, out: str, args: BuildArgs) -> None:
def renderAll(self, out: Path, args: BuildArgs) -> None:
style = ""
style += readFile(__file__.replace("__init__.py", f"{args.theme}.css"))
style += readFile(Path(__file__).parent / f"{args.theme}.css")

styleFile = os.path.join(SITE_DIR, "style.css")
if os.path.exists(styleFile):
styleFile = SITE_DIR / "style.css"
if styleFile.exists():
style += "\n\n\n"
style += readFile(styleFile)

md = markdown.Markdown(extensions=["meta"])
files = shell.find(SITE_DIR)
for file in files:
if file.endswith(".json"):
for file in SITE_DIR.rglob("*"):
if file.is_dir() or file.suffix == ".json":
continue

relDir = file.replace(SITE_DIR + "/", "")
print(f"Processing {relDir}")
output = os.path.join(out, relDir)
if not file.endswith(".md"):
shell.mkdir(os.path.dirname(output))
relDir = file.relative_to(SITE_DIR)

output = out / relDir
if not file.suffix == ".md":
output.parent.mkdir(parents=True, exist_ok=True)
shell.cp(file, output)
continue

output = output.replace(".md", ".html")
output = output.with_suffix(".html")

print(f"Rendering {file} -> {output}")

shell.mkdir(os.path.dirname(output))
output.parent.mkdir(parents=True, exist_ok=True)

mdContent = readFile(path=file)
mdContent = readFile(file)
htmlContent = md.convert(mdContent)

title = md.Meta.get("title", [""])[0]
Expand Down Expand Up @@ -166,19 +164,19 @@ def _():
@cli.command("s", "cat/serve", "Serve the site")
def _(args: BuildArgs):
shell.rmrf(SITE_BUILD_DIR)
shell.mkdir(SITE_BUILD_DIR)
SITE_BUILD_DIR.mkdir(parents=True)
site = Site.load()
site.renderAll(SITE_BUILD_DIR, args)

print(f"{CAT} Serving site")
shell.exec(sys.executable, "-m", "http.server", "-d", SITE_BUILD_DIR)
print(f"{CAT} Serving site at folder {SITE_BUILD_DIR}")
shell.exec(sys.executable, "-m", "http.server", "-d", str(SITE_BUILD_DIR))


@cli.command("e", "cat/init", "Initialize the site")
def _():
shell.mkdir(SITE_DIR)
SITE_DIR.mkdir(parents=True, exist_ok=True)
writeFile(
os.path.join(SITE_DIR, "site.json"),
SITE_DIR / "site.json",
"""
{
"favicon": "🐱",
Expand All @@ -191,7 +189,7 @@ def _():
)

writeFile(
os.path.join(SITE_DIR, "index.md"),
SITE_DIR / "index.md",
"""
This is the home page of the site. You can edit this file to change the content of the home page.
""",
Expand Down

0 comments on commit c566a49

Please sign in to comment.