diff --git a/mkdocs_table_reader_plugin/fences.py b/mkdocs_table_reader_plugin/fences.py new file mode 100644 index 0000000..57a32da --- /dev/null +++ b/mkdocs_table_reader_plugin/fences.py @@ -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. + diff --git a/setup.py b/setup.py index 5c589e5..d95dd7a 100644 --- a/setup.py +++ b/setup.py @@ -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": [ diff --git a/tests/fixtures/superfence/assets/tables/basic_table.csv b/tests/fixtures/superfence/assets/tables/basic_table.csv new file mode 100644 index 0000000..6743b2e --- /dev/null +++ b/tests/fixtures/superfence/assets/tables/basic_table.csv @@ -0,0 +1,5 @@ +"a","b" +40,73 +50,52 +531456,80 +"name","table1" \ No newline at end of file diff --git a/tests/fixtures/superfence/docs/index.md b/tests/fixtures/superfence/docs/index.md new file mode 100644 index 0000000..1972446 --- /dev/null +++ b/tests/fixtures/superfence/docs/index.md @@ -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) + + + +This one should fail (typo in path) + + + +## An HTML table + + + + + + + + + + + + + + + + + + + + + +
col1col2
014
123
\ No newline at end of file diff --git a/tests/fixtures/superfence/mkdocs.yml b/tests/fixtures/superfence/mkdocs.yml new file mode 100644 index 0000000..914eb2d --- /dev/null +++ b/tests/fixtures/superfence/mkdocs.yml @@ -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 \ No newline at end of file