Skip to content

Commit

Permalink
Fix templates. Move more stuff to Utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvarga committed May 6, 2010
1 parent f3dfa24 commit e9413f8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 40 deletions.
3 changes: 2 additions & 1 deletion lib/sitemap_generator.rb
Expand Up @@ -3,14 +3,15 @@
require 'sitemap_generator/link_set'
require 'sitemap_generator/helper'
require 'sitemap_generator/templates'
require 'sitemap_generator/utilities'

module SitemapGenerator
silence_warnings do
VERSION = File.read(File.dirname(__FILE__) + "/../VERSION").strip
MAX_ENTRIES = 50_000
Sitemap = LinkSet.new
end

class << self
attr_accessor :root, :templates
end
Expand Down
26 changes: 2 additions & 24 deletions lib/sitemap_generator/link_set.rb
Expand Up @@ -61,7 +61,7 @@ def write_pending
def write_sitemap(file = upcoming_file)
buffer = ""
xml = Builder::XmlMarkup.new(:target => buffer)
eval(File.read(SitemapGenerator.templates[:sitemap_xml]), binding)
eval(SitemapGenerator.templates.sitemap_xml, binding)
filename = File.join(RAILS_ROOT, "public", file)
write_file(filename, buffer)
show_progress("Sitemap", filename, buffer) if verbose
Expand All @@ -73,7 +73,7 @@ def write_sitemap(file = upcoming_file)
def write_index
buffer = ""
xml = Builder::XmlMarkup.new(:target => buffer)
eval(File.read(SitemapGenerator.templates[:sitemap_index]), binding)
eval(SitemapGenerator.templates.sitemap_index, binding)
filename = File.join(RAILS_ROOT, "public", index_file)
write_file(filename, buffer)
show_progress("Sitemap Index", filename, buffer) if verbose
Expand Down Expand Up @@ -142,28 +142,6 @@ def show_progress(title, filename, buffer)
puts "** #{title} too big! The uncompressed size exceeds 10Mb" if buffer.size > 10.megabytes
end

# Copy templates/sitemap.rb to config if not there yet.
def install_sitemap_rb
if File.exist?(File.join(RAILS_ROOT, 'config/sitemap.rb'))
puts "already exists: config/sitemap.rb, file not copied"
else
FileUtils.cp(SitemapGenerator.templates[:sitemap_sample], File.join(RAILS_ROOT, 'config/sitemap.rb'))
puts "created: config/sitemap.rb"
end
end

# Remove config/sitemap.rb if exists.
def uninstall_sitemap_rb
if File.exist?(File.join(RAILS_ROOT, 'config/sitemap.rb'))
File.rm(File.join(RAILS_ROOT, 'config/sitemap.rb'))
end
end

# Clean sitemap files in output directory.
def clean_files
FileUtils.rm(Dir[File.join(RAILS_ROOT, 'public/sitemap*.xml.gz')])
end

# Ping search engines passing sitemap location.
def ping_search_engines
super index_file
Expand Down
21 changes: 13 additions & 8 deletions lib/sitemap_generator/templates.rb
Expand Up @@ -11,7 +11,8 @@ class Templates
:sitemap_xml => 'xml_sitemap.builder',
:sitemap_sample => 'sitemap.rb',
}


# Dynamically define accessors for each key defined in <tt>FILES</tt>
attr_accessor *FILES.keys
FILES.keys.each do |name|
eval <<-END
Expand All @@ -20,19 +21,23 @@ class Templates
end
END
end

def initialize(root = SitemapGenerator.root)
@root = root
end

def template_path(file)
File.join(@root, 'templates', file)

# Return the full path to a template.
#
# <tt>file</tt> template symbol e.g. <tt>:sitemap_index</tt>
def template_path(template)
File.join(@root, 'templates', self.class::FILES[template])
end

protected


# Read the template file and return its contents.
def read_template(template)
File.read(template_path(self.class::FILES[template]))
File.read(template_path(template))
end
end
end
10 changes: 8 additions & 2 deletions lib/sitemap_generator/utilities.rb
@@ -1,13 +1,14 @@
module SitemapGenerator
module Utilities
extend self

# Copy templates/sitemap.rb to config if not there yet.
def install_sitemap_rb
if File.exist?(File.join(RAILS_ROOT, 'config/sitemap.rb'))
puts "already exists: config/sitemap.rb, file not copied"
else
FileUtils.cp(
SitemapGenerator.templates.template_path(:sitemap_sample),
SitemapGenerator.templates.template_path(:sitemap_sample),
File.join(RAILS_ROOT, 'config/sitemap.rb'))
puts "created: config/sitemap.rb"
end
Expand All @@ -18,6 +19,11 @@ def uninstall_sitemap_rb
if File.exist?(File.join(RAILS_ROOT, 'config/sitemap.rb'))
File.rm(File.join(RAILS_ROOT, 'config/sitemap.rb'))
end
end
end

# Clean sitemap files in output directory.
def clean_files
FileUtils.rm(Dir[File.join(RAILS_ROOT, 'public/sitemap*.xml.gz')])
end
end
end
2 changes: 1 addition & 1 deletion rails/install.rb
@@ -1,2 +1,2 @@
# Install hook code here
SitemapGenerator::Sitemap.install_sitemap_rb
SitemapGenerator::Utilities.install_sitemap_rb
2 changes: 1 addition & 1 deletion rails/uninstall.rb
@@ -1,2 +1,2 @@
# Uninstall hook code here
SitemapGenerator::Sitemap.uninstall_sitemap_rb
SitemapGenerator::Utilities.uninstall_sitemap_rb
4 changes: 3 additions & 1 deletion sitemap_generator.gemspec
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Adam Salter", "Karl Varga"]
s.date = %q{2010-04-01}
s.date = %q{2010-05-06}
s.description = %q{Installs as a plugin or Gem to easily generate enterprise class Sitemaps readable by all search engines. Automatically ping search engines to notify them of new sitemaps, including Google, Yahoo and Bing. Provides rake tasks to easily manage your sitemaps. Won't clobber your old sitemaps if the new one fails to generate. Setup a cron schedule and never worry about your sitemaps again.}
s.email = %q{kjvarga@gmail.com}
s.extra_rdoc_files = [
Expand All @@ -26,6 +26,8 @@ Gem::Specification.new do |s|
"lib/sitemap_generator/link_set.rb",
"lib/sitemap_generator/mapper.rb",
"lib/sitemap_generator/tasks.rb",
"lib/sitemap_generator/templates.rb",
"lib/sitemap_generator/utilities.rb",
"rails/install.rb",
"rails/uninstall.rb",
"tasks/sitemap_generator_tasks.rake",
Expand Down
4 changes: 2 additions & 2 deletions tasks/sitemap_generator_tasks.rake
Expand Up @@ -4,12 +4,12 @@ require 'sitemap_generator'
namespace :sitemap do
desc "Install a default config/sitemap.rb file"
task :install do
SitemapGenerator::Sitemap.install_sitemap_rb
SitemapGenerator::Utilities.install_sitemap_rb
end

desc "Delete all Sitemap files in public/ directory"
task :clean do
SitemapGenerator::Sitemap.clean_files
SitemapGenerator::Utilities.clean_files
end

desc "Create Sitemap XML files in public/ directory (rake -s for no output)"
Expand Down

0 comments on commit e9413f8

Please sign in to comment.