Jump to content

Superscript tag inside ReplaceSubstring rule


MeeshKB

Recommended Posts

I currently have this rule which replaces the word "LEED" with "LEED®".

 

var s = Rule("Fit Name Degree Designation");
s = ReplaceSubstring(s, "LEED", "LEED<®>");
return s;

The one tweak I need to make is to superscript the Registered Trademark symbol. Just not sure of the syntax for doing that within the ReplaceSubstring expression.

 

Could someone please point me in the right direction? Thanks so much!

Link to comment
Share on other sites

You need to use superscript tags. Edits made in red:

var s = Rule("Fit Name Degree Designation");
s = ReplaceSubstring(s, "LEED", "LEED<[color="Red"]superscript>[/color]®[color="red"]</superscript[/color]>");
return s;

Link to comment
Share on other sites

  • 2 months later...

Hello,

I am running into the same issue, however the <superscript></superscript> tags don't seem to be working for me. It's obvious the code is working:

 

return Field("Disclaimer").replace(/SM/g,<superscript>SM</superscript>);

 

because I can replace the SM with anything else, and it does change it, however, does not make is superscript.

 

I've tried changing fonts to see if that made a difference, but it has not. I am running 9.1.0 on Windows.

 

Any help would be greatly appreciated!

Link to comment
Share on other sites

  • 1 year later...
You need to use superscript tags. Edits made in red:

var s = Rule("Fit Name Degree Designation");
s = ReplaceSubstring(s, "LEED", "LEED<[color="Red"]superscript>[/color]®[color="red"]</superscript[/color]>");
return s;

 

Hello, I have a customer who sends us documents that are plastered with disclaimer characters (ex. +, ^, etc.) that they expect us to change to superscript. Currently we highlight each disclaimer character in the text frame and click the "sup" button. I attempted javascript to remedy this with the code provided here modified for my purposes. Is it possible to locate and replace characters within a text frame to superscript? Thank you. Here is my attempt:

 

FusionPro.thisRuleReturnsTaggedText = true;

var myFrame = FindTextFrame("FullBody");

myFrame = ReplaceSubstring(myFrame, "+", "<superscript>+</superscript>");

return myFrame;

Link to comment
Share on other sites

Hello, I have a customer who sends us documents that are plastered with disclaimer characters (ex. +, ^, etc.) that they expect us to change to superscript. Currently we highlight each disclaimer character in the text frame and click the "sup" button.

You could try putting this in your OnRecordStart rule:

var chars = [
 '\\^',
 '\\+',
 // Add as many as you like
];

var text = FindTextFrame('FullBody').content
 .replace(/<variable name="([^"]*)">/g, 
   function(s,p) { 
     return FieldOrRule(p);
   })
 .replace(new RegExp('(' + chars.join('|') + ')', 'g'), 
   '<superscript>$1</superscript>');

FindTextFrame('FullBody').content = text;

 

The above code will superscript all occurrences of +/^ in a text frame named "FullBody." You can add additional characters to be superscripted to the 'chars' array. Keep in mind that both plus signs and carets have special meanings in regular expressions so they must be escaped.

Link to comment
Share on other sites

When I add in "†" and "‡" characters the rule doesn't seem to move them, also is there a way to make a similar rule to replace place holders? For example "<<first>>" being automatically replaced with "Field("First Name")"? Edited by VernF88
Link to comment
Share on other sites

var chars = {
// Find:        Replace:
 '\\^':        '<superscript>^</superscript>',
 '\\+':        '<superscript>+</superscript>',
 '<<first>>':  Field("First Name"),
}

var text = FindTextFrame('FullBody').content
 .replace(/<variable name="([^"]*)">/g, 
   function(s,p) { 
     return FieldOrRule(p);
   });

for (var i in chars)
 text = text.replace(new RegExp(i, 'g'), chars[i]);

FindTextFrame('FullBody').content = text;

Link to comment
Share on other sites

var chars = {
// Find:        Replace:
 '\\^':        '<superscript>^</superscript>',
 '\\+':        '<superscript>+</superscript>',
 '<<first>>':  Field("First Name"),
}

