Class: SiSU_Modify::ConvertMarkup

Inherits:
Object
  • Object
show all
Includes:
SiSU_Response
Defined in:
lib/sisu/sst_convert_markup.rb

Instance Method Summary collapse

Methods included from SiSU_Response

#answer?, #available_selections_, #query, #response?, #response_short, #response_strict, #selection_options, #selections_available_

Constructor Details

#initialize(opt) ⇒ ConvertMarkup

Returns a new instance of ConvertMarkup.



65
66
67
68
69
# File 'lib/sisu/sst_convert_markup.rb', line 65

def initialize(opt)
  @opt=opt
  @description='This is a script that contains canned text conversions for reuse'
  @warn='WARNING, PROCEED AT YOUR OWN RISK, will make file changes.'
end

Instance Method Details

#conversionObject



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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/sisu/sst_convert_markup.rb', line 191

def conversion
  #%% do it                                   -------------------------->
  if @opt.files \
  and @opt.files.length > 0
    mr=nil
    #%% changes to make m match, r replace      -------------------------->
    if @opt.selections.str =~/--help/ then help
    elsif @opt.selections.str =~/(?:convert|to)[=-](?:xml |sxs|sax|sxd|dom|sxn|node)/
      ext=case @opt.selections.str
      when /(?:convert|to)[=-](?:xml|sxs|sax)/ then '.sxs.xml'
      when /(?:convert|to)[=-](?:sxd|dom)/     then '.sxd.xml'
      when /(?:convert|to)[=-](?:sxn|node)/    then '.sxn.xml'
      end
      message("#{@opt.files.inspect}\n\nWARNING, PROCEED AT YOUR OWN RISK,\noverwriting any equivalent file with the extension #{ext}")
      mr=case @opt.selections.str
      when /(?:convert|to)[=-](?:sxs|sax|xml )/ then convert_to_simple_xml_model_sax
      when /(?:convert|to)[=-](?:sxd|dom)/      then convert_to_simple_xml_model_dom
      when /(?:convert|to)[=-](?:sxn|node)/     then convert_to_simple_xml_model_node
      else help
      end
    else
      mr=case @opt.selections.str
      when /(?:(?:37)?to-?38|--(?:convert|to)[=-](?:current|0.38))/           then convert_37_to_38
      when /(?:(?:38)?to-?37|--(?:convert|to)[=-](?:0.37))/                   then convert_38_to_37
      when /(?:36to37)/                                                       then convert_filename_36_to_37
      when /(?:convert|from)[=-]kdi/                                          then convert_kdi_to_sst
      when /(?:(?:convert|from)[=-])?(?:xml_to_sst|xml2sst|sxml|sxs|sxd|sxd)/ then convert_s_xml_to_sst
      when /(?:convert|to)[=-]footnotes/                                      then convert_footnotes
      when /convert|default/                                                  then current_match_and_replace
      else help
      end
    end
    unless @opt.selections.str =~/kdi/
      match_and_replace=mr
      #start_processing =/not used in this example/i
      end_processing =/END\s+OF\s+FILE/
      i=@opt.fns
      if i =~/(?:\.sst|\.ssm|\.ssi)$/
        @new,@matched,@flag_start,@flag_end,@empty1,@empty2=true,false,false,false,false,false
        o="#{i}.bk" #o is for old
        markup_version=SiSU_Markup::MarkupIdentify.new(@opt).markup_version?
        if (@opt.selections.str=~/37/ and markup_version=~/0.38/) \
        or (@opt.selections.str=~/current|38/ and markup_version=~/0.37/)
          puts "#{i} #{markup_version}"
          file=File.open(i,'r')
          cont=file.readlines
          file.close
          cont.each do |y|
            match_and_replace.each do |m,r,w|
              if y =~m \
              and y =~w
                if @new
                  @new=false
                  File.unlink(o) if File.exist?(o)
                  File.rename(i,o)
                  File.unlink(i) if File.exist?(i)
                  @file=File.new(i,'w')
                  @matched=true
                  break
                end
              end
            end
          end
          if @matched
            puts "conversion match in #{i}" unless @opt.act[:quiet][:set]==:on
            @flag_start=true
            cont.each do |y|
              if y =~end_processing
                @flag_end=true
              end
              if @flag_start \
              and not @flag_end
                match_and_replace.each do |m,r,w|
                  if y =~m \
                  and y =~w
                    puts m.inspect + ' -> ' + r unless @opt.act[:quiet][:set]==:on
                    if (@opt.act[:verbose][:set]==:on \
                    || @opt.act[:verbose_plus][:set]==:on \
                    || @opt.act[:maintenance][:set]==:on)
                      puts "in:  #{y}"
                    end
                    y.gsub!(m,r) if m and r
                    if (@opt.act[:verbose][:set]==:on \
                    || @opt.act[:verbose_plus][:set]==:on \
                    || @opt.act[:maintenance][:set]==:on)
                      puts "out: #{y}"
                    end
                  end
                end
              end
              @empty1=(y=~/^\s*$/) \
              ? true
              : false
              @file.puts y unless (@empty1==true and @empty2==true)
              @empty2=(y=~/^\s*$/) \
              ? true
              : false
            end
            @file.close
          else puts "NO conversion match in #{i}" unless @opt.act[:quiet][:set]==:on
          end
        else
          if (@opt.act[:verbose][:set]==:on \
          || @opt.act[:verbose_plus][:set]==:on \
          || @opt.act[:maintenance][:set]==:on)
            puts "Requested conversion #{@opt.selections.str} markup #{markup_version} identified in #{i}"
          end
        end
      end
    end
  else puts 'this routine makes permanent changes to the contents of the files matched, as instructed within [no matches]'
  end
