Saturday, December 26, 2015

Creating web directory with ls and some C++

I was saving a lot of webpages to my local machine so I could refer to them even if I was unable to access the internet. But it started getting difficult to organize them. I figured I could make a directory page with links to all of the other pages saved on my machine. That worked ok until I had several hundred pages and I was adding new pages every day. So I decided to write an a little program that would read from my list of urls. You can do this with any list of urls and create a link directory. I'll get the listing with the ls command and pipe the to a text file.
ls /root/Desktop/webpages/ > linklist.txt
 Now I'll run the program and read the linklist and output the html page with with links to all of the pages. You'll have to changes some of the filenames as some of the titles are not going to be link friendly.

I'm sure you could do this more of this automatically by reading the page title from the each file and using that as the anchor text in your links. You could also eliminate bad character combinations that might cause parsing errors for your web browser. I might get around to coding in some other features when I have time or care more.

Usage: program <infile> <outfile>
Example: directorymaker webpages.txt directory.html
//reads through a file created by ls > txt line by line and outputs it to html
#include <string>
#include <sstream>
#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;

int main (int argc, char* argv[2])

//open output file (myfile) so data can be written. Names the output file
{ofstream myfile;
//argv[2] accepts the the file FILEPATH as a commandline argument. Names the output file
myfile.open (argv[2]);

//declare the variable line from our input file (argv[1]) names the FILEPATH to the input file
{ string line; ifstream infile (argv[1]);
//write out our header for html page
myfile << "<html>" << endl;
myfile << "<head><title>directory of local webpages</title></head>" << endl;
//beginning of body and table
myfile << "<body>" << endl;
myfile << "<table>" << endl;

//check if the input file is open
if (infile.is_open())

{ while ( getline (infile,line) )

myfile << "<tr><td>" << "<b>" << "<a href='" << line << "'>" << line << "</a>" << "</b>" << "</td><td>" << endl;

//close the input file
infile.close();

// write out the end of the page
myfile << "</table>" << endl;
myfile << "<p>" << "<a " << "href=" << "\"" << "index.html" << "\">" << "SHA512 hash and plaintext pairs" << "</a>" << "</p>" << endl;
myfile << "<p>" << "<a " << "href=" << "\"" << "code.html" << "\">" << "code and downloads" << "</a>" << "</p>" << endl;
myfile << "<p>" << "<a " << "href=" << "\"" << "sitemap.html" << "\">" << "site map" << "</a>" << "</p>" << endl;
myfile << "<p>" << "<a " << "href=" << "\"" << "advertise.html" << "\">" << "advertise on this site" << "</a>" << "</p>" << endl;
myfile << "<p>" << "pages by vailixi" << "</p>"<< endl;
myfile << "</body>" << endl;
//close the output file
myfile.close();
}
// if the input file cannot be found output a message
else cout << "Unable to open file" << endl;
}

return 0; }

No comments:

Post a Comment