5 ], function(locale, Handlebars, DiffMatchPatch) {
8 * Return a text as markdown.
9 * Currently only a little helper to replace apidoc-inline Links (#Group:Name).
10 * Should be replaced with a full markdown lib.
13 Handlebars.registerHelper('markdown', function(text) {
17 text = text.replace(/((\[(.*?)\])?\(#)((.+?):(.+?))(\))/mg, function(match, p1, p2, p3, p4, p5, p6) {
18 var link = p3 || p5 + '/' + p6;
19 return '<a href="#api-' + p5 + '-' + p6 + '">' + link + '</a>';
25 * start/stop timer for simple performance check.
28 Handlebars.registerHelper('startTimer', function(text) {
33 Handlebars.registerHelper('stopTimer', function(text) {
34 console.log(new Date() - timer);
39 * Return localized Text.
42 Handlebars.registerHelper('__', function(text) {
43 return locale.__(text);
50 Handlebars.registerHelper('cl', function(obj) {
56 * Replace underscore with space.
59 Handlebars.registerHelper('underscoreToSpace', function(text) {
60 return text.replace(/(_+)/g, ' ');
66 Handlebars.registerHelper('assign', function(name) {
67 if(arguments.length > 0) {
68 var type = typeof(arguments[1]);
70 if(type === 'string' || type === 'number' || type === 'boolean') arg = arguments[1];
71 Handlebars.registerHelper(name, function() { return arg; });
79 Handlebars.registerHelper('nl2br', function(text) {
80 return _handlebarsNewlineToBreak(text);
86 Handlebars.registerHelper('if_eq', function(context, options) {
87 var compare = context;
88 // Get length if context is an object
89 if (context instanceof Object && ! (options.hash.compare instanceof Object))
90 compare = Object.keys(context).length;
92 if (compare === options.hash.compare)
93 return options.fn(this);
95 return options.inverse(this);
101 Handlebars.registerHelper('if_gt', function(context, options) {
102 var compare = context;
103 // Get length if context is an object
104 if (context instanceof Object && ! (options.hash.compare instanceof Object))
105 compare = Object.keys(context).length;
107 if(compare > options.hash.compare)
108 return options.fn(this);
110 return options.inverse(this);
116 var templateCache = {};
117 Handlebars.registerHelper('subTemplate', function(name, sourceContext) {
118 if ( ! templateCache[name])
119 templateCache[name] = Handlebars.compile($('#template-' + name).html());
121 var template = templateCache[name];
122 var templateContext = $.extend({}, this, sourceContext.hash);
123 return new Handlebars.SafeString( template(templateContext) );
129 Handlebars.registerHelper('toLowerCase', function(value) {
130 return (value && typeof value === 'string') ? value.toLowerCase() : '';
136 Handlebars.registerHelper('splitFill', function(value, splitChar, fillChar) {
137 var splits = value.split(splitChar);
138 return new Array(splits.length).join(fillChar) + splits[splits.length - 1];
142 * Convert Newline to HTML-Break (nl2br).
144 * @param {String} text
147 function _handlebarsNewlineToBreak(text) {
148 return ('' + text).replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
154 Handlebars.registerHelper('each_compare_list_field', function(source, compare, options) {
155 var fieldName = options.hash.field;
158 source.forEach(function(entry) {
160 values['key'] = entry[fieldName];
161 newSource.push(values);
167 compare.forEach(function(entry) {
169 values['key'] = entry[fieldName];
170 newCompare.push(values);
173 return _handlebarsEachCompared('key', newSource, newCompare, options);
179 Handlebars.registerHelper('each_compare_keys', function(source, compare, options) {
182 var sourceFields = Object.keys(source);
183 sourceFields.forEach(function(name) {
185 values['value'] = source[name];
186 values['key'] = name;
187 newSource.push(values);
193 var compareFields = Object.keys(compare);
194 compareFields.forEach(function(name) {
196 values['value'] = compare[name];
197 values['key'] = name;
198 newCompare.push(values);
201 return _handlebarsEachCompared('key', newSource, newCompare, options);
207 Handlebars.registerHelper('each_compare_field', function(source, compare, options) {
208 return _handlebarsEachCompared('field', source, compare, options);
214 Handlebars.registerHelper('each_compare_title', function(source, compare, options) {
215 return _handlebarsEachCompared('title', source, compare, options);
221 Handlebars.registerHelper('reformat', function(source, type){
224 return JSON.stringify(JSON.parse(source.trim()),null, " ");
234 Handlebars.registerHelper('showDiff', function(source, compare, options) {
236 if(source === compare) {
245 var d = diffMatchPatch.diff_main(compare, source);
246 diffMatchPatch.diff_cleanupSemantic(d);
247 ds = diffMatchPatch.diff_prettyHtml(d);
248 ds = ds.replace(/¶/gm, '');
250 if(options === 'nl2br')
251 ds = _handlebarsNewlineToBreak(ds);
259 function _handlebarsEachCompared(fieldname, source, compare, options)
264 source.forEach(function(sourceEntry) {
267 compare.forEach(function(compareEntry) {
268 if(sourceEntry[fieldname] === compareEntry[fieldname]) {
272 compare: compareEntry,
294 compare.forEach(function(compareEntry) {
297 source.forEach(function(sourceEntry) {
298 if(sourceEntry[fieldname] === compareEntry[fieldname])
305 compare: compareEntry,
315 var length = dataList.length;
316 for (var index in dataList) {
317 if(index == (length - 1))
318 dataList[index]['_last'] = true;
319 ret = ret + options.fn(dataList[index]);
324 var diffMatchPatch = new DiffMatchPatch();
329 DiffMatchPatch.prototype.diff_prettyHtml = function(diffs) {
331 var pattern_amp = /&/g;
332 var pattern_lt = /</g;
333 var pattern_gt = />/g;
334 var pattern_para = /\n/g;
335 for (var x = 0; x < diffs.length; x++) {
336 var op = diffs[x][0]; // Operation (insert, delete, equal)
337 var data = diffs[x][1]; // Text of change.
338 var text = data.replace(pattern_amp, '&').replace(pattern_lt, '<')
339 .replace(pattern_gt, '>').replace(pattern_para, '¶<br>');
342 html[x] = '<ins>' + text + '</ins>';
345 html[x] = '<del>' + text + '</del>';
348 html[x] = '<span>' + text + '</span>';
352 return html.join('');