snowindy/scriptlet4docx

Unable to use quotes in expressions

jobinbasani opened this issue · 7 comments

I'm trying to do a String comparison operation and show/hide text in a template. The code is as below:

<% if(P_KEY == "ARF") { %>

Display ARF Text

<% } else { %>

Display non-ARF text

<% } %>

This throws an exception as below:

groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed: 
GStringTemplateScript16.groovy: 2: Invalid variable name. Must start with a letter but was: ?ARF? 
. At [2:395]  @ line 2, column 395. 
   cd6c79a8e03"""; if(P_KEY == ?ARF?) { 
                                 ^ 

1 error 

        at groovy.text.GStringTemplateEngine$GStringTemplate.<init>(GStringTemplateEngine.java:190) 
        at groovy.text.GStringTemplateEngine.createTemplate(GStringTemplateEngine.java:105) 
        at groovy.text.TemplateEngine.createTemplate(TemplateEngine.java:38) 
        at org.scriptlet4docx.docx.DocxTemplater.processCleanedTemplate(DocxTemplater.java:158) 
        at org.scriptlet4docx.docx.DocxTemplater.process(DocxTemplater.java:255) 
        at org.scriptlet4docx.docx.DocxTemplater.processAndReturnInputStream(DocxTemplater.java:213) 
        at org.scriptlet4docx.docx.DocxTemplater.process(DocxTemplater.java:230) 

I tried to use def key="ARF" and then use the variable to compare, but that also didnt work.
I'm using v0.7.6
Can you please take a look?

In the commit bb0f435, looks like you took care of <, > and ", but looks like quotes are converted to ?. So the replace function may not work correctly. < and > were escaped to &lt; and &gt; and the replace method will correctly put them back. Since " is not converted to &quot;, but ? instead, replace function wont work.
Hope this helps you...just started learning Groovy a day ago...I dont know the correct solution to this :)

Thank you for the sharing this. I've got your point about quotes escaping.
Thing is, I don't have MS word at my PC, so it would be helpful if you could attach your docx with example.

Hello!
As I unzip your docx document and go look into document.xml, I can see that:
you used symbol “ (hex code 93) instead of normally used symbol " (hex code 22).

Could you try using the quote symbol I provided for this template and see how it goes?

// Probably I will try to handle your symbol (93) as well, in next release. Is there an apostrophe symbol with different code you may use?

Just tried to copy-paste the quotation mark you provided in the expression and it works!

But if I try to type in the quotation mark from my laptop keyboard, it fails. Looks like my keyboard only generates code 93.

Can you please include this symbol also?

Yep I figured the symbols out. All of them should be treated as ".

«»“”‘’

Fixed in 0.8.0. version will appear in maven central within several days.