end

#convert_37_to_38Object

%% substitutions to be made



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sisu/sst_convert_markup.rb', line 108

def convert_37_to_38
  message("#{@warn}\nConvert sisu markup from 0.37 to 0.38")
  [
    [/^0~(\S+?)([+-])\s+/,    '@\1:\2 ',               //],
    [/^0~(\S+)\s+/,           '@\1: ',                 //],
    [/^@toc:\s+/,             '@structure: ',          //],
    [/^1~/,                   ':A~',                   //],
    [/^2~/,                   ':B~',                   //],
    [/^3~/,                   ':C~',                   //],
    [/^4~/,                   '1~',                    //],
    [/^5~/,                   '2~',                    //],
    [/^6~/,                   '3~',                    //],
    [/^7~/,                   '4~',                    //],
    [/^8~/,                   '5~',                    //],
    [/^9~/,                   '6~',                    //],
    [/1/,                     ':A',                    /^@(?:level|markup):\s/],
    [/2/,                     ':B',                    /^@(?:level|markup):\s/],
    [/3/,                     ':C',                    /^@(?:level|markup):\s/],
    [/4/,                     '1',                     /^@(?:level|markup):\s/],
    [/5/,                     '2',                     /^@(?:level|markup):\s/],
    [/6/,                     '3',                     /^@(?:level|markup):\s/]
  ]
end

#convert_38_to_37Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sisu/sst_convert_markup.rb', line 131

def convert_38_to_37
  message("#{@warn}\nConvert sisu markup from 0.38 to 0.37")
  [
    [/^@(\S+?):([+-])\s+/,    '0~\1\2 ',               //],
    [/^@(\S+?):\s+/,          '0~\1 ',                 //],
    [/^0~structure\s+/,       '0~toc ',                //],
    [/^1~/,                   '4~',                    //],
    [/^2~/,                   '5~',                    //],
    [/^3~/,                   '6~',                    //],
    [/^4~/,                   '7~',                    //],
    [/^5~/,                   '8~',                    //],
    [/^6~/,                   '9~',                    //],
    [/^:?A~/,                 '1~',                    //],
    [/^:?B~/,                 '2~',                    //],
    [/^:?C~/,                 '3~',                    //],
    [/1/,                     '4',                     /^0~(?:level|markup)\s/],
    [/2/,                     '5',                     /^0~(?:level|markup)\s/],
    [/3/,                     '6',                     /^0~(?:level|markup)\s/],
    [/:?A/,                   '1',                     /^0~(?:level|markup)\s/],
    [/:?B/,                   '2',                     /^0~(?:level|markup)\s/],
    [/:?C/,                   '3',                     /^0~(?:level|markup)\s/]
  ]
end

#convert_filename_36_to_37Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/sisu/sst_convert_markup.rb', line 154

def convert_filename_36_to_37
  @opt.files.each do |f|
    s=case f
    when /(\.s[1-3])$/ then f.sub($1,'.sst')
    when /(\.r[1-3])$/ then f.sub($1,'.ssm')
    when /(\.ri)$/     then f.sub($1,'.ssi')
    else f
    end
    pwd=Dir.pwd
    unless f==s
      unless File.exist?("#{pwd}/#{s}")
        puts "./#{f} -> ./#{s}"
        FileUtils::cp("#{pwd}/#{f}","#{pwd}/#{s}")
      else "File already exists, < #{s} >  will not overwrite"
      end
    end
  end
end

#convert_footnotesObject



187
188
189
190
# File 'lib/sisu/sst_convert_markup.rb', line 187

def convert_footnotes
  require_relative 'sst_do_inline_footnotes'
  SiSU_ConvertFootnotes::Source.new(@opt).read
end

#convert_kdi_to_sstObject



181
182
183
# File 'lib/sisu/sst_convert_markup.rb', line 181

def convert_kdi_to_sst
  SiSU_Kdissert::Convert.new(@opt).read
end

#convert_s_xml_to_sstObject



184
185
186
# File 'lib/sisu/sst_convert_markup.rb', line 184

def convert_s_xml_to_sst
  SiSU_sstFromXML::Convert.new(@opt).read
end

#convert_to_simple_xml_model_domObject



175
176
177
# File 'lib/sisu/sst_convert_markup.rb', line 175

def convert_to_simple_xml_model_dom
  SiSU_simple_xml_model_dom::Convert.new(@opt).read
end

#convert_to_simple_xml_model_nodeObject



178
179
180
# File 'lib/sisu/sst_convert_markup.rb', line 178

def convert_to_simple_xml_model_node
  SiSU_simple_xml_model_node::Convert.new(@opt).read
end

#convert_to_simple_xml_model_saxObject



172
173
174
# File 'lib/sisu/sst_convert_markup.rb', line 172

def convert_to_simple_xml_model_sax
  SiSU_SimpleXML_ModelSax::Convert.new(@opt).read
end

#current_match_and_replaceObject



70
71
72
# File 'lib/sisu/sst_convert_markup.rb', line 70

def current_match_and_replace
  convert_37_to_38
end

#helpObject



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
# File 'lib/sisu/sst_convert_markup.rb', line 80

def help
print <<WOK

#{@description}

sisu --convert --to38 [filename/wildcard]
  converts pre 0.37 sisu markup to 0.38 experimental
  [--37to38]

sisu --convert --to37 [filename/wildcard]
  converts pre 0.37 sisu markup to 0.38 experimental
  [--38to37]

sisu --convert --36to37 [filename/wildcard]
  converts pre 0.36 file-name, to 0.37 file-name
  [--36to37]

sisu --identify [filename]
  attempts to identify markup version used in file

sisu --query [version number]
  gives short summary of distinguishing characteristic
  of that version of markup

WOK
  exit
end

#message(text) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/sisu/sst_convert_markup.rb', line 73

def message(text)
  response=''
  unless @opt.cmd=~/QQ/ \
  or @opt.act[:quiet][:set]==:on
    response=response?(%{#{  text}\nProceed? })
  end
end