Skip to content

Commit

Permalink
Feat: custom hbs file extensions support
Browse files Browse the repository at this point in the history
  • Loading branch information
A committed Sep 8, 2022
1 parent 55f7e9f commit 975387f
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 23 deletions.
24 changes: 14 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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' <[email protected]>"]
license = "Commons Clause"
Expand Down
1 change: 0 additions & 1 deletion src/blog/blog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from src.lib import fs
from src.blog.layout import Layout
from src.dataclasses.config_data import ConfigData

Expand Down
4 changes: 2 additions & 2 deletions src/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = {
Expand Down
11 changes: 9 additions & 2 deletions src/dataclasses/content_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion src/entities/inline_image.py
Original file line number Diff line number Diff line change
@@ -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"(\!\[(.*?)\]\((.*?)\))"
Expand Down
1 change: 0 additions & 1 deletion src/entities/obsidian_embed.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/entities/reference_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
1 change: 0 additions & 1 deletion src/lib/watcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
from watchdog.observers import Observer


Expand Down
6 changes: 3 additions & 3 deletions src/obsidian/page.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import itertools
from src.converters import handlebars, markdown
from src.dataclasses.content_data import ContentData
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions tests/dataclasses/content_data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"

Expand Down

1 comment on commit 975387f

@github-actions
Copy link

Choose a reason for hiding this comment

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

Coverage

Coverage Report
FileStmtsMissCoverMissing
src
   __init__.py00100% 
   cli.py323232 0%
   config.py20100% 
src/blog
   __init__.py00100% 
   blog.py888 0%
   layout.py290100% 
src/converters
   __init__.py00100% 
   handlebars.py70100% 
   markdown.py40100% 
src/dataclasses
   asset_data.py180100% 
   config_data.py230100% 
   content_data.py7399 88%
src/entities
   image.py90100% 
   inline_image.py1922 89%
   obsidian_embed.py6222 97%
   obsidian_link.py5888 86%
   parser.py3311 97%
   reference_image.py2111 95%
src/lib
   fs.py491717 65%
src/obsidian
   __init__.py00100% 
   page.py6288 87%
   vault.py121212 0%
src/preprocessors
   further_reading.py3244 88%
   include_header.py1222 83%
src/tasks
   __init__.py00100% 
   builder.py212121 0%
   preflight_check.py121212 0%
   server.py101010 0%
   watcher.py303030 0%
src/tree
   node.py280100% 
TOTAL66617973% 

Please sign in to comment.