[script] new_content permet de créer les entete pour une nouvelle page

This commit is contained in:
max/sooulix 2024-07-07 19:53:14 +02:00
parent 345304a615
commit 971d761212
1 changed files with 93 additions and 0 deletions

93
new_content.py Normal file
View File

@ -0,0 +1,93 @@
#!/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()