var text = FindTextFrame('FullBody').content
 .replace(/<variable name="([^"]*)">/g, 
   function(s,p) { 
     return FieldOrRule(p);
   });

for (var i in chars)
 text = text.replace(new RegExp(i, 'g'), chars[i]);

FindTextFrame('FullBody').content = text;

 

Here's what I've got after completing what I would want done in the format above:

var chars = {

// Find:        Replace:

 '\\^':        '<superscript>^</superscript>',

 '\\+':        '<superscript>+</superscript>',

 '\\†':        '<superscript>†</superscript>',

 '\\‡':        '<superscript>‡</superscript>',     

 '<<first>>':  Field("First Name"),

 '<<last>>':    Field("Last Name"),

 '<<year>>':    Field("Year"),

 '<<make>>':    Field("Make"),

 '<<model>>':   Field("Model"),

 '<<model2>>':  Field("Model2"),

 '<<model3>>':  Field("Model3"),

 '<<V1>>':      Field("V1"),

 '<<V2>>':      Field("V2"),

 '<<V3>>':      Rule("V3R"),

 '<<V4>>':      Rule("V4R"),

 '<<V5>>':      Field("V5"),

 '<<more>>':    Rule("MoreR"),

 '<<actual>>':  Rule("ActualR"),

 '<<winno>>':   Field("WinNo"),

}



var text = FindTextFrame('FullBody').content

 .replace(/<variable name="([^"]*)">/g, 

   function(s,p) { 

     return FieldOrRule(p);

   });



for (var i in chars)

 text = text.replace(new RegExp(i, 'g'), chars[i]);



FindTextFrame('FullBody').content = text;

The only thing that is affecting inside of the text frame is the "+" and "^" characters. Am I keying something wrong?

Edited by VernF88
Link to comment
Share on other sites

Okay for troubleshooting purposes I input 'first' instead of '<<first>>'. It then inserted the field, so it's the <<>> characters causing it to not work properly. Still not sure why the "†" and "‡" characters are not going to superscript though.
Link to comment
Share on other sites

You could try putting this in your OnRecordStart rule:

var chars = [
 '\\^',
 '\\+',
 // Add as many as you like
];

var text = FindTextFrame('FullBody').content
 .replace(/<variable name="([^"]*)">/g, 
   function(s,p) { 
     return FieldOrRule(p);
   })
 .replace(new RegExp('(' + chars.join('|') + ')', 'g'), 
   '<superscript>$1</superscript>');

FindTextFrame('FullBody').content = text;

 

The above code will superscript all occurrences of +/^ in a text frame named "FullBody." You can add additional characters to be superscripted to the 'chars' array. Keep in mind that both plus signs and carets have special meanings in regular expressions so they must be escaped.

Or you could just do this:

var frame = FindTextFrame('FullBody');
frame.content = frame.content.replace(/([\^\+]+)/g, '<superscript>$1</superscript>');

Also, why are you doing this?

  .replace(/<variable name="([^"]*)">/g, 
   function(s,p) { 
     return FieldOrRule(p);
   })

Link to comment
Share on other sites

When I add in "†" and "‡" characters the rule doesn't seem to move them, also is there a way to make a similar rule to replace place holders? For example "<<first>>" being automatically replaced with "Field("First Name")"?

Where are these "place holders" coming from? Is this from a Data Merge in InDesign? If so, then in FusionPro 10, those will automatically be converted to variables when you export.

Link to comment
Share on other sites

Or you could just do this:

var frame = FindTextFrame('FullBody');
frame.content = frame.content.replace(/([\^\+]+)/g, '<superscript>$1</superscript>');

Also, why are you doing this?

 

Hello Dan, Currently we've been attempting this:

