Files
blog/app/build.py

60 lines
1.8 KiB
Python

from obsidian_parser import Vault
import shutil
import markdown
from pathlib import Path
import os
def html_file(filename: str, contentPath: Path): #renders markwown from filename
filePath = contentPath / f"{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
# TO DO account for vault existing and not being a valid repo
from git import Repo
url = os.getenv("OBSIDIAN_VAULT_URL")
token = os.getenv("OBSIDIAN_VAULT_TOKEN")
if token:
print ("token found")
url = f"https://{token}@{url}"
if os.path.exists(os.path.join(dest, '.git')):
print (f"pulling vault from {url} in {dest}")
repo = Repo(dest)
origin = repo.remotes.origin
origin.pull()
return 1
print (f"building vault from {url} in {dest}")
Repo.clone_from(url, dest)
print("finished vault!")
return 1
def public_notes(src: str): # return a list of notes tagged with public from an obsidian directory
# build vault from source
vault = Vault(src)
if vault:
print ("found vault")
else:
print("could not find vault")
return []
# return a list ofnotes
return vault.get_notes_with_tag("public")
# def public_vault(dest: str, url = "", token = ""): # build the public vault in dest from an obsidian repo in src
# for note in public_notes(src):
# print(note.title)
# shutil.copy2(f"{note.path}", dest)