aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v5/sitemaps.rb
blob: a8cb5e9796217726b58b165ae3e8656070a79492 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# encoding: utf-8
=begin

 * Name: SiSU

 * Description: a framework for document structuring, publishing and search

 * Author: Ralph Amissah

 * Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
   2007, 2008, 2009, 2010, 2011, 2012, 2013 Ralph Amissah, All Rights Reserved.

 * License: GPL 3 or later:

   SiSU, a framework for document structuring, publishing and search

   Copyright (C) Ralph Amissah

   This program is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the Free
   Software Foundation, either version 3 of the License, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
   more details.

   You should have received a copy of the GNU General Public License along with
   this program. If not, see <http://www.gnu.org/licenses/>.

   If you have Internet connection, the latest version of the GPL should be
   available at these locations:
   <http://www.fsf.org/licensing/licenses/gpl.html>
   <http://www.gnu.org/licenses/gpl.html>

   <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>

 * SiSU uses:
   * Standard SiSU markup syntax,
   * Standard SiSU meta-markup syntax, and the
   * Standard SiSU object citation numbering and system

 * Hompages:
   <http://www.jus.uio.no/sisu>
   <http://www.sisudoc.org>

 * Download:
   <http://www.sisudoc.org/sisu/en/SiSU/download.html>

 * Git
   <http://sources.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
   <http://sources.sisudoc.org/?p=code/sisu.git;a=blob;f=lib/sisu/v5/sitemaps.rb;hb=HEAD>

 * Ralph Amissah
   <ralph@amissah.com>
   <ralph.amissah@gmail.com>

 ** Description: sitemap created from parameters extracted from input file(s)

=end
module SiSU_Sitemaps
  require_relative 'param'                              # param.rb
    include SiSU_Param
  require_relative 'sysenv'                             # sysenv.rb
    include SiSU_Env
  require_relative 'shared_xml'                         # shared_xml.rb
    include SiSU_XML_Munge
  class Source
    def initialize(opt)
      @opt=opt
    end
    def read
      songsheet
    end
    def songsheet
      begin
        @sys=SiSU_Env::SystemCall.new
        fn_set_lang=SiSU_Env::StandardiseLanguage.new(@opt.lng).language
        @fn=SiSU_Env::EnvCall.new(@opt.fns).lang(fn_set_lang[:c])
        if @opt.cmd =~/Y/
          @md=SiSU_Param::Parameters.new(@opt).get
          @trans=SiSU_XML_Munge::Trans.new(@md) #check @md is required
          @env=SiSU_Env::InfoEnv.new(@md.fns)
#         @file=SiSU_Env::FileOp.new(@md)
          @rdf=SiSU_XML_Tags::RDF.new(@md)
          @fnb_utf8_xml=@md.fnb.dup
          @trans.char_enc.utf8(@fnb_utf8_xml) if @sys.locale =~/utf-?8/i #% utf8
          output_map(sitemap)
        elsif @opt.mod.inspect =~/--sitemaps/
          @sitemap_idx_fn='sitemapindex.xml'
          @env=SiSU_Env::InfoEnv.new
          output_idx(sitemap_index)
          SiSU_Screen::Ansi.new(@opt.cmd,"sitemap index:","#{@env.path.output}/#{@sitemap_idx_fn}").result unless @opt.cmd =~/q/
        end
      rescue
        SiSU_Errors::InfoError.new($!,$@,@opt.cmd,@opt.fns).error do
          __LINE__.to_s + ':' + __FILE__
        end
      ensure
      end
    end
    def make_file(path,filename)
      (File.writable?("#{path}/.")) \
      ? (File.new("#{path}/#{filename}",'w+'))
      : (SiSU_Screen::Ansi.new('',"is the file or directory writable?, could not create #{filename}").warn)
    end
    def output_map(sitemap)
      path=@md.file.output_path.sitemaps.dir
      filename=@fn[:sitemap]
      touch_path=@md.file.output_path.sitemaps.dir
      touch_filename=@fn[:sitemap_touch]
      SiSU_Env::FileOp.new(@md).make_path(path)
      file=SiSU_Env::FileOp.new(@md).make_file(path,filename)
      file << sitemap
      if FileTest.file?("#{touch_path}/#{touch_filename}")
        FileUtils::rm("#{touch_path}/#{touch_filename}")
      end
    end
    def output_idx(sitemap)
      path=@env.path.output
      filename=@sitemap_idx_fn
      make_path(path)
      file=make_file(path,filename)
      file << sitemap
    end
    def sitemap_index
      sitemap_files=Dir.glob("#{@env.path.sitemaps}/sitemap_*.xml")
      sitemap_idx=[]
      sitemap_idx << <<WOK
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemaps/0.9
   http://www.sitemaps.org/schemas/sitemaps/sitemap.xsd"
     xmlns="http://www.sitemaps.org/schemas/sitemapindex/0.9">
