d: utils_response, rely on module (remove class) extend module
[software/sisu] / lib / sisu / develop / sst_convert_markup.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** A conversion script for canned substitutions, a fairly generic
8 simple tool that can be used to store other canned conversions, used here for
9 altering SiSU markup
10
11 ** Author: Ralph Amissah
12 <ralph@amissah.com>
13 <ralph.amissah@gmail.com>
14
15 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
16 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
17 All Rights Reserved.
18
19 ** License: GPL 3 or later:
20
21 SiSU, a framework for document structuring, publishing and search
22
23 Copyright (C) Ralph Amissah
24
25 This program is free software: you can redistribute it and/or modify it
26 under the terms of the GNU General Public License as published by the Free
27 Software Foundation, either version 3 of the License, or (at your option)
28 any later version.
29
30 This program is distributed in the hope that it will be useful, but WITHOUT
31 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
33 more details.
34
35 You should have received a copy of the GNU General Public License along with
36 this program. If not, see <http://www.gnu.org/licenses/>.
37
38 If you have Internet connection, the latest version of the GPL should be
39 available at these locations:
40 <http://www.fsf.org/licensing/licenses/gpl.html>
41 <http://www.gnu.org/licenses/gpl.html>
42
43 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
44
45 ** SiSU uses:
46 * Standard SiSU markup syntax,
47 * Standard SiSU meta-markup syntax, and the
48 * Standard SiSU object citation numbering and system
49
50 ** Hompages:
51 <http://www.jus.uio.no/sisu>
52 <http://www.sisudoc.org>
53
54 ** Git
55 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
56 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/develop/sst_convert_markup.rb;hb=HEAD>
57
58 =end
59 module SiSU_Modify
60 require_relative 'sst_identify_markup' # sst_identify_markup.rb
61 require_relative 'sst_from_xml' # sst_from_xml.rb
62 require_relative 'utils_response' # utils_response.rb
63 class ConvertMarkup
64 include SiSU_Response
65 def initialize(opt)
66 @opt=opt
67 @description='This is a script that contains canned text conversions for reuse'
68 @warn='WARNING, PROCEED AT YOUR OWN RISK, will make file changes.'
69 end
70 def current_match_and_replace
71 convert_37_to_38
72 end
73 def message(text)
74 response=''
75 unless @opt.cmd=~/QQ/ \
76 or @opt.act[:quiet][:set]==:on
77 response=response?(%{#{ text}\nProceed? })
78 end
79 end
80 def help
81 print <<WOK
82
83 #{@description}
84
85 sisu --convert --to38 [filename/wildcard]
86 converts pre 0.37 sisu markup to 0.38 experimental
87 [--37to38]
88
89 sisu --convert --to37 [filename/wildcard]
90 converts pre 0.37 sisu markup to 0.38 experimental
91 [--38to37]
92
93 sisu --convert --36to37 [filename/wildcard]
94 converts pre 0.36 file-name, to 0.37 file-name
95 [--36to37]
96
97 sisu --identify [filename]
98 attempts to identify markup version used in file
99
100 sisu --query [version number]
101 gives short summary of distinguishing characteristic
102 of that version of markup
103
104 WOK
105 exit
106 end
107 #%% substitutions to be made
108 def convert_37_to_38
109 message("#{@warn}\nConvert sisu markup from 0.37 to 0.38")
110 [
111 [/^0~(\S+?)([+-])\s+/, '@\1:\2 ', //],
112 [/^0~(\S+)\s+/, '@\1: ', //],
113 [/^@toc:\s+/, '@structure: ', //],
114 [/^1~/, ':A~', //],
115 [/^2~/, ':B~', //],
116 [/^3~/, ':C~', //],
117 [/^4~/, '1~', //],
118 [/^5~/, '2~', //],
119 [/^6~/, '3~', //],
120 [/^7~/, '4~', //],
121 [/^8~/, '5~', //],
122 [/^9~/, '6~', //],
123 [/1/, ':A', /^@(?:level|markup):\s/],
124 [/2/, ':B', /^@(?:level|markup):\s/],
125 [/3/, ':C', /^@(?:level|markup):\s/],
126 [/4/, '1', /^@(?:level|markup):\s/],
127 [/5/, '2', /^@(?:level|markup):\s/],
128 [/6/, '3', /^@(?:level|markup):\s/]
129 ]
130 end
131 def convert_38_to_37
132 message("#{@warn}\nConvert sisu markup from 0.38 to 0.37")
133 [
134 [/^@(\S+?):([+-])\s+/, '0~\1\2 ', //],
135 [/^@(\S+?):\s+/, '0~\1 ', //],
136 [/^0~structure\s+/, '0~toc ', //],
137 [/^1~/, '4~', //],
138 [/^2~/, '5~', //],
139 [/^3~/, '6~', //],
140 [/^4~/, '7~', //],
141 [/^5~/, '8~', //],
142 [/^6~/, '9~', //],
143 [/^:?A~/, '1~', //],
144 [/^:?B~/, '2~', //],
145 [/^:?C~/, '3~', //],
146 [/1/, '4', /^0~(?:level|markup)\s/],
147 [/2/, '5', /^0~(?:level|markup)\s/],
148 [/3/, '6', /^0~(?:level|markup)\s/],
149 [/:?A/, '1', /^0~(?:level|markup)\s/],
150 [/:?B/, '2', /^0~(?:level|markup)\s/],
151 [/:?C/, '3', /^0~(?:level|markup)\s/]
152 ]
153 end
154 def convert_filename_36_to_37
155 @opt.files.each do |f|
156 s=case f
157 when /(\.s[1-3])$/ then f.sub($1,'.sst')
158 when /(\.r[1-3])$/ then f.sub($1,'.ssm')
159 when /(\.ri)$/ then f.sub($1,'.ssi')
160 else f
161 end
162 pwd=Dir.pwd
163 unless f==s
164 unless File.exist?("#{pwd}/#{s}")
165 puts "./#{f} -> ./#{s}"
166 FileUtils::cp("#{pwd}/#{f}","#{pwd}/#{s}")
167 else "File already exists, < #{s} > will not overwrite"
168 end
169 end
170 end
171 end
172 def convert_to_simple_xml_model_sax
173 SiSU_SimpleXML_ModelSax::Convert.new(@opt).read
174 end
175 def convert_to_simple_xml_model_dom
176 SiSU_simple_xml_model_dom::Convert.new(@opt).read
177 end
178 def convert_to_simple_xml_model_node
179 SiSU_simple_xml_model_node::Convert.new(@opt).read
180 end
181 def convert_kdi_to_sst
182 SiSU_Kdissert::Convert.new(@opt).read
183 end
184 def convert_s_xml_to_sst
185 SiSU_sstFromXML::Convert.new(@opt).read
186 end
187 def convert_footnotes
188 require_relative 'sst_do_inline_footnotes'
189 SiSU_ConvertFootnotes::Source.new(@opt).read
190 end
191 def conversion
192 #%% do it -------------------------->
193 if @opt.files \
194 and @opt.files.length > 0
195 mr=nil
196 #%% changes to make m match, r replace -------------------------->
197 if @opt.selections.str =~/--help/ then help
198 elsif @opt.selections.str =~/(?:convert|to)[=-](?:xml |sxs|sax|sxd|dom|sxn|node)/
199 ext=case @opt.selections.str
200 when /(?:convert|to)[=-](?:xml|sxs|sax)/ then '.sxs.xml'
201 when /(?:convert|to)[=-](?:sxd|dom)/ then '.sxd.xml'
202 when /(?:convert|to)[=-](?:sxn|node)/ then '.sxn.xml'
203 end
204 message("#{@opt.files.inspect}\n\nWARNING, PROCEED AT YOUR OWN RISK,\noverwriting any equivalent file with the extension #{ext}")
205 mr=case @opt.selections.str
206 when /(?:convert|to)[=-](?:sxs|sax|xml )/ then convert_to_simple_xml_model_sax
207 when /(?:convert|to)[=-](?:sxd|dom)/ then convert_to_simple_xml_model_dom
208 when /(?:convert|to)[=-](?:sxn|node)/ then convert_to_simple_xml_model_node
209 else help
210 end
211 else
212 mr=case @opt.selections.str
213 when /(?:(?:37)?to-?38|--(?:convert|to)[=-](?:current|0.38))/ then convert_37_to_38
214 when /(?:(?:38)?to-?37|--(?:convert|to)[=-](?:0.37))/ then convert_38_to_37
215 when /(?:36to37)/ then convert_filename_36_to_37
216 when /(?:convert|from)[=-]kdi/ then convert_kdi_to_sst
217 when /(?:(?:convert|from)[=-])?(?:xml_to_sst|xml2sst|sxml|sxs|sxd|sxd)/ then convert_s_xml_to_sst
218 when /(?:convert|to)[=-]footnotes/ then convert_footnotes
219 when /convert|default/ then current_match_and_replace
220 else help
221 end
222 end
223 unless @opt.selections.str =~/kdi/
224 match_and_replace=mr
225 #start_processing =/not used in this example/i
226 end_processing =/END\s+OF\s+FILE/
227 i=@opt.fns
228 if i =~/(?:\.sst|\.ssm|\.ssi)$/
229 @new,@matched,@flag_start,@flag_end,@empty1,@empty2=true,false,false,false,false,false
230 o="#{i}.bk" #o is for old
231 markup_version=SiSU_Markup::MarkupIdentify.new(@opt).markup_version?
232 if (@opt.selections.str=~/37/ and markup_version=~/0.38/) \
233 or (@opt.selections.str=~/current|38/ and markup_version=~/0.37/)
234 puts "#{i} #{markup_version}"
235 file=File.open(i,'r')
236 cont=file.readlines
237 file.close
238 cont.each do |y|
239 match_and_replace.each do |m,r,w|
240 if y =~m \
241 and y =~w
242 if @new
243 @new=false
244 File.unlink(o) if File.exist?(o)
245 File.rename(i,o)
246 File.unlink(i) if File.exist?(i)
247 @file=File.new(i,'w')
248 @matched=true
249 break
250 end
251 end
252 end
253 end
254 if @matched
255 puts "conversion match in #{i}" unless @opt.act[:quiet][:set]==:on
256 @flag_start=true
257 cont.each do |y|
258 if y =~end_processing
259 @flag_end=true
260 end
261 if @flag_start \
262 and not @flag_end
263 match_and_replace.each do |m,r,w|
264 if y =~m \
265 and y =~w
266 puts m.inspect + ' -> ' + r unless @opt.act[:quiet][:set]==:on
267 if (@opt.act[:verbose][:set]==:on \
268 || @opt.act[:verbose_plus][:set]==:on \
269 || @opt.act[:maintenance][:set]==:on)
270 puts "in: #{y}"
271 end
272 y.gsub!(m,r) if m and r
273 if (@opt.act[:verbose][:set]==:on \
274 || @opt.act[:verbose_plus][:set]==:on \
275 || @opt.act[:maintenance][:set]==:on)
276 puts "out: #{y}"
277 end
278 end
279 end
280 end
281 @empty1=(y=~/^\s*$/) \
282 ? true
283 : false
284 @file.puts y unless (@empty1==true and @empty2==true)
285 @empty2=(y=~/^\s*$/) \
286 ? true
287 : false
288 end
289 @file.close
290 else puts "NO conversion match in #{i}" unless @opt.act[:quiet][:set]==:on
291 end
292 else
293 if (@opt.act[:verbose][:set]==:on \
294 || @opt.act[:verbose_plus][:set]==:on \
295 || @opt.act[:maintenance][:set]==:on)
296 puts "Requested conversion #{@opt.selections.str} markup #{markup_version} identified in #{i}"
297 end
298 end
299 end
300 end
301 else puts 'this routine makes permanent changes to the contents of the files matched, as instructed within [no matches]'
302 end
303 end
304 end
305 end
306 #%% files to match for this conversion set ------------------------->
307 require_relative 'hub_options' # hub_options.rb
308 argv=$*
309 base_path=Dir.pwd
310 @opt=SiSU_Commandline::Options.new(argv,base_path)
311 case @opt.selections.str
312 when /=kdi/
313 SiSU_Modify::ConvertMarkup.new(@opt).conversion
314 when /(?:36|37|38)?to-?(?:37|38)|--convert|--to|--from|default/
315 @opt.files.each do |fns|
316 @opt.fns=fns
317 SiSU_Modify::ConvertMarkup.new(@opt).conversion
318 end
319 else
320 @opt.selections.str='--help'
321 SiSU_Modify::ConvertMarkup.new(@opt).help
322 end
323 __END__