I have a perl script that is given HTML (say in $html) and needs to parse $html into formatting appropriate for a .js (JavaScript) file that will be called via a <script type="text/javascript" src="myfile.js"> tag. My problem is that if $html contains JavaScript code instead of plain HTML, that JavaScript code does NOT need to be formatted (since it is already JavaScript). For instance, if I wanted to display a Google AdSense ad via this .js file, my script would have to parse the following (contained in $html):

<script type="text/javascript"><!-- google_ad_client = "0123456789"; google_alternate_color = "FFFFFF"; google_ad_width = 120; google_ad_height = 90; google_ad_format = "120x90_0ads_al_s"; //2007-05-16: adsense4u google_ad_channel = "1328300801"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "000000"; google_color_text = "000000"; google_color_url = "78B749"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.c +om/pagead/show_ads.js"> </script>

I want my script to be able to completely remove the first <script> and </script> tags (but not the code inbetween) since what is contained inbetween is pure JavaScript and should be kept intact/unparsed. However, the second <script> tag contains a "src" and so I want this tag to be parsed into a format suitable for a document.write(); JS statement. So it should look something like this after being parsed:

<!-- google_ad_client = "0123456789"; google_alternate_color = "FFFFFF"; google_ad_width = 120; google_ad_height = 90; google_ad_format = "120x90_0ads_al_s"; //2007-05-16: adsense4u google_ad_channel = "1328300801"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "000000"; google_color_text = "000000"; google_color_url = "78B749"; //--> document.write('<'+'script type="text/javascript" src="http://pagead2. +googlesyndication.com/pagead/show_ads.js">'); document.write('<'+'/script>');

In the end, my goal is to be able to take the original HTML/JavaScript mix and parse it, via perl, into a form suitable for a .js file. I am not sure where to start and would appreciate any help provided. Thanks!

In reply to Parsing HTML code for formatting into JavaScript by anxara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.