Fence code blocks in literate scripts
CoffeeScript JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
bin
lib
src
.gitignore
.npmignore
Cakefile
README.md
package.json
test.coffee

README.md

Fence

Convert your literate script to a GH-flavored code-fenced variant ... or vice versa.

Handy if you want to have your code blocks highlighted when posting your literate script as a Gist or as a README.md file for your repo ... or to convert a markdown file with fenced code blocks to literate source.

Note that this utility only converts code blocks in markdown files from one format to another. See this gist for a demonstration of how to run or extract code blocks from any literate source file.

Installing with npm install fence-filter should yield:

$ which fence
/usr/local/bin/fence

Example

Suppose we have a file, demo.python.md, containing ...

# Demo

    print "hello world!"

And now we want to fence the code blocks in this file so that they'll be rendered nicely when people look at our demo file on Github. That is, we want ...

# Demo

```python
print "hello world!"
```

... so that the codeblock is rendered all pretty:

print "hello world!"

Usage

fence demo.python.md > demo.md
fence demo.coffee.md > demo.md
fence demo.litcoffee > demo.md
cat demo.python.md | fence --lang python > demo.md
cat demo.coffee.md | fence --lang coffee > demo.md

You can also "undo" fencing:

fence --undo demo.md > demo.litcoffee
cat demo.md | fence --undo > demo.litcoffee

Round-trip:

fence demo.litcoffee | fence --undo > demo.litcoffee

Credit

HT to @jashkenas, this is but a tweak of journo's Cakefile.