Skip to content

Instantly share code, notes, and snippets.

@crhan
Created September 12, 2012 11:14
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save crhan/3705998 to your computer and use it in GitHub Desktop.
Ruhoh Sitemap Generator
plugins:
sitemap_generator: # all optional
file_name: sitemap.xml
exclude_id:
- search.html
- pages.html
change_frequency_custom_name: changefreq
priority_custom_variable_name: priority
# Sitemap Generator is a Ruhoh plugin that generates a sitemap.xml file by
# traversing all of Ruhoh::DB.pages and Ruhoh::DB.posts object.
#
# How to Use:
# 1.) Copy this file to your blog's plugins folder within your Ruhoh project
# 2.) Custom settings in the ruhoh's site.yml (See Notes)
#
# Customizations:
# 1.) If there are any files you don't want to included in the sitemap, add
# their ID (find it in `ruhoh payload`) to the *exclude_id* yaml config
# 2.) If you want to include the optional changefreq and priority attributes,
# simply include custom variables in the YAML Front Matter of that file.
# The names of these custom variables are defined in the config file
# named:
# - change_frequency_custom_name #default "changefreq"
# - priority_custom_variable_name #default "priority"
#
# Notes:
# 1.) The lastmod time is determined by the latest from the following(TODO):
# - git log date of the page or post
# - git log date of included layout
# - git log date of included layout within that layout
# - ...
# 2.) Full config of this plugin (all is optional)
#
# #plugins:
# # sitemap_generator:
# # file_name: sitemap.xml
# # exclude_id:
# # - search.html
# # change_frequency_custom_name: changefreq
# # priority_custom_variable_name: priority
#
# Author: Ruohan Chen
# Site: https://github.com/crhan/ruhoh_plugins
# Distributed Under A Creative Commons License
# - http://creativecommons.org/licenses/by-sa/3.0/
require 'nokogiri'
class Ruhoh
module Compiler
module Sitemap
def self.run(target, page)
production_url = Ruhoh::DB.site['config']['production_url']
config = Ruhoh::DB.site['plugins']['sitemap_generator'] || {} rescue {}
sitemap_file_name = config['file_name'] || "sitemap.xml"
exclude_id = config['exclude_id'] || []
change_frequency_custom_name = config['change_frequency_custom_name'] || "changefreq"
priority_custom_name = config['priority_custom_variable_name'] || "priority"
sitemap = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
xml.urlset(:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9") {
Ruhoh::DB.posts['chronological'].each do |post_id|
post = Ruhoh::DB.posts['dictionary'][post_id]
page.change(post_id)
xml.url {
xml.loc_ "#{production_url}#{post['url']}"
xml.lastmod_ File.mtime(post_id).iso8601
xml.priority_ post[priority_custom_name] if !!post[priority_custom_name]
xml.changefreq_ post[change_frequency_custom_name] if !!post[change_frequency_custom_name]
}
end #posts
Ruhoh::DB.pages.each do |id, page|
next if exclude_id.include? id
xml.url {
xml.loc_ "#{production_url}#{page['url']}"
xml.lastmod_ File.mtime("pages/#{id}").iso8601
xml.priority_ page[priority_custom_name] if !!page[priority_custom_name]
xml.changefreq_ page[change_frequency_custom_name] if !!page[change_frequency_custom_name]
}
end #pages
}
end
File.open(File.join(target, sitemap_file_name), 'w') { |p| p.puts sitemap.to_xml }
Ruhoh::Friend.say { green "processed: #{sitemap_file_name}" }
end
end #Sitemap
end #Compiler
end #Ruhoh
@douo
Copy link

douo commented Sep 17, 2012

thanks

@crhan
Copy link
Author

crhan commented Sep 26, 2012

@douo 时间格式写错了(sitemap 要求 ISO8601 格式的时间), 刚改好...

@imamo
Copy link

imamo commented Jul 9, 2013

Will this plugin work with Ruhoh2.X ?

@xudejian
Copy link

xudejian commented Aug 1, 2013

@imamo
Copy link

imamo commented Aug 24, 2013

@xudejian thanks for right direction ;) working great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment