add text to next line?

G

Geoff Cox

Hello,

I am trying to find <body> in an html file and then add some text to
the line after the <body> tag ... the code below adds the text at the
end of the file. How do I get the text on the next line after
<body>?

Cheers

geoff


use warnings;
use strict;

use File::Find;

my $dir = 'd:/a-winfiles/mfox/html/few';


find sub {
my $name = $_;
if (-d $_) {return;};
if ($name !~ /^print/) {
open (IN, "$name");
my $line;
while (defined ($line = <IN>)) {
if ($line =~ /<body>/i) {
open (OUT,">>$name");
print OUT ("\n freddy");
}
}
}

}, $dir;
 
A

Anno Siegel

Geoff Cox said:
Hello,

I am trying to find <body> in an html file and then add some text to
the line after the <body> tag ... the code below adds the text at the
end of the file. How do I get the text on the next line after
<body>?

Cheers

geoff


use warnings;
use strict;

use File::Find;

my $dir = 'd:/a-winfiles/mfox/html/few';


find sub {
my $name = $_;
if (-d $_) {return;};
if ($name !~ /^print/) {
open (IN, "$name");
my $line;
while (defined ($line = <IN>)) {
if ($line =~ /<body>/i) {
open (OUT,">>$name");
print OUT ("\n freddy");
}
}
}

}, $dir;

Parsing HTML this way will be fragile. Not every occurrence of "<body>"
must be an HTML tag.

To do this, you will basically copy the input file to the output.
While doing this, watch each line for <body> and keep the result
for the next round. If this indicates that the last line contained
"<body>", change the current line before printing it. This is
the loop (untested):

my $seen_body;
while ( <IN> ) {
if ( $seen_body ) {
# make the necessary changes to $_
}
print OUT $_;
$seen_body = /<body>/;
}

This assumes that the changes don't change the absence or presence
of "<body>" in the modified line.

Anno
 
T

Tad McClellan

Geoff Cox said:
I am trying to find <body> in an html file


Then it might look like this you know:

<body >
or
<body

What will your code do if this occurs before the real body start tag?

<!-- add stuff after the <body> tag. -->


You should use a module that understands HTML for processing HTML data.

and then add some text to
the line after the <body> tag ... the code below adds the text at the
end of the file.


Because you are open()ing the file for appending.

How do I get the text on the next line after
<body>?


perldoc -q line

How do I change one line in a file/delete a line in a
file/insert a line in the middle of a file/append to the
beginning of a file?


You are expected to check the Perl FAQ *before* posting to the
Perl newsgroup.

if (-d $_) {return;};


return if -d; # does the same thing without obfuscating punctuation

open (IN, "$name");
^ ^
^ ^ What's wrong with always quoting "$vars"?


You should always, yes *always*, check the return value from open():

open(IN, $name) or die "could not open '$name' $!";
 
G

Geoff Cox

Then it might look like this you know:

<body >
or
<body

Tad,

The reason I have asked about this is that I have 200 html files which
use large fonts for the text. For prinitng purposes I wish to have a
copy of each file but with small font text..

The idea is to have a link in each large font file to the smaller font
copy, which has the same name except it has print- at the beginning of
the name.

I can easily add (using find/replace function of html editor) a link
to each file with say "print-file" in the link. I would then need a
Perl programme to replace file with the name of the file.

Any pointers please on simply being able to find a word and replace it
with another word?

Cheers

Geoff
 
P

Peter Hickman

Geoff said:
The reason I have asked about this is that I have 200 html files which
use large fonts for the text. For prinitng purposes I wish to have a
copy of each file but with small font text..

The idea is to have a link in each large font file to the smaller font
copy, which has the same name except it has print- at the beginning of
the name.

I can easily add (using find/replace function of html editor) a link
to each file with say "print-file" in the link. I would then need a
Perl programme to replace file with the name of the file.

Any pointers please on simply being able to find a word and replace it
with another word?

Cheers

Geoff

What you prehaps should be looking at is CSS, you can have one stylesheet for
screen display and another for printing. The use just has to select that they
wish to print the page and the printing stylesheet will be used.
 
R

Roy Johnson

Geoff Cox said:
Any pointers please on simply being able to find a word and replace it
with another word?

Assuming only one body tag per file:

perl -pe 's/<body>/<new body>/' file.html > file-print.html
 
R

Roy Johnson

Sorry, I answered the question you posted there, but not the question
you started this thread about. To add a line after each line
containing <body>:

while (<>) {
print;
print "Whatever new text\n" if /<body>/;
}
 
G

Geoff Cox

What you prehaps should be looking at is CSS, you can have one stylesheet for
screen display and another for printing. The use just has to select that they
wish to print the page and the printing stylesheet will be used.

Peter,

that sounds interesting ... how does yser select the print style
sheet?

Cheers

Geoff
 
G

Geoff Cox

Sorry, I answered the question you posted there, but not the question
you started this thread about. To add a line after each line
containing <body>:

while (<>) {
print;
print "Whatever new text\n" if /<body>/;
}

Roy,

I will use above idea in a minute but following code works except it
does not attack the sub folders ... can you see why? Also having got
the temp- files, do I delete the original file and rename the temp
file with that name or is there a better way?

Cheers Geoff


use warnings;
use strict;
use File::Find;

my $dir = 'html/test';

find sub {

my $name = $_;

if (($name =~ /.html$/) && ($name !~ /^print/)) {

open (IN, "$name");
while (defined (my $line =<IN>)) {
if ($line =~ /<body>/i) {
$line =~ s/<body>/<body>\nSelect page with smaller font <a
href="print-$name">for printing<\/a>/i;
}
open (OUT, ">>html/test/temp-$name");
print OUT ($line);

}

}

}, $dir;
 
V

Vlad Tepes

* Geoff Cox said:
Peter,

that sounds interesting ... how does yser select the print style
sheet?

The user doesn't have to. The stylesheet specifies the fonts used
for screen as well as fonts used for printing. When the user chooses
to print the page, the print-styles are chosen automatically by the
browser.

* { font-family: sans-serif, arial, helvetica }

@media print { /* fonts used for printing: */
body { margin: 1cm }
h1 { font-family: serif, Times; }
p { font-size: 11pt }
}


Another possibility is to link the html to two different stylesheets, one
for the screen and one to be used for printing the page:

<link rel="stylesheet" type="text/css" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" media="print" href="print.css">

( This is untested and off-topic in this group. Search the web and
ask in a group for html or css for more and better information about
css and selective styles. )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top