Created
September 13, 2012 06:23
-
-
Save tiye/3712310 to your computer and use it in GitHub Desktop.
willow is a tiny markup language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`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`# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<pre><p> </p> | |
<p><code>bold</code> with <code>#</code></p> | |
<p><code>code</code> inline code with <code>`</code></p> | |
<p>block with indent with 2 `space`indent for more blocks,and more outdent</p> | |
<p> </p> | |
<p>link <a href="http://google.com">http://google.com</a></p> | |
<p>mixed <code>###</code> <b>`sdf`sdf`</b></p> | |
<p> </p> | |
</pre> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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, ' ') | |
plain = (line) -> | |
line.replace(/>/g,'>') | |
.replace(/</g,'<') | |
.replace(/\t/g,' ') | |
mark_line = (line) -> | |
line.replace(/>/g,'>') | |
.replace(/</g,'<') | |
.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