Files
blog/app/build.py
2026-03-05 00:02:39 -06:00

62 lines
1.9 KiB
Python

from obsidian_parser import Vault
import shutil
import markdown
from pathlib import Path
import os
def html_file(filename, contentPath): #renders markwown from filename
filePath = Path(f"{contentPath}/{filename}.md")
# 3. Protect against missing files
if not filePath.is_file():
return f"<h1>404</h1><p>Could not find {filename}.md in {filePath}</p>", 404
# open the file for reading
with open(filePath, "r", encoding="utf-8") as f:
textContent = f.read()
# convert it to markdown
htmlContent = markdown.markdown(textContent)
return htmlContent
def obsidian_vault(dest = "/vault"): # makes sure there is a vault in dest
from git import Repo
url = os.getenv("OBSIDIAN_VAULT_URL")
token = os.getenv("OBSIDIAN_VAULT_TOKEN")
if not(token):
print ("token not found, cant build vault")
raise NameError("tokenNotFound")
return 0
url = f"https://{token}@{url}"
if os.path.exists(os.path.join(dest, '.git')):
#TODO handle merge conflictsjjj
print (f"pulling vault from {url} in {dest}")
repo = Repo(dest)
origin = repo.remotes.origin
origin.fetch()
origin.pull(strategy_option='theirs')
print ("vault updated")
return 1
print (f"building vault from {url} in {dest}")
Repo.clone_from(url, dest)
print("cloned vault!")
return 1
def public_vault(privateVault = "/vault", dest = "/content"): # build the public vault in dest from an obsidian repo in src
vault = Vault(privateVault)
if not(vault):
print("could not find vault")
raise NameError("vaultNotFound")
return 0
print(f"valid vault{vault}")
publicNotes = vault.get_notes_with_tag("public")
print(f"publicNotes: {publicNotes}")
for note in publicNotes:
print(note.title)
shutil.copy2(f"{note.path}", dest)