Merge pull request #3 from luqmana/master

Compile Time Static Regex
This commit is contained in:
Jacob Parker 2012-02-27 16:55:18 -08:00
commit d790af3051
3 changed files with 18 additions and 12 deletions

View File

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

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

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