CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
insert image in html

 



artperl
Novice

Apr 23, 2015, 8:16 AM

Post #1 of 10 (4413 views)
insert image in html Can't Post

hello gurus,

me again... really appreciate your inputs.
Im looking for efficient way to insert an image:
<img src="mychart.png" alt="Chart" />

in an existing HTML file.
The bigger challenge is, I have to insert the image after matching 2 separate lines.

thanks in advance for your usual help...


Zhris
Enthusiast

Apr 23, 2015, 8:37 AM

Post #2 of 10 (4409 views)
Re: [artperl] insert image in html [In reply to] Can't Post

One option would be to use HTML::TreeBuilder in a similar manner to when you removed empty table rows ( http://perlguru.com/gforum.cgi?post=81375 ). Use HTML::Element's look_down method to find the lines, then the preinsert or postinsert method to insert a new element before or after one of the lines.

There are other HTML parser / writer modules available on CPAN which you may wish to explore instead, or depending on the complexity of the match, a regular expression may be more practical.

Chris


artperl
Novice

Apr 23, 2015, 8:56 AM

Post #3 of 10 (4396 views)
Re: [Zhris] insert image in html [In reply to] Can't Post

Hi Chris,

I actually ended up using your suggested regexp to remove the <tr></tr> pairing as I found out something get broken using the HTML::TreeBuilder (it most likely my mistake).

Please find atached "cleaned" html file.
What im trying to do now is to insert 2 images, 1 after each table (after this line: </tr></table><hr><br> but I have to match certain field first (parameter number) above it.

can you please give me some guidelines?... thanks much!...


Zhris
Enthusiast

Apr 23, 2015, 9:09 AM

Post #4 of 10 (4391 views)
Re: [artperl] insert image in html [In reply to] Can't Post

Ah I see, HTML::TreeBuilder tries its best to make malformed HTML well formed as best as it can, this was probably why your HTML "broke", but I can't be certain since you had only supplied a portion of your document. When working with malformed HTML, I sometimes use a "clean up" regexp before parsing, but it depends on a case by case basis.

I think you forgot to attach your file.

Chris


artperl
Novice

Apr 23, 2015, 9:10 AM

Post #5 of 10 (4389 views)
Re: [Zhris] insert image in html [In reply to] Can't Post

oops... here's the file Chris...
Attachments: 20150326_141738_75b.html (7.60 KB)


Zhris
Enthusiast

Apr 23, 2015, 9:34 AM

Post #6 of 10 (4381 views)
Re: [artperl] insert image in html [In reply to] Can't Post

Thanks,

I believe you need the img under each of the two tables that have a parameter number field. Note the first of these tables ends with "</tr></table><hr><br>" while the second ends with "</tr></table><br><hr>".

You describe the HTML file as "cleaned" yet it is indeed malformed. As per your previous experience, HTML::TreeBuilder and / or other html parsers will have difficulty retaining the documents structure, would this be a problem?

Chris


artperl
Novice

Apr 23, 2015, 9:39 AM

Post #7 of 10 (4379 views)
Re: [Zhris] insert image in html [In reply to] Can't Post

would a regexp be easier to use here Chris?
i'll just match the parameter number then look for the next appearing </table> line then insert the image.

but im struggling how to do this in simple/efficient regexp.


Zhris
Enthusiast

Apr 23, 2015, 9:44 AM

Post #8 of 10 (4377 views)
Re: [artperl] insert image in html [In reply to] Can't Post

I don't condone using a regexp in this case, but here is a quick dirty search and replace to get you started:


Code
s{Parameter number.+?(\d+)</td>.+?</tr>\s*</table>\s*<(?:br|hr)>\s*<(?:br|hr)>\K}{<img src="mychart$1.png" alt="Chart" />}sg;


I've made the assumption you want to capture the parameter number and insert it somewhere in your img element.

Chris


artperl
Novice

Apr 24, 2015, 1:27 AM

Post #9 of 10 (4281 views)
Re: [Zhris] insert image in html [In reply to] Can't Post

Hi Chris,

So I have this in my code ff your regexp:

while (<NEWTEMP>)
{
my $str =~ s{$Tparam</td>.+?</tr>\s*</table>\s*<(?:br|hr)>\s*<(?:br|hr)>\K}{<img src="$splitLot~$Tparam.png" alt="$Tparam Chart" />}sg;
print;
}
close (NEWTEMP);

$Tparam is the parameter number im looking for to insert the image.
It's not working... can you help me determine what else im missing?
thanks much!!!!!!!


Zhris
Enthusiast

Apr 24, 2015, 12:14 PM

Post #10 of 10 (4140 views)
Re: [artperl] insert image in html [In reply to] Can't Post

Hi,

The regexp processes the entire html document, therefore you need to slurp your file handle into a string first.

If you want to match a specific parameter number, then you must keep in mind that there is a possibility the same number may occur elsewhere. Therefore you should:

- be more strict when matching by incorporating more of the surrounding syntax

Code
s{Parameter number</b></td>\s*<td[^>]*>\Q$Tparam\E.+?</tr>\s*</table>\s*(?:<(?:br|hr)>\s*){2,2}\K}{<img src="$splitLot~$Tparam.png" alt="$Tparam Chart" />}sg;


- or match all blocks then only add the img where the parameter number equals your desired parameter number.

Code
s{Parameter number.+?(\d+)</td>.+?</tr>\s*</table>\s*(?:<(?:br|hr)>\s*){2,2}\K}{($1==$Tparam)?qq'<img src="$splitLot~$Tparam.png" alt="$Tparam Chart" />':''}sge;


etc...


Code
my $splitLot = 'blah'; 
my $Tparam = '177';

my $string = do { local $/ = undef; <NEWTEMP> };

$string =~ s{Parameter number</b></td>\s*<td[^>]*>\Q$Tparam\E.+?</tr>\s*</table>\s*(?:<(?:br|hr)>\s*){2,2}\K}{<img src="$splitLot~$Tparam.png" alt="$Tparam Chart" />}sg;

print $string;


Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives