module XmlSitemap module RenderEngine private # Render with Nokogiri gem # def render_nokogiri unless defined? Nokogiri raise ArgumentError, "Nokogiri not found!" end builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml| xml.urlset(XmlSitemap::MAP_SCHEMA_OPTIONS) { |s| @items.each do |item| s.url do |u| u.loc item.target # Format and image tag specifications found at http://support.google.com/webmasters/bin/answer.py?hl=en&answer=178636 if item.image_location u["image"].image do |a| a["image"].loc item.image_location a["image"].caption item.image_caption if item.image_caption a["image"].title item.image_title if item.image_title a["image"].license item.image_license if item.image_license a["image"].geo_location item.image_geolocation if item.image_geolocation end end # Format and video tag specifications found at http://support.google.com/webmasters/bin/answer.py?hl=en&answer=80472&topic=10079&ctx=topic#2 if item.video_thumbnail_location && item.video_title && item.video_description && (item.video_content_location || item.video_player_location) u["video"].video do |a| a["video"].thumbnail_loc item.video_thumbnail_location a["video"].title item.video_title a["video"].description item.video_description a["video"].content_loc item.video_content_location if item.video_content_location a["video"].player_loc item.video_player_location if item.video_player_location a["video"].duration item.video_duration.to_s if item.video_duration a["video"].expiration_date item.video_expiration_date_value if item.video_expiration_date a["video"].rating item.video_rating.to_s if item.video_rating a["video"].view_count item.video_view_count.to_s if item.video_view_count a["video"].publication_date item.video_publication_date_value if item.video_publication_date a["video"].family_friendly item.video_family_friendly if item.video_family_friendly a["video"].category item.video_category if item.video_category a["video"].restriction item.video_restriction, :relationship => "allow" if item.video_restriction a["video"].gallery_loc item.video_gallery_location if item.video_gallery_location a["video"].price item.video_price.to_s, :currency => "USD" if item.video_price a["video"].requires_subscription item.video_requires_subscription if item.video_requires_subscription a["video"].uploader item.video_uploader if item.video_uploader a["video"].platform item.video_platform, :relationship => "allow" if item.video_platform a["video"].live item.video_live if item.video_live end end u.lastmod item.lastmod_value u.changefreq item.changefreq.to_s if item.changefreq u.priority item.priority.to_s if item.priority end end } end builder.to_xml end # Render with Builder gem # def render_builder xml = Builder::XmlMarkup.new(:indent => 2) xml.instruct!(:xml, :version => '1.0', :encoding => 'UTF-8') xml.urlset(XmlSitemap::MAP_SCHEMA_OPTIONS) { |s| @items.each do |item| s.url do |u| u.loc item.target # Format and image tag specifications found at http://support.google.com/webmasters/bin/answer.py?hl=en&answer=178636 if item.image_location u.image :image do |a| a.tag! "image:loc", CGI::escapeHTML(item.image_location) a.tag! "image:caption", CGI::escapeHTML(item.image_caption) if item.image_caption a.tag! "image:title", CGI::escapeHTML(item.image_title) if item.image_title a.tag! "image:license", CGI::escapeHTML(item.image_license) if item.image_license a.tag! "image:geo_location", CGI::escapeHTML(item.image_geolocation) if item.image_geolocation end end # Format and video tag specifications found at http://support.google.com/webmasters/bin/answer.py?hl=en&answer=80472&topic=10079&ctx=topic#2 if item.video_thumbnail_location && item.video_title && item.video_description && (item.video_content_location || item.video_player_location) u.video :video do |a| a.tag! "video:thumbnail_loc", CGI::escapeHTML(item.video_thumbnail_location) a.tag! "video:title", CGI::escapeHTML(item.video_title) a.tag! "video:description", CGI::escapeHTML(item.video_description) a.tag! "video:content_loc", CGI::escapeHTML(item.video_content_location) if item.video_content_location a.tag! "video:player_loc", CGI::escapeHTML(item.video_player_location) if item.video_player_location a.tag! "video:duration", CGI::escapeHTML(item.video_duration.to_s) if item.video_duration a.tag! "video:expiration_date", CGI::escapeHTML(item.video_expiration_date_value) if item.video_expiration_date a.tag! "video:rating", CGI::escapeHTML(item.video_rating.to_s) if item.video_rating a.tag! "video:view_count", CGI::escapeHTML(item.video_view_count.to_s) if item.video_view_count a.tag! "video:publication_date", CGI::escapeHTML(item.video_publication_date_value) if item.video_publication_date a.tag! "video:family_friendly", CGI::escapeHTML(item.video_family_friendly) if item.video_family_friendly a.tag! "video:category", CGI::escapeHTML(item.video_category) if item.video_category a.tag! "video:restriction", CGI::escapeHTML(item.video_restriction), :relationship => "allow" if item.video_restriction a.tag! "video:gallery_loc", CGI::escapeHTML(item.video_gallery_location) if item.video_gallery_location a.tag! "video:price", CGI::escapeHTML(item.video_price.to_s), :currency => "USD" if item.video_price a.tag! "video:requires_subscription", CGI::escapeHTML(item.video_requires_subscription) if item.video_requires_subscription a.tag! "video:uploader", CGI::escapeHTML(item.video_uploader) if item.video_uploader a.tag! "video:platform", CGI::escapeHTML(item.video_platform), :relationship => "allow" if item.video_platform a.tag! "video:live", CGI::escapeHTML(item.video_live) if item.video_live end end u.lastmod item.lastmod_value u.changefreq item.changefreq.to_s if item.changefreq u.priority item.priority.to_s if item.priority end end }.to_s end # Render with plain strings # def render_string result = '' + "\n\n" item_results = [] @items.each do |item| item_string = " \n" item_string << " #{CGI::escapeHTML(item.target)}\n" # Format and image tag specifications found at http://support.google.com/webmasters/bin/answer.py?hl=en&answer=178636 if item.image_location item_string << " \n" item_string << " #{CGI::escapeHTML(item.image_location)}\n" item_string << " #{CGI::escapeHTML(item.image_caption)}\n" if item.image_caption item_string << " #{CGI::escapeHTML(item.image_title)}\n" if item.image_title item_string << " #{CGI::escapeHTML(item.image_license)}\n" if item.image_license item_string << " #{CGI::escapeHTML(item.image_geolocation)}\n" if item.image_geolocation item_string << " \n" end # Format and video tag specifications found at http://support.google.com/webmasters/bin/answer.py?hl=en&answer=80472&topic=10079&ctx=topic#2 if item.video_thumbnail_location && item.video_title && item.video_description && (item.video_content_location || item.video_player_location) item_string << " \n" item_string << " #{CGI::escapeHTML(item.video_thumbnail_location)}\n" item_string << " #{CGI::escapeHTML(item.video_title)}\n" item_string << " #{CGI::escapeHTML(item.video_description)}\n" item_string << " #{CGI::escapeHTML(item.video_content_location)}\n" if item.video_content_location item_string << " #{CGI::escapeHTML(item.video_player_location)}\n" if item.video_player_location item_string << " #{CGI::escapeHTML(item.video_duration.to_s)}\n" if item.video_duration item_string << " #{item.video_expiration_date_value}\n" if item.video_expiration_date item_string << " #{CGI::escapeHTML(item.video_rating.to_s)}\n" if item.video_rating item_string << " #{CGI::escapeHTML(item.video_view_count.to_s)}\n" if item.video_view_count item_string << " #{item.video_publication_date_value}\n" if item.video_publication_date item_string << " #{CGI::escapeHTML(item.video_family_friendly)}\n" if item.video_family_friendly item_string << " #{CGI::escapeHTML(item.video_category)}\n" if item.video_category item_string << " #{CGI::escapeHTML(item.video_restriction)}\n" if item.video_restriction item_string << " #{CGI::escapeHTML(item.video_gallery_location)}\n" if item.video_gallery_location item_string << " #{CGI::escapeHTML(item.video_price.to_s)}\n" if item.video_price item_string << " #{CGI::escapeHTML(item.video_requires_subscription)}\n" if item.video_requires_subscription item_string << " #{CGI::escapeHTML(item.video_uploader)}\n" if item.video_uploader item_string << " #{CGI::escapeHTML(item.video_platform)}\n" if item.video_platform item_string << " #{CGI::escapeHTML(item.video_live)}\n" if item.video_live item_string << " \n" end item_string << " #{item.lastmod_value}\n" item_string << " #{item.changefreq}\n" if item.changefreq item_string << " #{item.priority}\n" if item.priority item_string << " \n" item_results << item_string end result << item_results.join("") result << "\n" result end end end