jimsite/jimsite/__init__.py

80 lines
2.8 KiB
Python

import os
import yaml
from dotmap import DotMap
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# TODO: add a way to change log level
from .common import filepath_or_string, GlobalVars, SiteConfig, dotmap_access
from .templating import format_html_template, map_templates, TemplateSelections
from .assets import pull_git_repo, copy_assets
from .articles import ArticleMetadata, load_markdown, build_articles, build_index
from .blog import build_blog_archive, build_rss_feed
def build_site(site: SiteConfig, templates: DotMap):
'''The pipeline for building a site defined in config.yaml.'''
logger.info(f'Building site "{site.title}".')
# Do not build a site marked as unpublished.
if not site.published:
logger.info(f'"{site.title}" is not published. Skipping.')
return None
# Initialize the build cache and web root, in case they do not exist.
logger.debug(f'Creating build cache: {site.build_cache}')
os.makedirs(site.build_cache, exist_ok = True)
logger.debug(f'Creating web root: {site.web_root}')
os.makedirs(site.web_root, exist_ok = True)
# If the site is built from a git repo, pull that repo into the build cache.
if site.git_repo:
logger.info(f'Cloning/pulling git repo from {site.git_repo}')
pull_git_repo(site.git_repo, site.build_cache)
# Copy the sites assets into the web root.
logger.info(f'Copying static assets.')
copy_assets(site)
# Load the site's articles into an index.
logger.info('Building index of articles.')
index = build_index(site)
# Determine which templates are to be used for explicit applications, e.g.
# the tag component.
logger.info('Loading selected templates.')
template_selections = TemplateSelections(site, templates)
# Generate HTML pages for the articles.
logger.info('Building articles.')
build_articles(site, index, templates, template_selections)
if len(site.articles or []):
logger.info('Building archive of articles.')
build_blog_archive(site, index, template_selections, templates = templates)
if 'rss' in (site.addons or []):
logger.info('Building RSS feed.')
build_rss_feed(site, index)
else:
logger.debug('Addon "rss" not elected.')
def build(config_filepath = './config.yaml'):
'''Pipeline for building the entire website, including all sites defined in config.yaml.'''
logger.info('Loading config.')
with open(config_filepath, 'r') as config_file:
config = yaml.safe_load(config_file.read())
logger.info('Loading global templates.')
templates = map_templates(config['templates_folder'])
for site in config['sites'].values():
build_site(SiteConfig(**site), templates)