Skip to content

Commit

Permalink
Widgets callable with Jinja macros
Browse files Browse the repository at this point in the history
Versions for blocks
Multiple content repertories
  • Loading branch information
Clément committed Jun 25, 2024
1 parent 0324300 commit 8dd1125
Show file tree
Hide file tree
Showing 21 changed files with 26 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<a href="{{ url('index') }}">Home</a>
</li>
<li class="list-item me-4">
<a href="{{ url_slug('page', 'temoignages-clients') }}">Customer testimonials (FR)</a>
<a href="{{ url('page', args=['temoignages-clients']) }}">Customer testimonials (FR)</a>
</li>
<li class="list-item me-4">
<a href="{{ static('assets/docs/galae_price_list.pdf') }}" target="_blank">Download the price
Expand All @@ -41,7 +41,7 @@
<a href="https://mail.galae.net/" target="_blank">Account management</a>
</li>
<li class="list-item me-4">
<a href="{{ url_slug('page', 'generateur-enregistrements-dns') }}">DNS record generator (FR)</a>
<a href="{{ url('page', args=['generateur-enregistrements-dns']) }}">DNS record generator (FR)</a>
</li>
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<a href="{{ url('index') }}">Accueil</a>
</li>
<li class="list-item me-4">
<a href="{{ url_slug('page', 'temoignages-clients') }}">Témoignages clients</a>
<a href="{{ url('page', args=['temoignages-clients']) }}">Témoignages clients</a>
</li>
<li class="list-item me-4">
<a href="{{ static('assets/docs/grille_tarifaire_galae.pdf') }}" target="_blank">Télécharger la
Expand All @@ -42,7 +42,7 @@
<a href="https://mail.galae.net/" target="_blank">Gestion de mon compte</a>
</li>
<li class="list-item me-4">
<a href="{{ url_slug('page', 'generateur-enregistrements-dns') }}">Générateur d'enregistrements DNS</a>
<a href="{{ url('page', args=['generateur-enregistrements-dns']) }}">Générateur d'enregistrements DNS</a>
</li>
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ engine jinja2

<h2>Page 1</h2>

<p>Go to <a href="{{ url_slug('page', 'page2') }}">page 2</a> </p>
<p>Go to <a href="{{ url('page', args=['page2']) }}">page 2</a> </p>

File renamed without changes.
3 changes: 0 additions & 3 deletions content/templates/jinja2/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
{{ block(
NAME = "navbar",
VERSION = object.metadata.lang|default(""),
DEFAULT_NAME = "navbar.fr.html"
) }}

<header>
{{ block(
NAME = "header",
VERSION = object.metadata.lang|default(""),
DEFAULT_NAME = "header.fr.html"
) }}
</header>

Expand All @@ -48,7 +46,6 @@
{{ block(
NAME = "footer",
VERSION = object.metadata.lang|default(""),
DEFAULT_NAME = "footer.fr.html"
) }}

<!-- Bootstrap core JS-->
Expand Down
4 changes: 2 additions & 2 deletions content/templates/jinja2/blocks/block.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

{% if VERSION != "" %}
{% include [
"blocks/" + NAME + "." + VERSION + ".html",
"blocks/" + NAME.split('.')[0] + '.' + VERSION + '.' + NAME.split('.')[-1],
"blocks/" + VERSION + '/' + NAME + ".html",
"blocks/" + VERSION + '/' + NAME ,
"blocks/" + DEFAULT_NAME + ".html",
"blocks/" + DEFAULT_NAME
] %}
Expand Down
3 changes: 0 additions & 3 deletions content/templates/jinja2/image.html

This file was deleted.

2 changes: 1 addition & 1 deletion content/templates/jinja2/widgets/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="navbar__container__right">
<a class="btn btn-sm btn-primary" href="{{ CTA_URL }}"
target="{{ CTA_TARGET }}">{{ CTA_LABEL }}</a>
<a href="{{ url_slug('page', CHANGE_LANG_URL) }}">
<a href="{{ url('page', args=[CHANGE_LANG_URL]) }}">
<img class="languageFlag" src="{{ static(CHANGE_LANG_FLAG_URL)}}" alt="{{ CHANGE_LANG_ALT }}"/>
</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ engine jinja2

<h2> Page 2 </h2>

<p>Go to <a href="{{ url_slug('page', 'page1') }}">page 1</a> </p>
<p>Go to <a href="{{ url('page', args=['page1']) }}">page 1</a> </p>
File renamed without changes.
File renamed without changes.
9 changes: 0 additions & 9 deletions jssg/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,12 @@
from jssg.models import Document
from django.conf import settings

def url_from_slug(view_name, slug) :
for path in settings.JSSG_PAGES_DIR :
files = path.rglob("*.md")
for f in files :
doc = Document.load(f)
if doc.metadata["slug"] == slug :
return "/" / doc.path.relative_to(path).with_suffix('.html').parent / (doc.metadata["slug"] + ".html")

