Skip to content

Commit

Permalink
Merge pull request #32 from bartfeenstra/clean-up-rendering
Browse files Browse the repository at this point in the history
Clean up rendering
  • Loading branch information
bartfeenstra authored Mar 11, 2019
2 parents 1e1bd5f + 8c9a01d commit 7516dec
Show file tree
Hide file tree
Showing 27 changed files with 73 additions and 71 deletions.
3 changes: 3 additions & 0 deletions betty/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from os.path import join, abspath

RESOURCE_PATH = join(abspath(__path__[0]), 'resources')
2 changes: 1 addition & 1 deletion betty/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _from_json(config_json: str) -> Configuration:
config_dict = loads(config_json)
except JSONDecodeError:
raise ValueError('Invalid JSON.')
with open(join(betty.__path__[0], 'config.schema.json')) as f:
with open(join(betty.RESOURCE_PATH, 'config.schema.json')) as f:
try:
validate(instance=config_dict, schema=load(f))
except ValidationError:
Expand Down
89 changes: 40 additions & 49 deletions betty/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@
import re
import shutil
from glob import glob
from os.path import join, splitext, abspath
from shutil import copytree
from os.path import join, splitext
from typing import Iterable

from jinja2 import Template, Environment, PackageLoader, select_autoescape, evalcontextfilter, escape
from jinja2 import Environment, select_autoescape, evalcontextfilter, escape, FileSystemLoader
from markupsafe import Markup

import betty
from betty.ancestry import Entity, Document
from betty.ancestry import Entity
from betty.site import Site


def render(site: Site) -> None:
_create_directory(site.configuration.output_directory_path)
_render_assets(site.configuration.output_directory_path)
render_documents(site.ancestry.documents.values(), site)
render_entity_type(site, site.ancestry.people.values(), 'person',
site.configuration.output_directory_path)
render_entity_type(site, site.ancestry.places.values(), 'place', site.configuration.output_directory_path)
render_entity_type(site, site.ancestry.events.values(), 'event',
site.configuration.output_directory_path)
_render_content(site)
environment = Environment(
loader=FileSystemLoader(join(betty.RESOURCE_PATH, 'templates')),
autoescape=select_autoescape(['html'])
)
environment.globals['site'] = site
environment.filters['paragraphs'] = _render_html_paragraphs

_render_public(site, environment)
_render_documents(site)
_render_entity_type(site, environment, site.ancestry.people.values(), 'person')
_render_entity_type(site, environment, site.ancestry.places.values(), 'place')
_render_entity_type(site, environment, site.ancestry.events.values(), 'event')


def _create_directory(path: str) -> None:
Expand All @@ -35,65 +37,54 @@ def _create_file(path: str) -> object:
return open(path, 'w')


def _create_document(path: str) -> object:
def _create_html_file(path: str) -> object:
return _create_file(os.path.join(path, 'index.html'))


def _render_assets(path: str) -> None:
copytree(join(betty.__path__[0], 'assets'), join(path, 'assets'))


def _render_content(site: Site) -> None:
template_root_path = join(abspath(betty.__path__[0]), 'templates')
content_root_path = join(template_root_path, 'content')
for content_path in glob(join(content_root_path, '**')):
template_path = content_path[len(template_root_path) + 1:]
destination_path = content_path[len(content_root_path) + 1:]
with _create_file(join(site.configuration.output_directory_path,
destination_path)) as f:
f.write(_get_template(site, template_path).render())
def _render_public(site: Site, environment: Environment) -> None:
template_loader = FileSystemLoader('/')
public_path = join(betty.RESOURCE_PATH, 'public')
for file_path in glob(join(public_path, '**')):
destination_path = join(site.configuration.output_directory_path, file_path[len(public_path) + 1:])
if file_path.endswith('.j2'):
destination_path = destination_path[:-3]
with _create_file(destination_path) as f:
template = template_loader.load(environment, file_path, environment.globals)
f.write(template.render())
else:
shutil.copy2(file_path, destination_path)


def render_documents(documents: Iterable[Document], site: Site) -> None:
def _render_documents(site: Site) -> None:
documents_directory_path = os.path.join(site.configuration.output_directory_path, 'document')
_create_directory(documents_directory_path)
for document in documents:
for document in site.ancestry.documents.values():
destination = os.path.join(documents_directory_path,
document.id + splitext(document.file.path)[1])
shutil.copy2(document.file.path, destination)


