Skip to content

Commit

Permalink
Move Jinja2 function url_for_slug and url_for_slug_path in jssg/templ…
Browse files Browse the repository at this point in the history
…atetags/
  • Loading branch information
Clément committed Aug 6, 2024
1 parent db164ab commit a931e7c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 80 deletions.
81 changes: 1 addition & 80 deletions jssg/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,12 @@
from django.core.management.base import BaseCommand

from jssg.templatetags.filter_opengraph_metadata import filter_opengraph_metadata

from jssg.models import Page
from jssg.templatetags.functions_url import url_for_slug, url_for_slug_path

from django.conf import settings

import importlib

import re

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)

def environment(**options):
env = Environment(**options)
env.globals.update(
Expand Down
78 changes: 78 additions & 0 deletions jssg/templatetags/functions_url.py
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)

0 comments on commit a931e7c

Please sign in to comment.