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

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old June 22nd, 2018, 07:55 AM
mjlongo mjlongo is offline
Junior Community Member
 
Join Date: Jul 2013
Location: Rochester NY
Posts: 37
Default Creating numerical lists from tagged data

Users input text into a variable data field. This data will include list items. Each new list row will be tagged with a "{" character. They will also have sublists that will be tagged with a "%" character. So for example: a user might type in some text that looks like this:

Code:
Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua Non odio euismod lacinia at quis risus. 
{Risus commodo viverra maecenas accumsan 
{lacus vel facilisis 
{consectetur adipiscing 
%elit sed do eiusmod tempor incididunt 
%ut labore et dolore magna aliqua on 
%odio euismod lacinia at quis risus.

The resulting data should end up looking like this when the data is processed:

Code:
Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua Non odio euismod lacinia at quis risus.      
     •Risus commodo viverra maecenas accumsan 
     • lacus vel facilisis 
     • consectetur adipiscing
          ° elit sed do eiusmod tempor incididunt 
          ° ut labore et dolore magna aliqua on 
          ° odio euismod lacinia at quis risus.

Using similar code as listed below, I am able to achieve the desired results.

Code:
var str = Field("PARAGRAPH1");

//Bullet Lines
str = str.replace(/({)([^{])/g,'<p override="true" hyphenate="false" tabstops="0;1250,Left,,;" lindent="2450"><t>&bull;&nbsp;&nbsp;$2');

return str;
My issue now is that the customer is wondering if instead of bullet points, could they have the first list be numbers and then the sublist be letters. I realize they can just type them in but it seems like a nice touch if this could be automated. The results would then look more like this:

Code:
Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua Non odio euismod lacinia at quis risus.      
     1. Risus commodo viverra maecenas accumsan 
     2. lacus vel facilisis 
     3.  consectetur adipiscing
          a. elit sed do eiusmod tempor incididunt 
          b. ut labore et dolore magna aliqua on 
          c. odio euismod lacinia at quis risus.

Any assistance with scripting this would be appreciated. Thanks!
Reply With Quote
  #2  
Old June 25th, 2018, 12:51 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,305
Default Re: Creating numerical lists from tagged data

You said that you got the desired results with "similar" code to what you listed. So my first task in answering the question is figuring out how to recreate something close to what you already had, which I did like this:
Code:
var text = "";
text += "Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor ";
text += "incididunt ut labore et dolore magna aliqua Non odio euismod lacinia at quis risus. ";
text += "{Risus commodo viverra maecenas accumsan ";
text += "{lacus vel facilisis ";
text += "{consectetur adipiscing ";
text += "%elit sed do eiusmod tempor incididunt ";
text += "%ut labore et dolore magna aliqua on ";
text += "%odio euismod lacinia at quis risus.";

//var str = Field("PARAGRAPH1");
var str = text;

//Bullet Lines
str = str.replace(/({)([^{])/g, '<p hyphenate="false" tabstops="0;R1250,Left,,;" lindent="2450"><t>&bull;&nbsp;&nbsp;$2');
str = str.replace(/(%)([^%])/g, '<p hyphenate="false" tabstops="0;R1250,Left,,;" lindent="2450"><t><t>&bull;&nbsp;&nbsp;$2');

return str;
(Note that you don't need "override=true" in the <p> tag. It's the default, and doesn't apply unless you have "br=false" anyway.)

The first step in making this work the way you want is to change the second parameter of each String.replace call to be a function (changing this code after the "Bullet Lines" comment):
Code:
str = str.replace(/({)([^{])/g, function(m, p1, p2) { return '<p hyphenate="false" tabstops="0;R1250,Left,,;" lindent="2450"><t>&bull;&nbsp;&nbsp;' + p2; } );
str = str.replace(/(%)([^%])/g, function(m, p1, p2) { return '<p hyphenate="false" tabstops="0;R1250,Left,,;" lindent="2450"><t><t>&bull;&nbsp;&nbsp;' + p2; } );
return str;
Now we can make that function do what we want:
Code:
var startNum1 = 1;
var startNum2 = 1;
str = str.replace(/({)([^{])/g, function(m, p1, p2) { return '<p hyphenate="false" tabstops="0;R1250,Left,,;" lindent="2450"><t>' + (startNum1++) + '.&nbsp;&nbsp;' + p2; } );
str = str.replace(/(%)([^%])/g, function(m, p1, p2) { return '<p hyphenate="false" tabstops="0;R1250,Left,,;" lindent="2450"><t><t>' + Chr(Asc('a') - 1 + startNum2++) + '.&nbsp;&nbsp;' + p2; } );
return str;
And that yields, I think, exactly what you want (though you may need to tweak the indents/tabs).

Now, this isn't a completely robust solution, and there are some issues. For one thing, it won't work right if you have multiple sections with the first-level indent, each of which has a list of the second-level indents below it, such as:
Code:
var text = "";
text += "Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor ";
text += "incididunt ut labore et dolore magna aliqua Non odio euismod lacinia at quis risus. ";
text += "{Risus commodo viverra maecenas accumsan ";
text += "{lacus vel facilisis ";
text += "{consectetur adipiscing ";
text += "%elit sed do eiusmod tempor incididunt ";
text += "%ut labore et dolore magna aliqua on ";
text += "%odio euismod lacinia at quis risus.";
text += "{second list: ";
text += "%bread ";
text += "%milk ";
text += "%eggs.";
That would be a little trickier to handle.
__________________
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
  #3  
Old June 26th, 2018, 08:36 AM
mjlongo mjlongo is offline
Junior Community Member
 
Join Date: Jul 2013
Location: Rochester NY
Posts: 37
Default Re: Creating numerical lists from tagged data

Dan,

Thank you very much. Worked perfectly. I was on the right track with function calls however I did not think to place the function where you did right in the same "str.replace" line.

There will be an overview for users on usage which will hopefully prevent the repeating of lists issue. If it becomes a necessity, I believe I could resolve the problem simply by using another character (such as a "}") for the new list.

I added the ability to choose either Bullets, Numbers/Letters or Roman Numerals. All three choices work great with the help of your code. I have included my entire code below for the benefit of this community. Please note that the triple-x letters within the roman numeral variables are automatically being switched by this forum to asterisk symbols. Must be something related to "adult" content. haha!

Thanks again!



Code:
var str = Field("PARAGRAPH1");

// Number and Letter list type variables
var startNum1 = 1;
var startNum2 = 1;

// roman numeral variables
var numeralCodesUP = [["","I","II","III","IV","V","VI","VII","VIII","IX"],         // Ones
                     ["","X","XX","***", "XL", "L", "LX", "LXX", "L***", "XC"],   // Tens
                     ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"]];        // Hundreds

var numeralCodesDN = [["","i","ii","iii","iv","v","vi","vii","viii","ix"],         // Ones
                     ["","x","xx","***", "xl", "l", "lx", "lxx", "l***", "xc"],   // Tens
                     ["","c","cc","ccc","cd","d","dc","dcc","dccc","cm"]];        // Hundreds

// Switch based on chosen list type
switch(Field("LISTTYPE")){

    case "BULLET":
    //  Bullet Lines - Use { to denote start
    str = str.replace(/({)([^{])/g,'<p hyphenate="false" tabstops="0;1250,Left,,;2450,Left,,;" lindent="2450"><t>&bull;<t>$2');

    // Circle Lines - Use % to denote start
    str = str.replace(/(%)([^%])/g,'<p hyphenate="false" tabstops="0;3400,Left,,;4700,Left,,;" lindent="4700" suboffset="32" subratio="100"><t><subscript>&cir;</subscript><t>$2');
    break;


    case "NUMBERS": 
    // Numbers Lines - Use { to denote start
    str = str.replace(/({)([^{])/g,function(m, p1, p2){return '<p hyphenate="false" tabstops="0;1250,Left,,;3700,Left,,;" lindent="3700"><t>' + (startNum1++) + '.<t>' + p2;});

    //Letter Lines - Use % to denote start
    str = str.replace(/(%)([^%])/g,function(m, p1, p2){return '<p tabstops="0;4400,Left,,;6500,Left,,;" lindent="6500"><t>' + Chr(Asc('a') - 1 + startNum2++) + '.<t>' + p2;});
    break;
    
    case "ROMAN":
    // Upper Roman Lines - Use { to denote start
    str = str.replace(/({)([^{])/g,function(m, p1, p2){return '<p hyphenate="false" tabstops="0;1250,Left,,;3700,Left,,;" lindent="3700"><t>' + (convert(startNum1++,1)) + '.<t>' + p2;});

    //Lower Roman Lines - Use % to denote start
    str = str.replace(/(%)([^%])/g,function(m, p1, p2){return '<p tabstops="0;4400,Left,,;6500,Left,,;" lindent="6500"><t>' + (convert(startNum2++,2)) + '.<t>' + p2;});
    break;
}

//regular Paragraph - Use # to denote start
str = str.replace(/(#)([^#])/g,'<br><br><p tabstops="0;0,Left,,;" lindent="0" rindent="0">$2');

// Function to convert to roman numerals
function convert(num,x) {
  var numeral = "";
  var digits = num.toString().split('').reverse();
  for (var i=0; i < digits.length; i++){
    if(x==1){numeral = numeralCodesUP[i][parseInt(digits[i])] + numeral;}
    else{numeral = numeralCodesDN[i][parseInt(digits[i])] + numeral;}
  }
  return numeral;  
}

return str;
Reply With Quote
Reply

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 01:08 PM.


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