def render_entity_type(site: Site, entities: Iterable[Entity], entity_type_name: str,
output_directory_path: str) -> None:
entity_type_path = os.path.join(output_directory_path, entity_type_name)
with _create_document(entity_type_path) as f:
f.write(_get_template(site, 'partials/list-%s.html' % entity_type_name).render({
def _render_entity_type(site: Site, environment: Environment, entities: Iterable[Entity],
entity_type_name: str) -> None:
entity_type_path = os.path.join(site.configuration.output_directory_path, entity_type_name)
with _create_html_file(entity_type_path) as f:
f.write(environment.get_template('list-%s.html.j2' % entity_type_name).render({
'entity_type_name': entity_type_name,
'entities': sorted(entities, key=lambda entity: entity.label),
}))
for entity in entities:
_render_entity(site, entity, entity_type_name, output_directory_path)
_render_entity(site, environment, entity, entity_type_name)


def _render_entity(site: Site, entity: Entity, entity_type_name: str, output_directory_path: str) -> None:
def _render_entity(site: Site, environment: Environment, entity: Entity, entity_type_name: str) -> None:
entity_path = os.path.join(
output_directory_path, entity_type_name, entity.id)
with _create_document(entity_path) as f:
f.write(_get_template(site, 'partials/%s.html' % entity_type_name).render({
site.configuration.output_directory_path, entity_type_name, entity.id)
with _create_html_file(entity_path) as f:
f.write(environment.get_template('%s.html.j2' % entity_type_name).render({
entity_type_name: entity,
}))


def _get_template(site: Site, name: str) -> Template:
environment = Environment(
loader=PackageLoader('betty', 'templates'),
autoescape=select_autoescape(['html'])
)
environment.globals['site'] = site
environment.filters['paragraphs'] = _render_html_paragraphs
return environment.get_template(name)


_paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')


Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
"short_name": "Betty",
"icons": [
{
"src": "/assets/betty-16x16.png",
"src": "/betty-16x16.png",
"sizes": "16x16",
"type": "image/png"
},
{
"src": "/assets/betty-32x32.png",
"src": "/betty-32x32.png",
"sizes": "32x32",
"type": "image/png"
},
{
"src": "/assets/betty-192x192.png",
"src": "/betty-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/assets/betty-512x512.png",
"src": "/betty-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{% extends 'partials/base.html' %}
{% extends 'base.html.j2' %}
{% set title = 'Welcome to Betty' %}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="generator" content="Betty (https://github.com/bartfeenstra/betty)" />
<link rel="stylesheet" href="/assets/betty.css"/>
<link rel="icon" href="/assets/betty.ico">
<link rel="manifest" href="/assets/betty.webmanifest">
<link rel="stylesheet" href="/betty.css"/>
<link rel="icon" href="/betty.ico">
<link rel="manifest" href="/betty.webmanifest">
</head>
<body>
<nav id="nav-primary">
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'partials/base.html' %}
{% extends 'base.html.j2' %}
{% set title = event.label %}
{% block content %}
{% if event.place %}
Expand Down
2 changes: 2 additions & 0 deletions betty/resources/templates/list-event.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% extends 'list.html.j2' %}
{% set title = 'Events' %}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{% extends 'partials/list.html' %}
{% extends 'list.html.j2' %}
{% set title = 'People' %}
{% block content %}
{% if entities %}
<ul class="entities">
{% for person in entities %}
<li class="{{ loop.cycle('odd', 'even') }}">
<a href="/person/{{ person.id }}">{{ person.label }}</a>
{% include 'partials/person-meta.html' %}
{% include 'person-meta.html.j2' %}
</li>
{% endfor %}
</ul>
Expand Down
2 changes: 2 additions & 0 deletions betty/resources/templates/list-place.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% extends 'list.html.j2' %}
{% set title = 'Places' %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'partials/base.html' %}
{% extends 'base.html.j2' %}
{% block content %}
{% if entities %}
<ul class="entities">
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'partials/base.html' %}
{% extends 'base.html.j2' %}
{% set title = person.label %}
{% block content %}
{% include 'partials/person-meta.html' %}
{% include 'person-meta.html.j2' %}

<h2>Parents</h2>
{% if person.parents %}
Expand Down Expand Up @@ -48,7 +48,7 @@ <h2>Siblings</h2>
{% if ns.documents %}
{{ documents }}
{% for document in ns.documents %}
{% include 'partials/document.html' %}
{% include 'document.html.j2' %}
{% endfor %}
{% endif %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'partials/base.html' %}
{% extends 'base.html.j2' %}
{% set title = place.label %}
{% block content %}
{% if place.coordinates %}
Expand Down
2 changes: 0 additions & 2 deletions betty/templates/partials/list-event.html

This file was deleted.

2 changes: 0 additions & 2 deletions betty/templates/partials/list-place.html

This file was deleted.

10 changes: 9 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Integrates Betty with Python's setuptools."""

import os
from os.path import join

from setuptools import setup, find_packages

import betty

ROOT_PATH = os.path.dirname(os.path.abspath(__file__))

with open('/'.join((ROOT_PATH, 'VERSION'))) as f:
Expand All @@ -23,6 +26,11 @@
# Allow this to fail, because we cannot guarantee this dependency is installed.
pass

betty_package_data = []
for (path, _, filenames) in list(os.walk(betty.RESOURCE_PATH)):
for filename in filenames:
betty_package_data.append(join(path, filename))

SETUP = {
'name': 'betty',
'description': 'Betty is a static ancestry site generator.',
Expand All @@ -45,7 +53,7 @@
])
],
'package_data': {
'betty': ['assets/**', 'config.schema.json', 'templates/content/**', 'templates/partials/**'],
'betty': betty_package_data,
},
}

Expand Down

0 comments on commit 7516dec

Please sign in to comment.