Permalink
Browse files

Enhance rewriting to support regular expressions.

  • Loading branch information...
1 parent b769caf commit c6c54ddcf9acea78a39fba2cc88c4ed4081fd87c @andrewdavey committed Apr 1, 2011
Showing with 23 additions and 9 deletions.
  1. +2 −1 readme.md
  2. +20 −7 src/Watcher.js
  3. +1 −1 src/vogue.js
View
@@ -23,7 +23,8 @@ Run the Vogue server.
vogue --port 8001 /path/to/website
--port : The port used for Vogue's HTTP server. Optional, defaults to 8001.
---rewrite : A rule in the form of token:replace-ment-token to help rewrite urls to filesystem paths
+--rewrite : A rule in the form of "regexp:replacement" (e.g. "v[0-9]/(.*)$:files/\$1" ) to help rewrite urls to filesystem paths.
+Submatches such as $1 will probably need to entered in your shell as \$1 to escape the $.
Open http://localhost:8001/ to see instructions for loading the Vogue client into your
web pages.
View
@@ -3,14 +3,30 @@ var fs = require('fs')
exports.Watcher = Watcher;
-function Watcher(webDirectory,rewrite) {
+function Watcher(webDirectory, rewrite) {
this.webDirectory = webDirectory;
- this.rewrite = rewrite;
// array of VogueClient objects
this.clients = [];
// filename -> number_of_clients_watching
this.fileWatcherCount = {};
+
+ if (rewrite) {
+ this.rewriteUrlToPath = createRewriter(rewrite);
+ }
+
+ function createRewriter(rewrite) {
+ var parts = rewrite.split(':');
+ if (parts.length === 2) {
+ var regex = new RegExp(parts[0]);
+ var replacement = parts[1];
+ return function (str) {
+ return str.replace(regex, replacement);
+ }
+ } else {
+ throw new Error('Rewrite must be of the form "regex:replacement".');
+ }
+ }
}
Watcher.prototype.addClient = function(client) {
@@ -22,11 +38,8 @@ Watcher.prototype.removeClient = function(client) {
};
Watcher.prototype.getFilenameForHref = function(href) {
- if(this.rewrite && typeof rewrite =="string") {
- var parts = this.rewrite.split(':');
- if(parts.length == 2) {
- href = href.replace(parts[0],parts[1]);
- }
+ if (this.rewriteUrlToPath) {
+ href = this.rewriteUrlToPath(href);
}
// Remove any querystring junk.
// e.g. "foo/bar.css?abc=123" --> "foo/bar.css"
View
@@ -82,7 +82,7 @@ function getOptions() {
{
name: ['--rewrite', '-r'],
type: 'string',
- help: 'foo:bar replaces foo in url with bar when looking for the file',
+ help: 'Expression of the form "regexp:replacement" rewrites a URL path into a file system path, relative to the website root directory. For example: --rewrite "v[0-9]/(.*)$:files/\\$1" would change "v1/demo.css" to "files/demo.css".',
'default': null
},
{

0 comments on commit c6c54dd

Please sign in to comment.