Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use perl to generate index.html

Status
Not open for further replies.

allensim81

Technical User
Apr 9, 2008
15
MY
Hi, i am new in perl.
I am trying to write a simple script to show an index.html.
however it works only 40%! only one item is shown in the index.html... can u please guide me step-by step? thanks in advance!
Code:
#!/usr/bin/perl

$httrackpath = "/usr/local/bin";
$websites="/data/websites";


opendir(DIR, $websites);
@files=readdir(DIR);
closedir(DIR);

for $file(@files) {
        ## List all files and directories from /data/websites ##
        next if ($file =~ /^\./); # Ignore anything start with dot "."
        next if (!-d "$websites/$file");  # Ignore if it is not a directory
        next if ($file =~ /hts-cache/); # Ignore if it is hts-cache dir
        print "[$file]\n";

        open(HTML, ">/root/index.html");



    print HTML "<html>\n<head>\n<title>$base$i</title>\n</head>\n<body>\n";
    print HTML "<a href=\"$base$next.html\"><img src=\"$base$i.jpg\"></a>\n";
    print HTML "</body>\n</html>\n";

    print HTML "<&middot; <A HREF=\"$file/index.html\">$file</A>";
    close(HTML);



        ## proceed with the next function ##

        ## End, next website ##
}
 
Hi

You are creating the /root/index.html again and again, each time the loop is executed. So each time the previous file will be overwritten and finally you will have the last created one.

Just move the [tt]open[/tt] and [tt]close[/tt] together with the [tt]print[/tt]ing of common parts out from the loop :
Code:
[gray]#!/usr/bin/perl[/gray]

[navy]$websites[/navy][teal]=[/teal][green][i]"/data/websites"[/i][/green][teal];[/teal]

[b]opendir[/b][teal]([/teal]DIR[teal],[/teal] [navy]$websites[/navy][teal]);[/teal]
[navy]@files[/navy][teal]=[/teal][b]readdir[/b][teal]([/teal]DIR[teal]);[/teal]
[b]closedir[/b][teal]([/teal]DIR[teal]);[/teal]

[highlight][b]open[/b][teal]([/teal]HTML[teal],[/teal] [green][i]">/root/index.html"[/i][/green][teal]);[/teal][/highlight]

[highlight][b]print[/b] HTML [green][i]"<html>\n<head>\n<title>$base$i</title>\n</head>\n<body>\n"[/i][/green][teal];[/teal][/highlight]

[b]for[/b] [navy]$file[/navy][teal]([/teal][navy]@files[/navy][teal])[/teal] [teal]{[/teal]
    [gray][tt] List all files and directories from /data/websites [/tt][/gray]
    [b]next[/b] [b]if[/b] [teal]([/teal][navy]$file[/navy] [teal]=~[/teal] [green][i]/^\./[/i][/green][teal]);[/teal] [gray]# Ignore anything start with dot "."[/gray]
    [b]next[/b] [b]if[/b] [teal](!-[/teal]d [green][i]"$websites/$file"[/i][/green][teal]);[/teal]  [gray]# Ignore if it is not a directory[/gray]
    [b]next[/b] [b]if[/b] [teal]([/teal][navy]$file[/navy] [teal]=~[/teal] [green][i]/hts-cache/[/i][/green][teal]);[/teal] [gray]# Ignore if it is hts-cache dir[/gray]
    [b]print[/b] [green][i]"[$file]\n"[/i][/green][teal];[/teal]

    [b]print[/b] HTML [green][i]"<a href=\"$base$next.html\"><img src=\"$base$i.jpg\"></a>\n"[/i][/green][teal];[/teal]

    [gray][tt] proceed with the next function [/tt][/gray]

    [gray][tt] End, next website [/tt][/gray]
[teal]}[/teal]

[highlight][b]print[/b] HTML [green][i]"</body>\n</html>\n"[/i][/green][teal];[/teal][/highlight]
[highlight][b]print[/b] HTML [green][i]"<&middot; <A HREF=\"$file/index.html\">$file</A>"[/i][/green][teal];[/teal][/highlight]

[highlight][b]close[/b][teal]([/teal]HTML[teal]);[/teal][/highlight]

