Create a gist now

Instantly share code, notes, and snippets.

Demo selective extraction of code blocks via Marked.js.

Demo

console.log 'hello'
print 'world!'
$ cat demo.coffee.md
Demo
====
console.log 'hello'
```python
print 'world!'
```
$ marko.coffee -h
marko [options] FILES
options:
-h, --help show help
-r, --run run code blocks
-x, --extract extract code blocks
[-l, --lang LANG extract fenced code blocks of specified sort]
$ marko.coffee -x demo.coffee.md
console.log 'hello'
$ marko.coffee -x --lang=python demo.coffee.md
print 'world!'
$ marko.coffee -x --lang=python demo.coffee.md | python -
world!
#!/usr/bin/env coffee
help = '''
marko [options] FILES
options:
-h, --help show help
-r, --run run code blocks
-x, --extract extract code blocks
[-l, --lang LANG extract fenced code blocks of specified sort]
'''
fs = require 'fs'
coffee = require 'coffee-script'
marked = require 'marked'
argv = require('optimist')
.boolean(['h', 'r', 'x'])
.alias('h', 'help')
.alias('r', 'run')
.alias('x', 'extract')
.alias('l', 'lang')
.argv
print = console.log
print help if argv.help
head = '''
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<body>
'''
codeFrom = (source, lang) ->
(t.text for t in marked.lexer(source) \
when t.type is 'code' and t.lang is lang).join "\n"
for file in argv._ when file.match /\.(litcoffee|md)/i
source = fs.readFileSync file, 'utf8'
if argv.run and /coffee/.test file
coffee.eval codeFrom source
else if argv.extract
print codeFrom(source, argv.lang)
else
print head, marked source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment