homepage-mfalves/new_content.py

94 lines
1.7 KiB
Python

#!/usr/bin/env python3
import re
from dataclasses import dataclass
from datetime import datetime
from typing import List
dateformat = lambda x: x.strftime("%Y-%m-%d %H:%M")
now = datetime.now()
default_slug = lambda x: re.sub(r'[^a-z0-9_]', '', x.lower().replace(' ', '_'))
existing_authors = []
# def scan_metadata():
# metadata = {}
# for _, _, files in os.walk('./content'):
# for file in files:
# with open(file) as fh:
# content = filter(fh.readlines())
#
#
#
#
# def read_authors():
@dataclass
class ContentMetadata:
title: str
date: str
modified: str
tags: List[str]
category: str
slug: str
authors: str
summary: str
def __repr__(self):
return f"""
{self.title}
{'#' * len(self.title)}
:date: {self.date}
:modified: {self.date}
:tags: {', '.join(self.tags)}
:category: {self.category}
:slug: {self.slug}
:authors: {self.authors}
:summary: {self.summary}
"""
def main():
title = input("Title?")
date = input(f"Creation date? [{dateformat(now)}]")
if len(date) == 0:
date = dateformat(now)
tags = []
tag = None
while tag is None or len(tag) > 0:
if tag is not None:
tags.append(tag)
tag = input("Add tag")
category = input("Category?")
slug = input(f"Slug? [{default_slug(title)}]")
if len(slug) == 0:
slug = default_slug(title)
authors = input("Authors?")
summary = input("Summary?")
content = ContentMetadata(
title,
date,
date,
tags,
category,
slug,
authors,
summary
)
with open(f'./{slug}.rst', 'w') as fh:
fh.write(str(content))
if __name__ == '__main__':
main()