run code-blocks in markdown files and insert the results immediately below, like a home-brewed jupyter notebook.
- basic functionality
- additional languages
- matplotlib support (TODO)
Either
1. execute with ./execute_md.py [SOURCE] [DEST]
or ./execute_md.py [SOURCE]
(which will print results to stdout), or
2. use it through python (or whatever you want to FFI that to)
from execute_md import execute_md open('dest.md', 'w').writelines(execute_md(open('src.md', 'r')))
This'll be ignored
The following block starts with
'''python#run
def f(x):
if x <= 1: return 1
return x * f(x-1)
print([f(x) for x in range(6)])
[1, 1, 2, 6, 24, 120]
The #run
tag is stripped from the final output, leaving us with just a codeblock starting with '''python
, followed by a second codeblock with output.
Again, just done with '''python#run
.
print(f(10))
3628800
This one uses one additional tag, now looking like '''python#run#new
. Snazzy.
print(f(11))
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'f' is not defined
'''python#run#unboxed
print('This is a test of *various* **markdown** ~~features~~.')
This is a test of various markdown features.
Hidden input-field with #hide
The source code block here looks like the following:
'''python#run#hide print(1+2) '''
However that gets dropped from the file, leaving us with just
3
This one uses '''python#run#output_first
:
7
print(3+4)
These tags can, of course, be combined. Just look at this very sentence in README_src.md π
fn main() {
let x = 2;
let y = 3;
println!("{}", x + y);
}
5
fn main() {
this is a syntax error
}
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `is`
--> <anon>:2:10
|
2 | this is a syntax error
| ^^ expected one of 8 possible tokens
error: aborting due to previous error
print(2+2)
4
#include <stdio.h>
#include <math.h>
int main() {
float x = 5.0;
float y = 0.4;
printf("%f\n", sqrt(x*y));
return 0;
}
1.414214
#include <iostream>
#include <cmath>
int main() {
float x = 1.;
float y = 2.;
std::cout << sqrt(-1) << std::endl;
return 0;
}
nan
# print the first few prime numbers separated by dashes
for ((i=2; i<37; i++)); do
for ((j=2; j<i; j++)); do
if (($i % $j == 0)); then break; fi
done
if (($i == $j)); then echo -n $i"-"; fi
done
# then finish off a few more with awk
awk 'BEGIN { RS = " "; ORS = "|" } {
for (i=2; i<$1; i++) {
if ($1 % i == 0) break
if ($1 == i+1) print $1
}
} END { ORS = "\n"; print "" }' <<< $(seq 37 100)
2-3-5-7-11-13-17-19-23-29-31-37|41|43|47|53|59|61|67|71|73|79|83|89|97|
package main
import "fmt"
func main() {
fmt.Println("go" + "lang")
}
golang
const x = '7'
const y = 1
console.log(x - y)
console.log(x + y)
6
71
local a = {1, nil, "test"}
for key, value in pairs(a) do
local b = value and print(value)
end
1
test
import kotlin.math.*
infix fun Int.greaterThanExp(x: Int) = this > exp(x.toFloat())
fun factorial(x: Int): Int = when (x) {
0 -> 1
else -> x * factorial(x - 1)
}
println(factorial(5) greaterThanExp 6)
println(factorial(6) greaterThanExp 7)
println(factorial(7) greaterThanExp 8)
false
false
true
main = (putStrLn . reverse) "!dlrow olleh"
hello world!
proc factorial(x: int): int =
if x <= 0: 1
else: x * factorial(x - 1)
echo "10! = ", factorial(10)
10! = 3628800
class Main {
static public function main() {
trace("hello world");
}
}
Main.hx:3: hello world
print_string "hello world\n"
hello world
class Main{
public static void main(String[] args){
System.out.println("hello world!");
}
}
hello world!
echo "hello world!"
hello world!
(example sourced from Wikipedia)
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
Hello World!