[gitg/wip/techlivezh/bug/720886: 5/7] Extract to 'exec_template' function



commit 7f91367e7b7c0a3d1b0df9c2175c828d3e4dded7
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 af1dad9..2f8447d 100644
--- a/libgitg/resources/diff-view-html-builder.js
+++ b/libgitg/resources/diff-view-html-builder.js
@@ -3,6 +3,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_hunk(hunk, lnstate, data)
 {
        var hunk_body = '';
@@ -122,44 +138,17 @@ function diff_file(file, lnstate, data)
 
        file_stats = lnstate.stagebutton + file_stats;
 
-       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 f = '';
 
-       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,
@@ -168,7 +157,6 @@ function diff_files(files, lines, maxlines, data)
                nexttick: 0,
                tickfreq: 0.01,
                stagebutton: '',
-               replacements: replacements,
        };
 
        if (data.settings.staged || data.settings.unstaged)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]