feat: added nice config functionality
This commit is contained in:
@@ -8,8 +8,7 @@ from typing import Optional, Union, Dict, Generator, List, DefaultDict
|
||||
from bs4 import BeautifulSoup
|
||||
from collections import defaultdict
|
||||
|
||||
from .config import SOURCE_DIRECTORY, DIST_DIRECTORY, LANGUAGE_INFORMATION, ARTICLE_PREVIEW_LENGTH, DEFAULT_LANGUAGE
|
||||
|
||||
from . import config
|
||||
|
||||
|
||||
def replace_values(template: str, values: Dict[str, str]) -> str:
|
||||
@@ -19,14 +18,14 @@ def replace_values(template: str, values: Dict[str, str]) -> str:
|
||||
return template
|
||||
|
||||
|
||||
def get_first_header_content(content):
|
||||
def get_first_header_content(content, fallback: str = ""):
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
for level in range(1, 7):
|
||||
header = soup.find(f'h{level}')
|
||||
if header:
|
||||
return header.get_text(strip=True)
|
||||
|
||||
return self.language_code.native_name
|
||||
return fallback
|
||||
|
||||
|
||||
class Template:
|
||||
@@ -48,23 +47,25 @@ class ArticleTranslation:
|
||||
self.article_content = self.file.read_text()
|
||||
if self.file.suffix == ".md":
|
||||
self.article_content = markdown.markdown(self.article_content)
|
||||
self.title = get_first_header_content(self.article_content)
|
||||
|
||||
self.location_in_tree = [self.language_code, *self.article_overview.location_in_tree]
|
||||
self.url = "/" + "/".join(self.location_in_tree)
|
||||
self.dist_path = Path(DIST_DIRECTORY, *self.location_in_tree)
|
||||
self.dist_path = Path(config.setup.dist_directory, *self.location_in_tree)
|
||||
|
||||
_language_info = DEFAULT_LANGUAGE
|
||||
_language_info = config.languages[config.formatting.fallback_language]
|
||||
parsed_language_code = self.language_code.lower().replace("-", "_")
|
||||
if parsed_language_code in LANGUAGE_INFORMATION:
|
||||
_language_info = LANGUAGE_INFORMATION[parsed_language_code]
|
||||
elif parsed_language_code.split("_")[0] in LANGUAGE_INFORMATION:
|
||||
_language_info = LANGUAGE_INFORMATION[parsed_language_code.split("_")[0]]
|
||||
if parsed_language_code in config.languages:
|
||||
_language_info = config.languages[parsed_language_code]
|
||||
elif parsed_language_code.split("_")[0] in config.languages:
|
||||
_language_info = config.languages[parsed_language_code.split("_")[0]]
|
||||
|
||||
self.language_name: str = _language_info["native_name"]
|
||||
self.language_flag: str = _language_info["flag"]
|
||||
self.priority: int = _language_info.get("priority", 0)
|
||||
|
||||
self.title = get_first_header_content(self.article_content, fallback=self.language_name)
|
||||
|
||||
|
||||
|
||||
def build(self):
|
||||
self.dist_path.mkdir(parents=True, exist_ok=True)
|
||||
@@ -76,7 +77,7 @@ class ArticleTranslation:
|
||||
def _get_values(self, return_foreign_articles: bool = True) -> Dict[str, str]:
|
||||
r = {
|
||||
"article_content": self.article_content,
|
||||
"article_preview": self.article_content[:ARTICLE_PREVIEW_LENGTH] + "...",
|
||||
"article_preview": self.article_content[:config.formatting.article_preview_length] + "...",
|
||||
"article_url": self.url,
|
||||
"article_overview_url": self.article_overview.url,
|
||||
"article_slug": self.article_overview.slug,
|
||||
@@ -118,7 +119,7 @@ class ArticleOverview:
|
||||
self.location_in_tree: List[str] = location_in_tree or []
|
||||
self.location_in_tree.append(self.slug)
|
||||
self.url = "/" + "/".join(self.location_in_tree)
|
||||
self.dist_path = Path(DIST_DIRECTORY, *self.location_in_tree)
|
||||
self.dist_path = Path(config.setup.dist_directory, *self.location_in_tree)
|
||||
|
||||
if self.slug in ARTICLE_LAKE:
|
||||
logger.error("two articles have the same name at %s and %r", ARTICLE_LAKE[self.slug].directory, self.directory)
|
||||
@@ -183,8 +184,8 @@ class ArticleOverview:
|
||||
|
||||
|
||||
def copy_static():
|
||||
src = str(Path(SOURCE_DIRECTORY, "static"))
|
||||
dst = str(Path(DIST_DIRECTORY, "static"))
|
||||
src = str(Path(config.setup.source_directory, "static"))
|
||||
dst = str(Path(config.setup.dist_directory, "static"))
|
||||
|
||||
|
||||
if not os.path.exists(src):
|
||||
@@ -217,7 +218,7 @@ def copy_static():
|
||||
|
||||
# GLOBALS
|
||||
logger = logging.getLogger("stsg.build")
|
||||
TEMPLATE = Template(Path(SOURCE_DIRECTORY, "templates"))
|
||||
TEMPLATE = Template(Path(config.setup.source_directory, "templates"))
|
||||
ARTICLE_LAKE: Dict[str, ArticleOverview] = {}
|
||||
ARTICLE_REFERENCE_VALUES: DefaultDict[str, Dict[str, str]] = defaultdict(dict)
|
||||
|
||||
@@ -225,7 +226,7 @@ def build():
|
||||
logger.info("building static page")
|
||||
|
||||
copy_static()
|
||||
tree = ArticleOverview(directory=Path(SOURCE_DIRECTORY, "articles"))
|
||||
tree = ArticleOverview(directory=Path(config.setup.source_directory, "articles"))
|
||||
|
||||
# build article reverence values
|
||||
for article_overview in ARTICLE_LAKE.values():
|
||||
|
||||
Reference in New Issue
Block a user