TotalChoiceNate Posted September 8, 2017 Posted September 8, 2017 Hello all! I am working with a bit of a pain customer. When I enter certain extended characters in the MCC Storefront it either switches them with greek symbols or switches them back to a standard char set (The character in question is this special apostrophe that their style guide requires). The support workaround for this is to type in the entity with a replace substring rule, but the way that they set it up is on an individual field basis, which on some templates can get quite long! So, my question is a two-fold question: Is there a way to clean up the code below in to a single rule rather than having a separate line for each character?Is there a way to do this as a global replace substring rule versus having to list each field on its own line? Code in question: //The end-user needs to enter in entity values to access special characters. However the following is necessary as MCC tries to "help" //in converting entities. The following replace substring will fix the "help" provided so that you can actually return the entity typed in. FusionPro.Composition.AddVariable("Date and Time Information",ReplaceSubstring(Field("Date and Time Information"),"&","&"),true); FusionPro.Composition.AddVariable("Event Information",ReplaceSubstring(Field("Event Information"),"&","&"),true); FusionPro.Composition.AddVariable("Direction 1 Information",ReplaceSubstring(Field("Direction 1 Information"),"&","&"),true); FusionPro.Composition.AddVariable("Direction 2 Information",ReplaceSubstring(Field("Direction 2 Information"),"&","&"),true); FusionPro.Composition.AddVariable("Direction 3 Information",ReplaceSubstring(Field("Direction 3 Information"),"&","&"),true); FusionPro.Composition.AddVariable("Direction 4 Information",ReplaceSubstring(Field("Direction 4 Information"),"&","&"),true); FusionPro.Composition.AddVariable("Date and Time Information",ReplaceSubstring(Field("Date and Time Information"),"&rsquor","’"),true); FusionPro.Composition.AddVariable("Event Information",ReplaceSubstring(Field("Event Information"),"&rsquor","’"),true); FusionPro.Composition.AddVariable("Direction 1 Information",ReplaceSubstring(Field("Direction 1 Information"),"&rsquor","’"),true); FusionPro.Composition.AddVariable("Direction 2 Information",ReplaceSubstring(Field("Direction 2 Information"),"&rsquor","’"),true); FusionPro.Composition.AddVariable("Direction 3 Information",ReplaceSubstring(Field("Direction 3 Information"),"&rsquor","’"),true); FusionPro.Composition.AddVariable("Direction 4 Information",ReplaceSubstring(Field("Direction 4 Information"),"&rsquor","’"),true); FusionPro.Composition.AddVariable("Date and Time Information",ReplaceSubstring(Field("Date and Time Information"),"'","’"),true); FusionPro.Composition.AddVariable("Event Information",ReplaceSubstring(Field("Event Information"),"'","’"),true); FusionPro.Composition.AddVariable("Direction 1 Information",ReplaceSubstring(Field("Direction 1 Information"),"'","’"),true); FusionPro.Composition.AddVariable("Direction 2 Information",ReplaceSubstring(Field("Direction 2 Information"),"'","’"),true); FusionPro.Composition.AddVariable("Direction 3 Information",ReplaceSubstring(Field("Direction 3 Information"),"'","’"),true); FusionPro.Composition.AddVariable("Direction 4 Information",ReplaceSubstring(Field("Direction 4 Information"),"'","’"),true); Quote
step Posted September 8, 2017 Posted September 8, 2017 I think this might work: // An object of values to find as the keys, // and the string to replace them with as the value. var charMap = { // Find : Replace "&" : "&", "&rsquor" : "’", "'" : "’", } // Loop through all of the fields. for (var field in FusionPro.Fields) { // Loop through all of the find/replace values for current field. for (var find in charMap) { var replace = charMap[find]; FusionPro.Composition.AddVariable(field, ReplaceSubstring(Field(field), find, replace), true); } } Quote
TotalChoiceNate Posted September 8, 2017 Author Posted September 8, 2017 Thanks for the reply, in your code which of the values do I need to replace with names from my template, if any? Quote
Dan Korn Posted September 9, 2017 Posted September 9, 2017 (edited) Thanks for the reply, in your code which of the values do I need to replace with names from my template, if any? None. That's the beauty of the "for (var field in FusionPro.Fields)" code; it just iterates through all of the fields in the data, whatever their names are. Although, I think there's a way to fix at least part of the problem (entities such as "&" in the output) without doing that. I suspect that these "extended" characters are showing up correctly when the data fields are used directly in text frames in your job, and that the problem is related to when you're calling the Field function in rules. So if you replace the Field calls in your other rules with TaggedDataField, and possibly check the "Treat returned strings as tagged text" box in those rules as well, I think that will prevent the entities from appearing in your output. As for why MarcomCentral inserts "straight" quotes instead of "curly" quotes, that's a question for the MarcomCentral forum. Edited September 9, 2017 by Dan Korn Quote
TotalChoiceNate Posted September 12, 2017 Author Posted September 12, 2017 That's correct, this rule appears to be the perfect solution, but I'm not sure it is seaching the fields to replace characters as nothing is changing. Normally these quotes are used to specify an Alumni year and by using the class year as its own field it inserts the apostrophe fine (see below) if (Field("Class Year") == "") return ""; else return ' ’'+Field("Class Year"); But once they type the custom text (same characters) in to a storefront field it breaks. I replaced the rule within the template to what I have below and it doesn't seem to be working. // An object of values to find as the keys, // and the string to replace them with as the value. var charMap = { // Find : Replace "’":"’", "’":"'", "¼":"¼", "½":"½", "¾":"¾", "—":"–", "—":"—", } // Loop through all of the fields. for (var [color="red"]field[/color] in FusionPro.[color="red"]Fields[/color]) { // Loop through all of the find/replace values for current field. for (var find in charMap) { var replace = charMap[find]; FusionPro.Composition.AddVariable([color="red"]field[/color], ReplaceSubstring([color="Red"]Field[/color]([color="red"]field[/color]), find, replace), true); } } Just so I understand correctly, I should replace the red text with "TaggedDataField"? Currently this is the only rule that I have in the template and is an OnRecordStart callback which doesn't give me the option to treated returned strings as tagged text. Quote
TotalChoiceNate Posted September 12, 2017 Author Posted September 12, 2017 Are you able to move the thread to the MarcomCentral forum or should I create new? Quote
Dan Korn Posted September 12, 2017 Posted September 12, 2017 That's correct, this rule appears to be the perfect solution, but I'm not sure it is seaching the fields to replace characters as nothing is changing. What specifically is not changing? How is the data being brought into the template? Does it simply call out the data field in the text? Or is there another rule involved? But once they type the custom text (same characters) in to a storefront field it breaks. I replaced the rule within the template to what I have below and it doesn't seem to be working. It's getting hard to analyze specifically what's going on in your template just from what's been posted in this thread. At the very least, seeing what your output looks like could be helpful. The collected template would probably be more illuminating. Though a full analysis of the issue may only be possible by going through Support so that I (or someone else here at MarcomCentral) could fully examine the files being used for the composition. It could be a font issue, or something else that needs to be fixed in a different way. Just so I understand correctly, I should replace the red text with "TaggedDataField"? No. When I said, "replace the Field calls in your other rules with TaggedDataField," I meant, any other rules besides OnRecordStart. Currently this is the only rule that I have in the template Okay. So you mean those data field values are being brought in directly in text frames? In that case, you shouldn't really need to do anything. FusionPro should properly process those entities in the tagged markup data that MarcomCentral is generating. and is an OnRecordStart callback which doesn't give me the option to treated returned strings as tagged text. Right. The OnRecordStart rule doesn't actually return anything that affects the composition, so the option of whether to treat the return value as tagged is moot. The way you're telling FusionPro whether to treat the value as tagged is in that last parameter in the call to FusionPro.Composition.AddVariable. Setting it to true is equivalent to having a rule with the same name as the first parameter (the variable name) and checking that "Treat as tagged" box. Setting it to false is the same as leaving that box unchecked. Are you able to move the thread to the MarcomCentral forum or should I create new? You could create a new thread there just about the question about how MarcomCentral inserts the quotes into the data file it generates. This thread can be continued though. Quote
lance Posted September 14, 2017 Posted September 14, 2017 That's correct, this rule appears to be the perfect solution, but I'm not sure it is seaching the fields to replace characters as nothing is changing. Normally these quotes are used to specify an Alumni year and by using the class year as its own field it inserts the apostrophe fine (see below) if (Field("Class Year") == "") return ""; else return ' ’'+Field("Class Year"); But once they type the custom text (same characters) in to a storefront field it breaks. I replaced the rule within the template to what I have below and it doesn't seem to be working. // An object of values to find as the keys, // and the string to replace them with as the value. var charMap = { // Find : Replace "’":"’", "’":"'", "¼":"¼", "½":"½", "¾":"¾", "—":"–", "—":"—", } // Loop through all of the fields. for (var [color="red"]field[/color] in FusionPro.[color="red"]Fields[/color]) { // Loop through all of the find/replace values for current field. for (var find in charMap) { var replace = charMap[find]; FusionPro.Composition.AddVariable([color="red"]field[/color], ReplaceSubstring([color="Red"]Field[/color]([color="red"]field[/color]), find, replace), true); } } Just so I understand correctly, I should replace the red text with "TaggedDataField"? Currently this is the only rule that I have in the template and is an OnRecordStart callback which doesn't give me the option to treated returned strings as tagged text. I was playing around with this, and I was getting the same issue, where nothing appeared to change. I think the problem is that as it loops through each entry in the charMap it is re-adding a variable, overwriting everything it had done up to that point (i.e. the changes aren't cumulative from entry to entry). So really all it is doing is a ReplaceSubstring() on the very last entry in the charMap. If that entry doesn't result in a change, then it appears as nothing has changed. I think. Quote
step Posted September 15, 2017 Posted September 15, 2017 I think the problem is that as it loops through each entry in the charMap it is re-adding a variable, overwriting everything it had done up to that point (i.e. the changes aren't cumulative from entry to entry). So really all it is doing is a ReplaceSubstring() on the very last entry in the charMap. If that entry doesn't result in a change, then it appears as nothing has changed. I think. The ReplaceSubstring function is replacing each occurrence of the 'find' variable in each FusionPro field with the corresponding 'replace' variable defined in the 'charMap' object and then redefining the value of the field with the result. Here's a static example to show you how the code does replace more than just the last character in the 'charMap': var charMap = { "R": "F", "ond": "and", "L" : "S", "ov": "m", "s": "lls", "d": "", "ay": "eld", } var field = 'Everyone Loves Raymond'; for (var find in charMap) { var replace = charMap[find]; field = ReplaceSubstring(field, find, replace); } return field; // Everyone Smells Feldman As Dan mentioned in an earlier post, it is hard to say why it's not working for you without being able to see your template. Quote
lance Posted September 15, 2017 Posted September 15, 2017 // An object of values to find as the keys, // and the string to replace them with as the value. var charMap = { // Find : Replace "&" : "&", "&rsquor" : "’", "'" : "’", } // Loop through all of the fields. for (var field in FusionPro.Fields) { // Loop through all of the find/replace values for current field. for (var find in charMap) { var replace = charMap[find]; FusionPro.Composition.[color="red"]AddVariable[/color](field, ReplaceSubstring(Field(field), find, replace), true); } } The red highlighted part is what I am questioning. Isn't this just re-adding (overwriting) and re-defining the variable each iteration through the for loop? Quote
step Posted September 15, 2017 Posted September 15, 2017 Isn't this just re-adding (overwriting) and re-defining the variable each iteration through the for loop? Yes. But, in doing so it updates the value returned by the "Field" function. Let's assume you have a field in your data called "Salutation" and that field contains "Dear Lance." Naturally, that is too formal and you'd instead like to be addressed as "The Lance-meister": var salutation = ReplaceSubstring( Field("Salutation"), // Field("Salutation") will return "Dear Lance" "Lance", // This is the value to be replaced "The Lance-meister" // This is the new value ); // This line will update the value returned by the "Field" function // for the "Salutation" field. FusionPro.Composition.AddVariable("Salutation", salutation); // On second thought, "The Lance-meister" sounds a little too 1985. // Let's update that to "The Lance-inator" FusionPro.Composition.AddVariable("Salutation", ReplaceSubstring( Field("Salutation"), // Field("Salutation") returns "Dear The Lance-meister" "-meister", // This is the value to be replaced "-inator" // This is the new value )); // "Dear?" That's no way to address The Lance-inator. Let's fix that. FusionPro.Composition.AddVariable("Salutation", ReplaceSubstring( Field("Salutation"), // Field("Salutation") returns "Dear The Lance-inator" "Dear", // This is the value to be replaced "Sup" // This is the new value )); Obviously, you wouldn't really write the code like that but I just wanted to illustrate what the for loop is doing in the original code. The above example could be re-written like so: var charMap = { // Find : Replace "Lance" : "The Lance-meister", "-meister" : "-inator", "Dear" : "Sup", } for (var field in FusionPro.Fields) { for (var find in charMap) { var replace = charMap[find]; FusionPro.Composition.AddVariable(field, ReplaceSubstring(Field(field), find, replace), true); } } That being said, if you're skeptical of overwriting the field value with AddVariable, you could assign the values to a variable and only update the field value one time once all of the necessary replacements have been made: // An object of values to find as the keys, // and the string to replace them with as the value. var charMap = { // Find : Replace "’":"’", "’":"'", "¼":"¼", "½":"½", "¾":"¾", "—":"–", "—":"—", } // Loop through all of the fields. for (var field in FusionPro.Fields) { [color="red"]var fieldVal = Field(field);[/color] // Loop through all of the find/replace values for current field. for (var find in charMap) { var replace = charMap[find]; [color="red"]fieldVal = ReplaceSubstring(fieldVal, find, replace);[/color] } // Update the field's value [color="Red"]FusionPro.Composition.AddVariable(field, fieldVal, true);[/color] } Quote
lance Posted September 18, 2017 Posted September 18, 2017 Thanks for the in-depth explanation. I think I have a better understanding of what is supposed to be happening. Unfortunately, it still doesn't work. I tried each of the first two code examples in your post. It should work like so: Dear Lance > Dear The Lance-meister > Dear The Lance-inator > Sup The Lance-inator. However, what I actually get is Sup Lance. Only the last replacement in the charMap is applied. If I reorder the list, the output changes to whatever is in the last position. The third example you have, where the values are written to a variable within each loop, returns the correct result. But, there is another issue. None of the three examples work when the field is used within a rule, only when used directly as a variable in the Variable Text Editor (using Insert and picking from the popup list). So what I have in my test file right now is a datafile with two fields: name and suffix. I have a value of 'first_last' for the name field and a value of 'suffix' for the suffix field. Here are my rules: OnRecordStart: var charMap = { "first" : "FIRST", "last" : "LAST", } for (var field in FusionPro.Fields) { for (var find in charMap) { var replace = charMap[find]; FusionPro.Composition.AddVariable(field, ReplaceSubstring(Field(field), find, replace), true); } } So that should change 'first_last' to 'FIRST_LAST'. If I insert the name variable directly into the Variable Text Editor I end up with a result of 'first_LAST' (only the last charMap entry is applied). This is my second rule: var n = ""; var suffix = ""; if (Field("name") != "") n = Field("name"); if (n != "" && Field("suffix") != "") suffix = ", " + Field("suffix"); return n + suffix; That should return (when combined with the OnRecordStart rule) 'FIRST_LAST, suffix'. Instead it returns 'first_last, suffix' (not even the partially working replacement is applied). I am also attaching my collect files in case that helps. Thankstn000002.zip Quote
step Posted September 18, 2017 Posted September 18, 2017 Oh okay, thank you for attaching your collected template. I see what you're saying. Well this is kind of awkward. After I posted that whole missive about how "AddVariable" works, it turns out I don't know how it works. I think this will give you the results you're looking for, though: Redefine the "Field" function and do the find/replace within it so that calls to the "Field" function within rules return the desired value. Then in your OnRecordStart callback loop through all of the fields, and set their values to the result of the "Field" function which will perform the find/replace on fields inserted directly into text frames. JavaScript Globals: Field = function(str) { var res = FusionPro.Fields[str] || ''; // An object of values to find as the keys, and the string to replace them with as the value. var charMap = { // find : replace "first" : "FIRST", "last" : "LAST", } // Loop through all of the find/replace values for current field. for (var find in charMap) { var replace = charMap[find]; res = ReplaceSubstring(res, find, replace); } return res; } OnRecordStart: // Loop through all of the fields. for (var field in FusionPro.Fields) FusionPro.Composition.AddVariable(field, Field(field), true); Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.