WIP: Note editing, markdown to html
[oweals/karmaworld.git] / karmaworld / apps / wysihtml5 / static / wysihtml5 / wysihtml-0.4.17 / src / lang / string.js
1 (function() {
2   var WHITE_SPACE_START = /^\s+/,
3       WHITE_SPACE_END   = /\s+$/,
4       ENTITY_REG_EXP    = /[&<>\t"]/g,
5       ENTITY_MAP = {
6         '&': '&amp;',
7         '<': '&lt;',
8         '>': '&gt;',
9         '"': "&quot;",
10         '\t':"&nbsp; "
11       };
12   wysihtml5.lang.string = function(str) {
13     str = String(str);
14     return {
15       /**
16        * @example
17        *    wysihtml5.lang.string("   foo   ").trim();
18        *    // => "foo"
19        */
20       trim: function() {
21         return str.replace(WHITE_SPACE_START, "").replace(WHITE_SPACE_END, "");
22       },
23
24       /**
25        * @example
26        *    wysihtml5.lang.string("Hello #{name}").interpolate({ name: "Christopher" });
27        *    // => "Hello Christopher"
28        */
29       interpolate: function(vars) {
30         for (var i in vars) {
31           str = this.replace("#{" + i + "}").by(vars[i]);
32         }
33         return str;
34       },
35
36       /**
37        * @example
38        *    wysihtml5.lang.string("Hello Tom").replace("Tom").with("Hans");
39        *    // => "Hello Hans"
40        */
41       replace: function(search) {
42         return {
43           by: function(replace) {
44             return str.split(search).join(replace);
45           }
46         };
47       },
48
49       /**
50        * @example
51        *    wysihtml5.lang.string("hello<br>").escapeHTML();
52        *    // => "hello&lt;br&gt;"
53        */
54       escapeHTML: function(linebreaks, convertSpaces) {
55         var html = str.replace(ENTITY_REG_EXP, function(c) { return ENTITY_MAP[c]; });
56         if (linebreaks) {
57           html = html.replace(/(?:\r\n|\r|\n)/g, '<br />');
58         }
59         if (convertSpaces) {
60           html = html.replace(/  /gi, "&nbsp; ");
61         }
62         return html;
63       }
64     };
65   };
66 })();