update some template functions

This commit is contained in:
Tyler Starr 2024-08-16 14:57:29 -07:00
parent b6736936a7
commit 1d8008ae0b
4 changed files with 17 additions and 13 deletions

View File

@ -37,19 +37,22 @@ def normalize_name(name: str, tags: str, time: Union[datetime.date, None] = None
f += "." + ext f += "." + ext
return f return f
def create_file(name: str, silo: str, template: str = "default.md"): def create_file(name: str, silo: str, content: str = ""):
if silo: if silo:
Path(silo).mkdir(exist_ok=True) Path(silo).mkdir(exist_ok=True)
if silo[-1] != "/": if silo[-1] != "/":
silo += "/" silo += "/"
content = apply_template(Path.home().joinpath(".config/z-py/templates"), template)
with open(silo + name, "w") as f: with open(silo + name, "w") as f:
f.write(content) f.write(content)
return return
def main(args): def main(args):
file = normalize_name(args.name, args.tags) file = normalize_name(args.name, args.tags)
create_file(file, args.silo) if args.outline:
content = apply_template(args, Path.home().joinpath(".config/z-py/templates"), args.outline)
else:
content = apply_template(args, Path.home().joinpath(".config/z-py/templates"), "default.md")
create_file(file, args.silo, content=content)
if __name__ == "__main__": if __name__ == "__main__":
main(None) main(None)

View File

@ -12,6 +12,6 @@ def read_file(p: Path, n: str):
def split_template(s: str) -> Tuple[List[str], str]: def split_template(s: str) -> Tuple[List[str], str]:
return re.findall(r"{([^}]+)}", s), re.sub(r"{([^}]+)}", "{}", s) return re.findall(r"{([^}]+)}", s), re.sub(r"{([^}]+)}", "{}", s)
def apply_template(path: Path, name: str): def apply_template(args, path: Path, name: str):
fns, s = split_template(read_file(path, name)) fns, s = split_template(read_file(path, name))
return s.format(*[eval("template_funcs." + fn)() for fn in fns]) return s.format(*[eval("template_funcs." + fn)(args) for fn in fns])

View File

@ -1,16 +1,16 @@
from datetime import datetime from datetime import datetime
def date(): def date(args):
return datetime.now().strftime("%Y%m%d") return datetime.now().strftime("%Y%m%d")
def time(): def time(args):
return datetime.now().strftime("%Y%m%dT%H%M%S") return datetime.now().strftime("%Y%m%dT%H%M%S")
def title(): def title(args):
return "TODO" return args.name
def silo(): def silo(args):
return "TODO" return args.silo
def tags(): def tags(args):
return "TODO" return args.tags

View File

@ -12,6 +12,7 @@ def init_parser():
create_parser.add_argument("name", help="name of note to create") create_parser.add_argument("name", help="name of note to create")
create_parser.add_argument("-t", "--tags", nargs="?", default="", const="", help="comma seperated list of tags for note") create_parser.add_argument("-t", "--tags", nargs="?", default="", const="", help="comma seperated list of tags for note")
create_parser.add_argument("-s", "--silo", nargs="?", default="", const="", help="Optionally create note in silo") create_parser.add_argument("-s", "--silo", nargs="?", default="", const="", help="Optionally create note in silo")
create_parser.add_argument("-o", "--outline", nargs="?", default="", const="", help="Optionally initialize note with outline")
tags_parser = sub_parsers.add_parser("tags", help="edit tags") tags_parser = sub_parsers.add_parser("tags", help="edit tags")
tags_parser.set_defaults(func=tags.main) tags_parser.set_defaults(func=tags.main)