[gitg/wip/techlivezh/diff-view-clean-up: 18/21] Extract to 'exec_template' function
- From: Techlive Zheng <techlivezh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gitg/wip/techlivezh/diff-view-clean-up: 18/21] Extract to 'exec_template' function
- Date: Sat, 21 Dec 2013 15:52:45 +0000 (UTC)
commit a4400c6c542ce299b6afd33aadb56121d483db2d
Author: Techlive Zheng <techlivezheng gmail com>
Date: Thu Dec 19 09:35:32 2013 +0800
Extract to 'exec_template' function
libgitg/resources/diff-view-html-builder.js | 48 ++++++++++-----------------
1 files changed, 18 insertions(+), 30 deletions(-)
---
diff --git a/libgitg/resources/diff-view-html-builder.js b/libgitg/resources/diff-view-html-builder.js
index 2405801..9e00224 100644
--- a/libgitg/resources/diff-view-html-builder.js
+++ b/libgitg/resources/diff-view-html-builder.js
@@ -8,6 +8,22 @@ function html_escape(s)
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
+function exec_template(template, replacements) {
+ for (var r in replacements)
+ {
+ // As we are using the repl in the later 'template.replace()'
+ // as the replacement in which character '$' is special, we
+ // need to make sure each occurence of '$' character in the
+ // replacement is represented as '$$' (which stands for a
+ // literal '$'), so, we need to use '$$$$' here to get '$$'.
+ var replacement = replacements[r].replace(/\$/g, '$$$$');
+ var placeholder = new RegExp('<!-- \\$\\{' + r + '\\} -->', 'g');
+ template = template.replace(placeholder, replacement);
+ }
+
+ return template;
+}
+
function diff_file(file, lnstate, data)
{
var added = 0;
@@ -132,42 +148,15 @@ function diff_file(file, lnstate, data)
/**
* run template
*/
- var template = data.file_template;
- var repls = {
+ return exec_template(data.file_template, {
'FILE_PATH': file_path,
'FILE_BODY': file_body,
'FILE_STATS': file_stats,
- };
-
- for (var r in repls)
- {
- // As we are using the repl in the later 'template.replace()'
- // as the replacement in which character '$' is special, we
- // need to make sure each occurence of '$' character in the
- // replacement is represented as '$$' (which stands for a
- // literal '$'), so, we need to use '$$$$' here to get '$$'.
- var repl = repls[r].replace(/\$/g, '$$$$');
- template = template.replace(lnstate.replacements[r], repl);
- }
-
- return template;
+ });
}
function diff_files(files, lines, maxlines, data)
{
- var repl = [
- 'FILE_PATH',
- 'FILE_BODY',
- 'FILE_STATS'
- ];
-
- var replacements = {};
-
- for (var r in repl)
- {
- replacements[repl[r]] = new RegExp('<!-- \\$\\{' + repl[r] + '\\} -->', 'g');
- }
-
var lnstate = {
lines: lines,
maxlines: maxlines,
@@ -176,7 +165,6 @@ function diff_files(files, lines, maxlines, data)
tickfreq: 0.01,
gutterdots: new Array(maxlines.toString().length + 1).join('.'),
stagebutton: '',
- replacements: replacements,
};
if (data.settings.staged || data.settings.unstaged)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]