Class: SiSU_TextRepresentation::ModifiedTextPlusHashDigest

Inherits:
Object
  • Object
show all
Defined in:
lib/sisu/shared_markup_alt.rb

Instance Method Summary collapse

Constructor Details

#initialize(md, x) ⇒ ModifiedTextPlusHashDigest

Returns a new instance of ModifiedTextPlusHashDigest.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/sisu/shared_markup_alt.rb', line 193

def initialize(md,x)
  @md=md
  if x.is_a?(String)
    @t_o,@s=nil,x
  else
    @t_o,@s=x,x.obj.dup
  end
  @env ||=SiSU_Env::InfoEnv.new(@md.fns)
  @sha_ = @env.digest(@md.opt).type
  begin
    case @sha_
    when :sha512
      require 'digest/sha2'
    when :sha256
      require 'digest/sha2'
    when :md5
      require 'digest/md5'
    end
  rescue LoadError
    SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).error((@sha_ ? 'digest/sha2' : 'digest/md5') + ' NOT FOUND')
  end
end

Instance Method Details

#compositeObject



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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/sisu/shared_markup_alt.rb', line 253

def composite
  def stripped_clean(txt)
    SiSU_TextRepresentation::Alter.new(txt).strip_clean_of_markup
  end
  def markup_reverted(txt)
    SiSU_TextRepresentation::Alter.new(txt).semi_revert_markup
  end
  def images(imgs)
    sys=SiSU_Env::SystemCall.new
    line_image=[]
    if imgs and imgs.length > 0
       @image_name,@image_dgst,@img=[],[],[]
       imgs.each do |i|
         image_source=if FileTest.file?("#{@env.path.image_source_include_local}/#{i}")
           @env.path.image_source_include_local
         elsif FileTest.file?("#{@env.path.image_source_include_remote}/#{i}")
           @env.path.image_source_include_remote
         elsif FileTest.file?("#{@env.path.image_source_include}/#{i}")
           @env.path.image_source_include
         else
           SiSU_Screen::Ansi.new(
             @md.opt.act[:color_state][:set],
             "ERROR - image:",
             %{"#{i}" missing},
             "search locations: #{@env.path.image_source_include_local}, #{@env.path.image_source_include_remote} and #{@env.path.image_source_include}"
           ).error2 unless @md.opt.act[:quiet][:set]==:on
           nil
         end
         img_type = /\S+\.(png|jpg|gif)/.match(i)[1]
         if image_source
           para_image = image_source + '/' + i
           image_name = i
           image_dgst =(@sha_ ? sys.sha256(para_image) : sys.md5(para_image))
         else
           image_name = i + ' [image missing]'
           image_dgst = ''
         end
         line_image << { img_dgst: image_dgst[1], img_name: image_name, img_type: img_type }
       end
    end
    line_image
  end
  def endnotes(en)
    en_dgst=[]
    if en and en.length > 0
      en.flatten.each do |e|
         note_no=e.gsub(/^([\d*+]+)\s+.+/,'\1')
         e=digest(stripped_clean(e))
         note_dgst=digest(e)
         en_dgst << { note_number: note_no, note_dgst: note_dgst }
      end
    end
    en_dgst
  end
  def dgst
    if @t_o.of !=:comment \
    && @t_o.of !=:structure \
    && @t_o.of !=:layout
      txt_stripped_dgst=digest(stripped_clean(@t_o))
      txt_markup_reverted_dgst=digest(markup_reverted(@t_o))
      endnotes_dgst=[]
      rgx_notes=/(?:#{Mx[:en_a_o]}|#{Mx[:en_b_o]})([\d*+]+\s+.+?)(?:#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/
      notes=@t_o.obj.scan(rgx_notes)
      endnotes_dgst=endnotes(notes)
      rgx_image=/#{Mx[:lnk_o]}(\S+\.(?:png|jpg|gif))\s.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/
      imgs=if (@t_o.is==:para \
      || @t_o.is==:image) \
      and @t_o.obj =~rgx_image
        imgs=@t_o.obj.scan(rgx_image).flatten
        line_image=images(imgs)
      end
      dgst={ is: @t_o.is, ocn: @t_o.ocn, dgst_stripped_txt: txt_stripped_dgst, dgst_markedup_txt: txt_markup_reverted_dgst }
      dgst[:endnotes]=endnotes_dgst if endnotes_dgst and endnotes_dgst.length > 0
      dgst[:images]=line_image if line_image and line_image.length > 0
    end
    dgst
  end
  self
end

#dgstObject



237
238
239
240
# File 'lib/sisu/shared_markup_alt.rb', line 237

def dgst
  txt_dgst=digest(txt)
  { txt: txt, dgst_txt: txt_dgst }
end

#digest(txt) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/sisu/shared_markup_alt.rb', line 215

def digest(txt)
  d=nil
  case @sha_
  when :sha512
    for hash_class in [ Digest::SHA512 ]
      d=hash_class.hexdigest(txt)
    end
  when :sha256
    for hash_class in [ Digest::SHA256 ]
      d=hash_class.hexdigest(txt)
    end
  when :md5
    for hash_class in [ Digest::MD5 ]
      d=hash_class.hexdigest(txt)
    end
  end
  d
end

#endnotes(en) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/sisu/shared_markup_alt.rb', line 295

def endnotes(en)
  en_dgst=[]
  if en and en.length > 0
    en.flatten.each do |e|
       note_no=e.gsub(/^([\d*+]+)\s+.+/,'\1')
       e=digest(stripped_clean(e))
       note_dgst=digest(e)
       en_dgst << { note_number: note_no, note_dgst: note_dgst }
    end
  end
  en_dgst
end

#images(imgs) ⇒ Object



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

def images(imgs)
  sys=SiSU_Env::SystemCall.new
  line_image=[]
  if imgs and imgs.length > 0
     @image_name,@image_dgst,@img=[],[],[]
     imgs.each do |i|
       image_source=if FileTest.file?("#{@env.path.image_source_include_local}/#{i}")
         @env.path.image_source_include_local
       elsif FileTest.file?("#{@env.path.image_source_include_remote}/#{i}")
         @env.path.image_source_include_remote
       elsif FileTest.file?("#{@env.path.image_source_include}/#{i}")
         @env.path.image_source_include
       else
         SiSU_Screen::Ansi.new(
           @md.opt.act[:color_state][:set],
           "ERROR - image:",
           %{"#{i}" missing},
           "search locations: #{@env.path.image_source_include_local}, #{@env.path.image_source_include_remote} and #{@env.path.image_source_include}"
         ).error2 unless @md.opt.act[:quiet][:set]==:on
         nil
       end
       img_type = /\S+\.(png|jpg|gif)/.match(i)[1]
       if image_source
         para_image = image_source + '/' + i
         image_name = i
         image_dgst =(@sha_ ? sys.sha256(para_image) : sys.md5(para_image))
       else
         image_name = i + ' [image missing]'
         image_dgst = ''
       end
       line_image << { img_dgst: image_dgst[1], img_name: image_name, img_type: img_type }
     end
  end
  line_image
end

#markup_reverted(txt) ⇒ Object



257
258
259
# File 'lib/sisu/shared_markup_alt.rb', line 257

def markup_reverted(txt)
  SiSU_TextRepresentation::Alter.new(txt).semi_revert_markup
end

#semi_revert_markupObject



243
244
245
246
247
248
249
250
251
252
# File 'lib/sisu/shared_markup_alt.rb', line 243

def semi_revert_markup
  def txt
    SiSU_TextRepresentation::Alter.new(@s).semi_revert_markup
  end
  def dgst
    txt_dgst=digest(txt)
    { txt: txt, dgst_txt: txt_dgst }
  end
  self
end

#strip_clean_of_markupObject



233
234
235
236
237
238
239
240
241
242
# File 'lib/sisu/shared_markup_alt.rb', line 233

def strip_clean_of_markup
  def txt
    SiSU_TextRepresentation::Alter.new(@s).strip_clean_of_markup
  end
  def dgst
    txt_dgst=digest(txt)
    { txt: txt, dgst_txt: txt_dgst }
  end
  self
end

#stripped_clean(txt) ⇒ Object



254
255
256
# File 'lib/sisu/shared_markup_alt.rb', line 254

def stripped_clean(txt)
  SiSU_TextRepresentation::Alter.new(txt).strip_clean_of_markup
end

#txtObject



234
235
236
# File 'lib/sisu/shared_markup_alt.rb', line 234

def txt
  SiSU_TextRepresentation::Alter.new(@s).strip_clean_of_markup
end