allow markdown files to specify a redirect

closes #13
This commit is contained in:
Brian S. Stephan 2021-06-06 22:24:35 -05:00
parent 27bb139a2b
commit 954f7f4e80
3 changed files with 44 additions and 24 deletions

View File

@ -39,35 +39,47 @@ def display_page(path):
logger.debug("redirect path: '%s'", redirect_path)
return redirect(redirect_path, code=301)
elif render_type == 'markdown':
try:
with app.open_instance_resource(resolved_path, 'r') as entry_file:
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(entry_file.name), get_localzone())
entry = entry_file.read()
except OSError:
logger.exception("resolved path '%s' could not be opened!", resolved_path)
abort(500)
else:
md = init_md()
content = Markup(md.convert(entry))
logger.debug("file metadata: %s", md.Meta)
parent_navs = generate_parent_navs(resolved_path)
page_name = (get_meta_str(md, 'title') if md.Meta.get('title') else
f'/{instance_resource_path_to_request_path(resolved_path)}')
page_title = f'{page_name} - {app.config["TITLE_SUFFIX"]}' if page_name else app.config['TITLE_SUFFIX']
logger.debug("title (potentially derived): %s", page_title)
template = get_meta_str(md, 'template') if md.Meta.get('template') else 'base.html'
return render(template, title=page_title, description=get_meta_str(md, 'description'),
image=get_meta_str(md, 'image'), base_url=request.base_url, content=content,
navs=parent_navs, mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'))
logger.debug("treating path '%s' as markdown '%s'", path, resolved_path)
return handle_markdown_file_path(resolved_path)
else:
logger.exception("unsupported render_type '%s'!?", render_type)
abort(500)
def handle_markdown_file_path(resolved_path):
"""Given a location on disk, attempt to open it and render the markdown within."""
try:
with app.open_instance_resource(resolved_path, 'r') as entry_file:
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(entry_file.name), get_localzone())
entry = entry_file.read()
except OSError:
logger.exception("resolved path '%s' could not be opened!", resolved_path)
abort(500)
else:
md = init_md()
content = Markup(md.convert(entry))
logger.debug("file metadata: %s", md.Meta)
parent_navs = generate_parent_navs(resolved_path)
page_name = (get_meta_str(md, 'title') if md.Meta.get('title') else
f'/{instance_resource_path_to_request_path(resolved_path)}')
page_title = f'{page_name} - {app.config["TITLE_SUFFIX"]}' if page_name else app.config['TITLE_SUFFIX']
logger.debug("title (potentially derived): %s", page_title)
template = get_meta_str(md, 'template') if md.Meta.get('template') else 'base.html'
# check if this has a HTTP redirect
redirect_url = get_meta_str(md, 'redirect') if md.Meta.get('redirect') else None
if redirect_url:
logger.debug("redirecting via meta tag to '%s'", redirect_url)
return redirect(redirect_url, code=301)
return render(template, title=page_title, description=get_meta_str(md, 'description'),
image=get_meta_str(md, 'image'), base_url=request.base_url, content=content,
navs=parent_navs, mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'))
def request_path_to_instance_resource_path(path):
"""Turn a request URL path to the full page path.

View File

@ -112,6 +112,13 @@ def test_page_with_forced_empty_title_just_shows_suffix(client):
assert b'<title>incorporeal.org</title>' in response.data
def test_page_with_redirect_meta_url_redirects(client):
"""Test that if a page specifies a URL to redirect to, that the site serves up a 301."""
response = client.get('/redirect')
assert response.status_code == 301
assert response.location == 'http://www.google.com/'
def test_page_has_modified_timestamp(client):
"""Test that pages have modified timestamps in them."""
response = client.get('/')

View File

@ -0,0 +1 @@
Redirect: http://www.google.com/