cartographer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2011-03-23
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/cartographer.rb
6
+ lib/xml/sitemap.haml
7
+ lib/xml/sitemap_uri.haml
8
+ test/test_cartographer.rb
data/README.txt ADDED
@@ -0,0 +1,85 @@
1
+ = Cartographer
2
+
3
+ http://github.com/erikh/cartographer/
4
+
5
+ == DESCRIPTION:
6
+
7
+ Assists in the creation of sitemaps that google, yahoo, etc, use. Has finding,
8
+ rewriting, and attribute manipulation before generation. Does not require any
9
+ external dependency other than haml.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ * Does not require rails or any web framework.
14
+ * Works with static generators.
15
+ * Is able to transform a subtree of documents into a proper sitemap.
16
+
17
+ == SYNOPSIS:
18
+
19
+ sm = Cartographer.new("http://example.org")
20
+ sm.add(Cartographer::URL.new(:location => URI.parse("http://example.org/ethereal")))
21
+ sm.add_tree("/some/doc/root") #=> adds all the files in the docroot
22
+ puts sm.to_xml #=> outputs your sitemap.
23
+
24
+ #
25
+ # filtering and remapping
26
+ #
27
+ sm = Cartographer.new("http://example.org")
28
+ # pass a block to get the paths found and rewrite them.
29
+ # return nil if you want that path (and sub-paths) to be omitted.
30
+ sm.add_tree("/some/doc/root") { |x| x.sub(/.html$/, '/') }
31
+ File.open('sitemap.xml', 'w') { |f| f << sm.to_xml }
32
+
33
+ See Cartographer and Cartographer::URL RDoc for more information.
34
+
35
+ If you need more information about the sitemap format:
36
+ http://en.wikipedia.org/wiki/Sitemaps
37
+
38
+ == REQUIREMENTS:
39
+
40
+ Users:: haml
41
+ Developers:: minitest, hoe-roodi, hoe-git, hoe-reek, nokogiri
42
+
43
+ == INSTALL:
44
+
45
+ * gem install cartographer
46
+
47
+ == DEVELOPERS:
48
+
49
+ For developers that use rvm, a 'rake install' task has been provided that will
50
+ avoid using sudo. You will still need to install the dependencies manually
51
+ instead of using 'rake newb'.
52
+
53
+ For everyone else..
54
+
55
+ After checking out the source, run:
56
+
57
+ $ rake newb
58
+
59
+ This task will install any missing dependencies, run the tests/specs,
60
+ and generate the RDoc.
61
+
62
+ == LICENSE:
63
+
64
+ (The MIT License)
65
+
66
+ Copyright (c) 2011 Erik Hollensbe and Zetetic, LLC.
67
+
68
+ Permission is hereby granted, free of charge, to any person obtaining
69
+ a copy of this software and associated documentation files (the
70
+ 'Software'), to deal in the Software without restriction, including
71
+ without limitation the rights to use, copy, modify, merge, publish,
72
+ distribute, sublicense, and/or sell copies of the Software, and to
73
+ permit persons to whom the Software is furnished to do so, subject to
74
+ the following conditions:
75
+
76
+ The above copyright notice and this permission notice shall be
77
+ included in all copies or substantial portions of the Software.
78
+
79
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
80
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
81
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
82
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
83
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
84
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
85
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.plugins.delete :rubyforge
7
+ Hoe.plugin :git
8
+ Hoe.plugin :rcov
9
+ Hoe.plugin :roodi
10
+ Hoe.plugin :reek
11
+ Hoe.plugin :minitest
12
+
13
+ spec = Hoe.spec 'cartographer' do
14
+ developer('Erik Hollensbe', 'erik@hollensbe.org')
15
+ developer('Team Zetetic', 'support@zetetic.net')
16
+
17
+ self.rubyforge_name = nil
18
+
19
+ extra_deps << ['haml', '>= 0']
20
+
21
+ extra_dev_deps << ['minitest', '>= 0']
22
+ extra_dev_deps << ['hoe-roodi', '>= 0']
23
+ extra_dev_deps << ['hoe-reek', '>= 0']
24
+ extra_dev_deps << ['hoe-git', '>= 0']
25
+ extra_dev_deps << ['nokogiri', '>= 0']
26
+ end
27
+
28
+ task :install => [:gem] do
29
+ sh "gem install pkg/#{spec.name}-#{spec.version}.gem"
30
+ end
31
+
32
+ # vim: syntax=ruby
@@ -0,0 +1,172 @@
1
+ require 'find'
2
+ require 'haml'
3
+ require 'uri'
4
+ require 'time'
5
+
6
+ #
7
+ # Cartographer is a sitemap manipulation library.
8
+ #
9
+ #
10
+ # sm = Cartographer.new("http://example.org")
11
+ # sm.add(Cartographer::URL.new(:location => URI.parse("http://example.org/ethereal")))
12
+ # sm.add_tree("/some/doc/root") #=> adds all the files in the docroot
13
+ # puts sm.to_xml #=> outputs your sitemap.
14
+ #
15
+ #
16
+ # # filtering and remapping
17
+ # sm = Cartographer.new("http://example.org")
18
+ # # pass a block to get the paths found and rewrite them.
19
+ # # return nil if you want that path (and sub-paths) to be omitted.
20
+ # sm.add_tree("/some/doc/root") { |x| x.sub(/.html$/, '/') }
21
+ # File.open('sitemap.xml', 'w') { |f| f << sm.to_xml }
22
+ #
23
+ # See Cartographer and Cartographer::URL RDoc for more information.
24
+ #
25
+ class Cartographer
26
+ VERSION = '1.0.0'
27
+
28
+ ##
29
+ # List of Cartographer::URL objects
30
+ attr_reader :urls
31
+
32
+ ##
33
+ # Your site's root path.
34
+ attr_reader :root_uri
35
+
36
+ ##
37
+ # The default change frequency for all URLs.
38
+ attr_accessor :default_changefreq
39
+
40
+ XML_TEMPLATE = File.expand_path("xml/sitemap.haml", File.dirname(File.expand_path(__FILE__)))
41
+
42
+ ##
43
+ # Constructor.
44
+ #
45
+ # Takes a root_uri which can be anything resolvable as a string, or URI::HTTP.
46
+ #
47
+ # Also takes a default change frequency which is just a string corresponding
48
+ # to the different types.
49
+ def initialize(root_uri, default_changefreq=nil)
50
+ @urls = []
51
+ @root_uri = root_uri.kind_of?(URI::HTTP) ? root_uri : URI.parse(root_uri.to_s)
52
+ @default_changefreq = default_changefreq
53
+ end
54
+
55
+ ##
56
+ #
57
+ # Adds a Cartographer::URL to the list of URLs for processing.
58
+ #
59
+ def add(url)
60
+ raise ArgumentError, "Please pass Cartographer::URL objects to this method" unless url.kind_of?(URL)
61
+ @urls.push(url)
62
+ end
63
+
64
+ ##
65
+ #
66
+ # Given a path, walks the path and transforms all the files and directories
67
+ # in it into Cartographer::URL objects.
68
+ #
69
+ # If given a block, it will yield each relative path to this block as the
70
+ # first argument. The return value of this block is used as the path, making
71
+ # them transformable. If you return nil, this path (and any sub-paths) are
72
+ # pruned from the find.
73
+ #
74
+ # See the Constructor for examples or README.txt.
75
+ #
76
+ def add_tree(base_path)
77
+ Find.find(base_path) do |path|
78
+ new_path = path.sub %r!^#{Regexp.escape base_path}!, ''
79
+ uri = root_uri.dup
80
+ stat = File.stat(path)
81
+
82
+ if File.directory?(path)
83
+ new_path += "/"
84
+ end
85
+
86
+ if block_given?
87
+ new_path = yield new_path
88
+ unless new_path
89
+ Find.prune
90
+ end
91
+ end
92
+
93
+ uri.path = new_path
94
+ add URL.new(:location => uri, :lastmod => stat.mtime, :changefreq => @default_changefreq)
95
+ end
96
+ end
97
+
98
+ ##
99
+ #
100
+ # Transforms the given Cartographer::URL list to the sitemap XML format.
101
+ #
102
+ def to_xml
103
+ Haml::Engine.new(File.read(XML_TEMPLATE)).render(self)
104
+ end
105
+
106
+ ##
107
+ # Cartographer::URL is a structure to enforce the sitemap format.
108
+ #
109
+ class URL
110
+ XML_TEMPLATE = File.expand_path("xml/sitemap_uri.haml", File.dirname(File.expand_path(__FILE__)))
111
+
112
+ ##
113
+ # Location of item, stored as a kind of URI::Generic. Required.
114
+ attr_accessor :location
115
+
116
+ ##
117
+ # Last modification time of this item.
118
+ attr_accessor :lastmod
119
+
120
+ ##
121
+ # Change frequency, stored as a string.
122
+ attr_accessor :changefreq
123
+
124
+ ##
125
+ # Priority, stored as a Numeric object, from 0 to 1 with decimal values included.
126
+ attr_accessor :priority
127
+
128
+
129
+ ##
130
+ #
131
+ # Constructor. Takes a hash of arguments:
132
+ #
133
+ # location:: A kind of URI::Generic
134
+ # lastmod:: A Time object which represents the last modification time.
135
+ # changefreq:: A string which contains the frequency.
136
+ # priority:: A decimal value (any kind of Numeric will do) from 0 - 1.
137
+ #
138
+ def initialize(args)
139
+ args.each do |key, value|
140
+ send(key.to_s + "=", value)
141
+ end
142
+
143
+ unless self.location
144
+ raise ArgumentError, "the location argument is required."
145
+ end
146
+
147
+ unless self.location.kind_of?(URI::Generic)
148
+ raise ArgumentError, "location must be a kind of URI::Generic"
149
+ end
150
+
151
+ if self.lastmod and !self.lastmod.kind_of?(Time)
152
+ raise ArgumentError, "lastmod must be a kind of Time."
153
+ end
154
+
155
+ if self.changefreq and !self.changefreq.kind_of?(String)
156
+ raise ArgumentError, "changefreq must be a kind of String"
157
+ end
158
+
159
+ if self.priority and !self.priority.kind_of?(Numeric)
160
+ raise ArgumentError, "priority must be a kind of Numeric"
161
+ end
162
+ end
163
+
164
+ ##
165
+ #
166
+ # Convert this object to the <url>...</url> representation.
167
+ #
168
+ def to_xml
169
+ Haml::Engine.new(File.read(XML_TEMPLATE)).render(self)
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,4 @@
1
+ !!! XML
2
+ %urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"}
3
+ - @urls.each do |url|
4
+ %url=url.to_xml
@@ -0,0 +1,7 @@
1
+ %loc=location.to_s
2
+ - if lastmod
3
+ %lastmod=lastmod.iso8601
4
+ - if changefreq
5
+ %changefreq=changefreq
6
+ - if priority
7
+ %priority=priority
@@ -0,0 +1,74 @@
1
+ require "minitest/unit"
2
+ require "cartographer"
3
+ require 'nokogiri'
4
+
5
+ class TestCartographer < MiniTest::Unit::TestCase
6
+ def test_constructor
7
+ sm = Cartographer.new("http://example.org")
8
+ assert_equal [], sm.urls
9
+ assert_kind_of URI::HTTP, sm.root_uri
10
+ assert_equal "http://example.org", sm.root_uri.to_s
11
+ refute sm.default_changefreq
12
+
13
+ sm = Cartographer.new("http://example.org", "always")
14
+ assert_equal "always", sm.default_changefreq
15
+ end
16
+
17
+ def test_add
18
+ sm = Cartographer.new("http://example.org")
19
+ assert_raises(ArgumentError) { sm.add("fart") }
20
+ sm.add(Cartographer::URL.new(:location => URI.parse("http://example.org/fart")))
21
+ end
22
+
23
+ def test_add_tree
24
+ sm = Cartographer.new("http://example.org")
25
+ sm.add_tree(File.expand_path("test"))
26
+
27
+ assert_includes(
28
+ sm.urls.map(&:location),
29
+ URI.parse("http://example.org/test_cartographer.rb")
30
+ )
31
+
32
+ sm = Cartographer.new("http://example.org", "always")
33
+ sm.add_tree(File.expand_path("test"))
34
+
35
+ url = sm.urls.select { |x| x.location.to_s == "http://example.org/test_cartographer.rb" }[0]
36
+
37
+ assert_equal "always", url.changefreq
38
+ assert_equal File.stat(__FILE__).mtime, url.lastmod
39
+
40
+ sm = Cartographer.new("http://example.org")
41
+ sm.add_tree(File.expand_path("test")) { |x| x.sub(/\.rb$/, '/') }
42
+
43
+ assert_includes(
44
+ sm.urls.map(&:location),
45
+ URI.parse("http://example.org/test_cartographer/")
46
+ )
47
+ end
48
+
49
+ def test_to_xml
50
+ sm = Cartographer.new("http://example.org")
51
+ sm.add_tree(File.expand_path("test"))
52
+
53
+ xml = Nokogiri::XML.parse(sm.to_xml)
54
+ assert_equal [ "http://example.org/", "http://example.org/test_cartographer.rb" ],
55
+ xml.root.xpath('//xmlns:loc', xml.root.namespaces).map(&:content)
56
+
57
+ sm = Cartographer.new("http://example.org", "always")
58
+ sm.add_tree(File.expand_path("test"))
59
+
60
+ xml = Nokogiri::XML.parse(sm.to_xml)
61
+ assert_equal [ "http://example.org/", "http://example.org/test_cartographer.rb" ],
62
+ xml.root.xpath('//xmlns:loc', xml.root.namespaces).map(&:content)
63
+
64
+ assert_equal [ "always", "always" ],
65
+ xml.root.xpath('//xmlns:changefreq', xml.root.namespaces).map(&:content)
66
+
67
+ sm = Cartographer.new("http://example.org")
68
+ sm.add_tree(File.expand_path("test")) { |x| x.sub(/\.rb$/, '/') }
69
+
70
+ xml = Nokogiri::XML.parse(sm.to_xml)
71
+ assert_equal [ "http://example.org/", "http://example.org/test_cartographer/" ],
72
+ xml.root.xpath('//xmlns:loc', xml.root.namespaces).map(&:content)
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cartographer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Erik Hollensbe
9
+ - Team Zetetic
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-03-26 00:00:00 -04:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: haml
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: "0"
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ prerelease: false
31
+ requirement: &id002 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: "0"
37
+ type: :development
38
+ version_requirements: *id002
39
+ - !ruby/object:Gem::Dependency
40
+ name: hoe-roodi
41
+ prerelease: false
42
+ requirement: &id003 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id003
50
+ - !ruby/object:Gem::Dependency
51
+ name: hoe-reek
52
+ prerelease: false
53
+ requirement: &id004 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id004
61
+ - !ruby/object:Gem::Dependency
62
+ name: hoe-git
63
+ prerelease: false
64
+ requirement: &id005 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id005
72
+ - !ruby/object:Gem::Dependency
73
+ name: nokogiri
74
+ prerelease: false
75
+ requirement: &id006 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id006
83
+ - !ruby/object:Gem::Dependency
84
+ name: hoe
85
+ prerelease: false
86
+ requirement: &id007 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.9.1
92
+ type: :development
93
+ version_requirements: *id007
94
+ description: |-
95
+ Assists in the creation of sitemaps that google, yahoo, etc, use. Has finding,
96
+ rewriting, and attribute manipulation before generation. Does not require any
97
+ external dependency other than haml.
98
+ email:
99
+ - erik@hollensbe.org
100
+ - support@zetetic.net
101
+ executables: []
102
+
103
+ extensions: []
104
+
105
+ extra_rdoc_files:
106
+ - History.txt
107
+ - Manifest.txt
108
+ - README.txt
109
+ files:
110
+ - History.txt
111
+ - Manifest.txt
112
+ - README.txt
113
+ - Rakefile
114
+ - lib/cartographer.rb
115
+ - lib/xml/sitemap.haml
116
+ - lib/xml/sitemap_uri.haml
117
+ - test/test_cartographer.rb
118
+ - .gemtest
119
+ has_rdoc: true
120
+ homepage: http://github.com/erikh/cartographer/
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options:
125
+ - --main
126
+ - README.txt
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: "0"
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: "0"
141
+ requirements: []
142
+
143
+ rubyforge_project:
144
+ rubygems_version: 1.6.2
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Assists in the creation of sitemaps that google, yahoo, etc, use
148
+ test_files:
149
+ - test/test_cartographer.rb