Merge branch 'master' into split_archive

This commit is contained in:
Bastian Venthur
2023-06-13 16:37:52 +02:00
7 changed files with 102 additions and 37 deletions

View File

@@ -16,13 +16,13 @@ import sys
from jinja2 import (
Environment,
ChoiceLoader,
FileSystemLoader,
PackageLoader,
Template,
TemplateNotFound,
)
import feedgenerator
import blag
from blag.markdown import markdown_factory, convert_markdown
from blag.devserver import serve
from blag.version import __VERSION__
@@ -185,15 +185,14 @@ def get_config(configfile: str) -> configparser.SectionProxy:
def environment_factory(
template_dir: str | None = None,
template_dir: str,
globals_: dict[str, object] | None = None,
) -> Environment:
"""Environment factory.
Creates a Jinja2 Environment with the default templates and
additional templates from `template_dir` loaded. If `globals` are
provided, they are attached to the environment and thus available to
all contexts.
Creates a Jinja2 Environment with the templates from `template_dir` loaded.
If `globals` are provided, they are attached to the environment and thus
available to all contexts.
Parameters
----------
@@ -206,13 +205,7 @@ def environment_factory(
jinja2.Environment
"""
# first we try the custom templates, and fall back the ones provided
# by blag
loaders: list[FileSystemLoader | PackageLoader] = []
if template_dir:
loaders.append(FileSystemLoader([template_dir]))
loaders.append(PackageLoader('blag', 'templates'))
env = Environment(loader=ChoiceLoader(loaders))
env = Environment(loader=FileSystemLoader(template_dir))
if globals_:
env.globals = globals_
return env
@@ -261,12 +254,22 @@ def build(args: argparse.Namespace) -> None:
env = environment_factory(args.template_dir, dict(site=config))
page_template = env.get_template('page.html')
article_template = env.get_template('article.html')
index_template = env.get_template('index.html')
archive_template = env.get_template('archive.html')
tags_template = env.get_template('tags.html')
tag_template = env.get_template('tag.html')
try:
page_template = env.get_template('page.html')
article_template = env.get_template('article.html')
index_template = env.get_template('index.html')
archive_template = env.get_template('archive.html')
tags_template = env.get_template('tags.html')
tag_template = env.get_template('tag.html')
except TemplateNotFound as exc:
tmpl = os.path.join(blag.__path__[0], 'templates')
logger.error(
f'Template "{exc.name}" not found in {args.template_dir}! '
'Consider running `blag quickstart` or copying the '
f'missing template from {tmpl}.'
)
sys.exit(1)
articles, pages = process_markdown(
convertibles,

View File

@@ -6,6 +6,10 @@
from __future__ import annotations
import configparser
import argparse
import shutil
import os
import blag
def get_input(question: str, default: str) -> str:
@@ -33,6 +37,22 @@ def get_input(question: str, default: str) -> str:
return reply
def copy_templates() -> None:
"""Copy templates into current directory.
It will not overwrite existing files.
"""
print("Copying templates...")
try:
shutil.copytree(
os.path.join(blag.__path__[0], 'templates'),
'templates',
)
except FileExistsError:
print("Templates already exist. Skipping.")
def quickstart(args: argparse.Namespace | None) -> None:
"""Quickstart.
@@ -71,3 +91,5 @@ def quickstart(args: argparse.Namespace | None) -> None:
}
with open('config.ini', 'w') as fh:
config.write(fh)
copy_templates()