wanders & mod time added
This commit is contained in:
26
build.py
26
build.py
@@ -3,6 +3,7 @@ import markdown
|
||||
import re
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
import yaml
|
||||
import time
|
||||
import json
|
||||
|
||||
class Website:
|
||||
@@ -14,7 +15,7 @@ class Website:
|
||||
self.pages = []
|
||||
self.tags = []
|
||||
self.all_images = []
|
||||
self.about_meta, self.about_content = self.fetch_page('content', 'about.md')
|
||||
self.about_meta, self.about_content, self.about_modified_time = self.fetch_page('content', 'about.md')
|
||||
|
||||
def build(self):
|
||||
self.fetch_pages()
|
||||
@@ -30,6 +31,8 @@ class Website:
|
||||
if page.endswith('.md'):
|
||||
with open(os.path.join('content/posts', page), 'r', encoding="utf8") as f:
|
||||
content = f.read()
|
||||
file_desc = f.fileno()
|
||||
file_status = os.fstat(file_desc)
|
||||
parts = content.split('---')
|
||||
metadata = yaml.safe_load(parts[1])
|
||||
md_content = ''.join(parts[2:])
|
||||
@@ -37,7 +40,7 @@ class Website:
|
||||
md_content = self.format_content(md_content)
|
||||
html_content = markdown.markdown(md_content)
|
||||
output_filename = os.path.splitext(page)[0] + '.html'
|
||||
new_page = Page(metadata, html_content, output_filename)
|
||||
new_page = Page(metadata, html_content, output_filename, file_status)
|
||||
self.pages.append(new_page)
|
||||
|
||||
def process_page(self):
|
||||
@@ -55,6 +58,7 @@ class Website:
|
||||
image=metadata.get('image', ''), # Assuming image is a string
|
||||
showcase=metadata.get('showcase', []),
|
||||
credits=metadata.get('credits', []), # Assuming credits is a string
|
||||
modified_time=page.get_modified_time_readable(),
|
||||
content=html_content)
|
||||
with open(os.path.join(self.output_dir_root, filename), 'w', encoding='utf8') as output_file:
|
||||
output_file.write(html_output)
|
||||
@@ -73,7 +77,8 @@ class Website:
|
||||
md_content = ''.join(parts[2:])
|
||||
md_content = re.sub(r'\(([^)]+)\)\[([^\]]+)\]', r'<a href="\2">\1</a>', md_content)
|
||||
html_content = markdown.markdown(md_content)
|
||||
return metadata, html_content
|
||||
about_modified_time = time.ctime(os.fstat(f.fileno()).st_mtime)
|
||||
return metadata, html_content, about_modified_time
|
||||
|
||||
def create_list(self):
|
||||
template = self.env.get_template('list.html')
|
||||
@@ -96,7 +101,8 @@ class Website:
|
||||
template = self.env.get_template('about.html')
|
||||
html_output = template.render(
|
||||
content=self.about_content,
|
||||
socials=self.about_meta
|
||||
socials=self.about_meta,
|
||||
modified_time = self.about_modified_time
|
||||
)
|
||||
with open(os.path.join('public', 'about.html'), 'w', encoding='utf8') as output_file:
|
||||
output_file.write(html_output)
|
||||
@@ -138,12 +144,13 @@ class Website:
|
||||
|
||||
with open(json_file, 'w', encoding='utf8') as f:
|
||||
json.dump(page_info_list, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
class Page:
|
||||
def __init__(self, metadata, md_content, filename):
|
||||
def __init__(self, metadata, md_content, filename, last_updated):
|
||||
self.metadata = metadata
|
||||
self.content = md_content
|
||||
self.filename = filename
|
||||
self.last_updated = last_updated
|
||||
self.title = self.metadata['title']
|
||||
self.type = self.metadata['type']
|
||||
self.year = self.metadata['year']
|
||||
@@ -185,6 +192,13 @@ class Page:
|
||||
def display_metadata(self):
|
||||
output = f"Title: {self.title}\nYear: {self.year}\nDate: {self.date}\nTags: {self.tags}\nType: {self.type}"
|
||||
print(output)
|
||||
|
||||
def get_modified_time_readable(self):
|
||||
if self.last_updated is not None:
|
||||
last_modified_time = self.last_updated.st_mtime
|
||||
last_modified_time_readable = time.ctime(last_modified_time)
|
||||
return last_modified_time_readable
|
||||
return None
|
||||
|
||||
def main():
|
||||
inst = Website('templates', 'public')
|
||||
|
||||
Reference in New Issue
Block a user