Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Make all.html

Part of StandardLibraryCategory

Description

Creates an HTML file that lists all of the other files in the directory.

Example

import std.file : write, listdir, getcwd, DirEntry;
import std.path : getBaseName;
import std.regexp : RegExp;
import std.stdio : writefln;

void main(char[][] args)
{ 
    const char[] pgmName = "MakeAllHtmlExample";

    bool outputToScreen;
    char[] pathStr;
    char[] outputFileStr;

    if(args.length < 2 || args.length > 3)
    {
        writefln("Usage: " ~ pgmName ~ " input_path [output_file]");
        return 0;
    }
    else
    {
        pathStr = args[1];
        if(args.length == 3)
            outputFileStr = args[2];
        else
            outputToScreen = true;
    }

    const html_begin = `<html>` ~ "\n" ~ 
      `<body>` ~ "\n" ~ 
      `<head>` ~ "\n" ~ 
      `<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">` ~ "\n" ~ 
      `<title>All Topics</title>` ~ "\n" ~ 
      `<link rel="stylesheet" type="text/css" href="style.css" >` ~ "\n" ~ 
      `</head>` ~ "\n" ~ 
      `<body>` ~ "\n" ~ 
      `<h1>All Files</h1><ul>`;
    const html_end = "</ul>\n</body>\n</html>";

    char[] wholeText = html_begin ~ \n ~ listdirfn(pathStr) ~ html_end;	
    if(outputToScreen)
        writefln(wholeText);
    else
        write(outputFileStr, wholeText);
}



char[] listdirfn(char[] pathname)
{
    char[] outputData;
    auto r = new RegExp(r"\.html$");
    bool callback(DirEntry* de)
    {
        if (de.isdir)
            listdir(de.name, & callback);
        else
            if(r.test(de.name) &&  getBaseName(de.name) != "all.html")
                outputData ~= `<li><a href="` ~ getBaseName(de.name) ~ `">` ~ 
                  getBaseName(de.name) ~ `</a></li>` ~ \r\n;			
        return true;
    }
    listdir(pathname, & callback);
    return outputData.dup;
}

Sample Batch File

@echo off
dmd MakeAllHtmlExample.d
MakeAllHtmlExample.exe
MakeAllHtmlExample.exe .
MakeAllHtmlExample.exe . all.html
MakeAllHtmlExample.exe . > all.html
pause

Testing

Tested with Digital Mars D Compiler v0.174 on Windows 2000.