diff --git a/.htaccess b/.htaccess
deleted file mode 100644
index 7b23492..0000000
--- a/.htaccess
+++ /dev/null
@@ -1,4 +0,0 @@
-RewriteEngine on
-RewriteBase /~j3parker/
-RewriteRule ^pub/(.*) - [L]
-RewriteRule ^(.*) bin/web [L]
diff --git a/src/web.d b/src/web.d
index 814cb3f..f2694e4 100755
--- a/src/web.d
+++ b/src/web.d
@@ -1,27 +1,27 @@
//#!/usr/bin/rdmd
-import std.stdio, std.path, std.process, std.file, std.array, std.string, std.algorithm, std.datetime, std.ascii;
+import std.stdio, std.path, std.process, std.file, std.array, std.string, std.algorithm, std.datetime, std.ascii, std.regex;
import config;
string dweb_root;
+string[string] template_variables;
+string[string] headers;
-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) {
+string 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("");
+ string result = "";
+ result ~= "";
+ result ~= ""
+ ~ flair ~ baseName(to) ~ (isdir ? "/" : "") ~ "";
+ result ~= "";
+ return result;
}
-void nav_tree_r(string url, string cur_loc, string[] subdirs) {
+string nav_tree_r(string url, string cur_loc, string[] subdirs) {
+ string result = "";
string[] dirs = array(map!"a.name"(dirEntries(dweb_root ~ "/srv/" ~ cur_loc, SpanMode.shallow)));
sort(dirs);
- if (dirs.length == 0) return;
+ if (dirs.length == 0) return "";
bool inserted_ul = false;
bool next = false;
string next_loc;
@@ -31,15 +31,15 @@ void nav_tree_r(string url, string cur_loc, string[] subdirs) {
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; }
+ if (!inserted_ul) { result ~= ""; inserted_ul = true; }
bool expand = subdirs.length > 0 && name == subdirs[0];
- write_link(url_root ~ stripExtension(s), expand);
+ result ~= 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("
");
+ result ~= "- ";
+ result ~= nav_tree_r(url, (cur_loc == "" ? "" : cur_loc ~ "/") ~ subdirs[0], subdirs[1..$]);
+ result ~= "
";
} else {
next = true;
next_loc = (cur_loc == "" ? "" : cur_loc ~ "/") ~ subdirs[0];
@@ -47,57 +47,27 @@ void nav_tree_r(string url, string cur_loc, string[] subdirs) {
}
}
- if (inserted_ul) html_pop("
");
- if (next) nav_tree_r(url, next_loc, subdirs[1..$]);
+ if (inserted_ul) result ~= "
";
+ if (next) result ~= nav_tree_r(url, next_loc, subdirs[1..$]);
+ return result;
}
-void do_nav_tree(string url) {
- html_push("");
- nav_tree_r(url, "", cast(string[])array(pathSplitter(url)));
- html_pop("
\n");
+string do_nav_tree(string url) {
+ return nav_tree_r(url, "", cast(string[])array(pathSplitter(url)));
}
-void not_found(string path) {
- html("The page " ~ path ~ "
does not exist. (404)");
-}
-
-void do_header() {
- html_push("\n");
+string not_found(string path) {
+ headers["Status"] = "404 Not Found";
+ return "The page " ~ path ~ "
does not exist. (404)";
}
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("");
+string do_content(string url) {
// first, see if we have something that wants to handle url outright
foreach (string glob, string h; handlers) {
if (globMatch(url, glob)) {
- html(shell(dweb_root ~ "/bin/" ~ h ~ " " ~ url));
- html_pop("
");
- return;
+ return shell(dweb_root ~ "/bin/" ~ h ~ " " ~ url);
}
}
// if that failed, see if we can handle the file
@@ -108,29 +78,13 @@ void do_content(string url) {
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;
+ return shell(dweb_root ~ "/bin/" ~ h ~ " " ~ f);
}
}
}
}
- if (baseName(url) != "index") not_found(url);
- html_pop("");
-}
-
-void do_footer() {
- html_push("\n");
+ if (baseName(url) != "index") return not_found(url);
+ return "";
}
bool evil(string s) {
@@ -138,36 +92,41 @@ bool evil(string s) {
return false;
}
+string simple_template(string text, string[string] vars) {
+ return std.regex.replace!((match) { return vars[match[1]]; })(text, regex("\\{\\{\\s*(\\w+)\\s*\\}\\}", "g"));
+}
+
+void send_headers() {
+ foreach (header, header_body; headers) {
+ writefln("%s: %s", header, header_body);
+ }
+ writeln();
+}
+
void main(string[] args) {
init_handlers();
dweb_root = getcwd()[0..$-4]; // take out bin/
-
- html("Content-type: text/html\n");
- html("");
- html_push("\n");
+ headers["Content-Type"] = "text/html; charset=UTF-8";
string url = getenv("REQUEST_URI")[url_root.length..$];
- if (evil(url)) { html ("bad url."); return; }
+ if (evil(url)) {
+ headers["Status"] = "400 Bad Request";
+ send_headers();
+ writeln("bad url.");
+ return;
+ }
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");
+ template_variables["url_root"] = url_root;
+ template_variables["site_title"] = site_title;
+ template_variables["site_subtitle"] = site_subtitle;
+ template_variables["pagename"] = pagename;
+ template_variables["nav_tree"] = do_nav_tree(url);
+ template_variables["content"] = do_content(url);
- html_pop("");
+ send_headers();
+ string default_template = readText(dweb_root ~ "/templates/default.html");
+ write(simple_template(default_template, template_variables));
}
diff --git a/templates/default.html b/templates/default.html
new file mode 100644
index 0000000..9381594
--- /dev/null
+++ b/templates/default.html
@@ -0,0 +1,41 @@
+
+
+
+ {{ pagename }}
+
+
+
+
+
+
+
+
+ {{ nav_tree }}
+
+
+
+ {{ content }}
+
+
+
+
+