UNPKG

kage.json

Version:

parse KAGE format to JSON for future use

161 lines 3.47 kB
// Generated by LiveScript 1.3.1 var isType, runningAsScript, strokeType, headType, tailType, parse, chars, x$; isType = require('prelude-ls').isType; runningAsScript = !module.parent; strokeType = { '99': 'link', '1': 'line', '2': 'curve', '3': 'bend-line', '4': 'OTSU', '6': 'complex-curve', '7': 'vert-slash' }; headType = { '0': 'free', '2': 'horiz-connect', '32': 'vert-connect', '12': 'left-top-corner', '22': 'right-top-corner', '7': 'narrow-for-dot' }; tailType = { '1': { '0': 'free', '2': 'horiz-connect', '32': 'vert-connect', '13': 'left-bottom-corner', '23': 'right-bottom-corner', '4': 'hook-left', '313': 'left-bottom-GT' }, '2': { '7': 'slash-left', '0': 'slash-right', '8': 'stop-for-dot', '4': 'hook-left', '5': 'hook-right' }, '3': { '0': 'free', '5': 'hook-up' }, '4': { '0': 'free', '5': 'hook-up' }, '6': { '7': 'slash-left', '0': 'slash-right', '8': 'stop-for-dot', '4': 'hook-left', '5': 'hook-right' }, '7': { '7': 'slash-left' } }; parse = function(it){ var parts, body, i$, len$, part, ref$, type, head, tail, x0, y0, x1, y1, x2, y2, x3, y3; if (!isType('String', it)) { return []; } parts = it.split(/\$|\s/); body = []; for (i$ = 0, len$ = parts.length; i$ < len$; ++i$) { part = parts[i$]; ref$ = part.split(':'), type = ref$[0], head = ref$[1], tail = ref$[2], x0 = ref$[3], y0 = ref$[4], x1 = ref$[5], y1 = ref$[6], x2 = ref$[7], y2 = ref$[8], x3 = ref$[9], y3 = ref$[10]; switch (+type) { case 99: body.push({ type: strokeType[type] || "unknown: " + type, src: x2, leftTop: { x: +x0, y: +y0 }, rightBottom: { x: +x1, y: +y1 } }); break; case 1: body.push({ type: strokeType[type] || "unknown: " + type, head: headType[head] || "unknown: " + head, tail: tailType[type][tail] || "unknown: " + tail, controlPoints: [ { x: +x0, y: +y0 }, { x: +x1, y: +y1 } ] }); break; case 2: case 3: case 4: body.push({ type: strokeType[type] || "unknown: " + type, head: headType[head] || "unknown: " + head, tail: tailType[type][tail] || "unknown: " + tail, controlPoints: [ { x: +x0, y: +y0 }, { x: +x1, y: +y1 }, { x: +x2, y: +y2 } ] }); break; case 6: case 7: body.push({ type: strokeType[type] || "unknown: " + type, head: headType[head] || "unknown: " + head, tail: tailType[type][tail] || "unknown: " + tail, controlPoints: [ { x: +x0, y: +y0 }, { x: +x1, y: +y1 }, { x: +x2, y: +y2 }, { x: +x3, y: +y3 } ] }); break; } } return body; }; if (runningAsScript) { chars = ''; x$ = process.stdin; x$.setEncoding('utf8'); x$.on('data', function(it){ return chars += it; }); x$.on('end', function(){ return console.log(JSON.stringify(parse(chars), null, 2)); }); } else { module.exports = { parse: parse }; }