What is the best practice for code runner for markdown files? #6629
epogrebnyak
started this conversation in
General
Replies: 1 comment
-
Thnking of adding something like: """Extract code blocks from markdown file
If this filename is md.py can use as:
poetry run python md.py | poetry run python
For more functionality: https://github.com/earldouglas/codedown
"""
from markdown_it import MarkdownIt
def strip(text: str) -> str:
return "\n".join([line.strip() for line in text.split("\n")])
def codeblock(text: str, info="python") -> str:
md = MarkdownIt('commonmark')
tokens = [t for t in md.parse(strip(text)) if t.type=="fence" and t.info==info]
return "\n\n".join([t.content for t in tokens])
text = """
```python
print("line with offset")
```
```python
print("another offset")
```
"""
print(codeblock(text)) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm running the code from documentation to make it up-to date, wonder is there is a better way than I do.
My workflow:
codedown
poetry run python
Code for this is here: https://github.com/epogrebnyak/abacus/blob/main/justfile#L60-L72
The workflow is far form perfect:
This a working, but not elegeant solution, so my question is - how do you test you the code in docs, what is th best practice to do so?
Specifically - is there a code extractor that understands fenced code blocks with offset?
Beta Was this translation helpful? Give feedback.
All reactions