From 975387f6310d3531165e22df17954aa536f8815d Mon Sep 17 00:00:00 2001 From: Anton Shuvalov Date: Thu, 8 Sep 2022 14:27:19 +0700 Subject: [PATCH] Feat: custom hbs file extensions support --- CHANGELOG.md | 24 ++++++++++++++---------- pyproject.toml | 2 +- src/blog/blog.py | 1 - src/builder/builder.py | 4 ++-- src/dataclasses/content_data.py | 11 +++++++++-- src/entities/inline_image.py | 1 - src/entities/obsidian_embed.py | 1 - src/entities/reference_image.py | 2 +- src/lib/watcher.py | 1 - src/obsidian/page.py | 6 +++--- tests/dataclasses/content_data_test.py | 7 +++++++ 11 files changed, 37 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4550cc2..2ed918c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,24 +1,28 @@ -0.1.6 -=== +# Changelog + +## 0.2.2 + +- Add support for custom file extensions. Now `rss.xml.hbs` is rendered as `rss.xml`. + +## 0.1.6 + - fix fast fix lol +## 0.1.5 [broken] -0.1.5 [broken] -=== - Fix broken preflight check -0.1.4 [broken] -=== +## 0.1.4 [broken] + - Refactor preprocessors to add page processing support - Add preflight check validation - Implement --version to use pyproject.toml -0.1.3 -=== +## 0.1.3 + - add config to the handlebars context -0.1.2 -=== +## 0.1.2 - Add poetry for build and deploy - Add publish action on commit to master diff --git a/pyproject.toml b/pyproject.toml index 49c9485..fa7d525 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "obsidian-blog" -version = "0.2.1" +version = "0.2.2" description = "Feature rich static site generator for obsidian.md" authors = ["'Anton Shuvalov' "] license = "Commons Clause" diff --git a/src/blog/blog.py b/src/blog/blog.py index 6304b1b..10a5e1e 100644 --- a/src/blog/blog.py +++ b/src/blog/blog.py @@ -1,4 +1,3 @@ -from src.lib import fs from src.blog.layout import Layout from src.dataclasses.config_data import ConfigData diff --git a/src/builder/builder.py b/src/builder/builder.py index abcdfe2..8a2cfdd 100644 --- a/src/builder/builder.py +++ b/src/builder/builder.py @@ -110,7 +110,7 @@ def process_assets(self, page): content_data.filename = url print(f" - [COPY ASSET]: {frm} to {to}") - except: + except Exception: # FIXME: Should skip abs paths and urls pass @@ -129,7 +129,7 @@ def preprocess_content(self, page): processor.process_entity(entity) def create_context(self, local_ctx=None): - if local_ctx == None: + if local_ctx is None: local_ctx = {} global_ctx = { diff --git a/src/dataclasses/content_data.py b/src/dataclasses/content_data.py index edfc909..56657ac 100644 --- a/src/dataclasses/content_data.py +++ b/src/dataclasses/content_data.py @@ -53,8 +53,15 @@ def slug(self): if isinstance(meta_slug, str): return f"{meta_slug}.html" file, _ = os.path.splitext(self.filename) - slug = slugify(os.path.basename(file)) - return f"{slug}.html" + + # If file doesn't have any explicitly specified extension, like in `file.xml.hbs`, + # append `.html` + if '.' not in file: + slug = slugify(os.path.basename(file)) + return f"{slug}.html" + + # Else use the rest of the filename as the slug + return os.path.basename(file) @property def id(self): diff --git a/src/entities/inline_image.py b/src/entities/inline_image.py index d087248..99895eb 100644 --- a/src/entities/inline_image.py +++ b/src/entities/inline_image.py @@ -1,6 +1,5 @@ import re from src.dataclasses.content_data import ContentData -from src.dataclasses.asset_data import AssetData from src.entities.image import Image INLINE_IMG_RE = r"(\!\[(.*?)\]\((.*?)\))" diff --git a/src/entities/obsidian_embed.py b/src/entities/obsidian_embed.py index a05e4b3..1906600 100644 --- a/src/entities/obsidian_embed.py +++ b/src/entities/obsidian_embed.py @@ -1,6 +1,5 @@ import os from slugify import slugify -from src.dataclasses.asset_data import AssetData from src.dataclasses.content_data import ContentData from src.entities.parser import get_all_of_types, markdownFabric from marko.ast_renderer import ASTRenderer diff --git a/src/entities/reference_image.py b/src/entities/reference_image.py index 16bdea6..5ccb64c 100644 --- a/src/entities/reference_image.py +++ b/src/entities/reference_image.py @@ -18,7 +18,7 @@ def get_all(entity): matches = re.findall(REFERENCE_IMG_RE, content) for match in matches: - placeholder, alt, key = match + placeholder, _, key = match link_re = re.compile("\\[" + key + "\\]:\\s(.*)") filename = re.findall(link_re, content)[0] diff --git a/src/lib/watcher.py b/src/lib/watcher.py index 15dff99..9e1d557 100644 --- a/src/lib/watcher.py +++ b/src/lib/watcher.py @@ -1,4 +1,3 @@ -import time from watchdog.observers import Observer diff --git a/src/obsidian/page.py b/src/obsidian/page.py index e06bb9c..7de8217 100644 --- a/src/obsidian/page.py +++ b/src/obsidian/page.py @@ -1,4 +1,3 @@ -import os import itertools from src.converters import handlebars, markdown from src.dataclasses.content_data import ContentData @@ -36,7 +35,7 @@ def get_all(pages_dir): for file in fs.get_files_in_dir(pages_dir, filter_partials=True): try: filename, meta, content = fs.load(file) - except UnicodeDecodeError as e: + except UnicodeDecodeError: continue content_data = ContentData(filename=filename, meta=meta, content=content) @@ -70,7 +69,8 @@ def render(self, context=None): content = markdown.render(content) try: return handlebars.render_template(content, context) - except: + except Exception as e: + print(f"RENDER_ERROR: {e}") return content def render_entities(self): diff --git a/tests/dataclasses/content_data_test.py b/tests/dataclasses/content_data_test.py index a704af9..c083e5c 100644 --- a/tests/dataclasses/content_data_test.py +++ b/tests/dataclasses/content_data_test.py @@ -5,6 +5,10 @@ def test_content_data_ext(): data = ContentData(filename="test.hbs") assert data.ext == ".hbs" + data = ContentData(filename="test.xml.hbs") + assert data.ext == ".hbs" + + data = ContentData(filename="test.md") assert data.ext == ".md" @@ -13,6 +17,9 @@ def test_content_data_slug(): data = ContentData(filename="test.hbs") assert data.slug == "test.html" + data = ContentData(filename="dir/test.xml.hbs") + assert data.slug == "test.xml" + data = ContentData(filename="test.hbs", meta={"slug": "abc"}) assert data.slug == "abc.html"