timvink / mkdocs-table-reader-plugin

MkDocs plugin that enables a markdown tag like {{ read_csv('table.csv') }} to directly insert various table formats into a page
https://timvink.github.io/mkdocs-table-reader-plugin/
MIT License
111 stars 18 forks source link

escape pipe symbol `|` #29

Closed mizraelson closed 1 year ago

mizraelson commented 1 year ago

Is there a way to escape | before converting to markdawn? I have a table with | symbols in cells. And I end up with these cells being splitted into columns.

timvink commented 1 year ago

Can you be more specific? Is your table in .csv format for example?

The normal way of escaping characters (prefixing with \) works fine for me. For example, here's a CSV file:

"a","b"
50,"b\|a"

Which renders as

image

mizraelson commented 1 year ago

Yes, but is it possible to do that without changing the original table? Like, let's say its a table which has | symbol in it. And it's a standard automatically-generated table,I cant alter its' format. Is there a way to kinda check for this pipe symbol and escape it inside the plagin before passing it to df.to_markdown()?

timvink commented 1 year ago

There are many options in both pd.read_csv and df.to_markdown. Have you found any that work for you?

You can use all those arguments in this plugin. See https://timvink.github.io/mkdocs-table-reader-plugin/customization/

mizraelson commented 1 year ago

I haven't found any that would solve it. The idea is that I have a file with a tab-delimited table which has | symbols in it (e.g. this is a single cell - "13|20|33|295|302||35.0;25|31|51|292|298||30.0;22|28|51|295|301||30.0"). I want to include this table in mkdocs. Right now I get columns separated by | instead of a single cell with a string.

timvink commented 1 year ago

I don't think this is an issue with this plugin. If you're not able to load the table into python using pandas, then there's something wrong with your table and you need to fix the input.

You can even write a little python function to do that for you, and trigger it to run on build (when you do mkdocs build or mkdocs serve) using this plugin: https://github.com/aklajnert/mkdocs-simple-hooks

Hope that helps!

proddy commented 1 year ago

I ran into this problem too and had to change my code to escape all |'s in a string. Would have been really nice if this library did it.

timvink commented 1 year ago

Ok I'll look into making this easier to do.

If you have an example table for me that would be great (so I can write a unit test with it)

proddy commented 1 year ago

It's really not important! It was just a few seconds work to code it on my side. But if you need a file this is what is generated for one of my projects. I use the pipe symbol to show the optional parameters

timvink commented 1 year ago

So this actually is a bug. pandas uses tabulate during the .to_markdown(), which uses the pipe table markdown format. So then mkdocs will add extra columns

image

I couldn't find any functionality in https://github.com/astanin/python-tabulate to deal with this. I will likely open an issue to see if they want to support it, or otherwise hack it into this plugin. Downside is obviously that searching & replacing characters in columns + all values will costs some performance, but in documentation at least we can expect reasonably small tables.

timvink commented 1 year ago

More details: https://github.com/astanin/python-tabulate/issues/241

I'm going to assume most people insert relatively small tables into their documentation, so looping over all cells and doing a regex to replace all possible pipe characters is a performance hit that could be acceptable. I could offer an option to disable it, but then I would have to run a bunch of benchmark and explain to users in the docs how much it effects performance. For now, not going to overengineer it and choosing for a fix that 'just works'.

Fix will be part of table-reader v2 (#39), which is work-in-progress

timvink commented 1 year ago

Solution in https://github.com/timvink/mkdocs-table-reader-plugin/pull/39/commits/45ee396e4e2c65a2801d72d6d7593c12bf5616b1

proddy commented 1 year ago

thanks @timvink for implementing this.