def environment(**options):
env = Environment(**options)
env.globals.update(
{
"static": static,
"url": reverse,
"url_slug": url_from_slug,
}
)
env.filters.update(
Expand Down
33 changes: 8 additions & 25 deletions jssg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def load(cls, path: Path) -> "Document":

@classmethod
def load_glob(
cls, path: Optional[List[Path]] = None, dir = "", glob: str = "*.md", all=False
cls, path: Optional[List[Path]] = None, glob: str = "*.md"
) -> Iterator["Document"]:
"""Load multiple document.
Expand All @@ -213,10 +213,7 @@ def load_glob(

files = []
for p in path :
if all :
files += (p / dir).rglob(glob)
else :
files += (p / dir).glob(glob)
files += p.glob(glob)
print(files)
return map(cls.load, files)

Expand All @@ -239,23 +236,16 @@ def __init__(self, content: str, **metadata) -> None:
except KeyError:
self.slug = slugify(self.title)

p = self.path
while (p not in self.BASE_DIR) :
p = p.parent
self.dir = str(self.path.relative_to(p).parent)
if self.dir == '.' :
self.dir = ''

@classmethod
def load_page_with_slug(cls, slug: str, dir : str) -> "Page":
return next(filter(lambda p: p.slug == slug, cls.load_glob(dir = dir)))
def load_page_with_slug(cls, slug: str) -> "Page":
return next(filter(lambda p: p.slug == slug, cls.load_glob()))

@classmethod
def load_glob(
cls, path: Optional[List[Path]] = None, dir = "", glob: str = "*.md", all = False
cls, path: Optional[List[Path]] = None, glob: str = "*.md"
) -> Iterator["Page"]:
"""Overridden only to make the static typing happy."""
return super().load_glob(path, dir, glob, all)
return super().load_glob(path, glob)


class Post(Page):
Expand All @@ -272,16 +262,9 @@ def __init__(self, content: str, **metadata) -> None:
super().__init__(content, **metadata)
self.timestamp = datetime.datetime.fromisoformat(metadata["date"])

p = self.path
while (p not in self.BASE_DIR) :
p = p.parent
self.dir = str(self.path.relative_to(p).parent)
if self.dir == '.' :
self.dir = ''

@classmethod
def load_glob(
cls, path: Optional[List[Path]] = None, dir = "", glob: str = "*.md", all = False
cls, path: Optional[List[Path]] = None, glob: str = "*.md"
) -> Iterator["Post"]:
"""Overridden only to make the static typing happy."""
return super().load_glob(path, dir, glob, all)
return super().load_glob(path, glob)
2 changes: 1 addition & 1 deletion jssg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@


# JSSG
JSSG_CONTENT_DIR = [BASE_DIR / "content"] + [Path.home() / "algoo" / "jssg" / "galae-content"] + [Path.home() / "algoo" / "jssg" / "common-content"]
JSSG_CONTENT_DIR = [BASE_DIR / "content"] + [BASE_DIR / "galae-content"] + [BASE_DIR / "common-content"]
JSSG_PAGES_DIR = [path / "pages" for path in JSSG_CONTENT_DIR]
JSSG_POSTS_DIR = [path / "posts" for path in JSSG_CONTENT_DIR]
JSSG_TEMPLATES_DIR = [path / "templates" for path in JSSG_CONTENT_DIR]
Expand Down
14 changes: 7 additions & 7 deletions jssg/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def get_pages():
return ({"dir":p.dir, "slug": p.slug} for p in Page.load_glob(all = True))
return ({"slug": p.slug} for p in Page.load_glob())

def get_posts():
return ({"slug": p.slug} for p in Post.load_glob())
Expand All @@ -31,17 +31,17 @@ def get_posts():
distill_path(
"", views.IndexView.as_view(), name="index", distill_file="index.html"
),
distill_path(
"pages/<slug:slug>.html",
views.PageView.as_view(),
name="page",
distill_func=get_pages,
),
distill_path("atom.xml", views.PostFeedsView(), name="atom_feed"),
distill_path(
"posts/<slug:slug>.html",
views.PostView.as_view(),
name="post",
distill_func=get_posts,
),
distill_re_path(
r'^(?P<dir>[a-zA-z0-9-/]*)/(?P<slug>[a-zA-z0-9-/]+).html',
views.PageView.as_view(),
name="page",
distill_func=get_pages,
),
]
6 changes: 1 addition & 5 deletions jssg/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ class PageView(DetailView):
template_name = "page.html"

def get_object(self, queryset=None) -> Model:
if "dir" not in self.kwargs.keys() :
self.kwargs["dir"] = ""
print(self.kwargs["dir"])
print(self.kwargs["slug"])
model = self.model.load_page_with_slug(self.kwargs["slug"], self.kwargs["dir"])
model = self.model.load_page_with_slug(self.kwargs["slug"])
return model


Expand All @@ -64,7 +61,6 @@ class IndexView(PageView):
template_name = "page.html"

def get_object(self, queryset=None) -> Model:
self.kwargs["dir"] = "en"
self.kwargs["slug"] = "en-index"
return super().get_object(queryset)

Expand Down

0 comments on commit 8dd1125

Please sign in to comment.