var chars = {
// Find:        Replace:
 '\\^':        '<superscript>^</superscript>',
 '\\+':        '<superscript>+</superscript>',
 '\\†':        '<superscript>†</superscript>',
 '\\‡':        '<superscript>‡</superscript>',     
 '<<first>>':   Field("First Name"),
 '<<last>>':    Field("Last Name"),
 '<<year>>':    Field("Year"),
 '<<make>>':    Field("Make"),
 '<<model>>':   Field("Model"),
 '<<model2>>':  Field("Model2"),
 '<<model3>>':  Field("Model3"),
 '<<V1>>':      Field("V1"),
 '<<V2>>':      Field("V2"),
 '<<V3>>':      Rule("V3R"),
 '<<V4>>':      Rule("V4R"),
 '<<V5>>':      Field("V5"),
 '<<more>>':    Rule("MoreR"),
 '<<actual>>':  Rule("ActualR"),
 '<<winno>>':   Field("WinNo"),
}
var text = FindTextFrame('FullBody').content
 .replace(/<variable name="([^"]*)">/g, 
   function(s,p) { 
     return FieldOrRule(p);
   });
for (var i in chars)
 text = text.replace(new RegExp(i, 'g'), chars[i]);
FindTextFrame('FullBody').content = text;

The rub is that the field's aren't filling because of the "<<>>" on each side of the space holder. Also the alt characters "†" and "‡" aren't being replaced with superscript.

Link to comment
Share on other sites

Also, why are you doing this?

 

I converted the '<variable name="...">' tags to the actual strings that they're supposed to return before replacing the symbols in case a field or rule being returned in the text frame contains one of the symbols.

Link to comment
Share on other sites

Okay for troubleshooting purposes I input 'first' instead of '<<first>>'. It then inserted the field, so it's the <<>> characters causing it to not work properly. Still not sure why the "†" and "‡" characters are not going to superscript though.

 

Maybe you could post the template and data that you're working with so that we could take a closer look?

 

My guess is that your template is converting those special characters to their HTML entities. Meaning the < is converted to < and the > is converted to >. So your find pattern is unable to come up with a match. I imagine the same is true for the daggers. If that assumption is true, this will probably work for you:

var chars = {
// Find:        Replace:
 '\\^':        '<superscript>^</superscript>',
 '\\+':        '<superscript>+</superscript>',
 '[color="red"]†[/color]':   '<superscript>†</superscript>',
 '[color="Red"]‡[/color]':   '<superscript>‡</superscript>',     
 '<<first>>':   Field("First Name"),
 '<<last>>':    Field("Last Name"),
 '<<year>>':    Field("Year"),
 '<<make>>':    Field("Make"),
 '<<model>>':   Field("Model"),
 '<<model2>>':  Field("Model2"),
 '<<model3>>':  Field("Model3"),
 '<<V1>>':      Field("V1"),
 '<<V2>>':      Field("V2"),
 '<<V3>>':      Rule("V3R"),
 '<<V4>>':      Rule("V4R"),
 '<<V5>>':      Field("V5"),
 '<<more>>':    Rule("MoreR"),
 '<<actual>>':  Rule("ActualR"),
 '<<winno>>':   Field("WinNo"),
}
var text = FindTextFrame('FullBody').content
 .replace(/<variable name="([^"]*)">/g, 
   function(s,p) { 
     return FieldOrRule(p);
   })
[color="red"] .replace(/&([lg])t;/g, 
   function(s,p) { 
     return p == 'l' ? '<' : '>';
   });[/color]

for (var i in chars)
 text = text.replace(new RegExp(i, 'g'), chars[i]);

FindTextFrame('FullBody').content = text;

Link to comment
Share on other sites

The only other thing I'm struggling on with this is trying to get something like this to work:

 '<<actual>>++':  Rule("ActualR1"),
 '<<actual>>+':  Rule("ActualR2"),
 '<<actual>>†':  Rule("ActualR3"),
 '<<actual>>‡':  Rule("ActualR4"),

The reason being is the "actual" field in the letter is 36 point, so the superscript doesn't go up high enough. I made 4 rules applying each occurrence of the character options at the appropriate superscript height. It won't recognize this as text within the frame though. Thank You!

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...