public
Created

Demo selective extraction of code blocks via Marked.js.

  • Download Gist
demo.coffee.md
Markdown

Demo

console.log 'hello'
print 'world!'
demo.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
$ 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!
marko.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.