127 lines
2.9 KiB
JavaScript
127 lines
2.9 KiB
JavaScript
|
/*
|
||
|
* © Mikko Ahlroth 2014
|
||
|
* WeeCRApp is open source software. For licensing information, please check
|
||
|
* the LICENCE file.
|
||
|
*/
|
||
|
|
||
|
// Utilities for parsing WeeChat color codes sent
|
||
|
// in the relay protocol
|
||
|
|
||
|
.pragma library
|
||
|
|
||
|
// Protocol specific constants
|
||
|
// Starting bytes
|
||
|
var COLOR_START = 0x19;
|
||
|
var SET_ATTR = 0x1A;
|
||
|
var REMOVE_ATTR = 0x1B;
|
||
|
var RESET = 0x1C;
|
||
|
|
||
|
var START_RE = /^(\u0019|\u001a|\u001b|\u001c)/;
|
||
|
|
||
|
// Attributes
|
||
|
var ATTR_BOLD = '*';
|
||
|
var ATTR_REVERSE = '!';
|
||
|
var ATTR_ITALIC = '/';
|
||
|
var ATTR_UNDERLINE = '_';
|
||
|
var ATTR_KEEP = '|';
|
||
|
|
||
|
// A section of text which has the given attributes:
|
||
|
// fgColor: Foreground color as weechat color number
|
||
|
// bgColor: Background color --//--
|
||
|
// attributes: List of attributes from the selection above,
|
||
|
// except ATTR_KEEP.
|
||
|
function CodedTextSection(str, fgColor, bgColor, attributes) {
|
||
|
this.str = str;
|
||
|
this.fgColor = fgColor;
|
||
|
this.bgColor = bgColor;
|
||
|
this.attributes = attributes;
|
||
|
}
|
||
|
|
||
|
// Parse coded text and return list of CodedTextSection
|
||
|
function parseString(str) {
|
||
|
var out = [];
|
||
|
var currentMode = null;
|
||
|
var currentFg = null;
|
||
|
var currentBg = null;
|
||
|
var currentAttr = [];
|
||
|
var currentStr = null;
|
||
|
var parts = str.split(START_RE);
|
||
|
|
||
|
for (var i = 0; i < parts.length; ++i) {
|
||
|
// Every even index is a content part
|
||
|
if ((i % 2) === 0) {
|
||
|
// The first part won't have any extra info
|
||
|
if (i !== 0) {
|
||
|
switch (currentMode) {
|
||
|
case COLOR_START:
|
||
|
currentStr = parseColorStart(parts[i], currentFg, currentBg, currentAttr);
|
||
|
break;
|
||
|
|
||
|
case SET_ATTR:
|
||
|
currentStr = parseSetAttr(parts[i], currentFg, currentBg, currentAttr);
|
||
|
break;
|
||
|
|
||
|
case REMOVE_ATTR:
|
||
|
currentStr = parseRemoveAttr(parts[i], currentFg, currentBg, currentAttr);
|
||
|
break;
|
||
|
|
||
|
case RESET:
|
||
|
currentFg = null;
|
||
|
currentBg = null;
|
||
|
currentAttr = null;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
out.push(new CodedTextSection(currentStr, currentFg, currentBg, currentAttr));
|
||
|
}
|
||
|
// The odd parts give the next mode
|
||
|
else {
|
||
|
currentMode = parts[i];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return out;
|
||
|
}
|
||
|
|
||
|
function parseColorStart(part, currentFg, currentBg, currentAttr) {
|
||
|
var data = null;
|
||
|
|
||
|
if (part[0] === 'F') {
|
||
|
data = parseASTD(part);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function parseSetAttr(part, currentFg, currentBg, currentAttr) {
|
||
|
|
||
|
}
|
||
|
|
||
|
function parseRemoveAttr(part, currentFg, currentBg, currentAttr) {
|
||
|
|
||
|
}
|
||
|
|
||
|
// Parse STD (standard color), removing it from the beginning of string
|
||
|
function parseSTD(str) {
|
||
|
|
||
|
}
|
||
|
|
||
|
// Parse EXT (extended color), --//--
|
||
|
function parseEXT(str) {
|
||
|
|
||
|
}
|
||
|
|
||
|
// Parse attributes + STD
|
||
|
function parseASTD(str) {
|
||
|
|
||
|
}
|
||
|
|
||
|
// Parse attributes + EXT
|
||
|
function parseAEXT(str) {
|
||
|
|
||
|
}
|
||
|
|
||
|
// Parse single attribute
|
||
|
function parseATTR(str) {
|
||
|
|
||
|
}
|