-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Jinja2 function url_for_slug and url_for_slug_path in jssg/templ…
…atetags/
- Loading branch information
Clément
committed
Aug 6, 2024
1 parent
db164ab
commit a931e7c
Showing
2 changed files
with
79 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import re | ||
from jssg.models import Page | ||
|
||
def url_for_slug(slug) : | ||
""" | ||
@param slug: the slug of the page to search | ||
@return: the string of the url corresponding to the page | ||
@error: raise an exception if the slug does not exist or it is not unique (eg same slug found in several folders) | ||
>>> url_for_slug('index') # the slug exists and is unique | ||
/en/index.html | ||
>>> url_for_slug('index-duplicated') # the slug exists in several pages | ||
Traceback (most recent call last): | ||
... | ||
Exception: slug 'index-duplicated' is not unique, found in : [pages/fr-index.md, pages/en-index.md] ; use url_for_slug_path() | ||
>>> url_for_slug('index-removed') # the slug does not exists | ||
Traceback (most recent call last): | ||
... | ||
Exception: slug 'index-removed' not found | ||
""" | ||
|
||
url = "" | ||
pages_with_slug = [] | ||
|
||
for page in Page.load_glob(all=True) : | ||
if page.slug == slug : # the page exists | ||
if pages_with_slug == [] : # the slug has not been found yet | ||
if page.rel_folder_path != '' : | ||
url = "/" + page.rel_folder_path + "/" + page.slug + ".html" | ||
else : | ||
url = "/" + page.slug + ".html" | ||
else : # the slug already exists | ||
url = "" | ||
pages_with_slug.append(str(page.path.relative_to(page.content_page_dir.parent))) | ||
|
||
if url == "" and pages_with_slug != [] : | ||
raise Exception("slug '%s' is not unique, found in : [%s] ; use url_for_slug_path()" % (slug, ", ".join(pages_with_slug))) | ||
elif url == "" : | ||
raise Exception("slug '%s' not found" % slug) | ||
return url | ||
|
||
def url_for_slug_path(url_path) : | ||
""" | ||
@param url_path: the url of the page to search (absolute path) | ||
@return: the string of the slug url corresponding to the page | ||
@error: raise an exception if the url is a dead link | ||
>>> url_for_slug_path('/en/index') # the page exists | ||
/en/index.html | ||
>>> url_for_slug_path('/en/index-removed') # the page does not exist | ||
Traceback (most recent call last): | ||
... | ||
Exception: page for '/en/index-removed' url not found (dead link) | ||
>>> url_for_slug_path('folder/index') | ||
Traceback (most recent call last): | ||
... | ||
Exception: url 'folder/index' is not valid ; correct urls are /<dir>/<slug> or /<slug> | ||
""" | ||
# Valid url are /<dir>/<slug>.html or /<slug>.html | ||
# Example: if url_path is "/tmp/folder/subfolder/thefile.html", then slug will be "thefile" and the dir will be "tmp/folder/subfolder" | ||
# Note : the dir does not start with '/' since the url parsed in url.py do not either | ||
try : | ||
_, dir, slug = re.findall(r"(^|^/([a-zA-Z0-9/-]+))/([a-zA-Z0-9-]+)$", url_path)[0] | ||
except : | ||
raise Exception("url '%s' is not valid ; correct urls are /<dir>/<slug> or /<slug>" % url_path) | ||
|
||
# Verify that the page exists | ||
for page in Page.load_glob(all=True) : | ||
if page.slug == slug and page.rel_folder_path == dir : | ||
return url_path + ".html" | ||
|
||
raise Exception("page for '%s' url not found (dead link)" % url_path) |