milk-it-sitemapper 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +18 -3
- data/lib/sitemapper/helpers.rb +12 -16
- data/lib/sitemapper/map.rb +34 -14
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -68,6 +68,21 @@ You can also do it on your sweepers (which is a better choice)
|
|
68
68
|
Sorry, we do not support Merb yet. (Actually, it will depend on the
|
69
69
|
demand)
|
70
70
|
|
71
|
+
=== Mapping multiple URLs
|
72
|
+
|
73
|
+
If you need to map multiple urls, it's high recommended to use #map_urls, to do so:
|
74
|
+
|
75
|
+
map.map_urls do |map|
|
76
|
+
map.map_url('http://www.example.org/about')
|
77
|
+
...
|
78
|
+
end
|
79
|
+
|
80
|
+
or, if you are in a Sweeper or Controller
|
81
|
+
|
82
|
+
map_urls do |map|
|
83
|
+
...
|
84
|
+
end
|
85
|
+
|
71
86
|
=== Other SEO techniques
|
72
87
|
|
73
88
|
With SEO, you'll have some view helpers to easily apply some SEO
|
@@ -121,18 +136,18 @@ If you need to change the default method lookup for page, you have two ways:
|
|
121
136
|
...
|
122
137
|
end
|
123
138
|
|
124
|
-
If you
|
125
|
-
carlos@milk-it.net
|
139
|
+
If you don't specify all the fields, it'll be merged with the Sitemapper.meta_lookup config.
|
126
140
|
|
127
141
|
=== Hope you enjoy!
|
128
142
|
|
143
|
+
If you want to suggest any other SEO technique, suggest it on carlos@milk-it.net
|
144
|
+
|
129
145
|
== To do:
|
130
146
|
|
131
147
|
* Add sitemap options to routes
|
132
148
|
* Create separate files for dynamic and static routes (then, we'll can
|
133
149
|
delete all the static on startup)
|
134
150
|
* Write test code (sorry, I really didn't it)
|
135
|
-
* Write the Sitemapper::Map#map_urls method
|
136
151
|
|
137
152
|
Copyright (c) 2008 Carlos Júnior, released under the MIT license
|
138
153
|
Copyright (c) 2008 Milk-it Software House, released under the MIT license
|
data/lib/sitemapper/helpers.rb
CHANGED
@@ -21,24 +21,20 @@ module Sitemapper
|
|
21
21
|
return nil # dummies safe!
|
22
22
|
end
|
23
23
|
|
24
|
-
def page_with_object(
|
25
|
-
|
24
|
+
def page_with_object(obj) # :nodoc:
|
25
|
+
obj = begin
|
26
|
+
lookup = Sitemapper.meta_lookup.merge(obj.class.sitemapper_config || {})
|
27
|
+
defs = {}
|
28
|
+
[:title, :desc, :keywords].each do |key|
|
29
|
+
method = lookup[key]
|
30
|
+
method = method.find {|m| obj.respond_to?(m)} if method.is_a?(Array)
|
26
31
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
methods.find {|m| obj.respond_to?(m)}
|
32
|
-
elsif methods.is_a?(String) || methods.is_a?(Symbol)
|
33
|
-
methods
|
34
|
-
end
|
35
|
-
logger.debug(">>> #{method}")
|
36
|
-
return method.nil?? nil : obj.send(method)
|
37
|
-
end # Do you think it's ugly? You have to see my grandma in underwear
|
32
|
+
defs[key] = method.nil?? nil : obj.send(method)
|
33
|
+
end # Do you think it's ugly? You have to see my grandma in underwear
|
34
|
+
defs
|
35
|
+
end unless obj.is_a?(Hash)
|
38
36
|
|
39
|
-
|
40
|
-
@_desc = lookup_method.call(defs, :desc)
|
41
|
-
@_keys = lookup_method.call(defs, :keywords)
|
37
|
+
return page_without_object(obj)
|
42
38
|
end
|
43
39
|
alias_method :page_without_object, :page
|
44
40
|
alias_method :page, :page_with_object
|
data/lib/sitemapper/map.rb
CHANGED
@@ -9,7 +9,7 @@ module Sitemapper
|
|
9
9
|
|
10
10
|
# Returns the site root (previously defined with site_root=)
|
11
11
|
def self.site_root
|
12
|
-
@@site_root
|
12
|
+
@@site_root ||= 'http://www.example.com/'
|
13
13
|
end
|
14
14
|
|
15
15
|
# Set the site root for the generated URLs
|
@@ -26,6 +26,7 @@ module Sitemapper
|
|
26
26
|
@builder = REXML::Document.new(File.exists?(file)? File.read(file) : nil)
|
27
27
|
@locker = Mutex.new
|
28
28
|
@file = file
|
29
|
+
@write = true
|
29
30
|
initialize_map
|
30
31
|
end
|
31
32
|
|
@@ -41,14 +42,8 @@ module Sitemapper
|
|
41
42
|
#
|
42
43
|
# See http://www.sitemaps.org/protocol.php
|
43
44
|
def map_url(loc, opts={})
|
44
|
-
lastmod, changefreq, priority = extract_options(opts)
|
45
45
|
@locker.synchronize do
|
46
|
-
|
47
|
-
(url.elements['loc'] || url.add_element('loc')).text = loc
|
48
|
-
(url.elements['lastmod'] || url.add_element('lastmod')).text = lastmod.strftime('%Y-%m-%d') if lastmod
|
49
|
-
(url.elements['changefreq'] || url.add_element('change_freq')).text = changefreq.to_s if changefreq
|
50
|
-
(url.elements['priority'] || url.add_element('priority')).text = '%.2f' % priority if priority
|
51
|
-
|
46
|
+
set_url_node(loc, opts)
|
52
47
|
write_file
|
53
48
|
end
|
54
49
|
end
|
@@ -61,8 +56,21 @@ module Sitemapper
|
|
61
56
|
map_url(URI.join(Map.site_root, path), opts)
|
62
57
|
end
|
63
58
|
|
64
|
-
|
65
|
-
|
59
|
+
# Map multiple URLs and write once (at the end)
|
60
|
+
#
|
61
|
+
# Usage:
|
62
|
+
#
|
63
|
+
# map.map_urls do |map|
|
64
|
+
# map.map_url('http://www.example.com/about')
|
65
|
+
# map.unmap_url('http://www.example.com/contact')
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
def map_urls
|
69
|
+
@write = false
|
70
|
+
yield(self)
|
71
|
+
@write = true
|
72
|
+
|
73
|
+
@locker.synchronize { write_file }
|
66
74
|
end
|
67
75
|
|
68
76
|
# Unmap the given localization (<tt>loc</tt>)
|
@@ -70,9 +78,7 @@ module Sitemapper
|
|
70
78
|
# * <tt>loc</tt> is the URL to be unmaped
|
71
79
|
def unmap_url(loc)
|
72
80
|
@locker.synchronize do
|
73
|
-
|
74
|
-
url.remove if url
|
75
|
-
|
81
|
+
unset_url_node(loc)
|
76
82
|
write_file
|
77
83
|
end
|
78
84
|
end
|
@@ -86,6 +92,20 @@ module Sitemapper
|
|
86
92
|
|
87
93
|
private
|
88
94
|
|
95
|
+
def set_url_node(loc, opts)
|
96
|
+
lastmod, changefreq, priority = extract_options(opts)
|
97
|
+
url = get_url(loc) || @builder.root.add_element('url')
|
98
|
+
(url.elements['loc'] || url.add_element('loc')).text = loc
|
99
|
+
(url.elements['lastmod'] || url.add_element('lastmod')).text = lastmod.strftime('%Y-%m-%d') if lastmod
|
100
|
+
(url.elements['changefreq'] || url.add_element('change_freq')).text = changefreq.to_s if changefreq
|
101
|
+
(url.elements['priority'] || url.add_element('priority')).text = '%.2f' % priority if priority
|
102
|
+
end
|
103
|
+
|
104
|
+
def unset_url_node(loc)
|
105
|
+
url = get_url(loc)
|
106
|
+
url.remove if url
|
107
|
+
end
|
108
|
+
|
89
109
|
# Extract the options to map an URL
|
90
110
|
#
|
91
111
|
# see <tt>map_url</tt> to understand.
|
@@ -104,7 +124,7 @@ module Sitemapper
|
|
104
124
|
|
105
125
|
# Write the file to disk (run only synchronized with @locker)
|
106
126
|
def write_file
|
107
|
-
File.open(@file, 'w') {|file| @builder.write(file, INDENT)}
|
127
|
+
File.open(@file, 'w') {|file| @builder.write(file, INDENT)} if @write
|
108
128
|
end
|
109
129
|
|
110
130
|
# Initialize the map (called on the boot, normally)
|