Skip to content

Commit

Permalink
Automatically add updated if published has a time
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Jan 12, 2024
1 parent 053e9a0 commit 3e5d1d5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
3 changes: 2 additions & 1 deletion htmd/example_site/posts/example.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
title: Example Post
author: Taylor
published: 2014-10-30
updated: 2014-10-30
tags: [first]

...
This is the post **text**.
28 changes: 20 additions & 8 deletions htmd/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def index():
return render_template('index.html', active='home', posts=latest[:4])


def set_post_time(post, property, current_time):
def set_post_time(post, property, date_time):
file_path = os.path.join(
app.config['FLATPAGES_ROOT'],
post.path + app.config['FLATPAGES_EXTENSION']
Expand All @@ -186,7 +186,10 @@ def set_post_time(post, property, current_time):
with open(file_path, 'w') as file:
for line in lines:
if not found and property in line:
line = f'{property}: {current_time.isoformat()}\n'
line = f'{property}: {date_time.isoformat()}\n'
found = True
elif not found and '...' in line:
file.write(f'{property}: {date_time.isoformat()}\n')
found = True
file.write(line)

Expand All @@ -212,19 +215,28 @@ def feed():
path=post.path
)

updated = post.meta.get('updated') or post.meta.get('published')
if isinstance(updated, datetime.date):
updated = datetime.datetime.combine(updated, current_time)
if 'updated' in post.meta:
if 'updated' not in post.meta:
published = post.meta.get('published')
if isinstance(published, datetime.datetime):
property = 'updated'
else:
property = 'published'
set_post_time(post, property, updated)
else:
property = 'updated'

datetime_field = post.meta.get(property)
if isinstance(datetime_field, datetime.date):
datetime_field = datetime.datetime.combine(
datetime_field, current_time
)
else:
datetime_field = datetime.datetime.now()
set_post_time(post, property, datetime_field)
atom.add(
post.meta.get('title'), post.html, content_type='html',
author=post.meta.get('author', app.config.get('DEFAULT_AUTHOR')),
url=url,
updated=updated,
updated=datetime_field,
)
ret = atom.get_response()
return ret
Expand Down
63 changes: 63 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,66 @@ def test_build_published_time_is_added():
for a_line in a_lines:
if 'updated' in a_line:
assert False, 'updated found in example post'


def test_build_updated_is_added():
# If published has a time
# and there is no updated then
# build will add updated with time
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(start)
# Remove updated from example post
with open(os.path.join('posts', 'example.md'), 'r') as post_file:
b_lines = post_file.readlines()
with open(os.path.join('posts', 'example.md'), 'w') as post_file:
for line in b_lines:
if 'updated' not in line:
post_file.write(line)
# First build adds published time
runner.invoke(build)
# Second build adds updated
runner.invoke(build)
with open(os.path.join('posts', 'example.md'), 'r') as post_file:
a_lines = post_file.readlines()
for a_line in a_lines:
if 'updated' in a_line:
a_updated = a_line

a_datetime_str = a_updated.replace('updated:', '').strip()
a_datetime = datetime.datetime.fromisoformat(a_datetime_str)

date_with_current_time = datetime.datetime.now().replace(year=a_datetime.year, month=a_datetime.month, day=a_datetime.day)
time_difference = abs(a_datetime - date_with_current_time)

# Verify published time is close to now
threshold_seconds = 60
assert time_difference.total_seconds() < threshold_seconds


def test_build_updated_is_added_once():
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(start)
# Remove updated from example post
with open(os.path.join('posts', 'example.md'), 'r') as post_file:
b_lines = post_file.readlines()
with open(os.path.join('posts', 'example.md'), 'w') as post_file:
for line in b_lines:
if 'updated' not in line:
post_file.write(line)
# add "..." to post content
post_file.write('...\n')

# First build adds published time
runner.invoke(build)
# Second build adds updated
runner.invoke(build)
with open(os.path.join('posts', 'example.md'), 'r') as post_file:
a_lines = post_file.readlines()
count = 0
for a_line in a_lines:
if 'updated' in a_line:
count += 1

assert count == 1

0 comments on commit 3e5d1d5

Please sign in to comment.