| 1 | # google_sitemap.rb |
|---|
| 2 | # Copyright (c) 2006 http://d.bulkitem.com/ |
|---|
| 3 | # Distributed under the GPL |
|---|
| 4 | |
|---|
| 5 | add_update_proc do |
|---|
| 6 | require 'time' |
|---|
| 7 | |
|---|
| 8 | headers = Array.new |
|---|
| 9 | header = Hash.new |
|---|
| 10 | |
|---|
| 11 | Dir.glob(@conf.data_path + '/????/*.td2') { |data_file| |
|---|
| 12 | File.open(data_file) { |buffer| |
|---|
| 13 | buffer.each { |line| |
|---|
| 14 | line.strip! |
|---|
| 15 | if line == "." then |
|---|
| 16 | if header['visible'] then |
|---|
| 17 | headers.push(header.clone) |
|---|
| 18 | end |
|---|
| 19 | header.clear |
|---|
| 20 | end |
|---|
| 21 | if %r|^Date: ([0-9]+)$|i =~ line then |
|---|
| 22 | header['loc'] = sprintf(@conf['google_sitemaps.uri_format'], $1) |
|---|
| 23 | end |
|---|
| 24 | if %r|^Last-Modified: ([0-9]+)$|i =~ line then |
|---|
| 25 | header['lastmod'] = Time.at($1.to_i).iso8601 |
|---|
| 26 | end |
|---|
| 27 | if %r|^Visible: (.+)$|i =~ line then |
|---|
| 28 | if $1.upcase == "TRUE" then |
|---|
| 29 | header['visible'] = true |
|---|
| 30 | else |
|---|
| 31 | header['visible'] = false |
|---|
| 32 | end |
|---|
| 33 | end |
|---|
| 34 | } |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | headers.sort! { |a, b| b['loc'] <=> a['loc']} |
|---|
| 39 | |
|---|
| 40 | top_page_uri = File::dirname(@conf['google_sitemaps.uri_format']) + '/' |
|---|
| 41 | now_datetime = Time.now.iso8601 |
|---|
| 42 | |
|---|
| 43 | File.open(@conf['google_sitemaps.output_file'], 'w') do |fp| |
|---|
| 44 | fp.write %Q[<?xml version="1.0" encoding="UTF-8"?>\n] |
|---|
| 45 | fp.write %Q[<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">\n] |
|---|
| 46 | fp.write %Q[ <url><loc>#{CGI::escapeHTML(top_page_uri)}</loc><lastmod>#{now_datetime}</lastmod></url>\n] |
|---|
| 47 | headers.each { |entry| |
|---|
| 48 | fp.write %Q[ <url><loc>#{CGI::escapeHTML(entry['loc'])}</loc><lastmod>#{entry['lastmod']}</lastmod></url>\n] |
|---|
| 49 | } |
|---|
| 50 | fp.write %Q[</urlset>\n] |
|---|
| 51 | end |
|---|
| 52 | end |
|---|
| 53 | |
|---|
| 54 | def saveconf_google_sitemaps |
|---|
| 55 | if @mode == 'saveconf' then |
|---|
| 56 | @conf['google_sitemaps.uri_format'] = @cgi.params['google_sitemaps.uri_format'][0] |
|---|
| 57 | @conf['google_sitemaps.output_file'] = @cgi.params['google_sitemaps.output_file'][0] |
|---|
| 58 | end |
|---|
| 59 | end |
|---|
| 60 | |
|---|