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 encoding="utf8" to resolve UnicodeDecodeError: 'gbk' codec can't… #4

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 2 deletions moffee/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def read_options(document_path) -> PageOption:
"""Read frontmatter options from the document path"""
with open(document_path, "r") as f:
with open(document_path, "r", encoding="utf8") as f:
document = f.read()
_, options = parse_frontmatter(document)
return options
Expand Down Expand Up @@ -96,7 +96,7 @@ def build(
document_path: str, output_dir: str, template_dir: str, theme_dir: str = None
):
"""Render document, create output directories and write result html."""
with open(document_path) as f:
with open(document_path, encoding="utf8") as f:
document = f.read()
asset_dir = os.path.join(output_dir, "assets")

Expand Down
20 changes: 10 additions & 10 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def setup_test_env():
Other Pages
![Image-1](image.png)
---
Paragraph 1
Paragraph 1 中文
___
Paragraph 2
Paragraph 2 أخذ العينات; اختبار التبقع; اخذ العينات; اعتيان
***
Paragraph 3
Paragraph 3 Арефьев.Виталий.Кириллович
***
![Image-2](image2.png)
"""
Expand All @@ -39,7 +39,7 @@ def setup_test_env():
os.mkdir(res_dir)

# Create various test files
with open(doc_path, "w") as f:
with open(doc_path, "w", encoding="utf8") as f:
f.write(doc)

with open(os.path.join(temp_dir, "image.png"), "w") as f:
Expand All @@ -57,7 +57,7 @@ def appeared(text, pattern):

def test_rendering(setup_test_env):
_, doc_path, _, _ = setup_test_env
with open(doc_path) as f:
with open(doc_path, encoding="utf8") as f:
doc = f.read()
html = render_jinja2(doc, template_dir())
assert appeared(html, "chunk-paragraph") == 5
Expand All @@ -82,7 +82,7 @@ def test_build(setup_test_env):
options = read_options(doc_path)
build(doc_path, output_dir, template_dir(), template_dir(options.theme))
j = os.path.join
with open(j(output_dir, "index.html")) as f:
with open(j(output_dir, "index.html"), encoding="utf8") as f:
output_html = f.read()

# output dir integrity
Expand All @@ -95,20 +95,20 @@ def test_build(setup_test_env):
assert name in output_html

# use beamer css
with open(j(output_dir, "css", "extension.css")) as f:
with open(j(output_dir, "css", "extension.css"), encoding="utf8") as f:
assert len(f.readlines()) > 2


def test_retrieve_structure():
doc = """
# Title
p0
## Heading1
## Heading1 中文
p1
### Subheading1
### Subheading1 أخذ العينات; اختبار التبقع; اخذ العينات; اعتيان
p2
## Heading2
### Subheading1
### Subheading1 Арефьев.Виталий.Кириллович
p3
# Title2
p4
Expand Down
Loading