gpoore / codebraid

Live code in Pandoc Markdown
BSD 3-Clause "New" or "Revised" License
367 stars 13 forks source link

Markdown strings don't render normally #58

Closed willbowditch closed 1 year ago

willbowditch commented 1 year ago

I'm having trouble using codebraid to generate a markdown table programatically.

Printing a string which contains markdown, with .cb-run, gives a modified version with show=stdout:raw. If I set stdout:verbatim the table appears normally, but then is within a code block.

Is this a bug or is there a way to print markdown directly into the document?


Example

pip install pandas tabulate

With the following codebraid document:

Regular print statements are not indented

```{.python .cb-run}
print('hi')

Statements which render markdown are indented:

import pandas as pd

df = pd.util.testing.makeDataFrame()
markdown_string = df.head(4).to_markdown()
print(markdown_string)

Just confirming there are no tabs at the start:

print(markdown_string[0:10])

Statements verbatim end up in code chunks:

print(markdown_string)

And running `codebraid pandoc --from markdown --to markdown example.md > example.rendered.md`

The resulting document is:

``````markdown
Regular print statements are not indented

hi

Statements which render markdown are indented:

                         A           B           C            D
  ------------ ----------- ----------- ----------- ------------
  nbukiFtbTp       1.49929    0.379485     1.11042    -0.450874
  NjDk9FKWQk      0.356847   -0.929118     1.83266   -0.0636046
  YxLiauK7iE      -2.29368   -0.810147   -0.186645      -2.6995
  KJDbnOjBNy     -0.864176    0.801141    -1.28186     0.906364

Just confirming there are no tabs at the start:

| 

Statements verbatim end up in code chunks:

``` stdout
|            |         A |         B |         C |          D |
|:-----------|----------:|----------:|----------:|-----------:|
| nbukiFtbTp |  1.49929  |  0.379485 |  1.11042  | -0.450874  |
| NjDk9FKWQk |  0.356847 | -0.929118 |  1.83266  | -0.0636046 |
| YxLiauK7iE | -2.29368  | -0.810147 | -0.186645 | -2.6995    |
| KJDbnOjBNy | -0.864176 |  0.801141 | -1.28186  |  0.906364  |

The desired output is:

``````md

|            |         A |         B |         C |          D |
|:-----------|----------:|----------:|----------:|-----------:|
| nbukiFtbTp |  1.49929  |  0.379485 |  1.11042  | -0.450874  |
| NjDk9FKWQk |  0.356847 | -0.929118 |  1.83266  | -0.0636046 |
| YxLiauK7iE | -2.29368  | -0.810147 | -0.186645 | -2.6995    |
| KJDbnOjBNy | -0.864176 |  0.801141 | -1.28186  |  0.906364  |

Thanks

gpoore commented 1 year ago

This seems to be caused by the way Pandoc processes tables when converting back to Markdown. It uses a different type of Markdown table than what was originally given and apparently automatically indents tables in that format by 2 spaces (which still produces a table, since it isn't 4 spaces).

Try --to markdown-simple_tables. That will disable the table format that Pandoc is using and should usually force a fallback to the format you want.

willbowditch commented 1 year ago

Thanks @gpoore - that has done the trick