Skip to content

Commit

Permalink
Merge pull request #13 from semion1956/master
Browse files Browse the repository at this point in the history
Formatting structured text
  • Loading branch information
semion1956 committed Jan 11, 2015
2 parents d3c8203 + 25c6294 commit 4e293b1
Show file tree
Hide file tree
Showing 19 changed files with 1,096 additions and 0 deletions.
51 changes: 51 additions & 0 deletions format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
define([
"./stt/breadcrumb",
"./stt/comma",
"./stt/custom",
"./stt/email",
"./stt/filepath",
"./stt/formula",
"./stt/sql",
"./stt/underscore",
"./stt/url",
"./stt/word",
"./stt/xpath"
], function (breadcrumb, comma, custom, email, filepath, formula, sql, underscore, url, word, xpath) {

function getHandler(type) { //jshint maxcomplexity: 15
switch (type) {
case "breadcrumb" :
return breadcrumb;
case "comma" :
return comma;
case "email" :
return email;
case "filepath" :
return filepath;
case "formula" :
return formula;
case "sql" :
return sql;
case "underscore" :
return underscore;
case "url" :
return url;
case "word" :
return word;
case "xpath" :
return xpath;
default:
return custom;
}
}

return {
getString: function (text, type, args, isRtl, locale) {
return getHandler(type).format(text, args, isRtl, false, locale);
},

getHtml: function (text, type, args, isRtl, locale) {
return getHandler(type).format(text, args, isRtl, true, locale);
}
};
});
106 changes: 106 additions & 0 deletions misc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
define([], function () {

var isBidiLocale = function (locale) {
var lang = !locale ? "" : locale.split("-")[0];
if (!lang || lang.length < 2) {
return false;
}
return ["iw", "he", "ar", "fa", "ur"].some(function (bidiLang) {
return bidiLang === lang;
});
};
var LRE = "\u202A";
var RLE = "\u202B";
var PDF = "\u202C";
var LRM = "\u200E";
var RLM = "\u200F";
var LRO = "\u202D";
var RLO = "\u202E";

return {
LRE: LRE,
RLE: RLE,
PDF: PDF,
LRM: LRM,
RLM: RLM,
LRO: LRO,
RLO: RLO,

getLocaleDetails: function (locale) {
if (!locale) {
locale = typeof navigator === "undefined" ? "" :
(navigator.language ||
navigator.userLanguage ||
"");
}
locale = locale.toLowerCase();
if (isBidiLocale(locale)) {
var full = locale.split("-");
return {lang: full[0], country: full[1] ? full[1] : ""};
}
return {lang: "not-bidi"};
},

removeUcc: function (text) {
if (text) {
return text.replace(/[\u200E\u200F\u202A-\u202E]/g, "");
}
return text;
},

getDirection: function (text, dir, guiDir, checkEnd) {
if (dir !== "auto" && (/^(rtl|ltr)$/i).test(dir)) {
return dir;
}
guiDir = (/^(rtl|ltr)$/i).test(guiDir) ? guiDir : "ltr";
var txt = !checkEnd ? text : text.split("").reverse().join("");
var fdc = /[A-Za-z\u05d0-\u065f\u066a-\u06ef\u06fa-\u07ff\ufb1d-\ufdff\ufe70-\ufefc]/.exec(txt);
return fdc ? (fdc[0] <= "z" ? "ltr" : "rtl") : guiDir;
},

hasArabicChar: function (text) {
var fdc = /[\u0600-\u065f\u066a-\u06ef\u06fa-\u07ff\ufb1d-\ufdff\ufe70-\ufefc]/.exec(text);
return !!fdc;
},

showMarks: function (text, guiDir) { //jshint maxcomplexity: 15
var result = "";
for (var i = 0; i < text.length; i++) {
var c = "" + text.charAt(i);
switch (c) {
case LRM:
result += "<LRM>";
break;
case RLM:
result += "<RLM>";
break;
case LRE:
result += "<LRE>";
break;
case RLE:
result += "<RLE>";
break;
case LRO:
result += "<LRO>";
break;
case RLO:
result += "<RLO>";
break;
case PDF:
result += "<PDF>";
break;
default:
result += c;
}
}
var mark = typeof(guiDir) === "undefined" || !((/^(rtl|ltr)$/i).test(guiDir)) ? "" :
guiDir === "rtl" ? RLO : LRO;
return mark + result + (mark === "" ? "" : PDF);
},

hideMarks: function (text) {
var txt = text.replace(/<LRM>/g, LRM).replace(/<RLM>/g, RLM).replace(/<LRE>/g, LRE);
return txt.replace(/<RLE>/g, RLE).replace(/<LRO>/g, LRO).replace(/<RLO>/g, RLO).replace(/<PDF>/g, PDF);
}
};
});
29 changes: 29 additions & 0 deletions stt/breadcrumb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
define(["./internal/stext"], function (stext) {

return {
format: function (text, args, isRtl, isHtml, locale) {

return stext.displayStructure(text,
{
guiDir: isRtl ? "rtl" : "ltr",
dir: args.dir ? args.dir : isRtl ? "rtl" : "ltr",
subs: {
content: ">",
continued: true,
subDir: isRtl ? "rtl" : "ltr"
},
cases: [{
args: {
subs: {
content: "<",
continued: true,
subDir: isRtl ? "ltr" : "rtl"
}
}
}
]
},
!!isHtml, locale);
}
};
});
14 changes: 14 additions & 0 deletions stt/comma.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
define(["./internal/stext"], function (stext) {

return {
format: function (text, args, isRtl, isHtml, locale) {
return stext.displayStructure(text,
{
guiDir: isRtl ? "rtl" : "ltr",
dir: "ltr",
points: ","
},
!!isHtml, locale);
}
};
});
14 changes: 14 additions & 0 deletions stt/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
define(["./internal/stext"], function (stext) {

return {
format: function (text, args, isRtl, isHtml, locale) {
var hArgs = {};
for (var prop in args) {
hArgs[prop] = args[prop];
}
hArgs.guiDir = isRtl ? "rtl" : "ltr";
hArgs.dir = args.dir ? args.dir : hArgs.guiDir;
return stext.displayStructure(text, hArgs, !!isHtml, locale);
}
};
});
40 changes: 40 additions & 0 deletions stt/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
define(["./internal/stext", "./handlers/common", "../misc"], function (stext, handler, misc) {
function getDir(text, locale) {
if (misc.getLocaleDetails(locale).lang !== "ar") {
return "ltr";
}
var ind = text.indexOf("@");
if (ind > 0 && ind < text.length - 1) {
return misc.hasArabicChar(text.substring(ind + 1)) ? "rtl" : "ltr";
}
return "ltr";
}

return {
format: function (text, args, isRtl, isHtml, locale) {
return stext.displayStructure(text,
{
guiDir: isRtl ? "rtl" : "ltr",
dir: getDir(text, locale),
points: "<>.:,;@",
cases: [{
handler: handler,
args: {
bounds: [{
startAfter: "\"",
endBefore: "\""
},
{
startAfter: "(",
endBefore: ")"
}
],
points: ""
}
}
]
},
!!isHtml, locale);
}
};
});
17 changes: 17 additions & 0 deletions stt/filepath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
define(["./internal/stext", "../misc"], function (stext, misc) {

return {
format: function (text, args, isRtl, isHtml, locale) {
if (!isHtml) {
text = misc.removeUcc(text);
}
return stext.displayStructure(text,
{
guiDir: isRtl ? "rtl" : "ltr",
dir: "ltr",
points: "/\\:."
},
!!isHtml, locale);
}
};
});
15 changes: 15 additions & 0 deletions stt/formula.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
define(["./internal/stext"], function (stext) {

return {
format: function (text, args, isRtl, isHtml, locale) {

return stext.displayStructure(text,
{
guiDir: isRtl ? "rtl" : "ltr",
dir: "ltr",
points: " /%^&[]<>=!?~:.,|()+-*{}",
},
!!isHtml, locale);
}
};
});
33 changes: 33 additions & 0 deletions stt/handlers/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
define(["./tools"], function (tools) {

return {
handle: function (content, segments, args, locale) {
var cases = [];
if (Array.isArray(args.cases)) {
cases = args.cases;
}
var points = [];
if (typeof(args.points) !== "undefined") {
if (Array.isArray(args.points)) {
points = args.points;
} else if (typeof(args.points) === "string") {
points = args.points.split("");
}
}
var subs = {};
if (typeof(args.subs) === "object") {
subs = args.subs;
}
var aBounds = [];
if (Array.isArray(args.bounds)) {
aBounds = args.bounds;
}

tools.handleBounds(segments, args, aBounds, content, locale);
tools.handleSubcontents(segments, args, subs, content, locale);
tools.handleCases(segments, args, cases, content, locale);
tools.handlePoints(segments, args, points, content, locale);
return segments;
}
};
});
Loading

0 comments on commit 4e293b1

Please sign in to comment.