|
#!/usr/bin/env coffee |
|
# |
|
# Crude hack to demo litcoffee interpolation |
|
|
|
help = ''' |
|
|
|
marko [options] FILES |
|
|
|
options: |
|
|
|
-h, --help show help |
|
-r, --run run code blocks |
|
-x, --extract extract code blocks |
|
-i, --interpolate interpolate values from code blocks |
|
|
|
''' |
|
|
|
fs = require 'fs' |
|
coffee = require 'coffee-script' |
|
marked = require 'marked' |
|
|
|
options = |
|
gfm: true, |
|
pedantic: false, |
|
sanitize: false, |
|
langPrefix: 'prettyprint lang-' |
|
highlight: (code, lang) -> code |
|
|
|
marked.setOptions options |
|
|
|
argv = require('optimist') |
|
.boolean(['h', 'r', 'x', 'i']) |
|
.alias('h', 'help') |
|
.alias('r', 'run') |
|
.alias('x', 'extract') |
|
.alias('i', 'interpolate') |
|
.argv |
|
|
|
print = console.log |
|
print help if argv.help |
|
|
|
head = ''' |
|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<link rel="stylesheet" href="style.css"> |
|
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script> |
|
<body> |
|
''' |
|
|
|
interpolate = (text) -> |
|
t = text.replace(/\n/g, '\\n').replace(/"/g, '\\"') |
|
coffee.eval '"' + t + '"' |
|
|
|
codeFrom = (source) -> |
|
(t.text for t in marked.lexer(source) when t.type is 'code').join "\n" |
|
|
|
parsed = (source) -> |
|
coffee.eval codeFrom source |
|
tokens = marked.lexer source, options |
|
for t in tokens |
|
if t.type is 'code' |
|
t.lang = 'coffee' if not t.lang |
|
else |
|
t.text = interpolate t.text |
|
marked.parser tokens, options |
|
|
|
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 |
|
else if argv.interpolate and /coffee/.test file |
|
print head, parsed source |
|
else |
|
print head, marked source |