From 1d3a63d75fce583032333a9d088a0d6d6569f58d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 7 Jul 2012 16:09:23 +0200 Subject: o Splitting 'hrefs' so the user can click on any path segment. --- package.json | 6 ++++++ routes/index.js | 26 ++++++++++++++++++++++++++ test/split.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ views/data.jade | 7 +++++-- 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 test/split.js diff --git a/package.json b/package.json index 3b3c99c..f8a0fcc 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,14 @@ "jade": "~0.26.3", "underscore": "~1.3.3" }, + "devDependencies": { + "mocha": "1.0.3" + }, "engines": { "node": "0.6.x", "npm": "1.1.x" + }, + "scripts": { + "test": "./node_modules/.bin/mocha" } } diff --git a/routes/index.js b/routes/index.js index fb81a41..cace65e 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,9 +4,35 @@ var collection_json = require('collection_json') , util = require('util') , _ = require('underscore'); +function split(str) { + var u = url.parse(str); + + var segments = u.pathname.replace(/\/$/, '').split('/'); + + var splits = _.map(_.range(segments.length), function(i) { + var x = _.clone(u); + x.pathname = _.first(segments, i + 1).join('/'); + x.search = ''; + if(i == 0) { + return [u.protocol + '//' + u.host, url.format(x)]; + } + else { + return ['/' + segments[i], url.format(x)]; + } + }); + + if(u.search) { + splits.push([(u.pathname.match(/\/$/) ? '/' : '') + u.search, url.format(u)]); + } + + return splits; +} +exports.split = split; + function urlgenerator(req) { var host = req.headers.host; return { + split: split, render: function(u) { return 'http://' + host + '/render?url=' + encodeURIComponent(u)}, delete: function(referer, u) { return 'http://' + host + '/delete?referer=' + encodeURIComponent(referer) + '&url=' + encodeURIComponent(u)}, isUrl: function(u) { diff --git a/test/split.js b/test/split.js new file mode 100644 index 0000000..4bf8277 --- /dev/null +++ b/test/split.js @@ -0,0 +1,44 @@ +var assert = require('assert') + , util = require('util') + , routes = require('../routes/index.js'); + +function assertSplit(url, expected) { + var actual = routes.split(url); + // console.log('actual ', util.inspect(actual)); + // console.log('expected', util.inspect(expected)); + assert.deepEqual(actual, expected); +} + +describe('routes.split', function() { + it('http://localhost:123/', function() { + assertSplit('http://localhost:123/', [ + ['http://localhost:123','http://localhost:123'] + ]); + }); + it('http://localhost:123/foo', function() { + assertSplit('http://localhost:123/foo', [ + ['http://localhost:123','http://localhost:123'], + ['/foo','http://localhost:123/foo'], + ]); + }); + it('http://localhost:123/foo/', function() { + assertSplit('http://localhost:123/foo/', [ + ['http://localhost:123','http://localhost:123'], + ['/foo','http://localhost:123/foo'], + ]); + }); + it('http://localhost:123/a?x=1&y=2', function() { + assertSplit('http://localhost:123/a?x=1&y=2', [ + ['http://localhost:123','http://localhost:123'], + ['/a','http://localhost:123/a'], + ['?x=1&y=2','http://localhost:123/a?x=1&y=2'] + ]); + }); + it('http://localhost:123/a/?x=1&y=2', function() { + assertSplit('http://localhost:123/a/?x=1&y=2', [ + ['http://localhost:123','http://localhost:123'], + ['/a','http://localhost:123/a'], + ['/?x=1&y=2','http://localhost:123/a/?x=1&y=2'] + ]); + }); +}); diff --git a/views/data.jade b/views/data.jade index dd79c12..dda1c55 100644 --- a/views/data.jade +++ b/views/data.jade @@ -6,13 +6,16 @@ mixin get_name(link, prefix, i) - var prefix = typeof prefix == 'string' ? prefix + ': ' : '' |#{prefix + (name || prompt || '#' + i)} +// TODO: Show show a 'copy' button to copy the entire link block href if typeof href != 'string' div: i no href else div - a(href=urlgenerator.render(href)) #{href} - | (opens in explorer) + - var splits = urlgenerator.split(href) + for split in splits + a(href=urlgenerator.render(split[1]), title='Explore #{split[1]}') #{split[0]} + | block link - var href = link.href -- cgit v1.2.3