class SiSU_TextRepresentation::ModifiedTextPlusHashDigest

Public Class Methods

new(md,x) click to toggle source
# 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

Public Instance Methods

composite() click to toggle source
# 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
dgst() click to toggle source
# File lib/sisu/shared_markup_alt.rb, line 237
def dgst
  txt_dgst=digest(txt)
  { txt: txt, dgst_txt: txt_dgst }
end
digest(txt) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/sisu/shared_markup_alt.rb, line 257
def markup_reverted(txt)
  SiSU_TextRepresentation::Alter.new(txt).semi_revert_markup
end
semi_revert_markup() click to toggle source
# 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_markup() click to toggle source
# 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) click to toggle source
# File lib/sisu/shared_markup_alt.rb, line 254
def stripped_clean(txt)
  SiSU_TextRepresentation::Alter.new(txt).strip_clean_of_markup
end
txt() click to toggle source
# File lib/sisu/shared_markup_alt.rb, line 234
def txt
  SiSU_TextRepresentation::Alter.new(@s).strip_clean_of_markup
end