Skip to content

Instantly share code, notes, and snippets.

@chezou
Last active January 9, 2022 05:43
Show Gist options
  • Save chezou/1b22b80a7e1a95990101b86fb57d398b to your computer and use it in GitHub Desktop.
Save chezou/1b22b80a7e1a95990101b86fb57d398b to your computer and use it in GitHub Desktop.
diff --git a/prelims/handler.py b/prelims/handler.py
index d4d416d..014bc7d 100644
--- a/prelims/handler.py
+++ b/prelims/handler.py
@@ -1,16 +1,18 @@
from .post import Post
import os
-
+from pathlib import Path
class StaticSitePostsHandler(object):
- def __init__(self, path_dir):
+ def __init__(self, path_dir, encoding='utf-8'):
assert os.path.isdir(os.path.expanduser(path_dir)), \
f'path {path_dir} is not a directory or does not exist'
- self.paths = [os.path.join(path_dir, f) for f in os.listdir(path_dir)]
+ exts = ['.md', '.html']
+ self.paths = [p for p in Path(path_dir).rglob('*') if p.suffix in exts]
self.processors = []
+ self.encoding = encoding
def register_processor(self, processor):
"""Add a front matter processor to the queue.
@@ -32,7 +34,7 @@ class StaticSitePostsHandler(object):
"""
posts = []
for path in self.paths:
- post = Post.load(path)
+ post = Post.load(path, self.encoding)
if post.is_valid():
posts.append(post)
return posts
diff --git a/prelims/post.py b/prelims/post.py
index 07ea660..56607f2 100644
--- a/prelims/post.py
+++ b/prelims/post.py
@@ -56,8 +56,8 @@ class Post(object):
f.write(content)
@staticmethod
- def load(path):
- with open(path) as f:
+ def load(path, encoding):
+ with open(path, encoding=encoding) as f:
raw_content = f.read()
front_matter = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment