updates
This commit is contained in:
35
classes.py
35
classes.py
@@ -11,31 +11,6 @@ from config import config
|
||||
#from jinja_env import env, content_item_template, index_template
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
# Get the script base_dir and check for templates dir
|
||||
if Path(f'themes/{config.theme}/templates').exists():
|
||||
base_dir = "."
|
||||
elif getattr(sys, 'frozen', False):
|
||||
# Running fron Pyinstaller-packed binary
|
||||
base_dir = Path(sys._MEIPASS)
|
||||
else:
|
||||
# Running as a plain Python app
|
||||
base_dir = Path(__file__).resolve().parent
|
||||
|
||||
# Prepare template rendering engine
|
||||
if Path(f"themes/{config.theme}/templates").exists():
|
||||
# Use the templates from the initialized site instance and the installed theme
|
||||
jinja_env_dir = f"themes/{config.theme}/templates"
|
||||
logger.debug(f"Using locally initialized templates from {jinja_env_dir}")
|
||||
else:
|
||||
# Use default templates from the default theme shipped with the app
|
||||
# i.e. PyInstaller-packed single executable
|
||||
logger.debug("Using shipped default temlpates.")
|
||||
jinja_env_dir = f"{base_dir}/themes/default/templates"
|
||||
env = Environment(loader=FileSystemLoader(f"{jinja_env_dir}"))
|
||||
env.globals['header_image'] = f"{base_dir}/themes/{config.theme}/static/images/header.jpg"
|
||||
index_template = env.get_template("index.html")
|
||||
content_item_template = env.get_template("content_item.html")
|
||||
|
||||
class ContentItem:
|
||||
def render_content(self, categories, target_file = False):
|
||||
if target_file:
|
||||
@@ -60,7 +35,7 @@ class ContentItem:
|
||||
logger.debug(f"Copying {self.js_file} to {js_targetfile}")
|
||||
shutil.copyfile(self.js_file, js_targetfile)
|
||||
with self.target_file.open("w", encoding="utf-8") as f:
|
||||
f.write(content_item_template.render(content_item = self, page_title = f"{config.site_name}: {self.title}", parent_path = '../', categories = categories, footer_data = footer_data))
|
||||
f.write(config.content_item_template.render(content_item = self, page_title = f"{config.site_name}: {self.title}", parent_path = '../', categories = categories, footer_data = footer_data))
|
||||
except Exception as e:
|
||||
logger.error(f"Renderer: {e}")
|
||||
|
||||
@@ -133,7 +108,7 @@ class Site:
|
||||
os.makedirs(subdir, exist_ok=True)
|
||||
|
||||
# Copy default theme
|
||||
shutil.copytree(f"{base_dir}/themes/default", "themes/default", dirs_exist_ok=True)
|
||||
shutil.copytree(f"{config.base_dir}/themes/default", "themes/default", dirs_exist_ok=True)
|
||||
|
||||
def get_content_items(self):
|
||||
logger.debug("Getting content items")
|
||||
@@ -164,7 +139,7 @@ class Site:
|
||||
subdir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Copy theme's static dir to 'public/static
|
||||
shutil.copytree(f"{base_dir}/themes/{config.theme}/static", f"{self.output_dir}/static", dirs_exist_ok=True)
|
||||
shutil.copytree(f"{config.base_dir}/themes/{config.theme}/static", f"{self.output_dir}/static", dirs_exist_ok=True)
|
||||
|
||||
# Get content items
|
||||
self.get_content_items()
|
||||
@@ -188,7 +163,7 @@ class Site:
|
||||
footer_data = ''
|
||||
visible_content_items = [c for c in self.content_items if c.data.get("hidden") != True]
|
||||
with (self.output_dir / "index.html").open("w", encoding="utf-8") as f:
|
||||
f.write(index_template.render(page_title = config.site_name, content_items=visible_content_items, categories = self.categories, footer_data = footer_data))
|
||||
f.write(config.index_template.render(page_title = config.site_name, content_items=visible_content_items, categories = self.categories, footer_data = footer_data))
|
||||
|
||||
# Render the categories indices
|
||||
if hasattr(config, "footer"):
|
||||
@@ -200,7 +175,7 @@ class Site:
|
||||
category_index = Path(f"{self.output_dir}/categories/{category}.html")
|
||||
category_items = [i for i in visible_content_items if category in i.data.get("categories")]
|
||||
with (category_index).open(mode="w", encoding="utf-8") as f:
|
||||
f.write(index_template.render(page_title=f"{config.site_name}: {category}", content_items=category_items, categories = self.categories, parent_path = '../', footer_data = footer_data))
|
||||
f.write(config.index_template.render(page_title=f"{config.site_name}: {category}", content_items=category_items, categories = self.categories, parent_path = '../', footer_data = footer_data))
|
||||
|
||||
logger.info(f"Created {len(self.content_items)} content items.")
|
||||
|
||||
31
config.py
31
config.py
@@ -1,4 +1,6 @@
|
||||
import os, yaml
|
||||
import os, sys, yaml
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from pathlib import Path
|
||||
|
||||
# Main config section
|
||||
#class Config:
|
||||
@@ -33,6 +35,33 @@ class Config:
|
||||
if config:
|
||||
self.update_from_dict(config)
|
||||
|
||||
|
||||
# Get the script base_dir and check for templates dir
|
||||
if Path(f'themes/{self.theme}/templates').exists():
|
||||
self.base_dir = "."
|
||||
elif getattr(sys, 'frozen', False):
|
||||
# Running fron Pyinstaller-packed binary
|
||||
self.base_dir = Path(sys._MEIPASS)
|
||||
else:
|
||||
# Running as a plain Python app
|
||||
self.base_dir = Path(__file__).resolve().parent
|
||||
|
||||
# Prepare template rendering engine
|
||||
if Path(f"themes/{self.theme}/templates").exists():
|
||||
# Use the templates from the initialized site instance and the installed theme
|
||||
jinja_env_dir = f"themes/{self.theme}/templates"
|
||||
#logger.debug(f"Using locally initialized templates from {jinja_env_dir}")
|
||||
else:
|
||||
# Use default templates from the default theme shipped with the app
|
||||
# i.e. PyInstaller-packed single executable
|
||||
#logger.debug("Using shipped default temlpates.")
|
||||
jinja_env_dir = f"{self.base_dir}/themes/default/templates"
|
||||
self.env = Environment(loader=FileSystemLoader(f"{jinja_env_dir}"))
|
||||
self.env.globals['header_image'] = f"{self.base_dir}/themes/{self.theme}/static/images/header.jpg"
|
||||
self.index_template = self.env.get_template("index.html")
|
||||
self.content_item_template = self.env.get_template("content_item.html")
|
||||
|
||||
|
||||
def update_from_dict(self, config_dict):
|
||||
for key, value in config_dict.items():
|
||||
setattr(self, key, value)
|
||||
|
||||
Reference in New Issue
Block a user