WOK
      sitemap_files.each do |s|
        f=s.gsub(/.+?\/sitemap_([^\/]+?)\.xml$/,'\1')
        @trans.char_enc.utf8(f) if @sys.locale =~/utf-?8/i #% utf8
sitemap_idx << <<WOK
  <sitemap>
    <loc>#{@env.path.url.remote}/#{f}/sitemap.xml</loc>
  </sitemap>
WOK
      end
      sitemap_idx << <<WOK
</sitemapindex>
WOK
      sitemap_idx.join
    end
    def sitemap
      if defined? @md.date.modified \
      and @md.date.modified=~/\d{4}-\d{2}-\d{2}/
        sitemap_date_modified
      else sitemap_no_date
      end
    end
    def sitemap_date_modified
<<WOK
<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemaps/0.9
  http://www.sitemaps.org/schemas/sitemaps/sitemap.xsd"
  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
#{@rdf.comment_xml}
  <url>
    <loc>#{@env.path.url.remote}/#{@fnb_utf8_xml}/#{@fn[:toc]}</loc>
    <lastmod>#{@md.date.modified}</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.7</priority>
  </url>
  <url>
    <loc>#{@env.path.url.remote}/#{@fnb_utf8_xml}/#{@fn[:doc]}</loc>
    <lastmod>#{@md.date.modified}</lastmod>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>#{@env.path.url.remote}/#{@fnb_utf8_xml}/#{@fn[:manifest]}</loc>
    <lastmod>#{@md.date.modified}</lastmod>
    <priority>0.5</priority>
  </url>
</urlset>
WOK
    end
    def sitemap_no_date
<<WOK
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
#{@rdf.comment_xml}
  <url>
    <loc>#{@env.path.url.remote}/#{@fnb_utf8_xml}/#{@fn[:toc]}</loc>
    <changefreq>monthly</changefreq>
    <priority>0.7</priority>
  </url>
  <url>
    <loc>#{@env.path.url.remote}/#{@fnb_utf8_xml}/#{@fn[:doc]}</loc>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>#{@env.path.url.remote}/#{@fnb_utf8_xml}/#{@fn[:manifest]}</loc>
    <priority>0.5</priority>
  </url>
</urlset>
WOK
    end
  end
end
__END__
* sanitize xml, pass through filter to ensure is valid - done but needs testing
* remote placement of sitemaps --sitemaps -R (probably makes more sense than doing against -Y [filename/wildcard]) - done but needs testing
* gzip sitemaps - not before testing / after testing
* issue with master documnts, naming and mapping, check multilingual

<!-- Document processing information:
     * Generated by: SiSU 0.48.6 of 2006w45/6 (20061111)
     * Ruby version: ruby 1.8.5 (2006-08-25) [i486-linux]
     *
     * Last Generated on: Sat Nov 18 15:28:08 +0000 2006
     * SiSU http://www.jus.uio.no/sisu
-->