Jump to content

Creating numerical lists from tagged data


mjlongo

Recommended Posts

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:

 

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:

 

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.

 

var str = Field("PARAGRAPH1");

//Bullet Lines
str = str.replace(/({)([^{])/g,'<p override="true" hyphenate="false" tabstops="0;1250,Left,,;" lindent="2450"><t>•  $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:

 

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!

Link to comment
Share on other sites

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:

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>•  $2');
str = str.replace(/(%)([^%])/g, '<p hyphenate="false" tabstops="0;R1250,Left,,;" lindent="2450"><t><t>•  $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):

str = str.replace(/({)([^{])/g, function(m, p1, p2) { return '<p hyphenate="false" tabstops="0;R1250,Left,,;" lindent="2450"><t>•  ' + p2; } );
str = str.replace(/(%)([^%])/g, function(m, p1, p2) { return '<p hyphenate="false" tabstops="0;R1250,Left,,;" lindent="2450"><t><t>•  ' + p2; } );
return str;

Now we can make that function do what we want:

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++) + '.  ' + 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++) + '.  ' + 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:

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.

Link to comment
Share on other sites

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!

 

 

 

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>•<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;

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...