From cc4fb936a8d95b78ff71548d7b6919fedec0dc16 Mon Sep 17 00:00:00 2001 From: timb Date: Thu, 16 Apr 2015 10:28:33 -0700 Subject: import mirc/irssi colorcodes --- js/clipboard.js | 40 ++++++++++++++++- js/parse.js | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 js/parse.js (limited to 'js') diff --git a/js/clipboard.js b/js/clipboard.js index d2d1592..b20528a 100644 --- a/js/clipboard.js +++ b/js/clipboard.js @@ -9,7 +9,7 @@ var clipboard = (function () { import_ascii.addEventListener("change", exports.setFormat("ascii")) import_irssi.addEventListener("change", exports.setFormat("irssi")) import_mirc.addEventListener("change", exports.setFormat("mirc")) - import_button.addEventListener("click", exports.import_text) + import_button.addEventListener("click", exports.import_colorcode) import_html.addEventListener("click", exports.import_html) export_button.addEventListener("click", exports.export_data) import_textarea.addEventListener("focus", exports.focus) @@ -112,10 +112,46 @@ var clipboard = (function () { }) }) }, + + import_colorcode: function () { + var data = import_textarea.value + + var irssi_style_regex = /^\s*\/exec -out printf "/; + + // turn irssi style into mirc style + if (data.match(irssi_style_regex)){ + data = data.replace(/\\x03/gm, '\x03') + data = data.replace(/\\n/gm, '\n') + data = data.replace(irssi_style_regex, '') + data = data.replace(/"\s*$/, '') + } + + // not a colorcode + if (!data.match(/\x03/)) + return exports.import_text(); + + var json = colorcode_to_json(data) + + canvas.resize(json.w, json.h) + canvas.clear() + + for (var y = 0, line; line = json.lines[y]; y++){ + var row = canvas.aa[y] + for (var x = 0, char; char = line[x]; x++){ + var lex = row[x] + lex.char = String.fromCharCode(char.value) + lex.fg = char.fg + lex.bg = char.bg + lex.opacity = 1 + lex.build() + } + } + + }, import_text: function () { var data = import_textarea.value - lines = data.split("\n") + var lines = data.split("\n") var width = lines.reduce(function(a,b){ console.log(a,b); return Math.max(a, b.length) }, 0) var height = lines.length if (width > 200) { diff --git a/js/parse.js b/js/parse.js new file mode 100644 index 0000000..8697afd --- /dev/null +++ b/js/parse.js @@ -0,0 +1,131 @@ +(function(){ + +var char_color = '\x03'; +var regexp_color = /(^[\d]{1,2})?(?:,([\d]{1,2}))?/; + +var style_chars = { + '\x02': 'bold', + '\x1d': 'italic', + '\x1f': 'underline', + '\x0f': 'reset', + '\x16': 'inverse' +}; + +var Style = function(style){ + this.b = style.b; + this.i = style.i; + this.u = style.u; + this.fg = style.fg; + this.bg = style.bg; +}; + +var style_fns = {}; + +style_fns.bold = function(style){ style.b = !style.b }; + +style_fns.italic = function(style){ style.i = !style.i }; + +style_fns.underline = function(style){ style.u = !style.u }; + +style_fns.inverse = function(style){ + var tmp = style.fg; + style.fg = style.bg; + style.bg = tmp; +}; + +style_fns.reset = function(style, base_style){ + style.b = base_style.b; + style.i = base_style.i; + style.u = base_style.u; + style.fg = base_style.fg; + style.bg = base_style.bg; +}; + +var colorcode_to_json = function(string, opts){ + opts = opts || {}; + var d = colorcode_to_json.defaults; + + var base_style = { + b: "b" in opts ? opts.b : d.b, + i: "i" in opts ? opts.i : d.i, + u: "u" in opts ? opts.u : d.u, + fg: "fg" in opts ? opts.fg : d.fg, + bg: "bg" in opts ? opts.bg : d.bg + }; + + var lines_in = string.split(/\r?\n/); + var lines_out = []; + var w = 0, h = 0; + + for (var i=0; i