From 596dc052667d5bd405b5f5b5edd4e2eb7e55957b Mon Sep 17 00:00:00 2001 From: frisch-raphael Date: Wed, 26 Jul 2017 15:12:26 +0200 Subject: [PATCH] better xlst meta character cleaning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We grew frustrated that metacharacters would leave empty newline after report generation so we made some change to the xlst generation function so that : It leaves less unclosed tag (there shouldn't be any really but we never know) It shouldn't leave any empty paragraph tag behind. This has the big advantage that the metacharaters don't leave any unwanted newline after the report has been generated. We also documented which statements to be alone on a newline. This should be more or less the same than with the previous function, but users might have to sightly adjust their report. Thus, this pull request should be ¬ finding:::DREAD_SCORE > 1 ¬ µzzzµ needs to be alone on a newline end of condition (¥) needs to be on a newline end of for-each (∆) needs to be on a newline † DREAD_SCORE > 1 †' needs to be on a newline ƒcodeƒ OUTPUT needs to be on a newline kudos to 4B3l0 for diging into the dark art of word XML --- helpers/xslt_generation.rb | 126 +++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 53 deletions(-) diff --git a/helpers/xslt_generation.rb b/helpers/xslt_generation.rb index fdac54db..a013fb09 100644 --- a/helpers/xslt_generation.rb +++ b/helpers/xslt_generation.rb @@ -18,16 +18,18 @@ def initialize(errorString) def generate_xslt(docx) -# hardcoded stuff +# Initialize the xsl @top = ' + progid="Word.Document" ' +@bottom = '' document = "" debug = false @@ -61,12 +63,13 @@ def generate_xslt(docx) count = count + 1 next end - + + # Execute when between two Ω omega = compress(omega) # now, we replace omega with the real deal # - # + # # replace[count] = "" count = count + 1 @@ -93,17 +96,18 @@ def generate_xslt(docx) next end + # Execute when between two § omega = compress(omega) # now, we replace omega with the real deal - # - # + # + # # replace[count] = "" count = count + 1 end - # remove all the Ω and put the document back together + # remove all the § and put the document back together document = replace.join("") @@ -125,12 +129,14 @@ def generate_xslt(docx) next end + # Execute when between two π omega = compress(omega) replace[count] = "" count = count + 1 end + # remove all the π and put the document back together document = replace.join("") ########################### @@ -149,14 +155,15 @@ def generate_xslt(docx) count = count + 1 next end - + + # Execute when between two ∞ omega = compress(omega) replace[count] = "" - count = count + 1 end + # remove all the π and put the document back together document = replace.join("") ############################### @@ -181,6 +188,7 @@ def generate_xslt(docx) next end + # Execute when between two æ omega = compress(omega) if omega =~ /:::/ @@ -194,11 +202,11 @@ def generate_xslt(docx) q = "" conditions.each do |condition| - # add uppercase/lowercase to allow users to test for string matches (e.g. type='Database') - q << "" unless q.include?("" end q << " + # and every necessary x = replace[count-1].reverse.sub("#{q}".reverse).reverse replace[count-1] = x end @@ -216,6 +224,7 @@ def generate_xslt(docx) else #skip back to the previous TABLEROW x = replace[count-1].reverse.sub("" unless q.include?("" end else for_iffies.push(0) end - - # we need to search backwards for '' or '") - space = replace[count-1].rindex(" space - x = replace[count-1].reverse.sub("".reverse,"#{q}".reverse).reverse - replace[count-1] = x + + # Replace everything behind ¬ in the current paragraph for + # and every necessary + x = replace[count-1].sub(/]*?>((?]).)*$/,"#{q}") + replace[count-1] = x + + tagIndex = replace[count+1].rindex("") + chooseIndex = replace[count+1].rindex("µ") + if chooseIndex.nil? or tagIndex < chooseIndex + # if there isn't any µ before the end of the paragraph, delete the rest of the paragraph + replace[count+1] = replace[count+1].sub(/^<\/w:t>.*?<\/w:r>.*?<\/w:p>/, '') else - x = replace[count-1].reverse.sub("#{q}" - if document.include?("".reverse,"#{cs}".reverse).reverse + # Replace everything behind the first † in the current paragraph for + x = replace[count-1].sub(/]*?>((?]).)*$/,"") replace[count-1] = x - + # Remove the rest of the paragraph + replace[count+1] = replace[count+1].sub(/^<\/w:t>.*?<\/w:r>.*?<\/w:p>/, '') + replace[count]='' count = count + 1 end + + # remove all the † and put the document back together document = replace.join("") ########################### # ÷ - otherwise . Used in XSLT choose loops - document = document.gsub('÷',"") + + q = "" + document.each_line("÷"){ |a| + if a =~ /÷/ + # replace the first before a ÷ for + x = a.reverse.sub("".reverse,"".reverse).reverse + a = x.gsub('÷','') + end + + q << a + } + document = q ########################### # ¥ - ends an if statement @@ -340,7 +365,7 @@ def generate_xslt(docx) document.each_line("¥"){ |a| if subst x = "" - # we need to search forwards for '' + # Replace the first after a ¥ for x = a.sub("","") a = x subst = false @@ -385,9 +410,10 @@ def generate_xslt(docx) next end + # Execute when between two ƒ omega = compress(omega) - # we need to search backwards for '' or ' behind the first ƒ in the current paragraph for woutspace = replace[count-1].rindex("") space = replace[count-1].rindex("' or '") - space = replace[count-1].rindex(" space - x = replace[count-1].reverse.sub("".reverse,"".reverse).reverse - replace[count-1] = x - else - x = replace[count-1].reverse.sub("" + count = count + 1 end + # remove all the µ and put the document back together document = replace.join("") ############################### @@ -455,7 +472,7 @@ def generate_xslt(docx) document.each_line("å"){ |a| if subst x = "" - # we need to search forwards for '' + # Replace the first after a å for x = a.sub("","") a = x subst = false @@ -506,7 +523,7 @@ def generate_xslt(docx) document.each_line("≠"){ |a| if subst x = "" - # we need to search forwards for '' + # Replace the first after a ≠ for x = a.sub("","") a = x subst = false @@ -520,15 +537,15 @@ def generate_xslt(docx) } document = q - ############################### # ∆ - end for-each # add end if's end_ifs = '' r_for_iffies.each do |fi| + # Replace each paragraph containing a ∆ by the appropritate number of and a end_ifs = ""*fi - document = document.sub('∆',"#{end_ifs}") + document = document.sub(/]*?>((?]).)*∆<\/w:t>.*?<\/w:r>.*?<\/w:p>/,"#{end_ifs}") end ########################### @@ -542,6 +559,9 @@ def generate_xslt(docx) # final changes placed here document = white_space(document) + # add in xslt footer + document = document + @bottom + #return the xslt return document end