From 82a8fe9649fe259cf71b61b1a345cc9419489564 Mon Sep 17 00:00:00 2001 From: phasefx Date: Sun, 17 Apr 2011 19:26:05 +0000 Subject: [PATCH] Add new macros for print templates. %-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 Signed-off-by: Thomas Berezansky Signed-off-by: Jason Etheridge git-svn-id: svn://svn.open-ils.org/ILS/branches/rel_2_1@20138 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../xul/staff_client/chrome/content/util/print.js | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Open-ILS/xul/staff_client/chrome/content/util/print.js b/Open-ILS/xul/staff_client/chrome/content/util/print.js index 8030de85da..aa1608a5a4 100644 --- a/Open-ILS/xul/staff_client/chrome/content/util/print.js +++ b/Open-ILS/xul/staff_client/chrome/content/util/print.js @@ -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); -- 2.11.0