Skip to content

Latest commit

 

History

History
187 lines (148 loc) · 3.25 KB

README_src.md

File metadata and controls

187 lines (148 loc) · 3.25 KB

execute.md

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)

Usage:

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'))) 

Test Cases

A standard, unflagged codeblock

This'll be ignored

A codeblock designed to be run

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)])

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.

Shared interpreter demo

Again, just done with '''python#run.

print(f(10))

A new interpreter instance

This one uses one additional tag, now looking like '''python#run#new. Snazzy.

print(f(11))

Unboxed output

'''python#run#unboxed

print('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

print(1+2)

Inverted output order

This one uses '''python#run#output_first:

print(3+4)
infix = ', of course,'
print(f"These tags can{infix} be combined. Just look at this very sentence in [README_src.md](https://raw.githubusercontent.com/FraserLee/execute-dot-md/main/README_src.md) 😉")

Languages

🦀 rust 🦀

!req(languages/rust_example1.rs)
!req(languages/rust_example2.rs)

🐍 python 🐍

print(2+2)

c

!req(languages/c_example.c)

c++

!req(languages/cpp_example.cpp)

bash 🐚

!req(languages/bash_example.sh)

go

!req(languages/go_example.go)

JS 🟨

!req(languages/js_example.js)

Lua

!req(languages/lua_example.lua)

Kotlin

!req(languages/kotlin_example.kts)

Haskell

main = (putStrLn . reverse) "!dlrow olleh"

Nim 👑

proc factorial(x: int): int =
    if x <= 0: 1
    else: x * factorial(x - 1)
  
echo "10! = ", factorial(10)

Haxe

class Main {
    static public function main() {
        trace("hello world");
    }
}

OCaml 🐫

print_string "hello world\n"

Java ☕

class Main{
    public static void main(String[] args){
        System.out.println("hello world!");
    }
}

Zsh

echo "hello world!"

Brainfuck

(example sourced from Wikipedia)

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.