Skip to content

Commit

Permalink
change the slug url format in url_for_slug_path()
Browse files Browse the repository at this point in the history
from /folder/test/file.html to /folder/test/file
  • Loading branch information
Clément committed Jul 12, 2024
1 parent 52888ae commit 8b99f0b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion content/templates/jinja2/blocks/en/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
CTA_LABEL="See pricing",
CTA_TARGET="",
CTA_URL="#pricing",
CHANGE_LANG_URL="/fr-index.html",
CHANGE_LANG_URL="/fr-index",
CHANGE_LANG_FLAG_URL="assets/img/lang-fr.svg",
CHANGE_LANG_ALT="Version française"
) }}
2 changes: 1 addition & 1 deletion content/templates/jinja2/blocks/fr/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
CTA_LABEL="Découvrir les tarifs",
CTA_TARGET="",
CTA_URL="#pricing",
CHANGE_LANG_URL="/en-index.html",
CHANGE_LANG_URL="/en-index",
CHANGE_LANG_FLAG_URL="assets/img/lang-gb.svg",
CHANGE_LANG_ALT="English version"
) }}
17 changes: 9 additions & 8 deletions jssg/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,35 @@ def url_for_slug(slug) :
def url_for_slug_path(url_path) :
"""
@param url_path: the url of the page to search (absolute path)
@return: the string of the url corresponding to the page
@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.html') # the page exists
>>> url_for_slug_path('/en/index') # the page exists
/en/index.html
>>> url_for_slug_path('/en/index-removed.html') # the page does not exist
>>> url_for_slug_path('/en/index-removed') # the page does not exist
Traceback (most recent call last):
...
Exception: page for '/en/index-removed.html' url not found (dead link)
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>.html or /<slug.html>
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-]+)\.html", url_path)[0]
_, 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>.html or /<slug>.html" % url_path)
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
return url_path + ".html"

raise Exception("page for '%s' url not found (dead link)" % url_path)

Expand Down

0 comments on commit 8b99f0b

Please sign in to comment.