Skip to content

Commit

Permalink
start implementing new superfences notation
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink authored Sep 9, 2023
1 parent 781fc82 commit 2c8df97
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
35 changes: 35 additions & 0 deletions mkdocs_table_reader_plugin/fences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pymdownx.superfences import _escape
from pymdownx.superfences import SuperFencesException

from mkdocs.exceptions import PluginError
from mkdocs_table_reader_plugin.readers import READERS

def fence_tablereader(source, language, class_name, options, md, **kwargs):
"""
Inspired by https://github.com/facelessuser/pymdown-extensions/blob/8ee5b5caec8f9373e025f50064585fb9d9b71f86/pymdownx/superfences.py#L146
""" # noqa
# if not some_validation_function(source):
# raise SuperFencesException from PluginError(f"Your vegalite syntax is not valid JSON. Fix:\n\n{source}")

classes = kwargs["classes"]
id_value = kwargs["id_value"]
attrs = kwargs["attrs"]

txt_input = str(_escape(source))
lines = txt_input.split('\n')
user_input = {}

for line in lines:
key, value = line.split(':', 1)
key = key.strip()
value = value.strip()
user_input[key] = value

breakpoint()
return READERS[user_input['reader']](user_input['filepath'])


# The problem is that this function returns markdown instead of HTML.
# A possible solution might to to use pandas .to_html() instead of .to_markdown()
# this requires adapting our READERS functions though.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"Programming Language :: Python :: 3.10",
"License :: OSI Approved :: MIT License",
],
install_requires=["mkdocs>=1.0", "pandas>=1.1", "tabulate>=0.8.7", "PyYAML>=5.4.1"],
install_requires=["mkdocs>=1.0", "pandas>=1.1", "tabulate>=0.8.7", "PyYAML>=5.4.1", "pymdown-extensions>=9.2"],
packages=find_packages(),
entry_points={
"mkdocs.plugins": [
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/superfence/assets/tables/basic_table.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"a","b"
40,73
50,52
531456,80
"name","table1"
47 changes: 47 additions & 0 deletions tests/fixtures/superfence/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# site title


{{ read_csv('assets/tables/basic_table.csv') }}

```tablereader
filepath: /workspaces/mkdocs-table-reader-plugin/tests/fixtures/superfence/assets/tables/basic_table.csv
reader: read_csv
```

This one should fail (typo in reader value)

<!-- ```tablereader
filepath: assets/tables/basic_table.csv
reader: read_cvs
``` -->

This one should fail (typo in path)

<!-- ```tablereader
filepath: table.csv
reader: read_csv
``` -->

## An HTML table

<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>col1</th>
<th>col2</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>4</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>3</td>
</tr>
</tbody>
</table>
13 changes: 13 additions & 0 deletions tests/fixtures/superfence/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
site_name: test git_table_reader site
use_directory_urls: false

plugins:
- search
- table-reader

markdown_extensions:
- pymdownx.superfences:
custom_fences:
- name: tablereader
class: tablereader
format: !!python/name:mkdocs_table_reader_plugin.fences.fence_tablereader

0 comments on commit 2c8df97

Please sign in to comment.