Skip to content

Commit 1e69a81

Browse files
committed
Merge branch 'master' of github.com:0atman/blaze
2 parents f31edc0 + 23c50b7 commit 1e69a81

File tree

2 files changed

+60
-38
lines changed

2 files changed

+60
-38
lines changed

README.md

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sudo wget blaze.oat.sh/blaze -O /usr/bin/blaze && \
77
sudo chmod +x /usr/bin/blaze
88
```
99

10-
(future upgrades can be done in-place with `blaze --upgrade`)
10+
> (future upgrades can be done in-place with `blaze --upgrade`)
1111
1212
Now go write your executable markdown with
1313

@@ -32,89 +32,94 @@ The second category I think is worth exploring.
3232

3333
It is into this ecosystem I present [Blaze](https://gist.github.com/0atman/5ea526a3ae26409da50dd7697eb700e8).
3434

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:
3637

37-
```shell
38-
#!/usr/bin/env sh
39-
args=$1
40-
script=$2
38+
`myscript.py.md`
39+
````markdown
40+
#!blaze python
4141

42-
file_extension=$(echo $script |awk -F . '{if (NF>1) {print $NF}}')
42+
# This file is just markdown
4343

44-
if [ "$file_extension" = "md" ]
45-
then
46-
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:
5347

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+
````
5552

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:
5754

58-
```python
59-
#!blaze python
60-
print("hi")
55+
```shell
56+
λ ./myscript.py.md
57+
hello world
6158
```
6259

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!
6461

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
6668
#!blaze pex arrow --
6769
import arrow
68-
print("run", arrow.now().humanize()) # blaze only processes .md files, plain scripts can be run as-normal
69-
```
7070

71+
# `Arrow` humanized dates example
72+
73+
```python
74+
print("run", arrow.now().humanize())
75+
```
76+
````
7177
> (Note that we are able to use pex's ephemeral venv trick to run python with any requirements pre-installed)
7278
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:
7480

75-
```python
81+
`mydoc.py.md`
82+
````markdown
7683
#!blaze pex flask flask_restful --
7784

7885
# Imports
7986
First the imports, this demo requires the `flask_restful` package.
8087
Then we set up the Flask wsgi application object, `app` and the api wrapper, `api`.
8188

82-
``python
89+
```python
8390
from flask import Flask
8491
from flask_restful import Resource, Api
8592

8693
app = Flask(__name__)
8794
api = Api(app)
88-
``
95+
```
8996

9097
# Flask Restful resources
9198
We define a single `HelloWorld` resource, that responds with a simple json
9299
object on a `GET` request.
93100

94-
``python
101+
```python
95102
class HelloWorld(Resource):
96103
def get(self):
97104
return {'hello': 'world'}
98-
``
105+
```
99106

100107
# Routing
101108
`api.add_resource()` wires the `Resource` class `HelloWorld` into the flask
102109
router at `/`.
103110

104-
``
111+
```
105112
api.add_resource(HelloWorld, '/')
106-
``
113+
```
107114

108115
# Run Server
109116
After we have created everything, we run the flask werkzeug server.
110117

111-
``
118+
```
112119
if __name__ == '__main__':
113120
app.run()
114-
``
115121
```
116-
117-
> (double backticks should be triple, but that messes with markdown highlighting - sorry!)
122+
````
118123

119124
Magic, right?
120125

@@ -135,6 +140,24 @@ hi
135140
./blaze-test.py 0.02s user 0.00s system 67% cpu 0.030 total
136141
```
137142

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+
138161
## Prior Art / Acknowledgements
139162

140163
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).

_config.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
theme: jekyll-theme-slate
22
show_downloads: true
33
title: Blaze
4-
description: Lightning-fast language-agnostic literate programming

0 commit comments

Comments
 (0)