From bc80d67ca1a43dc27511f3b42591c6d4975aa664 Mon Sep 17 00:00:00 2001 From: Jacob Parker Date: Wed, 22 Feb 2012 17:11:10 -0500 Subject: [PATCH] dweb 0.5: handlers, fixes, tidying. --- bin/{changelog => changelog.sh} | 0 pub/style/style.css | 1 - src/config.d | 9 +- src/web.d | 149 +++++++++++--------------------- srv/@changelog | 0 srv/{.dweb => @dweb.md} | 0 6 files changed, 58 insertions(+), 101 deletions(-) rename bin/{changelog => changelog.sh} (100%) create mode 100644 srv/@changelog rename srv/{.dweb => @dweb.md} (100%) diff --git a/bin/changelog b/bin/changelog.sh similarity index 100% rename from bin/changelog rename to bin/changelog.sh diff --git a/pub/style/style.css b/pub/style/style.css index e0e60f7..150f43a 100644 --- a/pub/style/style.css +++ b/pub/style/style.css @@ -95,7 +95,6 @@ a:hover { text-decoration: underline; } /* # Horiz-side # */ #horiz-side-bar { width:100%; - float left; clear: both; border: 0; margin: 0; 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(""); + 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("changelog"); html_pop("
"); - html_pop(""); html_push("
"); @@ -127,37 +83,33 @@ void do_header() { html_pop("
\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("
"); html_push("
"); - html("Powered by dweb"); + html("Powered by dweb"); html_pop("
"); html_push("
"); @@ -168,21 +120,22 @@ void do_footer() { } 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 path = site_root ~ url; - string pagename = last_in_path(url); + string pagename = baseName(url); if (pagename.length != 0) pagename = " - " ~ pagename; pagename = site_title ~ pagename; html_push(""); html("" ~ pagename ~ ""); html(""); - html(""); html(""); html(""); html_pop("\n"); @@ -190,10 +143,10 @@ void main(string[] args) { html_push(""); if (page_container) html_push("
"); do_header(); - do_nav_tree(path); - do_content(path); + do_nav_tree(url); + do_content(url); do_footer(); - if (page_container) html_push("
"); + if (page_container) html_pop("
"); html_pop("\n"); html_pop(""); diff --git a/srv/@changelog b/srv/@changelog new file mode 100644 index 0000000..e69de29 diff --git a/srv/.dweb b/srv/@dweb.md similarity index 100% rename from srv/.dweb rename to srv/@dweb.md