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

Add default metadata #40

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ Otherwise, you have to configure the following settings :
- `JFME_DOMAIN` : the domain name of your website, for instance `"https://www.example.com"` (used in sitemap file)
- `JFME_CONTENT_DIRS` : a list of directories where to look for the site content

Other useful settings :
- Default metadata : `JFME_DEFAULT_METADATA_DICT` and `JFME_DEFAULT_METADATA_PATH` allow to set default metadata for pages and posts. The first one is a python dictionary and the second one is a Path to a file having the same format as metadata section in pages.
The order, from less to most priority is : `JFME_DEFAULT_METADATA_DICT` then `JFME_DEFAULT_METADATA_PATH` then page matadata.

### `Dockerfile` :
- In the `# Copy source dir` section, add `COPY <content-dir>/ <content-dir>/` for each content directory in `JFME_CONTENT_DIRS`

Expand Down
2 changes: 1 addition & 1 deletion content/templates/jinja2/allwidgets.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('page', args=[CHANGE_LANG_URL]) }}">
<a href="{{ url_for_slug_path(CHANGE_LANG_URL) }}">
<img class="languageFlag" src="{{ static(CHANGE_LANG_FLAG_URL)}}" alt="{{ CHANGE_LANG_ALT }}"/>
</a>
</div>
Expand Down
Empty file added jssg/default_metadata.txt
Empty file.
10 changes: 7 additions & 3 deletions jssg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Iterator, Mapping, Optional, List

import markdown2
import re
from django.conf import settings
from django.template import Context, Template, engines
from django.utils.text import slugify
Expand Down Expand Up @@ -58,7 +59,6 @@ def content(self) -> str:

:return: the rendered document
"""
import re
# INFO DA 2024-02-18 - Replace "{{{ }}}" pattern into one-line pattern
# this is usefull in order to exploit multi-line includes
# {{{ include "block.html" with
Expand Down Expand Up @@ -118,11 +118,16 @@ def load(cls, path: Path) -> "Document":
:return: The loaded document
"""
_path = path
metadata = {}
metadata = settings.JFME_DEFAULT_METADATA_DICT.copy()
data = {}
json_data = ""
content = StringIO()

with settings.JFME_DEFAULT_METADATA_PATH.open() as f:
for line in f :
key, value = map(str.strip, re.split("[\s]", line, maxsplit=1))
Copy link
Member

Choose a reason for hiding this comment

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

It would be interesting to reuse the comment possibility that is implemented in content descriptor (lines starting with a #)

Look at model source code:

                        if line.startswith("#"):
                            continue  # remove comment lines

I suggest to refactor/factorize parsing source code in a function.

It means in global parsing algorithm, replace:

                        if line.strip() == "":  # ignore empty lines
                            continue
                        if line.startswith("#"):  # ignore comment lines
                            continue

                        # Parse a metadata key value pair
                        # key, value = map(str.strip, line.split("", maxsplit=1))
                        import re
                        key, value = map(str.strip, re.split("[\s]", line, maxsplit=1))
                        # FIXME  print("KEY {} : {} (line is: {})".format(key, value, line))
                        metadata[key] = value

with something like:

                        try:
                            key, value = parse_metadata_line(line)
                            metadata[key] = value
                        except EmptyLine:
                            [...]
                        except CommentLine as comment:
                            [...]

And reuse the algorithm/function for default metadata file parsing

metadata[key] = value

with path.open() as f:
# States:
# 0: search the metadata start block
Expand Down Expand Up @@ -152,7 +157,6 @@ def load(cls, path: Path) -> "Document":

# Parse a metadata key value pair
# key, value = map(str.strip, line.split("", maxsplit=1))
import re
key, value = map(str.strip, re.split("[\s]", line, maxsplit=1))
# FIXME print("KEY {} : {} (line is: {})".format(key, value, line))
metadata[key] = value
Expand Down
3 changes: 2 additions & 1 deletion jssg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
JFME_POSTS_DIRS = [path / "posts" for path in JFME_CONTENT_DIRS]
JFME_TEMPLATES_DIRS = [path / "templates" for path in JFME_CONTENT_DIRS]
JFME_STATIC_DIRS = [path / "static" for path in JFME_CONTENT_DIRS]

JFME_DEFAULT_METADATA_DICT = {"slug": "index", } # The order of include is : JFME_DEFAULT_METADATA_DICT then JFME_DEFAULT_METADATA_PATH then page metadata
JFME_DEFAULT_METADATA_PATH = BASE_DIR / "jssg" / "default_metadata.txt" # If a metadata is specified more than once, the last included is retained
Copy link
Member

Choose a reason for hiding this comment

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

JFME_DEFAULT_METADATA_PATH -> JFME_DEFAULT_METADATA_FILEPATH



# Application definition
Expand Down
Loading