From eea17aa7ff1d70733cd761192b777814026129f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Thu, 18 Jul 2024 11:15:54 +0200 Subject: [PATCH 1/4] Add JFME_DEFAULT_METADATA_DICT and JFME_DEFAULT_METADATA_PATH settings --- content/templates/jinja2/allwidgets.html | 2 +- jssg/default_metadata.txt | 0 jssg/models.py | 10 +++++++--- jssg/settings.py | 3 ++- 4 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 jssg/default_metadata.txt diff --git a/content/templates/jinja2/allwidgets.html b/content/templates/jinja2/allwidgets.html index dcc18fc..166ffd4 100644 --- a/content/templates/jinja2/allwidgets.html +++ b/content/templates/jinja2/allwidgets.html @@ -19,7 +19,7 @@ diff --git a/jssg/default_metadata.txt b/jssg/default_metadata.txt new file mode 100644 index 0000000..e69de29 diff --git a/jssg/models.py b/jssg/models.py index e8e5b15..d958da9 100644 --- a/jssg/models.py +++ b/jssg/models.py @@ -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 @@ -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 @@ -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)) + metadata[key] = value + with path.open() as f: # States: # 0: search the metadata start block @@ -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 diff --git a/jssg/settings.py b/jssg/settings.py index a0ce03b..89fa47f 100644 --- a/jssg/settings.py +++ b/jssg/settings.py @@ -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", } +JFME_DEFAULT_METADATA_PATH = BASE_DIR / "jssg" / "default_metadata.txt" # Application definition From 76b00ebf5aa8f84b34e38480bfac07e25d55999b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Tue, 23 Jul 2024 16:15:19 +0200 Subject: [PATCH 2/4] Add doc for settings --- README.md | 4 ++++ jssg/settings.py | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7f76fc1..248fc48 100644 --- a/README.md +++ b/README.md @@ -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 / /` for each content directory in `JFME_CONTENT_DIRS` diff --git a/jssg/settings.py b/jssg/settings.py index 89fa47f..1d494de 100644 --- a/jssg/settings.py +++ b/jssg/settings.py @@ -52,8 +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", } -JFME_DEFAULT_METADATA_PATH = BASE_DIR / "jssg" / "default_metadata.txt" +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 # Application definition From 8e01bea95d3d94cf85055958b710c8b1a04c13c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 24 Jul 2024 10:21:57 +0200 Subject: [PATCH 3/4] refactor metadata line parsing --- jssg/models.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/jssg/models.py b/jssg/models.py index d958da9..33f58d5 100644 --- a/jssg/models.py +++ b/jssg/models.py @@ -28,6 +28,11 @@ from django.core.management.commands.runserver import Command as runserver +class EmptyLine(Exception) : + pass +class CommentLine(Exception) : + pass + class Document: """A document. @@ -125,8 +130,14 @@ def load(cls, path: Path) -> "Document": with settings.JFME_DEFAULT_METADATA_PATH.open() as f: for line in f : - key, value = map(str.strip, re.split("[\s]", line, maxsplit=1)) - metadata[key] = value + try : + # Parse a metadata key value pair + key, value = cls.parse_metadata_line(line) + metadata[key] = value + except EmptyLine : # ignore empty lines + continue + except CommentLine : # ignore comment lines + continue with path.open() as f: # States: @@ -150,16 +161,14 @@ def load(cls, path: Path) -> "Document": # Metadata end block found state = 2 else: - if line.strip() == "": # ignore empty lines + try : + # Parse a metadata key value pair + key, value = cls.parse_metadata_line(line) + metadata[key] = value + except EmptyLine : # ignore empty lines continue - if line.startswith("#"): # ignore comment lines + except CommentLine : # ignore comment lines continue - - # Parse a metadata key value pair - # key, value = map(str.strip, line.split("", maxsplit=1)) - key, value = map(str.strip, re.split("[\s]", line, maxsplit=1)) - # FIXME print("KEY {} : {} (line is: {})".format(key, value, line)) - metadata[key] = value elif state == 2: if line.rstrip().startswith("---"): # data end block found @@ -222,7 +231,15 @@ def load_glob( files += (p / dir).glob(glob) # print(files) return map(cls.load, files) - + + @classmethod + def parse_metadata_line(cls, line) : + if line.strip() == "": # ignore empty lines + raise EmptyLine() + if line.startswith("#"): # ignore comment lines + raise CommentLine(line) + # key, value = map(str.strip, line.split("", maxsplit=1)) + return map(str.strip, re.split("[\s]", line, maxsplit=1)) class Page(Document): """A webpage, with a title and some content.""" From ad9638961bdb8575b757f85054377f9f9b245a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 24 Jul 2024 10:28:26 +0200 Subject: [PATCH 4/4] Rename JFME_DEFAULT_METADATA_PATH to JFME_DEFAULT_METADATA_FILEPATH --- README.md | 4 ++-- jssg/models.py | 2 +- jssg/settings.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 248fc48..3343f8f 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ Otherwise, you have to configure the following settings : - `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. +- Default metadata : `JFME_DEFAULT_METADATA_DICT` and `JFME_DEFAULT_METADATA_FILEPATH` 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_FILEPATH` then page matadata. ### `Dockerfile` : - In the `# Copy source dir` section, add `COPY / /` for each content directory in `JFME_CONTENT_DIRS` diff --git a/jssg/models.py b/jssg/models.py index 33f58d5..37e59cc 100644 --- a/jssg/models.py +++ b/jssg/models.py @@ -128,7 +128,7 @@ def load(cls, path: Path) -> "Document": json_data = "" content = StringIO() - with settings.JFME_DEFAULT_METADATA_PATH.open() as f: + with settings.JFME_DEFAULT_METADATA_FILEPATH.open() as f: for line in f : try : # Parse a metadata key value pair diff --git a/jssg/settings.py b/jssg/settings.py index 1d494de..e579bfc 100644 --- a/jssg/settings.py +++ b/jssg/settings.py @@ -52,8 +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 +JFME_DEFAULT_METADATA_DICT = {"slug": "index", } # The order of include is : JFME_DEFAULT_METADATA_DICT then JFME_DEFAULT_METADATA_FILEPATH then page metadata +JFME_DEFAULT_METADATA_FILEPATH = BASE_DIR / "jssg" / "default_metadata.txt" # If a metadata is specified more than once, the last included is retained # Application definition