Web Automation: Generating Dynamic Tables of Contents Page 4
. Line 2 stuffs the URL we will want to replace the local path with, in the variable
. Line 3 calls the
Get_Depthfunction on our initial director and adds 1 to it (adding 1 is important, because logically we'll never be in a directory that is at the same depth of the
htdocsdirectory (eg. the
confdirectories are at the same depth--we won't be going there)). Line 4 calls the
Get_Dirsfunction to obtain a list of subdirectories in the root of our webspace. Lines 5 and 6 send the default HTTP content header to the browser. Line 7 starts the unordered list. Line 8 starts iterating over the
@dirsarray using a
whileloop. Line 9 removes the first item from the
@dirse array, and stuffs it into the
scalar (My rationale for doing this is scalability. By using a
whileloop instead of a
forloop, and removing items with
shift, it keeps the array from growing unnecessarily large and needlessly consuming resources. If I didn't remove the entries as I used them, at the end of this script the
@dirsarray would contain all of the directories in webspace--this is not necessary). Line 10 calls the
Get_Depthfunction to obtain the depth of the current directory, and stuffs the value into the
scalar. Line 11 first calls the
Get_Dirsfunction to obtain a list of subdirectories, and then prepends that list to the beginning of the
@dirsarray (making them the "next" to be iterated onto). Line 12 says, "if the current depth is greater than the previous depth, indent." Line 13 catches the occurrences when the current depth is less than the previous depth. Line 14 calculates the difference of the current and previous depths and store the result in
. Line 15 print the
</ul>tag however many time is necessary according to the difference between the current and previous depths (note the
xoperator). Line 16 ends this
If. Line 17 assigns the
. Line 18 concatenates the current directory path with
index.html, and stores the value in
. Line 19 says, "unless
exists, skip to the next iteration" (which is the same as saying "if
does not exist, skip to the next iteration"). This is to prevent directories that don't have an
index.htmlfile from making things icky. Line 20 calls the
, and assigns that value to the
scalar. Line 21 contains a substitution regular expression that replaces the base directory part of the path we placed in line 1, with the base URL we provided in line 2. Line 22 prints the title of the page as a link to the page, as a list item. Line 23 ends this
whileloop. Line 24 prints the final
As always, this script is not the end-all of table-of-contents generators- But it is a good place to start. It is short and fairly memory efficient. It scales fairly well to sites with 5000 directories, and perhaps even beyond.
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...