diff --git a/bin/changelog b/bin/changelog.sh
similarity index 100%
rename from bin/changelog
rename to bin/changelog.sh
diff --git a/src/config.d b/src/config.d
index f6813c3..a133c26 100644
--- a/src/config.d
+++ b/src/config.d
@@ -1,6 +1,4 @@
const string url_root = "/~j3parker/";
-const string dweb_root = "/users/j3parker/www/";
-const string site_root = dweb_root ~ "srv/";
const string site_title = "This is a Title";
const string site_subtitle = "but this is a subtitle";
@@ -8,3 +6,10 @@ const string site_subtitle = "but this is a subtitle";
const bool nav_tree_vert = true;
const bool nav_tree_chev = true;
const bool page_container = false;
+
+string[string] handlers;
+
+void init_handlers() {
+ handlers["*.md"] = "contrib/Markdown.pl";
+ handlers["changelog"] = "changelog.sh";
+}
diff --git a/src/web.d b/src/web.d
index 7e17553..5013d7d 100755
--- a/src/web.d
+++ b/src/web.d
@@ -1,105 +1,62 @@
//#!/usr/bin/rdmd
-import std.stdio;
-import std.process;
-import std.file;
-import std.array;
-import std.string;
-import std.algorithm;
+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 root, string file, bool expand) {
- bool isdir = dirExists(file);
+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 ~last_in_path(file) ~ (isdir ? "/" : "")
- ~ "");
+ html(""
+ ~ flair ~ baseName(to) ~ (isdir ? "/" : "") ~ "");
html_pop("");
}
-void nav_tree_r(string root, string cur_loc, string[] subdirs) {
- string[] dirs = dir(cur_loc);
+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;
- html_push("");
+ bool inserted_ul = false;
bool next = false;
string next_loc;
foreach(string s; dirs) {
- bool hidden = last_in_path(s).length > 0 && last_in_path(s)[0] == '.';
- bool expand = s[cur_loc.length..$] == (subdirs.length == 0 ? "" : subdirs[0]);
- if (hidden && !expand) continue;
- write_link(root, s, expand);
- if (expand && isDir(cur_loc ~ subdirs[0])) {
+ 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(root, cur_loc ~ subdirs[0] ~ "/", subdirs[1..$]);
+ nav_tree_r(url, (cur_loc == "" ? "" : cur_loc ~ "/") ~ subdirs[0], subdirs[1..$]);
html_pop("
");
} else {
next = true;
- next_loc = cur_loc ~ subdirs[0] ~ "/";
+ next_loc = (cur_loc == "" ? "" : cur_loc ~ "/") ~ subdirs[0];
}
}
}
- html_pop("
");
- if (next) nav_tree_r(root, next_loc, subdirs[1..$]);
+
+ if (inserted_ul) html_pop("
");
+ if (next) nav_tree_r(url, next_loc, subdirs[1..$]);
}
-// this could be better
-string[] dir(string path) {
- string[] files;
- foreach(string s; dirEntries(path, SpanMode.shallow)) {
- if (s[max(0,$-8)..$] != "index.md") files ~= s;
- }
- sort(files);
- return files;
-}
-
-void do_nav_tree(string path) {
- try { if (isDir(path) && path[path.length - 1] != '/') { path = site_root; } }
- catch (Exception e) { path = site_root; }
-
+void do_nav_tree(string url) {
html_push("");
-
- string root = get_root_dir(path);
- string[] subdirs = explode_slashes(path[site_root.length..$]);
- nav_tree_r(root, site_root, subdirs);
-
+ nav_tree_r(url, "", cast(string[])array(pathSplitter(url)));
html_pop("
\n");
}
-string last_in_path(string path) {
- auto i = max(path.length,1) - 1;
- while (i-- > 0) if (path[i] == '/') return path[min(i+1,$)..$];
- return path;
-}
-
-string chomp_slashes(string path) { return chompPrefix(chomp(path, "/"), "/"); }
-string[] explode_slashes(string path) { return split(chomp_slashes(path), "/"); }
-
-string get_root_dir(string path) {
- if (path == "") return "";
- if (isDir(path)) return path[path.length - 1] == '/' ? path : path ~ "/";
- auto i = path.length - 1;
- while (--i > 0) if (path[i] == '/') return path[0..i];
- return "/";
-}
-
-string construct_rel_link(string src, string dst) {
- string rel = "";
- string[] srcs = explode_slashes(src);
- string[] dsts = explode_slashes(dst);
- ulong i = 0;
- while(i < srcs.length && i < dsts.length && srcs[i] == dsts[i]) i++;
- if (i == srcs.length && i == dsts.length) return ".";
- foreach (ulong j; 0..(srcs.length - i)) rel ~= "../";
- foreach (string s; dsts[i..$]) rel ~= s ~ "/";
- return isDir(dst) ? rel : rel[0..$-1];
-}
-
void not_found(string path) {
html("The page " ~ path ~ "
does not exist. (404)");
}
@@ -111,7 +68,6 @@ void do_header() {
html_push("");
-
html_pop("");
html_push("\n");
}
-void do_markdown(string file) {
- // sanity check file first?!?!?
- html(shell("cat " ~ file ~ "|" ~ dweb_root ~ "bin/contrib/Markdown.pl"));
-}
-
-bool fileExists(string path) { try { if (isFile(path)) return true; else return false; } catch (Exception e) { return false; } }
bool dirExists(string path) { try { if (isDir(path)) return true; else return false; } catch (Exception e) { return false; } }
-void do_content(string path) {
+void do_content(string url) {
html_push("");
- string url = path[site_root.length..$];
- switch (url) {
- case "changelog":
- html(shell(dweb_root ~ "bin/changelog"));
- break;
- default:
- try {
- if (isDir(path) && fileExists(path ~ "index.md")) path ~= "index.md";
- if (fileExists(path)) do_markdown(path);
- } catch (Exception e) {
- not_found(path);
+ 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;
+ }
}
+ }
}
- html_pop("\n");
+ if (baseName(url) != "index") not_found(url);
+ html_pop("");
}
void do_footer() {
html_push("