96314445b98aed86a7ac5a6c587b4f7f2b10976f.svn-base 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. if (!dojo._hasResource["dojox.dtl.filter.strings"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dojox.dtl.filter.strings"] = true;
  7. dojo.provide("dojox.dtl.filter.strings");
  8. dojo.require("dojox.dtl.filter.htmlstrings");
  9. dojo.require("dojox.string.sprintf");
  10. dojo.require("dojox.string.tokenize");
  11. dojo.mixin(dojox.dtl.filter.strings, {
  12. addslashes : function(value) {
  13. // summary: Adds slashes - useful for passing strings to JavaScript,
  14. // for example.
  15. return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(
  16. /'/g, "\\'");
  17. },
  18. capfirst : function(value) {
  19. // summary: Capitalizes the first character of the value
  20. value = "" + value;
  21. return value.charAt(0).toUpperCase() + value.substring(1);
  22. },
  23. center : function(value, arg) {
  24. // summary: Centers the value in a field of a given width
  25. arg = arg || value.length;
  26. value = value + "";
  27. var diff = arg - value.length;
  28. if (diff % 2) {
  29. value = value + " ";
  30. diff -= 1;
  31. }
  32. for (var i = 0; i < diff; i += 2) {
  33. value = " " + value + " ";
  34. }
  35. return value;
  36. },
  37. cut : function(value, arg) {
  38. // summary: Removes all values of arg from the given string
  39. arg = arg + "" || "";
  40. value = value + "";
  41. return value.replace(new RegExp(arg, "g"), "");
  42. },
  43. _fix_ampersands : /&(?!(\w+|#\d+);)/g,
  44. fix_ampersands : function(value) {
  45. // summary: Replaces ampersands with ``&amp;`` entities
  46. return value.replace(dojox.dtl.filter.strings._fix_ampersands,
  47. "&amp;");
  48. },
  49. floatformat : function(value, arg) {
  50. // summary: Format a number according to arg
  51. // description:
  52. // If called without an argument, displays a floating point
  53. // number as 34.2 -- but only if there's a point to be displayed.
  54. // With a positive numeric argument, it displays that many decimal
  55. // places
  56. // always.
  57. // With a negative numeric argument, it will display that many
  58. // decimal
  59. // places -- but only if there's places to be displayed.
  60. arg = parseInt(arg || -1);
  61. value = parseFloat(value);
  62. var m = value - value.toFixed(0);
  63. if (!m && arg < 0) {
  64. return value.toFixed();
  65. }
  66. value = value.toFixed(Math.abs(arg));
  67. return (arg < 0) ? parseFloat(value) + "" : value;
  68. },
  69. iriencode : function(value) {
  70. return dojox.dtl.text.urlquote(value, "/#%[]=:;$&()+,!");
  71. },
  72. linenumbers : function(value) {
  73. // summary: Displays text with line numbers
  74. var df = dojox.dtl.filter;
  75. var lines = value.split("\n");
  76. var output = [];
  77. var width = (lines.length + "").length;
  78. for (var i = 0, line; i < lines.length; i++) {
  79. line = lines[i];
  80. output.push(df.strings.ljust(i + 1, width) + ". "
  81. + df.htmlstrings.escape(line));
  82. }
  83. return output.join("\n");
  84. },
  85. ljust : function(value, arg) {
  86. value = value + "";
  87. arg = parseInt(arg);
  88. while (value.length < arg) {
  89. value = value + " ";
  90. }
  91. return value;
  92. },
  93. lower : function(value) {
  94. // summary: Converts a string into all lowercase
  95. return (value + "").toLowerCase();
  96. },
  97. make_list : function(value) {
  98. // summary:
  99. // Returns the value turned into a list. For an integer, it's a list
  100. // of
  101. // digits. For a string, it's a list of characters.
  102. var output = [];
  103. if (typeof value == "number") {
  104. value = value + "";
  105. }
  106. if (value.charAt) {
  107. for (var i = 0; i < value.length; i++) {
  108. output.push(value.charAt(i));
  109. }
  110. return output;
  111. }
  112. if (typeof value == "object") {
  113. for (var key in value) {
  114. output.push(value[key]);
  115. }
  116. return output;
  117. }
  118. return [];
  119. },
  120. rjust : function(value, arg) {
  121. value = value + "";
  122. arg = parseInt(arg);
  123. while (value.length < arg) {
  124. value = " " + value;
  125. }
  126. return value;
  127. },
  128. slugify : function(value) {
  129. // summary: Converts to lowercase, removes
  130. // non-alpha chars and converts spaces to hyphens
  131. value = value.replace(/[^\w\s-]/g, "").toLowerCase();
  132. return value.replace(/[\-\s]+/g, "-");
  133. },
  134. _strings : {},
  135. stringformat : function(value, arg) {
  136. // summary:
  137. // Formats the variable according to the argument, a string
  138. // formatting specifier.
  139. // This specifier uses Python string formating syntax, with the
  140. // exception that
  141. // the leading "%" is dropped.
  142. arg = "" + arg;
  143. var strings = dojox.dtl.filter.strings._strings;
  144. if (!strings[arg]) {
  145. strings[arg] = new dojox.string.sprintf.Formatter("%" + arg);
  146. }
  147. return strings[arg].format(value);
  148. },
  149. title : function(value) {
  150. // summary: Converts a string into titlecase
  151. var last, title = "";
  152. for (var i = 0, current; i < value.length; i++) {
  153. current = value.charAt(i);
  154. if (last == " " || last == "\n" || last == "\t" || !last) {
  155. title += current.toUpperCase();
  156. } else {
  157. title += current.toLowerCase();
  158. }
  159. last = current;
  160. }
  161. return title;
  162. },
  163. _truncatewords : /[ \n\r\t]/,
  164. truncatewords : function(value, arg) {
  165. // summary: Truncates a string after a certain number of words
  166. // arg: Integer
  167. // Number of words to truncate after
  168. arg = parseInt(arg);
  169. if (!arg) {
  170. return value;
  171. }
  172. for (var i = 0, j = value.length, count = 0, current, last; i < value.length; i++) {
  173. current = value.charAt(i);
  174. if (dojox.dtl.filter.strings._truncatewords.test(last)) {
  175. if (!dojox.dtl.filter.strings._truncatewords.test(current)) {
  176. ++count;
  177. if (count == arg) {
  178. return value.substring(0, j + 1);
  179. }
  180. }
  181. } else if (!dojox.dtl.filter.strings._truncatewords
  182. .test(current)) {
  183. j = i;
  184. }
  185. last = current;
  186. }
  187. return value;
  188. },
  189. _truncate_words : /(&.*?;|<.*?>|(\w[\w-]*))/g,
  190. _truncate_tag : /<(\/)?([^ ]+?)(?: (\/)| .*?)?>/,
  191. _truncate_singlets : {
  192. br : true,
  193. col : true,
  194. link : true,
  195. base : true,
  196. img : true,
  197. param : true,
  198. area : true,
  199. hr : true,
  200. input : true
  201. },
  202. truncatewords_html : function(value, arg) {
  203. arg = parseInt(arg);
  204. if (arg <= 0) {
  205. return "";
  206. }
  207. var strings = dojox.dtl.filter.strings;
  208. var words = 0;
  209. var open = [];
  210. var output = dojox.string.tokenize(value, strings._truncate_words,
  211. function(all, word) {
  212. if (word) {
  213. // It's an actual non-HTML word
  214. ++words;
  215. if (words < arg) {
  216. return word;
  217. } else if (words == arg) {
  218. return word + " ...";
  219. }
  220. }
  221. // Check for tag
  222. var tag = all.match(strings._truncate_tag);
  223. if (!tag || words >= arg) {
  224. // Don't worry about non tags or tags after our
  225. // truncate point
  226. return;
  227. }
  228. var closing = tag[1];
  229. var tagname = tag[2].toLowerCase();
  230. var selfclosing = tag[3];
  231. if (closing || strings._truncate_singlets[tagname]) {
  232. } else if (closing) {
  233. var i = dojo.indexOf(open, tagname);
  234. if (i != -1) {
  235. open = open.slice(i + 1);
  236. }
  237. } else {
  238. open.unshift(tagname);
  239. }
  240. return all;
  241. }).join("");
  242. output = output.replace(/\s+$/g, "");
  243. for (var i = 0, tag; tag = open[i]; i++) {
  244. output += "</" + tag + ">";
  245. }
  246. return output;
  247. },
  248. upper : function(value) {
  249. return value.toUpperCase();
  250. },
  251. urlencode : function(value) {
  252. return dojox.dtl.text.urlquote(value);
  253. },
  254. _urlize : /^((?:[(>]|&lt;)*)(.*?)((?:[.,)>\n]|&gt;)*)$/,
  255. _urlize2 : /^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$/,
  256. urlize : function(value) {
  257. return dojox.dtl.filter.strings.urlizetrunc(value);
  258. },
  259. urlizetrunc : function(value, arg) {
  260. arg = parseInt(arg);
  261. return dojox.string.tokenize(value, /(\S+)/g, function(word) {
  262. var matches = dojox.dtl.filter.strings._urlize.exec(word);
  263. if (!matches) {
  264. return word;
  265. }
  266. var lead = matches[1];
  267. var middle = matches[2];
  268. var trail = matches[3];
  269. var startsWww = middle.indexOf("www.") == 0;
  270. var hasAt = middle.indexOf("@") != -1;
  271. var hasColon = middle.indexOf(":") != -1;
  272. var startsHttp = middle.indexOf("http://") == 0;
  273. var startsHttps = middle.indexOf("https://") == 0;
  274. var firstAlpha = /[a-zA-Z0-9]/.test(middle.charAt(0));
  275. var last4 = middle.substring(middle.length - 4);
  276. var trimmed = middle;
  277. if (arg > 3) {
  278. trimmed = trimmed.substring(0, arg - 3) + "...";
  279. }
  280. if (startsWww
  281. || (!hasAt && !startsHttp && middle.length
  282. && firstAlpha && (last4 == ".org"
  283. || last4 == ".net" || last4 == ".com"))) {
  284. return '<a href="http://' + middle + '" rel="nofollow">'
  285. + trimmed + '</a>';
  286. } else if (startsHttp || startsHttps) {
  287. return '<a href="' + middle + '" rel="nofollow">' + trimmed
  288. + '</a>';
  289. } else if (hasAt && !startsWww && !hasColon
  290. && dojox.dtl.filter.strings._urlize2.test(middle)) {
  291. return '<a href="mailto:' + middle + '">' + middle + '</a>';
  292. }
  293. return word;
  294. }).join("");
  295. },
  296. wordcount : function(value) {
  297. return dojox.dtl.text.pySplit(value).length;
  298. },
  299. wordwrap : function(value, arg) {
  300. arg = parseInt(arg);
  301. // summary: Wraps words at specified line length
  302. var output = [];
  303. var parts = value.split(/ /g);
  304. if (parts.length) {
  305. var word = parts.shift();
  306. output.push(word);
  307. var pos = word.length - word.lastIndexOf("\n") - 1;
  308. for (var i = 0; i < parts.length; i++) {
  309. word = parts[i];
  310. if (word.indexOf("\n") != -1) {
  311. var lines = word.split(/\n/g);
  312. } else {
  313. var lines = [word];
  314. }
  315. pos += lines[0].length + 1;
  316. if (arg && pos > arg) {
  317. output.push("\n");
  318. pos = lines[lines.length - 1].length;
  319. } else {
  320. output.push(" ");
  321. if (lines.length > 1) {
  322. pos = lines[lines.length - 1].length;
  323. }
  324. }
  325. output.push(word);
  326. }
  327. }
  328. return output.join("");
  329. }
  330. });
  331. }