//#!/usr/bin/rdmd
import std.stdio, std.path, std.process, std.file, std.array, std.string, std.algorithm, std.datetime;
import config;
string dweb_root;
string indent = "";
void html(string s) { writeln(indent ~ s); }
void html_pop(string s) { indent = indent[0..max(0, $-4)]; html(s); }
void html_push(string s) { html(s); indent ~= " "; }
void write_link(string to, bool expand) {
bool isdir = dirExists(dweb_root ~ "/srv/" ~ to[url_root.length..$]);
string flair = nav_tree_chev? (expand? "» " : "› ") : "";
html_push("
");
html(""
~ flair ~ baseName(to) ~ (isdir ? "/" : "") ~ "");
html_pop("");
}
void nav_tree_r(string url, string cur_loc, string[] subdirs) {
string[] dirs = array(map!"a.name"(dirEntries(dweb_root ~ "/srv/" ~ cur_loc, SpanMode.shallow)));
sort(dirs);
if (dirs.length == 0) return;
bool inserted_ul = false;
bool next = false;
string next_loc;
foreach(string s; dirs) {
s= s[(dweb_root ~ "/srv/").length..$];
string name = stripExtension(baseName(s));
if (name.length == 0) continue; // e.g. ".md", should we do something else with these files?
if (name == "index") continue; // "index" will never appear in the nav_tree.
if (name[0] == '@') continue; // hidden file
if (!inserted_ul) { html_push(""); inserted_ul = true; }
bool expand = subdirs.length > 0 && name == subdirs[0];
write_link(url_root ~ stripExtension(s), expand);
if (expand && isDir(dweb_root ~ "/srv/" ~ s)) {
if (nav_tree_vert) {
html_push("- ");
nav_tree_r(url, (cur_loc == "" ? "" : cur_loc ~ "/") ~ subdirs[0], subdirs[1..$]);
html_pop("
");
} else {
next = true;
next_loc = (cur_loc == "" ? "" : cur_loc ~ "/") ~ subdirs[0];
}
}
}
if (inserted_ul) html_pop("
");
if (next) nav_tree_r(url, next_loc, subdirs[1..$]);
}
void do_nav_tree(string url) {
html_push("");
nav_tree_r(url, "", cast(string[])array(pathSplitter(url)));
html_pop("
\n");
}
void not_found(string path) {
html("The page " ~ path ~ "
does not exist. (404)");
}
void do_header() {
html_push("\n");
}
bool dirExists(string path) { try { if (isDir(path)) return true; else return false; } catch (Exception e) { return false; } }
void do_content(string url) {
html_push("");
if (url == "" ? false : url[$-1] == '/') url ~= "index";
foreach (f; array(map!"a.name"(dirEntries(dirName(dweb_root ~ "/srv/" ~ url), SpanMode.shallow)))) {
if (isDir(f)) continue;
string name = baseName(f); name = name[0] == '@' ? name[1..$] : name;
if (stripExtension(name) == baseName(url)) {
foreach (string glob, string h; handlers) {
if (globMatch(name, glob)) {
html(shell(dweb_root ~ "/bin/" ~ h ~ " " ~ f));
html_pop("
");
return;
}
}
}
}
if (baseName(url) != "index") not_found(url);
html_pop("");
}
void do_footer() {
html_push("\n");
}
void main(string[] args) {
init_handlers();
dweb_root = getcwd()[0..$-4]; // take out bin/
html("Content-type: text/html\n");
html("");
html_push("\n");
string url = getenv("SCRIPT_URL")[url_root.length..$];
string pagename = baseName(url);
if (pagename.length != 0) pagename = " - " ~ pagename;
pagename = site_title ~ pagename;
html_push("");
html("" ~ pagename ~ "");
html("");
html("");
html("");
html_pop("\n");
html_push("");
if (page_container) html_push("");
do_header();
do_nav_tree(url);
do_content(url);
do_footer();
if (page_container) html_pop("
");
html_pop("\n");
html_pop("");
}