Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree structure #34

Merged
merged 11 commits into from
Jul 12, 2024
Merged

Tree structure #34

merged 11 commits into from
Jul 12, 2024

Conversation

ClmntBcqt
Copy link
Contributor

PR for issue #12

@ClmntBcqt ClmntBcqt requested a review from lebouquetin July 11, 2024 08:12
jssg/jinja2.py Outdated

def environment(**options):
env = Environment(**options)
env.globals.update(
{
"static": static,
"url": reverse,
"url_slug": url_from_slug,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think naming url_for_slug() this will make templates easier to understand: the usage will be more clear:

<p>Go to <a href="{{ url_for_slug('page1') }}">page 1</a> </p>

jssg/jinja2.py Outdated

def environment(**options):
env = Environment(**options)
env.globals.update(
{
"static": static,
"url": reverse,
"url_slug": url_from_slug,
"url_abs" : url_absolute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the same time, I suggest to rename url_abs to url_for_path() or url_for_slug_path()

jssg/jinja2.py Outdated

from jssg.models import Page

def url_from_slug(slug) :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add typing + some explanation:

def url_from_slug(slug: str) -> str:
    """
    example of slug: xxx
    example of result: xxx
    raises an exception if the slug is not unique (eg same slug found in several folders)
    """

jssg/jinja2.py Outdated
pages_with_slug += str(page.path.relative_to(page.page_dir.parent)) + ", "

if url == "" and pages_with_slug != "" :
raise Exception("url_slug() : slug '%s' is not unique, found in : [%s] ; use url_abs()" % (slug, pages_with_slug[:-2]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should return the list of paths where the slug is found. Eg: /fr/produits/tracim.html, /en/products/tracim.html so that the user knows where the conflict comes from. (I'm not sure, but I think you return only one path?)

By the way, the coed pages_with_slug[:-2]) is not clear at all. What is the :-2 (I mean, even if I know python, I don't know what data is to be found in this selector)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't directly return a list since it is the return value that is printed in the jinja2 template.
But if the slug is not unique, an exception is raised, and all the pages that have this slugs are printed in the error message to inform the user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the pages_with_slug[:-2]) @ClmntBcqt?

jssg/jinja2.py Outdated

def url_absolute(url_path) :
dir = '/'.join(url_path.split('/')[:-1])
slug = ''.join((''.join(url_path.split('/')[-1])).split('.')[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code is quite complex. It looks like the slug is the filename part without extension? If so, look at https://stackoverflow.com/questions/678236/how-do-i-get-the-filename-without-the-extension-from-a-path-in-python for an easy-to-read solution.
What about https://note.nkmk.me/en/python-os-basename-dirname-split-splitext/#get-the-directory-folder-name-from-a-path-ospathdirname for the dir part? Is this what you implement?

This kind of code should at least come with an example. Something like Example: if url_path is "/tmp/folder/subfolder/thefile.html", then slug will be "thefile" and the dir will be "/tmp/folder/subfolder"

Examples are extremly usefull in this context because there are always questions about "absolute or relative ?" "traling slash or no trailing slash?" etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used re.findall() so we can check the format of the url given by user too

jssg/jinja2.py Outdated
for page in Page.get_pages() :
if page["slug"] == slug and dir == "" :
return "/" + slug + ".html"
elif page["slug"] == slug and "dir" in page.keys() and page["dir"] == dir :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the condition about "dir" being in the keys? Is that a metadata? something else? Does this mean that a page (Document?) object as an attribute named dir? If so, I suggest to give a more explicity name. Is it a parent folder? a folder path? A parent folder path?

The more explicit the name is, the more comprehensible it is for future maintainers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed dir to rel_folder_path.
I changed this part to iterate into Page.load_glob() instead, like this we have access to attributes slug and rel_folder_path (the page folder path relative to its content pages directory)

@ClmntBcqt ClmntBcqt requested a review from lebouquetin July 12, 2024 12:41
from /folder/test/file.html to /folder/test/file
@ClmntBcqt ClmntBcqt merged commit 1b67b84 into main Jul 12, 2024
2 checks passed
@ClmntBcqt ClmntBcqt deleted the feat__12_tree_structure branch July 12, 2024 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants