absolutize 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -1,9 +1,9 @@
1
1
 
2
- h1. Absolutize v0.0.1
2
+ h1. Absolutize v0.0.2
3
3
 
4
- h2. Intro
4
+ h2. Intro
5
5
 
6
- I know, I know, i should just fix URI.join, tough i haven't!
6
+ Absolutize is essentially a replication of the URI.join or URI.merge methods, however, these strictly adhere to html standards, unfortunately the rest of the world doesn't. The absolutize gem attempts to replicate url parsing that browsers use, ignoring the encoding issues etc.
7
7
 
8
8
  h2. Installation
9
9
 
@@ -13,10 +13,47 @@ bq. gem install absolutize
13
13
 
14
14
  h2. Usage
15
15
 
16
- absol = Absolutize.new(base_url)
17
- uri = absol.url(relative_url)
16
+ h4. new(base_url, options)
18
17
 
19
- h3. Methods
18
+ Creates a new absolutize object based on a base_url
20
19
 
21
- new - creates a new absolutize object based on a base_url
22
- url - absolutizes the relative_url based on the base_url
20
+ * base_url - This is the base url for the absolutize method, it should either be the url of the current page or if a base tag is supplied it should be the href attribute of the base tag
21
+
22
+ * options - Options are passed in as a hash,
23
+ ** :remove_anchors - removes any anchor tags (false)
24
+ ** :force_escaping - forces escaping of urls if it hasn't been done already (true)
25
+
26
+ bq. absolutizer = Absolutize.new("http://www.vamosa.com/mainfolder/", :remove_anchors => true)
27
+
28
+ h4. url(rel_url)
29
+
30
+ Absolutizes the url that is passed in based on the options that have already been configured
31
+
32
+ * rel_url - The url to absolutize, this is the url that is used on the page and should be absolutized in relation to the base url.
33
+
34
+ bq. absolutizer.url("subfolder/index.html")
35
+
36
+
37
+ h2. License
38
+
39
+ h3. The MIT License
40
+
41
+ Copyright (c) 2010 Vamosa Limited
42
+
43
+ Permission is hereby granted, free of charge, to any person obtaining a copy
44
+ of this software and associated documentation files (the "Software"), to deal
45
+ in the Software without restriction, including without limitation the rights
46
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
47
+ copies of the Software, and to permit persons to whom the Software is
48
+ furnished to do so, subject to the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be included in
51
+ all copies or substantial portions of the Software.
52
+
53
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
56
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
59
+ THE SOFTWARE.
data/lib/absolutize.rb CHANGED
@@ -4,13 +4,19 @@ require File.expand_path(File.dirname(__FILE__) + '/absolutize')
4
4
 
5
5
  class Absolutize
6
6
 
7
- def initialize(base_url)
7
+ def initialize(base_url, options = {})
8
8
  @base_url = base_url
9
+ @options = options
10
+
11
+ @options[:remove_anchors] = false
12
+ @options[:force_escaping] = true
13
+
9
14
  end
10
15
 
11
16
  def url(new_url)
12
17
  # encode the url if the new url contains spaces but doesn't contain %, i.e isn't already encoded
13
- new_url = URI.encode(new_url, " <>\{\}|\\\^\[\]`") if not new_url =~ /%/
18
+ new_url = new_url.split("#").first if new_url.include?"#" and @options[:remove_anchors]
19
+ new_url = URI.encode(new_url, " <>\{\}|\\\^\[\]`") if not new_url =~ /%/ and @options[:force_escaping]
14
20
  begin
15
21
  URI.join(@base_url, new_url).to_s
16
22
  rescue URI::InvalidURIError => urie
@@ -24,11 +30,13 @@ class Absolutize
24
30
  "#{uri.scheme}://#{uri.host}#{new_url}"
25
31
  end
26
32
  elsif new_url =~ /\Ahttp/
27
- # new url is absolute anyway
33
+ #new url is absolute anyway
28
34
  new_url
29
35
  else
30
36
  raise "Unable to absolutize #{base_url} and #{new_url}"
31
37
  end
32
38
  end
39
+
40
+ ### remove double slashes
33
41
  end
34
42
  end
data/lib/absolutize.rb~ CHANGED
@@ -1,15 +1,22 @@
1
1
  require 'rubygems'
2
+ require 'uri'
2
3
  require File.expand_path(File.dirname(__FILE__) + '/absolutize')
3
4
 
4
5
  class Absolutize
5
6
 
6
- def initialize(base_url)
7
+ def initialize(base_url, options = {})
7
8
  @base_url = base_url
9
+ @options = options
10
+
11
+ @options[:remove_anchors] = false
12
+ @options[:force_escaping] = true
13
+
8
14
  end
9
15
 
10
16
  def url(new_url)
11
17
  # encode the url if the new url contains spaces but doesn't contain %, i.e isn't already encoded
12
- new_url = URI.encode(new_url, " <>\{\}|\\\^\[\]`") if not new_url =~ /%/
18
+ new_url = new_url.split("#").first if new_url.include?"#" and @options[:remove_anchors]
19
+ new_url = URI.encode(new_url, " <>\{\}|\\\^\[\]`") if not new_url =~ /%/ and @options[:force_escaping]
13
20
  begin
14
21
  URI.join(@base_url, new_url).to_s
15
22
  rescue URI::InvalidURIError => urie
@@ -23,11 +30,13 @@ class Absolutize
23
30
  "#{uri.scheme}://#{uri.host}#{new_url}"
24
31
  end
25
32
  elsif new_url =~ /\Ahttp/
26
- # new url is absolute anyway
33
+ #new url is absolute anyway
27
34
  new_url
28
35
  else
29
36
  raise "Unable to absolutize #{base_url} and #{new_url}"
30
37
  end
31
38
  end
39
+
40
+ ### remove double slashes
32
41
  end
33
42
  end
@@ -15,9 +15,9 @@ describe Absolutize do
15
15
  @absolutize.url("http://www.bbc.co.uk/").to_s.should == "http://www.bbc.co.uk/"
16
16
  end
17
17
  it "should return the absolute url for a relative to root url" do
18
- @absolutize.url("/asdf.html").to_s.should == "http://www.bbc.co.uk/asdf.html"
18
+ @absolutize.url("/asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
19
19
  end
20
20
  it "should return the absolute url for a relative to folder url" do
21
- @absolutize.url("asdf.html").to_s.should == "http://www.bbc.co.uk/asdf.html"
21
+ @absolutize.url("asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
22
22
  end
23
23
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: absolutize
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Stewart McKee
@@ -37,7 +37,7 @@ files:
37
37
  - lib/absolutize.rb~
38
38
  - README.textile
39
39
  has_rdoc: false
40
- homepage: http://www.vamosa.com/
40
+ homepage: http://github.com/vamosa/absolutize
41
41
  licenses: []
42
42
 
43
43
  post_install_message: