Skip to content

Commit

Permalink
Merge pull request #69 from bartfeenstra/html-time
Browse files Browse the repository at this point in the history
Implement the HTML <time> element.
  • Loading branch information
bartfeenstra authored Mar 30, 2019
2 parents e04672c + 096a2c6 commit 9fe2084
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
20 changes: 2 additions & 18 deletions betty/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from markupsafe import Markup

import betty
from betty.ancestry import Entity, Date
from betty.ancestry import Entity
from betty.json import JSONEncoder
from betty.npm import install, BETTY_INSTANCE_NPM_DIR
from betty.path import iterfiles
Expand All @@ -24,11 +24,11 @@ def render(site: Site) -> None:
autoescape=select_autoescape(['html'])
)
environment.globals['site'] = site
environment.globals['calendar'] = calendar
environment.filters['flatten'] = _render_flatten
environment.filters['walk'] = _render_walk
environment.filters['json'] = _render_json
environment.filters['paragraphs'] = _render_html_paragraphs
environment.filters['date'] = _render_date

_render_public(site, environment)
_render_webpack(site, environment)
Expand Down Expand Up @@ -167,19 +167,3 @@ def _render_html_paragraphs(eval_ctx, text: str) -> Union[str, Markup]:
if eval_ctx.autoescape:
result = Markup(result)
return result


def _render_date(date: Date) -> str:
# All components.
if date.year and date.month and date.day:
return '%s %d, %d' % (calendar.month_name[date.month], date.day, date.year)
# No year.
if not date.year and date.month and date.day:
return '%s %d' % (calendar.month_name[date.month], date.day)
# No month.
if date.year and not date.month:
return str(date.year)
# No day.
if date.year and date.month and not date.day:
return '%s, %d' % (calendar.month_name[date.month], date.year)
return 'unknown date'
3 changes: 2 additions & 1 deletion betty/resources/templates/event-dimensions.html.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% if event.date %}
{{ event.date | date }}
{% import 'macro/date.html.j2' as dateMacros %}
{{ dateMacros.format(event.date) }}
{% endif %}
{% if event.place and event.place != place_context %}
in
Expand Down
26 changes: 26 additions & 0 deletions betty/resources/templates/macro/date.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{%- macro format(date) -%}
{# All components. #}
{%- if date.year and date.month and date.day -%}
{%- set formatted = calendar.month_name[date.month] + ' ' + date.day | string + ', ' + date.year | string -%}
{%- set timevals = [date.year, '{:0>2}'.format(date.month), '{:0>2}'.format(date.day)] -%}
{# No year, but month and day. #}
{%- elif not date.year and date.month and date.day -%}
{%- set formatted = calendar.month_name[date.month] + ' ' + date.day | string -%}
{%- set timevals = ['{:0>2}'.format(date.month), '{:0>2}'.format(date.day)] -%}
{# No month, but year. #}
{%- elif date.year and not date.month -%}
{%- set formatted = date.year | string -%}
{%- set timevals = [date.year] -%}
{# No day, but year and month. #}
{%- elif date.year and date.month and not date.day -%}
{%- set formatted = calendar.month_name[date.month] + ', ' + date.year | string -%}
{%- set timevals = [date.year, '{:0>2}'.format(date.month)] -%}
{%- else -%}
{%- set formatted = 'unknown date' -%}
{%- endif -%}
{%- if timevals is defined -%}
<time datetime="{{ timevals | join('-') }}">{{ formatted }}</time>
{%- else -%}
{{ formatted }}
{%- endif -%}
{%- endmacro -%}

0 comments on commit 9fe2084

Please sign in to comment.