darkhelmet-darkext 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -15,6 +15,7 @@ lib/darkext/integer.rb
15
15
  lib/darkext/io.rb
16
16
  lib/darkext/numeric.rb
17
17
  lib/darkext/object.rb
18
+ lib/darkext/sitemap_generator.rb
18
19
  lib/darkext/statistics.rb
19
20
  lib/darkext/string.rb
20
21
  lib/darkext/symbol.rb
data/lib/darkext/hash.rb CHANGED
@@ -8,4 +8,34 @@ class Hash
8
8
  def with_defaults!(defaults)
9
9
  self.merge!(defaults) { |key,old,new| old.nil? ? new : old }
10
10
  end
11
+
12
+ def nested_find(*keys)
13
+ v = self
14
+ keys.each do |key|
15
+ v = v[key]
16
+ return nil if v.nil?
17
+ end
18
+ return v
19
+ end
20
+
21
+ # http://snippets.dzone.com/posts/show/4146
22
+ def deep_merge!(second)
23
+ # From: http://www.ruby-forum.com/topic/142809
24
+ # Author: Stefan Rusterholz
25
+ merger = proc { |key,v1,v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
26
+ self.merge!(second, &merger)
27
+ end
28
+
29
+ def nested_hash(array)
30
+ node = self
31
+ array.each do |i|
32
+ node[i]=Hash.new if node[i].nil?
33
+ node = node[i]
34
+ end
35
+ self
36
+ end
37
+
38
+ def merge_nested_hash!(nested_hash)
39
+ deep_merge!(nested_hash)
40
+ end
11
41
  end
@@ -0,0 +1,90 @@
1
+ require 'hpricot'
2
+ require 'open-uri'
3
+ require 'builder'
4
+ require 'darkext/hash'
5
+
6
+ module Darkext
7
+ class Darkext::SitemapBuilder
8
+ FORUM_CLASS_DEFAULT = 'forumtitle'
9
+ TOPIC_CLASS_DEFAULT = 'topictitle'
10
+ FORUM_PRIORITY_DEFAULT = 0.8
11
+ TOPIC_PRIORITY_DEFAULT = 0.5
12
+ PAGE_PRIORITY_DEFAULT = 0.1
13
+ FORUM_CHANGE_FREQ_DEFAULT = 'daily'
14
+ TOPIC_CHANGE_FREQ_DEFAULT = 'hourly'
15
+ PAGE_CHANGE_FREQ_DEFAULT = 'monthly'
16
+
17
+ def initialize(opts={})
18
+ defaults = {
19
+ :forum => {
20
+ :class => FORUM_CLASS_DEFAULT,
21
+ :priority => FORUM_PRIORITY_DEFAULT,
22
+ :change_freq => FORUM_CHANGE_FREQ_DEFAULT
23
+ },
24
+ :topic => {
25
+ :class => TOPIC_CLASS_DEFAULT,
26
+ :priority => TOPIC_PRIORITY_DEFAULT,
27
+ :change_freq => TOPIC_CHANGE_FREQ_DEFAULT
28
+ },
29
+ :page => {
30
+ :priority => PAGE_PRIORITY_DEFAULT,
31
+ :change_freq => PAGE_CHANGE_FREQ_DEFAULT
32
+ }
33
+ }
34
+ defaults.deep_merge!(opts)
35
+
36
+ @options = defaults
37
+
38
+ @forums = Array.new
39
+ @topics = Array.new
40
+ @pages = Array.new
41
+ end
42
+
43
+ def index(url, target = $stdout)
44
+ @pages << url
45
+
46
+ root = open(url) { |f| Hpricot(f) }
47
+ (root/"a.#{@options.nested_find(:forum,:class)}").each do |link|
48
+ index_forum(link.attributes['href'].to_s)
49
+ end
50
+
51
+ xml = Builder::XmlMarkup.new(:target => $stdout, :indent => 1)
52
+ xml.instruct!
53
+ xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
54
+ @pages.each do |page|
55
+ xml.url do
56
+ xml.loc(page)
57
+ xml.changefreq(@options.nested_find(:page,:change_freq))
58
+ xml.priority(@options.nested_find(:page,:priority))
59
+ end
60
+ end
61
+ @forums.each do |forum|
62
+ xml.url do
63
+ xml.loc(forum)
64
+ xml.changefreq(@options.nested_find(:forum,:change_freq))
65
+ xml.priority(@options.nested_find(:forum,:priority))
66
+ end
67
+ end
68
+ @topics.each do |topic|
69
+ xml.url do
70
+ xml.loc(topic)
71
+ xml.changefreq(@options.nested_find(:topic,:change_freq))
72
+ xml.priority(@options.nested_find(:topic,:priority))
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ private
79
+ def index_forum(href)
80
+ root = open(href) { |f| Hpricot(f) }
81
+ (root/"a.#{@options.nested_find(:forum,:class)}").each do |link|
82
+ index_forum(link.attributes['href'].to_s)
83
+ end
84
+ (root/"a.#{@options.nested_find(:topic,:class)}").each do |link|
85
+ @topics << link.attributes['href'].to_s
86
+ end
87
+ @forums << href
88
+ end
89
+ end
90
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: darkhelmet-darkext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Huckstep
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-18 00:00:00 -08:00
12
+ date: 2009-01-25 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -58,6 +58,7 @@ files:
58
58
  - lib/darkext/io.rb
59
59
  - lib/darkext/numeric.rb
60
60
  - lib/darkext/object.rb
61
+ - lib/darkext/sitemap_generator.rb
61
62
  - lib/darkext/statistics.rb
62
63
  - lib/darkext/string.rb
63
64
  - lib/darkext/symbol.rb