c&d: project dir structure, libs moved under new branch names
[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 def initialize(opt)
65 @opt=opt
66 @description='This is a script that contains canned text conversions for reuse'
67 @response=SiSU_Response::Response.new
68 @ask=SiSU_Response::Response.new
69 @warn='WARNING, PROCEED AT YOUR OWN RISK, will make file changes.'
70 end
71 def current_match_and_replace
72 convert_37_to_38
73 end
74 def message(text)
75 response=''
76 unless @opt.cmd=~/QQ/ \
77 or @opt.act[:quiet][:set]==:on
78 response=@ask.response?(%{#{ text}\nProceed? })
79 end
80 end
81 def help
82 print <<WOK
83
84 #{@description}
85
86 sisu --convert --to38 [filename/wildcard]
87 converts pre 0.37 sisu markup to 0.38 experimental
88 [--37to38]
89
90 sisu --convert --to37 [filename/wildcard]
91 converts pre 0.37 sisu markup to 0.38 experimental
92 [--38to37]
93
94 sisu --convert --36to37 [filename/wildcard]
95 converts pre 0.36 file-name, to 0.37 file-name
96 [--36to37]
97
98 sisu --identify [filename]
99 attempts to identify markup version used in file
100
101 sisu --query [version number]
102 gives short summary of distinguishing characteristic
103 of that version of markup
104
105 WOK
106 exit
107 end
108 #%% substitutions to be made
109 def convert_37_to_38
110 message("#{@warn}\nConvert sisu markup from 0.37 to 0.38")
111 [
112 [/^0~(\S+?)([+-])\s+/, '@\1:\2 ', //],
113 [/^0~(\S+)\s+/, '@\1: ', //],
114 [/^@toc:\s+/, '@structure: ', //],
115 [/^1~/, ':A~', //],
116 [/^2~/, ':B~', //],
117 [/^3~/, ':C~', //],
118 [/^4~/, '1~', //],
119 [/^5~/, '2~', //],
120 [/^6~/, '3~', //],
121 [/^7~/, '4~', //],
122 [/^8~/, '5~', //],
123 [/^9~/, '6~', //],
124 [/1/, ':A', /^@(?:level|markup):\s/],
125 [/2/, ':B', /^@(?:level|markup):\s/],
126 [/3/, ':C', /^@(?:level|markup):\s/],
127 [/4/, '1', /^@(?:level|markup):\s/],
128 [/5/, '2', /^@(?:level|markup):\s/],
129 [/6/, '3', /^@(?:level|markup):\s/]
130 ]
131 end
132 def convert_38_to_37
133 message("#{@warn}\nConvert sisu markup from 0.38 to 0.37")
134 [
135 [/^@(\S+?):([+-])\s+/, '0~\1\2 ', //],
136 [/^@(\S+?):\s+/, '0~\1 ', //],
137 [/^0~structure\s+/, '0~toc ', //],
138 [/^1~/, '4~', //],
139 [/^2~/, '5~', //],
140 [/^3~/, '6~', //],
141 [/^4~/, '7~', //],
142 [/^5~/, '8~', //],
143 [/^6~/, '9~', //],
144 [/^:?A~/, '1~', //],
145 [/^:?B~/, '2~', //],
146 [/^:?C~/, '3~', //],
147 [/1/, '4', /^0~(?:level|markup)\s/],
148 [/2/, '5', /^0~(?:level|markup)\s/],
149 [/3/, '6', /^0~(?:level|markup)\s/],
150 [/:?A/, '1', /^0~(?:level|markup)\s/],
151 [/:?B/, '2', /^0~(?:level|markup)\s/],
152 [/:?C/, '3', /^0~(?:level|markup)\s/]
153 ]
154 end
155 def convert_filename_36_to_37
156 @opt.files.each do |f|
157 s=case f
158 when /(\.s[1-3])$/ then f.sub($1,'.sst')
159 when /(\.r[1-3])$/ then f.sub($1,'.ssm')
160 when /(\.ri)$/ then f.sub($1,'.ssi')
161 else f
162 end
163 pwd=Dir.pwd
164 unless f==s
165 unless File.exist?("#{pwd}/#{s}")
166 puts "./#{f} -> ./#{s}"
167 FileUtils::cp("#{pwd}/#{f}","#{pwd}/#{s}")
168 else "File already exists, < #{s} > will not overwrite"
169 end
170 end
171 end
172 end
173 def convert_to_simple_xml_model_sax
174 SiSU_SimpleXML_ModelSax::Convert.new(@opt).read
175 end
176 def convert_to_simple_xml_model_dom
177 SiSU_simple_xml_model_dom::Convert.new(@opt).read
178 end
179 def convert_to_simple_xml_model_node
180 SiSU_simple_xml_model_node::Convert.new(@opt).read
181 end
182 def convert_kdi_to_sst
183 SiSU_Kdissert::Convert.new(@opt).read
184 end
185 def convert_s_xml_to_sst
186 SiSU_sstFromXML::Convert.new(@opt).read
187 end
188 def convert_footnotes
189 require_relative 'sst_do_inline_footnotes'
190 SiSU_ConvertFootnotes::Source.new(@opt).read
191 end
192 def conversion
193 #%% do it -------------------------->
194 if @opt.files \
195 and @opt.files.length > 0
196 mr=nil
197 #%% changes to make m match, r replace -------------------------->
198 if @opt.selections.str =~/--help/ then help
199 elsif @opt.selections.str =~/(?:convert|to)[=-](?:xml |sxs|sax|sxd|dom|sxn|node)/
200 ext=case @opt.selections.str
201 when /(?:convert|to)[=-](?:xml|sxs|sax)/ then '.sxs.xml'
202 when /(?:convert|to)[=-](?:sxd|dom)/ then '.sxd.xml'
203 when /(?:convert|to)[=-](?:sxn|node)/ then '.sxn.xml'
204 end
205 message("#{@opt.files.inspect}\n\nWARNING, PROCEED AT YOUR OWN RISK,\noverwriting any equivalent file with the extension #{ext}")
206 mr=case @opt.selections.str
207 when /(?:convert|to)[=-](?:sxs|sax|xml )/ then convert_to_simple_xml_model_sax
208 when /(?:convert|to)[=-](?:sxd|dom)/ then convert_to_simple_xml_model_dom
209 when /(?:convert|to)[=-](?:sxn|node)/ then convert_to_simple_xml_model_node
210 else help
211 end
212 else
213 mr=case @opt.selections.str
214 when /(?:(?:37)?to-?38|--(?:convert|to)[=-](?:current|0.38))/ then convert_37_to_38
215 when /(?:(?:38)?to-?37|--(?:convert|to)[=-](?:0.37))/ then convert_38_to_37
216 when /(?:36to37)/ then convert_filename_36_to_37
217 when /(?:convert|from)[=-]kdi/ then convert_kdi_to_sst
218 when /(?:(?:convert|from)[=-])?(?:xml_to_sst|xml2sst|sxml|sxs|sxd|sxd)/ then convert_s_xml_to_sst
219 when /(?:convert|to)[=-]footnotes/ then convert_footnotes
220 when /convert|default/ then current_match_and_replace
221 else help
222 end
223 end
224 unless @opt.selections.str =~/kdi/
225 match_and_replace=mr
226 #start_processing =/not used in this example/i
227 end_processing =/END\s+OF\s+FILE/
228 i=@opt.fns
229 if i =~/(?:\.sst|\.ssm|\.ssi)$/
230 @new,@matched,@flag_start,@flag_end,@empty1,@empty2=true,false,false,false,false,false
231 o="#{i}.bk" #o is for old
232 markup_version=SiSU_Markup::MarkupIdentify.new(@opt).markup_version?
233 if (@opt.selections.str=~/37/ and markup_version=~/0.38/) \
234 or (@opt.selections.str=~/current|38/ and markup_version=~/0.37/)
235 puts "#{i} #{markup_version}"
236 file=File.open(i,'r')
237 cont=file.readlines
238 file.close
239 cont.each do |y|
240 match_and_replace.each do |m,r,w|
241 if y =~m \
242 and y =~w
243 if @new
244 @new=false
245 File.unlink(o) if File.exist?(o)
246 File.rename(i,o)
247 File.unlink(i) if File.exist?(i)
248 @file=File.new(i,'w')
249 @matched=true
250 break
251 end
252 end
253 end
254 end
255 if @matched
256 puts "conversion match in #{i}" unless @opt.act[:quiet][:set]==:on
257 @flag_start=true
258 cont.each do |y|
259 if y =~end_processing
260 @flag_end=true
261 end
262 if @flag_start \
263 and not @flag_end
264 match_and_replace.each do |m,r,w|
265 if y =~m \
266 and y =~w
267 puts m.inspect + ' -> ' + r unless @opt.act[:quiet][:set]==:on
268 if (@opt.act[:verbose][:set]==:on \
269 || @opt.act[:verbose_plus][:set]==:on \
270 || @opt.act[:maintenance][:set]==:on)
271 puts "in: #{y}"
272 end
273 y.gsub!(m,r) if m and r
274 if (@opt.act[:verbose][:set]==:on \
275 || @opt.act[:verbose_plus][:set]==:on \
276 || @opt.act[:maintenance][:set]==:on)
277 puts "out: #{y}"
278 end
279 end
280 end
281 end
282 @empty1=(y=~/^\s*$/) \
283 ? true
284 : false
285 @file.puts y unless (@empty1==true and @empty2==true)
286 @empty2=(y=~/^\s*$/) \
287 ? true
288 : false
289 end
290 @file.close
291 else puts "NO conversion match in #{i}" unless @opt.act[:quiet][:set]==:on
292 end
293 else
294 if (@opt.act[:verbose][:set]==:on \
295 || @opt.act[:verbose_plus][:set]==:on \
296 || @opt.act[:maintenance][:set]==:on)
297 puts "Requested conversion #{@opt.selections.str} markup #{markup_version} identified in #{i}"
298 end
299 end
300 end
301 end
302 else puts 'this routine makes permanent changes to the contents of the files matched, as instructed within [no matches]'
303 end
304 end
305 end
306 end
307 #%% files to match for this conversion set ------------------------->
308 require_relative 'hub_options' # hub_options.rb
309 argv=$*
310 base_path=Dir.pwd
311 @opt=SiSU_Commandline::Options.new(argv,base_path)
312 case @opt.selections.str
313 when /=kdi/
314 SiSU_Modify::ConvertMarkup.new(@opt).conversion
315 when /(?:36|37|38)?to-?(?:37|38)|--convert|--to|--from|default/
316 @opt.files.each do |fns|
317 @opt.fns=fns
318 SiSU_Modify::ConvertMarkup.new(@opt).conversion
319 end
320 else
321 @opt.selections.str='--help'
322 SiSU_Modify::ConvertMarkup.new(@opt).help
323 end
324 __END__