File: google_sitemaps.rb

package info (click to toggle)
tdiary-contrib 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,772 kB
  • sloc: ruby: 17,305; javascript: 8,263; lisp: 562; xml: 451; php: 61; sql: 40; makefile: 18
file content (60 lines) | stat: -rw-r--r-- 1,841 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# google_sitemap.rb
# Copyright (c) 2006 http://d.bulkitem.com/
# Distributed under the GPL

add_update_proc do
  require 'time'

  headers = Array.new
  header = Hash.new

  Dir.glob(@conf.data_path + '/????/*.td2') { |data_file|
    File.open(data_file) { |buffer|
      buffer.each { |line|
        line.strip!
        if line == "." then
          if  header['visible'] then
            headers.push(header.clone)
          end
          header.clear
        end
        if %r|^Date: ([0-9]+)$|i =~ line then
          header['loc'] = sprintf(@conf['google_sitemaps.uri_format'], $1)
        end
        if %r|^Last-Modified: ([0-9]+)$|i =~ line then
          header['lastmod'] = Time.at($1.to_i).iso8601
        end
        if %r|^Visible: (.+)$|i =~ line then
          if $1.upcase == "TRUE" then
            header['visible'] = true
          else
            header['visible'] = false
          end
        end
      }
    }
  }

  headers.sort! { |a, b| b['loc'] <=> a['loc']}

  top_page_uri = File::dirname(@conf['google_sitemaps.uri_format']) + '/'
  now_datetime = Time.now.iso8601

  File.open(@conf['google_sitemaps.output_file'], 'w') do |fp|
    fp.write %Q[<?xml version="1.0" encoding="UTF-8"?>\n]
    fp.write %Q[<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">\n]
    fp.write %Q[ <url><loc>#{CGI::escapeHTML(top_page_uri)}</loc><lastmod>#{now_datetime}</lastmod></url>\n]
    headers.each { |entry|
      fp.write %Q[ <url><loc>#{CGI::escapeHTML(entry['loc'])}</loc><lastmod>#{entry['lastmod']}</lastmod></url>\n]
    }
    fp.write %Q[</urlset>\n]
  end
end

def saveconf_google_sitemaps
  if @mode == 'saveconf' then
    @conf['google_sitemaps.uri_format'] = @cgi.params['google_sitemaps.uri_format'][0]
    @conf['google_sitemaps.output_file'] = @cgi.params['google_sitemaps.output_file'][0]
  end
end