nedbat / cog

Small bits of Python computation for static files
MIT License
340 stars 26 forks source link

Using cog with a markdown file and fenced code blocks #6

Closed fmaida closed 5 years ago

fmaida commented 5 years ago

Hello, I'm sorry to bother you but would it be possible to use cog with python snippets put between fenced code, inside a markdown file ?

For example can this markdown file:

This is my source code

```python
def sum_numbers(a, b):
    return a + b

sum_numbers(3, 4)    
```

and this is the result:

```cog
```

Be translated into this:

This is my source code

```python
def sum_numbers(a, b):
    return a + b

sum_numbers(3, 4)    
```

and this is the result:

```cog
7
```

And then, getting this final output from a markdown parser:

This is my source code

def sum_numbers(a, b):
    return a + b

sum_numbers(3, 4)    

and this is the result:

7
nedbat commented 5 years ago

You could use cog in a markdown file (HTML comments will be useful). But I'm not sure there's a way to combine the HTML comments, Python comments, and Markdown triple-backticks in a way that will keep all of the software happy and produce the right result.

fmaida commented 5 years ago

Thank you.

janto commented 3 years ago

I was playing around and made the following that seems to work :) https://gist.github.com/janto/2f779791b95e46c4abe7c9211af7a19b

(unable to paste the code here since the markdown gets interpreted)

which will give:

janto commented 3 years ago

This is a markdown file with some cog wrapped Python code...


def f():
    return "cog is awesome"

result = f()

which results in output like this

result: 'cog is awesome'

Markdown ends :)

janto commented 3 years ago

My version 2 that is shorter and gives color highlighting of python code in my editor: https://gist.github.com/janto/c5176c5dc3968bdca146e59245ed4788