Feherke.
 
I have to wonder why you want to create the file "index.html" when you could just have your script as CGI dynamically generate it every time the page is requested.



Trojan.
 
Hi

Trojan said:
I have to wonder why you want to create the file "index.html" when you could just have your script as CGI dynamically generate it every time the page is requested.
Good question, but I can imagine some reasons :
[ul]
[li]Exactly for that : to generate it once, instead of every time.[/li]
[li]To have a static file available during scheduled maintenance of the site.[/li]
[li]To include it in documentation shipped on CD/together with an application.[/li]
[li]To use it as input for an application which does not handle HTTP itself.[/li]
[/ul]
I skipped the obvious reasons.


Feherke.
 
HI thanks for the reply!
I amended the code according and it works! Thanks for the guidiance...

Now i got another problem. How can i allocate the following CSS text into my perl script? Help...

CSS Text:
<style type="text/css">
<!--

body {
margin: 0; padding: 0; margin-bottom: 15px; margin-top: 8px;
background: #77b;
}
body, td {
font: 14px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
}

#subTitle {
background: #000; color: #fff; padding: 4px; font-weight: bold;
}

#siteNavigation a, #siteNavigation .current {
font-weight: bold; color: #448;
}
#siteNavigation a:link { text-decoration: none; }
#siteNavigation a:visited { text-decoration: none; }

#siteNavigation .current { background-color: #ccd; }

#siteNavigation a:hover { text-decoration: none; background-color: #fff; color: #000; }
#siteNavigation a:active { text-decoration: none; background-color: #ccc; }


