This commit is contained in:
sg
2025-06-10 11:37:57 +03:00
parent 45d12ab97d
commit 26c4b9c1e1
2 changed files with 22 additions and 28 deletions

View File

@@ -8,15 +8,7 @@ from logger import logger
from config import Config
from jinja_env import env, content_item_template, index_template
class DefaultFrontmatterHeader:
def __init__(self, title):
self.title = f"title: '{title}'\n"
self.date = f"date: '{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}'\n"
self.omit_second_title = f"omit_second_title: '{str(False)}'\n"
self.description = "description: \n"
self.author = "author: \n"
class ContentItemPrototype:
class ContentItem:
def render_content(self):
logger.debug(f"Rendering {self.source_filename} to {Config.OUTPUT_DIR}/{self.target_filename}")
if self.image_file.exists():
@@ -31,20 +23,22 @@ class ContentItemPrototype:
def parse_content(self):
try:
self.source_filename = Path(self.source_filename)
self.subdir = self.source_filename.parent
logger.debug(f"Parsing item {self.source_filename}")
self.subdir = self.source_filename.parent
self.slug = self.source_filename.stem
self.target_filename = Path(f"{Config.OUTPUT_DIR}/{self.source_filename.parent}/{self.source_filename.stem}.html")
self.data = frontmatter.load(self.source_filename)
self.html = markdown.markdown(self.data.content)
self.html = markdown.markdown(self.data.content.replace('\n', ' \n'))
self.url = "<a href=" + f"{self.subdir}/{self.slug}.html" + "> ...read more</a>"
self.preview = self.html[:300] + self.url
self.title = self.data.get("title", self.slug)
self.omit_second_title = self.data.get("omit_second_title", False)
self.date = self.data.get("date", "2000-01-01T00:00:00+03:00")
self.image_file = self.source_filename.stem + ".jpg"
self.css_file = self.source_filename.stem + ".css"
self.js_file = self.source_filename.stem + ".js"
self.categories = self.data.get("categories", str([]))
self.hidden = self.data.get("hidden", str(False))
self.image_file = Path(self.source_filename.stem + ".jpg")
self.css_file = Path(self.source_filename.stem + ".css")
self.js_file = Path(self.source_filename.stem + ".js")
except Exception as e:
logger.error(e)
@@ -52,20 +46,21 @@ class ContentItemPrototype:
with open(self.source_filename, mode="w", encoding="utf-8") as f:
f.writelines([
"---\n",
self.default_frontmatter_header.title,
self.default_frontmatter_header.date,
self.default_frontmatter_header.description,
self.default_frontmatter_header.author,
self.default_frontmatter_header.omit_second_title,
"---\n"
f"title: {self.source_filename.stem}\n",
f"date: '{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}'\n",
"description: ''\n",
"author: ''\n",
"categories: ['default']\n",
"hidden: 'False'\n",
"omit_second_title: 'False'\n",
"---\n",
"\n\n\n"
])
def __init__(self, filename, title = None):
def __init__(self, filename):
self.source_filename = filename
self.title = title or self.source_filename.stem
self.default_frontmatter_header = DefaultFrontmatterHeader(self.title)
class SitePrototype:
class Site:
def init_site():
logger.info("Initializing new site")
content_dir = Path(Config.CONTENT_DIR)

View File

@@ -8,11 +8,10 @@ from logger import logger
def create_content(filename):
filename = Path(f"{Config.CONTENT_DIR}/{filename}.md")
if not filename.exists():
fh = DefaultFrontmatterHeader(filename.stem)
logger.debug(f"Creating new content {filename}")
with filename.open("w", encoding="utf-8") as f:
f.writelines(['---\n', fh.title, fh.date, fh.description, fh.author, fh.omit_second_title, '---\n', '\n', '\n'])
new_content_item = ContentItem(filename)
new_content_item.create_content()
def edit_content(filename):
logger.debug(f"Editing content {filename}")
editor = os.environ.get('EDITOR', 'vim')