rewrite generate_parent_navs
* works on a path now, not a file location * as such is sliiiiiightly easier to understand * now also puts the current page in the nav * fixed failing test where this caused an error (rather than 404) on non-existent paths
This commit is contained in:
parent
0993147dea
commit
059108c37b
@ -18,11 +18,11 @@ md = markdown.Markdown(extensions=['meta', 'tables'])
|
||||
def display_page(path):
|
||||
"""Get the file contents of the requested path and render the file."""
|
||||
resolved_path = resolve_page_file(path)
|
||||
parent_navs = generate_parent_navs(resolved_path)
|
||||
logger.debug("received request for path '%s', resolved to '%s'", path, resolved_path)
|
||||
try:
|
||||
with app.open_instance_resource(resolved_path, 'r') as entry_file:
|
||||
logger.debug("file '%s' found", resolved_path)
|
||||
parent_navs = generate_parent_navs(path)
|
||||
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(entry_file.name), get_localzone())
|
||||
entry = entry_file.read()
|
||||
except FileNotFoundError:
|
||||
@ -52,17 +52,19 @@ def resolve_page_file(path):
|
||||
|
||||
def generate_parent_navs(path):
|
||||
"""Create a series of paths/links to navigate up from the given path."""
|
||||
parent_dir = os.path.dirname(path)
|
||||
if parent_dir == 'pages':
|
||||
# derive additional path/location stuff based on path
|
||||
resolved_path = resolve_page_file(path)
|
||||
parent_dir = os.path.dirname(resolved_path)
|
||||
parent_path = '/'.join(path[:-1].split('/')[:-1]) + '/'
|
||||
|
||||
logger.debug("path: '%s'; parent path: '%s'; resolved path: '%s'; parent dir: '%s'",
|
||||
path, parent_path, resolved_path, parent_dir)
|
||||
|
||||
if path in ('index', '/'):
|
||||
return [(app.config['TITLE_SUFFIX'], '/')]
|
||||
elif path.endswith('index.md'):
|
||||
# if we're on an index.md, don't link to ourselves as we're our own parent
|
||||
return generate_parent_navs(parent_dir)
|
||||
else:
|
||||
parent_path = f'{parent_dir}/'.replace('pages/', '/', 1)
|
||||
resolved_parent_path = resolve_page_file(parent_path)
|
||||
with app.open_instance_resource(resolved_parent_path, 'r') as entry_file:
|
||||
with app.open_instance_resource(resolved_path, 'r') as entry_file:
|
||||
entry = entry_file.read()
|
||||
_ = Markup(md.convert(entry))
|
||||
parent_name = " ".join(md.Meta.get('title')) if md.Meta.get('title') else os.path.basename(parent_dir)
|
||||
return generate_parent_navs(parent_dir) + [(parent_name, parent_path)]
|
||||
page_name = " ".join(md.Meta.get('title')) if md.Meta.get('title') else os.path.basename(resolved_path)
|
||||
return generate_parent_navs(parent_path) + [(page_name, path)]
|
||||
|
@ -48,20 +48,27 @@ def test_page_has_modified_timestamp(client):
|
||||
|
||||
def test_generate_page_navs_index(app):
|
||||
with app.app_context():
|
||||
assert generate_parent_navs('pages/index.md') == [('incorporeal.org', '/')]
|
||||
assert generate_parent_navs('/') == [('incorporeal.org', '/')]
|
||||
|
||||
|
||||
def test_generate_page_navs_alternate_index(app):
|
||||
with app.app_context():
|
||||
assert generate_parent_navs('index') == [('incorporeal.org', '/')]
|
||||
|
||||
|
||||
def test_generate_page_navs_subdir_index(app):
|
||||
with app.app_context():
|
||||
assert generate_parent_navs('pages/subdir/index.md') == [('incorporeal.org', '/')]
|
||||
assert generate_parent_navs('/subdir/') == [('incorporeal.org', '/'), ('index.md', '/subdir/')]
|
||||
|
||||
|
||||
def test_generate_page_navs_subdir_real_page(app):
|
||||
with app.app_context():
|
||||
assert generate_parent_navs('pages/subdir/page.md') == [('incorporeal.org', '/'), ('subdir', '/subdir/')]
|
||||
assert generate_parent_navs('/subdir/page') == [('incorporeal.org', '/'), ('index.md', '/subdir/'),
|
||||
('Page', '/subdir/page')]
|
||||
|
||||
|
||||
def test_generate_page_navs_subdir_with_title_parsing_real_page(app):
|
||||
with app.app_context():
|
||||
assert generate_parent_navs('pages/subdir-with-title/page.md') == [('incorporeal.org', '/'),
|
||||
('SUB!', '/subdir-with-title/')]
|
||||
assert generate_parent_navs('/subdir-with-title/page') == [('incorporeal.org', '/'),
|
||||
('SUB!', '/subdir-with-title/'),
|
||||
('page.md', '/subdir-with-title/page')]
|
||||
|
Loading…
Reference in New Issue
Block a user