Class: HTML::Converter

Inherits:
Object show all
Includes:
KanjiConverter, StripTags
Defined in:
lib/langhelp/langhelp-base.rb

Constant Summary

Constants included from StripTags

StripTags::ALT, StripTags::NQ, StripTags::Q

Constants included from KanjiConverter

KanjiConverter::KCONVERTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StripTags

#html2txt!

Methods included from KanjiConverter

#encoding, #kconv

Constructor Details

#initialize(x = {}) ⇒ Converter

Returns a new instance of Converter.



460
461
462
463
464
465
466
467
# File 'lib/langhelp/langhelp-base.rb', line 460

def initialize(x={})
  @filename = x[:tagify_html]
  @noconv = x[:noconv]
  @recursive = x[:recursive]

  @html_obj = x[:html_obj]
  @conf = x[:conf]
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



469
470
471
# File 'lib/langhelp/langhelp-base.rb', line 469

def conf
  @conf
end

#tagsObject (readonly)

Returns the value of attribute tags.



468
469
470
# File 'lib/langhelp/langhelp-base.rb', line 468

def tags
  @tags
end

Instance Method Details

#chop_meta_charset!(html) ⇒ Object

Chop meta-charset tag to avoid mojibake



472
473
474
# File 'lib/langhelp/langhelp-base.rb', line 472

def chop_meta_charset!(html)
  html.sub!(/<meta[^>]*charset[^>]+>/mi,'')
end

#copy_and_set_filename_unless_writableObject



531
532
533
534
535
536
537
538
539
540
541
# File 'lib/langhelp/langhelp-base.rb', line 531

def copy_and_set_filename_unless_writable
  @original_filename = @filename
  unless File.writable? @filename
    newfile = "#{$langhelp_home}#{@filename}"
    FileUtils.mkdir_p File.dirname(newfile)
    FileUtils.rm_f newfile
    FileUtils.cp @filename, newfile
    FileUtils.chmod 0644, newfile
    @filename = newfile
  end
end

#tagifyObject



544
545
546
547
548
549
550
551
552
# File 'lib/langhelp/langhelp-base.rb', line 544

def tagify
#      $stderr.puts "Tagifying #{@filename}..."
  copy_and_set_filename_unless_writable
  open(@filename, "rb+") do |f|
    after = _tagify(kconv{ f.read })
    f.rewind
    f.write(after)
  end
end

#treat_recursive(html) ⇒ Object



477
478
479
480
481
482
483
484
485
486
# File 'lib/langhelp/langhelp-base.rb', line 477

def treat_recursive(html)
  if @recursive == :next
    if html =~ /<link[^>]*rel=#{Q}next#{Q}[^>]*href=#{Q}(#{NQ})#{Q}/o
      uri = URI.parse("file://"+@original_filename)
      return if $1 == ''
      href = uri.merge($1)
      @html_obj.add_html href.path
    end
  end
end