[gitg/wip/techlivezh/diff-view-clean-up: 18/21] Extract to 'exec_template' function



commit c9e08ffc3e9cf109067c8e0b022cd72ebf244ca6
Author: Techlive Zheng <techlivezheng gmail com>
Date:   Thu Dec 19 09:35:32 2013 +0800

    Extract to 'exec_template' function
    
    https://bugzilla.gnome.org/show_bug.cgi?id=720886

 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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
 }
 
+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]