Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Formatting Text

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old July 12th, 2016, 12:06 PM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Smart Quotes in Rich Text Editor and Plain Text Editor

I am having issues with perfecting this smart quote javascript. I have a template using both plain and rich text. I have successfully gotten the smart quotes to show in the correct direction a majority of the time.

One issue is when an apostrophe is used as the first character in the field, both in a plain text and rich text field.

The second issue is when quotes the first character in the rich text editor only.


My code is:

Rich Text Rule
Code:
function replaceFunction(field)
{
    // replace quotes at the beginning or end of the entire string first
    field = field.replace(/^\"(?=[\w|$][^<>]*<)/, "&ldquor;");
    field = field.replace(/^\'(?=[\w|$][^<>]*<)/, "&lsquor;");
    field = field.replace(/\"$(?=[^<>]*<)/, "&rdquor;");
    field = field.replace(/\'$(?=[^<>]*<)/, "&rsquor;");
 
    // now replace quotes before or inside words
    field = field.replace(/\s"(?=[\w|$][^<>]*<)/g, "&nbsp;&ldquor;");
    field = field.replace(/\s'(?=[\w|$][^<>]*<)/g, "&nbsp;&lsquor;");
 
    // finally, replace all other quotes (after words)
    field = field.replace(/\"(?=[^<>]*<)/g, "&rdquor;");
    field = field.replace(/\'(?=[^<>]*<)/g, "&rsquor;");
 
    return field;
}

if (Field("Body") != "")
    return replaceFunction(Feild("Body"))
    else
    return "";

Plain Text Rule
Code:
function replaceFunction(field)
{
    // replace quotes at the beginning or end of the entire string first
    field = field.replace(/^\"/, "&ldquor;");
    field = field.replace(/^\'/, "&lsquor;");
    field = field.replace(/\"$/, "&rdquor;");
    field = field.replace(/\'$/, "&rsquor;");
 
    // now replace quotes before or inside words
    field = field.replace(/\s"(?=[\w|$])/g, "&nbsp;&ldquor;");
    field = field.replace(/\s'(?=[\w|$])/g, "&nbsp;&lsquor;");

    // finally, replace all other quotes (after words)
    field = field.replace(/\"/g, "&rdquor;");
    field = field.replace(/\'/g, "&rsquor;");
 
    return field;
}

if (Field("Headline") != "")
    return replaceFunction(Field("Headline"))
    else
    return "";


I tested modifying the codes a few ways but don't really know how to right the correct fix. I added a line to the "now replace quotes before or inside words" but wasn't able to get it right exactly.

During testing, I could add the following line to the replace quotes before or inside words to fix the double quotes:
Code:
    field = field.replace(/\"(?=[\w|$][^<>]*<)/g, "&ldquor;");
, but I can't do this for the single quote because it changes the direction of an apostrophe (&rsquor;) to a left single quote.




Can someone help me as I am not very good at creating javascript code?

Thanks!
Reply With Quote
  #2  
Old July 15th, 2016, 01:18 PM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Smart Quotes in Rich Text Editor and Plain Text Editor

How can I write that I only want to change an apostrophe if it's surrounded by other letters? I'm guessing the any character code is $ but I'm not sure.

would it be field = field.replace(/\$'$(?=[\w|$])/g, "&rsquor;");
Reply With Quote
  #3  
Old July 19th, 2016, 02:18 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,320
Default Re: Smart Quotes in Rich Text Editor and Plain Text Editor

Quote:
Originally Posted by jl_arnold View Post
How can I write that I only want to change an apostrophe if it's surrounded by other letters? I'm guessing the any character code is $ but I'm not sure.

would it be field = field.replace(/\$'$(?=[\w|$])/g, "&rsquor;");
There's no need to guess. Everything you ever wanted to know about JavaScript Regular Expressions and more can be found here:
https://developer.mozilla.org/en-US/...ar_Expressions

The "any character" character in a Regular Expression is actually the dot, or period, character.

However, in this case, I think the distinction you want to make is whether the apostrophe is next to a "word" character, not any character. For most purposes, a word character is matched by \w in a Regular Expression.

Also, besides the Mozilla reference, the other great resources that you have at your fingertips for help with JavaScript are search engines such as Google, and programming knowledge and Q&A sites such as Stack Overflow.

A Google search for "JavaScript replace with smart quotes" turns up lots of hits, including this one:
http://codereview.stackexchange.com/...nd-apostrophes

Cribbing from the answer there:
http://codereview.stackexchange.com/a/77177

We get this:
Code:
field = field.replace(/"([^"]*)"/g, "“$1”").replace(/(\w)'(\w)/g, "$1’$2").replace(/'([^']*)'/g, "‘$1’");
Which I believe meets the requirements.

Note that I'm simply using actual curly quote characters here instead of markup entities such as "&rsquor;". If you use entities, you need to check the "Treat returned strings as tagged text" box for the rule, but then you also need to call functions such as TaggedTextFromRaw on the parts of the data that you aren't replacing, to make sure that things like literal ampersands appear correctly in the output. You could call TaggedTextFromRaw on the whole thing first, then replace the &apos; and &quot; entities that result, but that gets even more complicated when you need to figure out whether an apostrophe is in the middle of a word or not. It's better to leave the format of the data (either tagged or non-tagged "raw" text) alone.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #4  
Old July 20th, 2016, 06:25 AM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Smart Quotes in Rich Text Editor and Plain Text Editor

Thanks Dan!

With this single line of code, I replaced my original 8 lines for the rule associated to the plain text editor.

I have found this doesn't work 100% with the rich text editor. The quotes are converting to the curly smart quotes, but it's not applying the font. The font type seems to be unicode character.

The font I'm using is very much a rounded curly quote, which appears when entering text in a plain text field. But in the Rich Text Editor, the quotes appear blocky and not rounded.

I tried editing the code by using the markup entities, such as "&rsquor;". I used RawTextfromTagged("rsquor;") + "$1" + RawTextfromTagged("&rsquor;") but that did not fix the issue with the font style of the quotes.

Can you also provided this code for the Rich Text Editor?

Thanks!
Reply With Quote
  #5  
Old July 20th, 2016, 10:07 AM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,320
Default Re: Smart Quotes in Rich Text Editor and Plain Text Editor

Quote:
Originally Posted by jl_arnold View Post
Thanks Dan!

With this single line of code, I replaced my original 8 lines for the rule associated to the plain text editor.
Great!
Quote:
Originally Posted by jl_arnold View Post
I have found this doesn't work 100% with the rich text editor. The quotes are converting to the curly smart quotes, but it's not applying the font. The font type seems to be unicode character.

The font I'm using is very much a rounded curly quote, which appears when entering text in a plain text field. But in the Rich Text Editor, the quotes appear blocky and not rounded.

I tried editing the code by using the markup entities, such as "&rsquor;". I used RawTextfromTagged("rsquor;") + "$1" + RawTextfromTagged("&rsquor;") but that did not fix the issue with the font style of the quotes.
I'm not sure I understand what is not appearing the way you want. A picture might be worth a thousand words here.
Quote:
Originally Posted by jl_arnold View Post
Can you also provided this code for the Rich Text Editor?
To what editor specifically are you referring? Do you mean an online editor in a web app such as MarcomCentral?
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #6  
Old July 20th, 2016, 10:39 AM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Smart Quotes in Rich Text Editor and Plain Text Editor

The image below shows the quotes correctly where the green background is, but the text has incorrect quotes. I also just noticed the font is not Promixa Nova which is used in the FusionPro template.





As for the Rich Text Editor, this is a feature in MarcomCentral that allows users to choose to bold certain text, or italicize, as well as center and left align. I have two smart quote rules, one when a plain text form field is used in MarcomCentral, and another for the rich text form fields. The JavaScript Regular Expression for the Rich Text Editor seems to ignore any content within a tag <>.

This extra part of the code contains data like (?=[^<>]*<) in one of the expressions I was previously using.

Does this make sense and help determine the code needed?
Reply With Quote
  #7  
Old July 20th, 2016, 10:40 AM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Smart Quotes in Rich Text Editor and Plain Text Editor

https://images.printable.com/imageli...c/1abk4uuy.pdf
Reply With Quote
  #8  
Old July 20th, 2016, 11:43 AM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,320
Default Re: Smart Quotes in Rich Text Editor and Plain Text Editor

Quote:
Originally Posted by jl_arnold View Post
The image below shows the quotes correctly where the green background is, but the text has incorrect quotes. I also just noticed the font is not Promixa Nova which is used in the FusionPro template.

Okay, thanks for the attachment. But I still don't see what you mean. All of the text in there seems to have the correct curly quotes applied. I've copied it from the PDF and pasted it here:
Quote:
“Please’s” ‘Work’s’
‘Please’s’ “Work’s”
“Please’s” ‘Work’s’ “Please’s”
‘Please’s’ “Work’s” ‘Please’s’
What specifically is not right about it?

Or how about this: What is the expected result, and exactly how is it different than the actual result?
Quote:
Originally Posted by jl_arnold View Post
As for the Rich Text Editor, this is a feature in MarcomCentral that allows users to choose to bold certain text, or italicize, as well as center and left align. I have two smart quote rules, one when a plain text form field is used in MarcomCentral, and another for the rich text form fields. The JavaScript Regular Expression for the Rich Text Editor seems to ignore any content within a tag <>.

This extra part of the code contains data like (?=[^<>]*<) in one of the expressions I was previously using.

Does this make sense and help determine the code needed?
Not really, sorry. If this part of the question is specific to MarcomCentral, it's generally more appropriate to post it in the sub-forum specific to the MarcomCentral web app. That said, when you use the RTE in MarcomCentral, it's generating tagged markup instead of "raw" flat-file data like is generally used in FP Desktop when designing templates. So, unlike your flat-file data, the data generated by Marcom probably already has entities such as &apos; in it. The best way to figure this out is to determine exactly what is in that MarcomCentral-generated data file. That's something that your BRM should be able to help with, or you can try asking in the MarcomCentral sub-forum.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #9  
Old July 20th, 2016, 12:05 PM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Smart Quotes in Rich Text Editor and Plain Text Editor

Dan,

The PDF does contain smart quotes in both sections, where the white text is 100% correct. This white text uses a plain text form field.

The black text is incorrect.

Downloading the PDF, you will see the text in white has rounded curly quotes. When you zoom in to the black text, it's squared smart quotes. The text color is also black, when it should be 80% black and the font type is Helvetica when it should be Proxima Nova A Light. This black text form field uses a third party "Rich Text Editor" within MarcomCentral to allow users in the storefront to make the font changes.

I'll post the Rich Text Editor question in the other forum you mentioned, but if you know what can be added to the code you originally provided to ignore any text found between and open tag (<) and closing tag (>), that would be extremely helpful.
Code:
field = field.replace(/"([^"]*)"/g, "“$1”").replace(/(\w)'(\w)/g, "$1’$2").replace(/'([^']*)'/g, "‘$1’");
Reply With Quote
  #10  
Old July 20th, 2016, 12:41 PM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Smart Quotes in Rich Text Editor and Plain Text Editor

Dan,

I am also including a composed PDF that uses the rule I previously had in place, to show you that the font type is being lost.



https://images.printable.com/imageli...c/cmiirg4e.pdf
Reply With Quote
Reply

Tags
smart quotes, typography

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 11:58 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.