Created
April 18, 2017 11:45
-
-
Save be5invis/660e31f11563582671709ba5ae36c4a9 to your computer and use it in GitHub Desktop.
This file contains 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
"use esnext"; | |
const code = `extract : (a -> Bool) -> List a -> Maybe (a, List a) | |
extract _ [] = Nothing | |
extract p (x::xs) with (p x) | |
| True = Just (x, xs) | |
| False = do | |
(y, xs') <- extract p xs | |
pure (y, x::xs')`; | |
function groupBy(a, f){ | |
let g = [], last = null, ans = [] | |
for(let x of a){ | |
let r = f(x) | |
if(r == last){ | |
g.push(x) | |
} else { | |
if(g.length) ans.push({group:g, tag:last}); | |
g = [x]; | |
} | |
last = r; | |
} | |
if(g.length) return ans.concat([{group:g, tag:last}]); else return ans; | |
} | |
function measureToken(token){ | |
return token.length | |
} | |
function escapeH(token){ | |
return token.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | |
} | |
function typesetLine(row){ | |
return groupBy(row, c => c === ' ') | |
.map(({group:cg}) => cg.join('')) | |
.map(token => `<td colspan="${measureToken(token)}">${escapeH(token)}</td>`) | |
.join(''); | |
} | |
function typeset(code){ | |
return groupBy(code.split('\n'), line => line.match(/^[ \t]*/)[0]) | |
.map(({group:g, tag:i}) => `<table style="margin-left:${i.length/2}em">` + g.map(r => `<tr>${typesetLine(r.trim())}</tr>`) | |
.join('') + "</table>") | |
.join('') | |
} | |
document.body.innerHTML = typeset(code); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment