Ryan crosser
Ryan crosser

Reputation: 45

Add <p> tags to plain text paragraph

I am trying to write a script to automate a rather long and painful process I am required to do at work.

Scenario:
In short, I am given a text document that has 4 lines of text and two line breaks, and this format repeats for 5 to 8 pages. I then have to extract 6 pieces of information from each "paragraph" and type them out into a table in another document.

Since the output I get is formatted (including the fields that I extract data from), I would like to be able to paste my text into a textbox, click convert, and have it return the results I need and list them in a table.

Where I need help/advise:
From my limited JavaScript knowledge, I am thinking that I need to set <p> tags around each 4-line "paragraph." Then I should be able to write a script that would select the <p> tag, extract the data I need, write it to the table, and repeat the process for the next <p> tag.

I know there are online options that will do this for me (textfixer.com, for example), but my system is not connected to the internet, and for security reasons, it will never be. This means I can not use PHP (which I know better) or any other server-side processing. I am hoping this can be done through JavaScript, so I can process it through a browser. This is a Windows machine so if there is another way to do it, I am open to ideas.

Upvotes: 1

Views: 4593

Answers (2)

Alex Vidal
Alex Vidal

Reputation: 4108

You could split the document on a series of three line breaks (one to account for the end of line break, two for the empty lines), and then rejoin them with paragraph tags:

// pull the document out
var doc = $("#my-document").val();

// split on double line breaks
// note that we actually split on triple to account for the trailing
// line break at the end of a paragraph
var parts = doc.split("\n\n\n");

// rejoin with paragraph tags
doc = parts.join("</p><p>");

// wrap the entire thing in open/close paragraphs
doc = "<p>" + doc + "</p>";

// and stick it in the output
$("#output").html(doc);

Example: http://jsfiddle.net/2uMaK/

Upvotes: 2

Monojit
Monojit

Reputation: 305

yeah,your approach is good. #paragraph will select the code

  #paragraph
  {
    //do whatever you want
  }

You can even define the table with id = 'table' and directly add data there from above selection.

eg:

 {
   var p=('#paragraph').fectchCode algo();
   $('#table').append(p);
 }

Upvotes: 0

Related Questions