Handlers now use static regex at compile-time.

This commit is contained in:
Luqman Aden 2012-02-25 22:32:41 -05:00
parent be986e8be3
commit 259b9f6476
2 changed files with 17 additions and 7 deletions

View File

@ -1,3 +1,5 @@
import std.regex;
const string url_root = "/";
const string site_title = "This is a Title";
@ -5,9 +7,17 @@ const string site_subtitle = "but this is a subtitle";
const bool nav_tree_vert = false;
string[string] handlers;
string[StaticRegex!char] handlers;
void init_handlers() {
handlers["(.*).md"] = "contrib/Markdown.pl";
handlers["changelog"] = "changelog.sh";
handler!("(.*).md").add("contrib/Markdown.pl");
handler!("changelog").add("changelog.sh");
}
template handler(string pattern) {
void add(string h) {
try {
handlers[ctRegex!(pattern)] = h;
} catch (std.regex.Exception re) { return; }
}
}

View File

@ -92,9 +92,9 @@ 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 rstr, string h; handlers) {
foreach (StaticRegex!char reg, string h; handlers) {
try {
if (match(url, regex(rstr, ""))) {
if (match(url, reg)) {
html(shell(dweb_root ~ "/bin/" ~ h ~ " " ~ url));
html_pop("</div>");
return;
@ -107,9 +107,9 @@ 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 rstr, string h; handlers) {
foreach (StaticRegex!char reg, string h; handlers) {
try {
if (match(name, regex(rstr, ""))) {
if (match(name, reg)) {
html(shell(dweb_root ~ "/bin/" ~ h ~ " " ~ f));
html_pop("</div>");
return;