Matching with regex instead of globbing.

This commit is contained in:
Luqman Aden 2012-02-24 21:37:13 -05:00
parent bb457c2fc3
commit be986e8be3
3 changed files with 18 additions and 18 deletions

View File

@ -1,4 +0,0 @@
RewriteEngine on
RewriteBase /~j3parker/
RewriteRule ^pub/(.*) - [L]
RewriteRule ^(.*) bin/web [L]

View File

@ -8,6 +8,6 @@ const bool nav_tree_vert = false;
string[string] handlers;
void init_handlers() {
handlers["*.md"] = "contrib/Markdown.pl";
handlers["(.*).md"] = "contrib/Markdown.pl";
handlers["changelog"] = "changelog.sh";
}

View File

@ -1,5 +1,5 @@
//#!/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;
@ -92,12 +92,14 @@ bool dirExists(string path) { try { if (isDir(path)) return true; else return
void do_content(string url) {
html_push("<div id=\"main-copy\"" ~ (nav_tree_vert? " class=\"main-copy-side-bar\"" : "") ~ ">");
// first, see if we have something that wants to handle url outright
foreach (string glob, string h; handlers) {
if (globMatch(url, glob)) {
foreach (string rstr, string h; handlers) {
try {
if (match(url, regex(rstr, ""))) {
html(shell(dweb_root ~ "/bin/" ~ h ~ " " ~ url));
html_pop("</div>");
return;
}
} catch (std.regex.RegexException re) { continue; }
}
// if that failed, see if we can handle the file
if (url == "" ? true : url[$-1] == '/') url ~= "index";
@ -105,12 +107,14 @@ void do_content(string url) {
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)) {
foreach (string rstr, string h; handlers) {
try {
if (match(name, regex(rstr, ""))) {
html(shell(dweb_root ~ "/bin/" ~ h ~ " " ~ f));
html_pop("</div>");
return;
}
} catch (std.regex.RegexException re) { continue; }
}
}
}