Skip to content

Instantly share code, notes, and snippets.

@tiye
Created September 13, 2012 06:23
Show Gist options
  • Save tiye/3712310 to your computer and use it in GitHub Desktop.
Save tiye/3712310 to your computer and use it in GitHub Desktop.
willow is a tiny markup language
`bold` with `#`
`code` inline code with `\``
block with indent with 2 `space`
indent for more blocks
and more
outdent
link http://google.com
mixed `#\##` #`sdf\`sdf`#
<pre><p>&nbsp;</p>
<p><code>bold</code>&nbsp;with&nbsp;<code>#</code></p>
<p><code>code</code>&nbsp;inline&nbsp;code&nbsp;with&nbsp;<code>`</code></p>
<p>block&nbsp;with&nbsp;indent&nbsp;with&nbsp;2&nbsp;`space`indent&nbsp;for&nbsp;more&nbsp;blocks,and&nbsp;more&nbsp;outdent</p>
<p>&nbsp;</p>
<p>link&nbsp;<a&nbsp;href="http://google.com">http://google.com</a></p>
<p>mixed&nbsp;<code>###</code>&nbsp;<b>`sdf`sdf`</b></p>
<p>&nbsp;</p>
</pre>
#!/usr/bin/env coffee
isArr = (a) -> Array.isArray a
last = (list) -> list[list.length - 1]
trimRight = (line) -> line.trimRight()
debug = no
show =
if debug then console.log
else -> ''
draw = (line) ->
line
.replace(/(https?:(\/\/)?\S+)/g, '<a href="$1">$1</a>')
.replace(/\s/g, '&nbsp;')
plain = (line) ->
line.replace(/>/g,'&gt;')
.replace(/</g,'&lt;')
.replace(/\t/g,' ')
mark_line = (line) ->
line.replace(/>/g,'&gt;')
.replace(/</g,'&lt;')
.replace(/\t/g,' ')
.replace(/\s/g, ' ')
fold = (arr) ->
show 'arr:: ', arr
lines = []
for line in arr
tail = lines.length - 1
if line.trim().length is 0
if isArr lines[tail]
lines[tail].push ''
else
lines.push ''
else if line[0..1] is ' '
if (isArr lines[tail])
lines[tail].push line[2..]
else
lines.push [line[2..]]
else
lines.push (mark_line line)
ret = []
for item in lines
if isArr item
stack = []
while (last item) is ''
stack.push ' '
item.pop()
ret.push (fold item)
ret.push space for space in stack
else ret.push item
show 'ret:: ', ret
ret
text = (line) ->
show 'text:: ', line
mode = 'text'
isEsc = no
token = ''
for c in line
show 'c:: ', c
if isEsc
token += c
isEsc = no
else
if mode is 'text'
if c is '#'
mode = 'bold'
token += '<b>'
else if c is '`'
mode = 'code'
token += '<code>'
else if c is '\\'
isEsc = yes
else
token += c
else if mode is 'bold'
if c is '#'
mode = 'text'
token += '</b>'
else if c is '\\'
isEsc = yes
else
token += c
else if mode is 'code'
if c is '`'
mode = 'text'
token += '</code>'
else if c is '\\'
isEsc = yes
else
token += c
draw token
div = (arr) ->
show 'div:: ', arr
html = ""
for line in arr
html +=
if isArr line
"<div>#{div line}</div>\n"
else
if line is '' then line = ' '
"<p>#{draw line}</p>\n"
html
render = (str) ->
arr = fold str.split('\n').map(trimRight).map(plain)
p = ''
for line in arr
if isArr is line
p += "<pre>#{div line}</pre>"
else
if line is '' then line = ' '
p += "<p>#{text line}</p>\n"
"<pre>#{p}</pre>"
if debug
data = """
`s#dfd#df` #d1``ss`f#
sdfsdf`dfsdf`
"""
show (render data)
else
input_file = ''
process.stdin.resume()
process.stdin.setEncoding 'utf8'
process.stdin.on 'data', (chunk) -> input_file += chunk
process.stdin.on 'end', ->
process.stdout.write (render input_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment