You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(future upgrades can be done in-place with `blaze --upgrade`)
10
+
> (future upgrades can be done in-place with `blaze --upgrade`)
11
11
12
12
Now go write your executable markdown with
13
13
@@ -32,89 +32,94 @@ The second category I think is worth exploring.
32
32
33
33
It is into this ecosystem I present [Blaze](https://gist.github.com/0atman/5ea526a3ae26409da50dd7697eb700e8).
34
34
35
-
## Blaze is Tiny:
35
+
## Usage
36
+
Fundamentally, Blaze is a drop-in replacement for `/usr/bin/env`. You stick it at the top of your script, and you can execute it. But Blaze's REAL trick, is that if it is called with an .md file, it only executes code inside triple-backtick codefences: This gives you is the ability to execute your markdown files as though they were normal scripts (it runs python, ruby, nodejs, shell, and likely many more). Here's a hello world example:
cat $script| awk '{ if (/^```/) { i++; next } if ( i % 2 == 1) { print } }'>$script.out
47
-
$args$script.out
48
-
rm $script.out
49
-
else
50
-
$args$script
51
-
fi
52
-
```
44
+
As is this text.
45
+
Whatever you put code in markdown's code fences,
46
+
will be executed by blaze:
53
47
54
-
(non-core code stipped from this example, for the real deal, check [the source](https://github.com/0atman/blaze/blob/master/blaze)
48
+
```python
49
+
print("hello world")
50
+
```
51
+
````
55
52
56
-
But what it gives you is the ability to execute your markdown files as though they were scripts: It is a drop-in replacement for `/usr/bin/env`:
53
+
If you were to run this file, you would see this:
57
54
58
-
```python
59
-
#!blaze python
60
-
print("hi")
55
+
```shell
56
+
λ ./myscript.py.md
57
+
hello world
61
58
```
62
59
63
-
It then allows as many paramaters to be passed to your interpreter as you like (unlike normal shebangs), which means you can use tools like [pex](https://github.com/pantsbuild/pex):
60
+
Congratulations, you just executed a markdown file!
64
61
65
-
```python
62
+
# Advanced Usage
63
+
64
+
Blaze also allows as many paramaters to be passed to your interpreter as you like (unlike normal shebangs), which means you can use tools like [pex](https://github.com/pantsbuild/pex):
65
+
66
+
`myscript.py`
67
+
````markdown
66
68
#!blaze pex arrow --
67
69
import arrow
68
-
print("run", arrow.now().humanize()) # blaze only processes .md files, plain scripts can be run as-normal
69
-
```
70
70
71
+
# `Arrow` humanized dates example
72
+
73
+
```python
74
+
print("run", arrow.now().humanize())
75
+
```
76
+
````
71
77
> (Note that we are able to use pex's ephemeral venv trick to run python with any requirements pre-installed)
72
78
73
-
Blaze's REAL trick, is that if it is called with a `.md` file, it only executes code inside triple-backtick codefences, as in this all-encompasing example of a literate program with built-in requirements:
79
+
Combine these techniques together, and you get an all-encompasing example of a webserver literate program with built-in requirements:
74
80
75
-
```python
81
+
`mydoc.py.md`
82
+
````markdown
76
83
#!blaze pex flask flask_restful --
77
84
78
85
# Imports
79
86
First the imports, this demo requires the `flask_restful` package.
80
87
Then we set up the Flask wsgi application object, `app` and the api wrapper, `api`.
81
88
82
-
``python
89
+
```python
83
90
from flask import Flask
84
91
from flask_restful import Resource, Api
85
92
86
93
app = Flask(__name__)
87
94
api = Api(app)
88
-
``
95
+
```
89
96
90
97
# Flask Restful resources
91
98
We define a single `HelloWorld` resource, that responds with a simple json
92
99
object on a `GET` request.
93
100
94
-
``python
101
+
```python
95
102
class HelloWorld(Resource):
96
103
def get(self):
97
104
return {'hello': 'world'}
98
-
``
105
+
```
99
106
100
107
# Routing
101
108
`api.add_resource()` wires the `Resource` class `HelloWorld` into the flask
102
109
router at `/`.
103
110
104
-
``
111
+
```
105
112
api.add_resource(HelloWorld, '/')
106
-
``
113
+
```
107
114
108
115
# Run Server
109
116
After we have created everything, we run the flask werkzeug server.
110
117
111
-
``
118
+
```
112
119
if __name__ == '__main__':
113
120
app.run()
114
-
``
115
121
```
116
-
117
-
> (double backticks should be triple, but that messes with markdown highlighting - sorry!)
122
+
````
118
123
119
124
Magic, right?
120
125
@@ -135,6 +140,24 @@ hi
135
140
./blaze-test.py 0.02s user 0.00s system 67% cpu 0.030 total
136
141
```
137
142
143
+
# Mechanics
144
+
145
+
Here's the basics of Blaze, a small shell script:
146
+
147
+
```shell
148
+
#!/usr/bin/env sh
149
+
args=$1
150
+
script=$2
151
+
152
+
cat $script| awk '{ if (/^```/) { i++; next } if ( i % 2 == 1) { print } }'>$script.out
153
+
$args$script.out
154
+
rm $script.out
155
+
```
156
+
157
+
> (non-core code stripped from this example, for the real deal, check [the source](https://github.com/0atman/blaze/blob/master/blaze)
158
+
159
+
As you can see blaze simply runs your script through an `awk` script to strip all text outside triple-backtick code fences, then runs it with the interpreter of your choice. There's nothing to it really!
160
+
138
161
## Prior Art / Acknowledgements
139
162
140
163
Blaze is currently a hacked-together LP tool that is only suitable for one-off scripts. I borrowed the awk code-fence-stripping code from [@trauber](https://gist.github.com/trauber/4955706).
0 commit comments