a:link { text-decoration: underline; color: #00f; }
a:visited { text-decoration: underline; color: #000; }
a:hover { text-decoration: underline; color: #c00; }
a:active { text-decoration: underline; }

#pageContent {
clear: both;
border-bottom: 6px solid #000;
padding: 10px; padding-top: 20px;
line-height: 1.65em;
background-image: url(backblue.gif);
background-repeat: no-repeat;
background-position: top right;
}

#pageContent, #siteNavigation {
background-color: #ccd;
}


.imgLeft { float: left; margin-right: 10px; margin-bottom: 10px; }
.imgRight { float: right; margin-left: 10px; margin-bottom: 10px; }

hr { height: 1px; color: #000; background-color: #000; margin-bottom: 15px; }

h1 { margin: 0; font-weight: bold; font-size: 2em; }
h2 { margin: 0; font-weight: bold; font-size: 1.6em; }
h3 { margin: 0; font-weight: bold; font-size: 1.3em; }
h4 { margin: 0; font-weight: bold; font-size: 1.18em; }

.blak { background-color: #000; }
.hide { display: none; }
.tableWidth { min-width: 400px; }

.tblRegular { border-collapse: collapse; }
.tblRegular td { padding: 6px; background-image: url(fade.gif); border: 2px solid #99c; }
.tblHeaderColor, .tblHeaderColor td { background: #99c; }
.tblNoBorder td { border: 0; }


// -->

Code:
#!/usr/bin/perl

$httrackpath = "/usr/local/bin";
$websites="/data/websites";

opendir(DIR, $websites);
@files=readdir(DIR);
closedir(DIR);

open(HTML, ">$websites/index2.html");
print HTML "<html>\n<head>\n<title>List of available projects - HTTrack Website Copier</title>\n</head>\n";
print HTML "<body><table width=\"76%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"3\" class=\"tableWidth\">\n";
print HTML "<tr><td id=\"subTitle\">HTTrack Website Copier - Open Source offline browser</td></tr></table>\n";
print HTML "<table width=\"76%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\" class=\"tableWidth\"><tr class=\"blak\"><td>\n";
print HTML "<table width=\"100%\" border=\"0\" align=\"center\" cellspacing=\"1\" cellpadding=\"0\"><tr><td colspan=\"6\">\n";
print HTML "<table width=\"100%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"10\"><tr> <td id=\"pageContent\">\n";
print HTML "<h1 ALIGN=Center>Index of locally available projects:</H1>\n";
print HTML "<table border=\"0\" width=\"100%%\" cellspacing=\"1\" cellpadding=\"0\">\n";
print HTML "<BR><H6 ALIGN=\"RIGHT\"><I>Mirror and index made by HTTrack Website Copier [XR&CO'2008]</I></H6>\n";
print HTML "<table width=\"76%\" border=\"0\" align=\"center\" valign=\"bottom\" cellspacing=\"0\" cellpadding=\"0\"><tr>\n";
print HTML "<td id=\"footer\"><small>&copy; 2008 Xavier Roche & other contributors - Web Design: Leto Kauler.</small></td></tr></table>\n";


for $file(@files) {
        ## List all files and directories from /data/websites ##
        next if ($file =~ /^\./); # Ignore anything start with dot "."
        next if (!-d "$websites/$file");  # Ignore if it is not a directory
        next if ($file =~ /hts-cache/); # Ignore if it is hts-cache dir
        print "[$file]\n";
        print HTML "<A HREF=\"$file/index.html\">$file</A><BR/>";

}
print HTML "</body></html>";
close(HTML);
 
Hi

I would not put it in the generated index.html. Better put that CSS ( without the HTML tags and comments ) into a separate file, let us say, style.css, then in the script :
Code:
[b]print[/b] HTML [green][i]"<html>\n<head>\n<title>List of available projects - HTTrack Website Copier</title>\n[highlight]<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n[/highlight]</head>\n"[/i][/green][teal];[/teal]

Feherke.
 
hi, tried. it doesnt work...
how to put the css text into seperate file?
I just save below at style.css
Is it correct?

Code:
<style type="text/css">
body {
    margin: 0;  padding: 0;  margin-bottom: 15px;  margin-top: 8px;
    background: #77b;
}
body, td {
    font: 14px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    }

#subTitle {
    background: #000;  color: #fff;  padding: 4px;  font-weight: bold; 
    }

#siteNavigation a, #siteNavigation .current {
    font-weight: bold;  color: #448;
    }
#siteNavigation a:link    { text-decoration: none; }
#siteNavigation a:visited { text-decoration: none; }

#siteNavigation .current { background-color: #ccd; }

#siteNavigation a:hover   { text-decoration: none;  background-color: #fff;  color: #000; }
#siteNavigation a:active  { text-decoration: none;  background-color: #ccc; }


a:link    { text-decoration: underline;  color: #00f; }
a:visited { text-decoration: underline;  color: #000; }
a:hover   { text-decoration: underline;  color: #c00; }
a:active  { text-decoration: underline; }

#pageContent {
    clear: both;
    border-bottom: 6px solid #000;
    padding: 10px;  padding-top: 20px;
    line-height: 1.65em;
    background-image: url(backblue.gif);
    background-repeat: no-repeat;
    background-position: top right;
    }

#pageContent, #siteNavigation {
    background-color: #ccd;
    }


.imgLeft  { float: left;   margin-right: 10px;  margin-bottom: 10px; }
.imgRight { float: right;  margin-left: 10px;   margin-bottom: 10px; }

hr { height: 1px;  color: #000;  background-color: #000;  margin-bottom: 15px; }

h1 { margin: 0;  font-weight: bold;  font-size: 2em; }
h2 { margin: 0;  font-weight: bold;  font-size: 1.6em; }
h3 { margin: 0;  font-weight: bold;  font-size: 1.3em; }
h4 { margin: 0;  font-weight: bold;  font-size: 1.18em; }

.blak { background-color: #000; }
.hide { display: none; }
.tableWidth { min-width: 400px; }

.tblRegular       { border-collapse: collapse; }
.tblRegular td    { padding: 6px;  background-image: url(fade.gif);  border: 2px solid #99c; }
.tblHeaderColor, .tblHeaderColor td { background: #99c; }
.tblNoBorder td   { border: 0; }
 
Assuming the perl script is in the cgi-bin, and the style-sheet is in the web root ( if it isn't you may have to move it there), you should make the path to the style sheet relative to the cgi-bin using ../ in the URI:

Code:
<link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\">\n

the cgi-bin may block a request for something like a style sheet document because most cgi-bins are setup to only respond to requests for scripts and not any other type of document.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top