json, an output representation, first pass
[software/sisu] / lib / sisu / hub.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** hub
8
9 ** Author: Ralph Amissah
10 [ralph@amissah.com]
11 [ralph.amissah@gmail.com]
12
13 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
14 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Ralph Amissah,
15 All Rights Reserved.
16
17 ** License: GPL 3 or later:
18
19 SiSU, a framework for document structuring, publishing and search
20
21 Copyright (C) Ralph Amissah
22
23 This program is free software: you can redistribute it and/or modify it
24 under the terms of the GNU General Public License as published by the Free
25 Software Foundation, either version 3 of the License, or (at your option)
26 any later version.
27
28 This program is distributed in the hope that it will be useful, but WITHOUT
29 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
31 more details.
32
33 You should have received a copy of the GNU General Public License along with
34 this program. If not, see [http://www.gnu.org/licenses/].
35
36 If you have Internet connection, the latest version of the GPL should be
37 available at these locations:
38 [http://www.fsf.org/licensing/licenses/gpl.html]
39 [http://www.gnu.org/licenses/gpl.html]
40
41 ** SiSU uses:
42 * Standard SiSU markup syntax,
43 * Standard SiSU meta-markup syntax, and the
44 * Standard SiSU object citation numbering and system
45
46 ** Hompages:
47 [http://www.jus.uio.no/sisu]
48 [http://www.sisudoc.org]
49
50 ** Git
51 [http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary]
52 [http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/hub.rb;hb=HEAD]
53
54 =end
55 module SiSU
56 require_relative 'constants' # constants.rb
57 require_relative 'se' # se.rb
58 include SiSU_Env
59 include SiSU_Screen
60 require_relative 'hub_actions' # hub_actions.rb
61 require_relative 'hub_loop_markup_files' # hub_loop_markup_files.rb
62 require_relative 'hub_options' # hub_options.rb
63 require_relative 'dp' # dp.rb
64 include SiSU_Param
65 require_relative 'utils' # utils.rb
66 begin
67 require 'uri'
68 rescue LoadError
69 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
70 error('uri NOT FOUND (LoadError)')
71 end
72 class HubMaster
73 def initialize(argv,sisu_runtime)
74 begin
75 opt=SiSU_Commandline::Options.new(argv,sisu_runtime)
76 SiSU::Processing.new(opt).actions_without_files
77 SiSU::Processing.new(opt).actions_on_files
78 SiSU::Processing.new(opt).actions_without_files_post
79 rescue
80 selection=(opt ? opt.selections.src : argv)
81 SiSU_Screen::Ansi.new(selection,$!,$@).rescue do
82 __LINE__.to_s + ':' + __FILE__
83 end
84 ensure
85 Dir.chdir(sisu_runtime[:call_path])
86 end
87 end
88 end
89 class Processing
90 begin
91 require 'fileutils'
92 include FileUtils
93 rescue LoadError
94 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
95 error('fileutils NOT FOUND (LoadError)')
96 end
97 @@env=nil
98 attr_accessor :op
99 def initialize(opt)
100 @opt=opt
101 @@env=@env=SiSU_Env::InfoEnv.new
102 @msg,@msgs='',nil
103 @tell=lambda {
104 SiSU_Screen::Ansi.new(
105 @opt.selections.str,
106 @msg,
107 "#{@msgs.inspect if @msgs}"
108 )
109 }
110 end
111 def remove_skipped_files_if_any_from_processing_files_array
112 if @remove_faulty_markup_files_array.length > 0
113 @opt.files = (@opt.files - @remove_faulty_markup_files_array)
114 end
115 end
116 def print_error_message_if_files_skipped
117 if @remove_faulty_markup_files_array.length > 0
118 puts '---'
119 STDERR.puts 'ERROR with file(s), did not process: ' +
120 @remove_faulty_markup_files_array.join(',')
121 end
122 end
123 def do_each_file_loop_check_and_perform_selected_actions(opt)
124 actions=SiSU_Hub_Actions::HubActions.new(opt)
125 actions.outputs.each_file.abstract_objects?
126 actions.outputs.each_file.qrcode?
127 actions.outputs.each_file.hash_digests?
128 actions.outputs.each_file.text?
129 actions.outputs.each_file.html?
130 actions.outputs.each_file.xhtml?
131 actions.outputs.each_file.xml?
132 actions.outputs.each_file.json?
133 actions.outputs.each_file.pdf?
134 actions.outputs.each_file.man_or_info?
135 actions.outputs.each_file.po4a_make?
136 actions.outputs.each_file.sqlite_discrete?
137 actions.outputs.each_file.manifest?
138 end
139 def do_each_file_loop_options
140 if @opt.files.length > 0
141 @opt.files.each_with_index do |fno,i|
142 @opt.fno=fno
143 @opt.fns=fno.
144 gsub(/(?:https?|file):\/\/\S+\/(\S+)\.sst$/,'\1.-sst').
145 gsub(/\.ssm$/,'.ssm.sst')
146 @opt.f_pth=@opt.f_pths[i]
147 if @opt.fns !~/\.-sst$/
148 @opt.pth=@opt.f_pths[i][:pth]
149 @opt.lng=@opt.f_pths[i][:lng]
150 else
151 @opt.pth=Dir.pwd
152 @opt.lng='en'
153 end
154 unless @opt.pth.nil?
155 @@pwd=@opt.pth
156 Dir.chdir(@opt.pth) #watch
157 end
158 #@env=SiSU_Env::InfoEnv.new(@opt.fns)
159 do_each_file_loop_check_and_perform_selected_actions(@opt)
160 end
161 else
162 do_each_file_loop_check_and_perform_selected_actions(@opt)
163 end
164 end
165 def do_loop_files_on_given_option_pre
166 begin
167 if @opt.act[:zap][:set]==:on #% --zap, -Z
168 SiSU_Hub_Loops::OptionLoopFiles.new(@opt).loop_files_on_given_option do
169 require_relative 'zap'
170 SiSU_Zap::Source.new(@opt).read # -Z zap.rb
171 end
172 end
173 ensure
174 end
175 end
176 def do_loop_files_on_given_option_post
177 actions=SiSU_Hub_Actions::HubActions.new(@opt)
178 if defined? actions.outputs.loop_files.share_source?
179 actions.outputs.loop_files.share_source?
180 end
181 if defined? actions.outputs.loop_files.run_termsheet?
182 actions.outputs.loop_files.run_termsheet?
183 end
184 if defined? actions.outputs.loop_files.po4a_setup?
185 actions.outputs.loop_files.po4a_setup?
186 end
187 if defined? actions.outputs.loop_files.sql?
188 actions.outputs.loop_files.sql?
189 end
190 SiSU_Hub_Actions::Operations.new.counter
191 if defined? actions.outputs.loop_files.manifest?
192 actions.outputs.loop_files.manifest?
193 end
194 if defined? actions.outputs.loop_files.sitemaps?
195 actions.outputs.loop_files.sitemaps?
196 end
197 if defined? actions.outputs.loop_files.urls?
198 actions.outputs.loop_files.urls?
199 end
200 end
201 def actions_without_files
202 actions=SiSU_Hub_Actions::HubActions.new(@opt)
203 actions.report.version_info?
204 actions.report.version_info_extra?
205 actions.prepare.site?
206 actions.prepare.sql?
207 end
208 def actions_without_files_post
209 actions=SiSU_Hub_Actions::HubActions.new(@opt)
210 actions.prepare.remote_site?
211 actions.prepare.search_form?
212 actions.prepare.webrick?
213 end
214 def actions_on_files
215 if @opt.act[:profile][:set]==:on
216 begin
217 require 'profile'
218 rescue LoadError
219 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
220 error('profile NOT FOUND (LoadError)')
221 end
222 end
223 actions=SiSU_Hub_Actions::HubActions.new(@opt)
224 actions.outputs.each_file.harvest?
225 actions.outputs.init?
226 do_loop_files_on_given_option_pre
227 do_each_file_loop_options
228 #remove_skipped_files_if_any_from_processing_files_array # NEEDS WORK
229 do_loop_files_on_given_option_post
230 #print_error_message_if_files_skipped
231 if (@opt.act[:verbose][:set]==:on \
232 || @opt.act[:verbose_plus][:set]==:on \
233 || @opt.act[:maintenance][:set]==:on)
234 @msg,@msgs="\tsisu -W [to start ruby web-server on output directory]\n",nil
235 end
236 if (@opt.act[:verbose][:set]==:on \
237 || @opt.act[:verbose_plus][:set]==:on \
238 || @opt.act[:maintenance][:set]==:on \
239 || @opt.act[:urls_selected][:set]==:on \
240 || @opt.act[:urls_all][:set]==:on)
241 @tell.call.print_brown unless @opt.files.join.empty?
242 end
243 if defined? @@env.processing_path.processing \
244 and @@env.user \
245 and FileTest.directory?(@@env.processing_path.processing) \
246 and @@env.processing_path.processing =~/#{@@env.user}$/
247 #clean tmp processing dir of content as is located in public area
248 if @@env.processing_path.processing_base_tmp =~/^\/tmp\/\S+/
249 FileUtils::cd(@@env.processing_path.processing_base_tmp) do
250 FileUtils::rm_rf('.') unless @opt.act[:maintenance][:set] ==:on
251 end
252 end
253 end
254 end
255 end
256 class HubClose
257 def initialize(call_path,argv)
258 begin
259 env=SiSU_Env::InfoEnv.new
260 rescue
261 ensure
262 if FileTest.directory?(env.processing_path.processing) \
263 and FileTest.directory?(env.processing_path.processing_base_tmp) \
264 and env.processing_path.processing_base_tmp =~ /#{env.processing_path.processing}/ \
265 and env.processing_path.processing_base_tmp =~/^\/tmp\/\S+/ \
266 and not argv.inspect =~/"--maintenance"|"-M"/
267 FileUtils::cd(env.processing_path.processing_base_tmp) do
268 FileUtils::rm_rf('.')
269 end
270 end
271 Dir.chdir(call_path)
272 end
273 end
274 end
275 end
276 __END__