Skip to content
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
6 changes: 6 additions & 0 deletions docs/readers.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Example:

{{ read_yaml('assets/tables/yaml_table.yml') | add_indentation(spaces=4) }}

The file is read as UTF-8. Use the `encoding` argument for files in another encoding, for example {% raw %}`{{ read_yaml('assets/tables/yaml_table.yml', encoding='cp1251') }}`{% endraw %}.


### `read_table`

Expand Down Expand Up @@ -210,6 +212,8 @@ Example:

{{ read_raw('assets/tables/markdown_table.md') | add_indentation(spaces=4) }}

The file is read as UTF-8. Use the `encoding` argument for files in another encoding, for example {% raw %}`{{ read_raw('assets/tables/markdown_table.md', encoding='cp1251') }}`{% endraw %}.


## Macros

Expand Down Expand Up @@ -269,6 +273,8 @@ Example:

{{ pd_read_yaml('assets/tables/yaml_table.yml').to_markdown(tablefmt="pipe", index=False) | add_indentation(spaces=4) }}

The file is read as UTF-8. Use the `encoding` argument for files in another encoding, for example {% raw %}`{{ pd_read_yaml('assets/tables/yaml_table.yml', encoding='cp1251') }}`{% endraw %}.


### `pd_read_table`

Expand Down
11 changes: 7 additions & 4 deletions src/mkdocs_table_reader_plugin/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,17 @@ def read_excel(*args, **kwargs) -> str:

@ParseArgs
def pd_read_yaml(*args, **kwargs) -> str:
encoding = kwargs.pop("encoding", "utf-8")
json_kwargs = kwargs_in_func(kwargs, pd.json_normalize)
with open(args[0]) as f:
with open(args[0], encoding=encoding) as f:
df = pd.json_normalize(yaml.safe_load(f), **json_kwargs)
return df

@ParseArgs
def read_yaml(*args, **kwargs) -> str:
encoding = kwargs.pop("encoding", "utf-8")
json_kwargs = kwargs_in_func(kwargs, pd.json_normalize)
with open(args[0]) as f:
with open(args[0], encoding=encoding) as f:
df = pd.json_normalize(yaml.safe_load(f), **json_kwargs)

markdown_kwargs = kwargs_not_in_func(kwargs, pd.json_normalize)
Expand Down Expand Up @@ -170,7 +172,8 @@ def read_raw(*args, **kwargs) -> str:
Returns:
str: file contents
"""
with open(args[0]) as f:
encoding = kwargs.pop("encoding", "utf-8")
with open(args[0], encoding=encoding) as f:
return f.read()


Expand All @@ -194,4 +197,4 @@ def read_raw(*args, **kwargs) -> str:
"pd_read_json": pd_read_json,
"pd_read_feather": pd_read_feather,
}
MACROS = {**READERS, **MACRO_ONLY}
MACROS = {**READERS, **MACRO_ONLY}
3 changes: 3 additions & 0 deletions tests/fixtures/encoding/assets/tables/cp1251_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| Òîâàð | Öåíà |
|--------|-----:|
| Ñûð | 539956 |
4 changes: 4 additions & 0 deletions tests/fixtures/encoding/assets/tables/cp1251_table.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Òîâàð: Õëåá
Öåíà: 531456
- Òîâàð: Ìîëîêî
Öåíà: 80
11 changes: 11 additions & 0 deletions tests/fixtures/encoding/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Test page

Files that are not UTF-8 encoded can be read by specifying the `encoding`:

## read_yaml

{{ read_yaml('assets/tables/cp1251_table.yml', encoding='cp1251') }}

## read_raw

{{ read_raw('assets/tables/cp1251_table.md', encoding='cp1251') }}
6 changes: 6 additions & 0 deletions tests/fixtures/encoding/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
site_name: test git_table_reader site
use_directory_urls: false

plugins:
- search
- table-reader
21 changes: 21 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,24 @@ def test_macros_jinja2_syntax(tmp_path):
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)


def test_non_utf8_encoding(tmp_path):
"""
A project where files are not UTF-8 encoded, and 'encoding' is specified.
"""

tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/encoding/mkdocs.yml", tmp_path
)

result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"

page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text(encoding="utf-8")
# read_yaml() inserted the cp1251 encoded yaml file
assert re.search(r"531456", contents)
assert re.search(r"Хлеб", contents)
# read_raw() inserted the cp1251 encoded markdown file
assert re.search(r"539956", contents)
assert re.search(r"Сыр", contents)
Loading