Add new macros for print templates.
authorphasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sun, 17 Apr 2011 19:25:38 +0000 (19:25 +0000)
committerphasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sun, 17 Apr 2011 19:25:38 +0000 (19:25 +0000)
%-TRIM%
Trims whitespace before the macro

%TRIM-%
Trims whitespace after the macro

%SUBSTR(#)%...%SUBSTR_END%
Take substring starting at position # to end of string.
If # is negative count backwards from end of string.

%SUBSTR(#,#)%...%SUBSTR_END%
Same as previous, but limit to second provided number characters after start point.
If second number is negative, count backwards instead of forwards.

TRIM macros inside of SUBSTR will be replaced first, then SUBSTR, then TRIM outside of SUBSTR.

Author: Thomas Berezansky <tsbere@mvlc.org>
Signed-off-by: Thomas Berezansky <tsbere@mvlc.org>
Signed-off-by: Jason Etheridge <jason@esilibrary.com>

git-svn-id: svn://svn.open-ils.org/ILS/trunk@20137 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/xul/staff_client/chrome/content/util/print.js

index 8030de8..aa1608a 100644 (file)
@@ -292,6 +292,35 @@ util.print.prototype = {
                 }
             } catch(E) { dump(E+'\n'); }
 
+            // Substrings
+            try {
+                var match;
+                // Pre-trim inside of substrings, and only inside of them
+                // This keeps the trim commands from being truncated
+                var substr_trim_patt=/(%SUBSTR\(-?\d+,?\s*(-?\d+)?\)%.*?)(\s*%-TRIM%|%TRIM-%\s*)(.*?%SUBSTR_END%)/;
+                while(match = substr_trim_patt.exec(s))
+                    s = s.replace(match[0], match[1] + match[4]);
+                // Then do the substrings themselves
+                var substr_patt=/%SUBSTR\((-?\d+),?\s*(-?\d+)?\)%(.*?)%SUBSTR_END%/;
+                while(match = substr_patt.exec(s)) {
+                    var substring_start = parseInt(match[1]);
+                    if(substring_start < 0) substring_start = match[3].length + substring_start;
+                    var substring_length = parseInt(match[2]);
+                    if(substring_length > 0)
+                        s = s.replace(match[0], match[3].substring(substring_start, substring_start + substring_length));
+                    else if(substring_length < 0)
+                        s = s.replace(match[0], match[3].substring(substring_start + substring_length, substring_start));
+                    else
+                        s = s.replace(match[0], match[3].substring(substring_start));
+                }
+            } catch(E) { dump(E+'\n'); }
+
+            // Cleanup unwanted whitespace
+            try {
+                s = s.replace(/%TRIM-%\s*/g,'');
+                s = s.replace(/\s*%-TRIM%/g,'');
+            } catch(E) { dump(E+'\n'); }
+
             return s;
         } catch(E) {
             alert('Error in print.js, template_sub(): ' + E);