Class: HTML2Index

Inherits:
Object
  • Object
show all
Includes:
BasicLogging, Nokogiri
Defined in:
lib/html2index.rb

Constant Summary collapse

@@log_level =
:debug

Instance Method Summary collapse

Methods included from BasicLogging

#clear_log, is_muted?, #level, #log, mute, #set_level, #set_target, #target

Constructor Details

#initialize(*argv) ⇒ HTML2Index

initialize the HTML2Index-object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/html2index.rb', line 40

def initialize(*argv)
  # ensure the configuration-file exist.
  options = ArgParser.parse(argv)
  $configuration = Configuration.new(options)
  $configuration.user_conf
  set_level $log_level
  @file = $configuration.source

  debug("read #{@file}, write #{$configuration.target}")
  msg = ''
  [:exist?, :readable?, :file?].each do |cond|
    if !File.send(cond, @file)
      error("Error: unsuitable file " <<@file << ": NOT " << cond.to_s.split('?')[0] << '!')
      exit false
    end
  end

  ftype = Marcel::Magic.by_path(@file).subtype

  debug('ftype for ' << @file << ' is ' << ftype.to_s)
  if(ftype && !ftype.empty? && !ftype.downcase.match(/(html|xml)/) )
    error(@file.dup << ' does not look like a valid file to scan: ' << ftype)
    exit false
  end

  @problem_log = File.join(temp_dir, PROBLEM_LOG)
  debug('calling generate() ') 
  generate()
end

Instance Method Details

#dict_listObject

create an unnumbered list of the consulted dictionaries.



71
72
73
74
75
76
77
78
# File 'lib/html2index.rb', line 71

def dict_list
  list = '<ul>'
  $configuration.dicts.each do |d|
    list << '<li>' << d.name << ' : ' << '<a href="' << d.url << '">' << d.url << '</a></li>' << "\n"
  end
  debug('list is ' << list.to_s)
  list << "</ul>\n"
end

#generateObject

Parses the html-file and generates the glossary.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/html2index.rb', line 83

def generate()
  info('Generating... PSE wait')
  begin
    @doc = HTML::Document.parse(File.open(@file ) )
    write_html()
  rescue SystemExit => ir
    info "Bye"
    exit true
  rescue Exception => ex
    error "line #{__LINE__}: " << ex.message
    exit false
  end
  if(File.exist?(@problem_log ))
    info "Some expressions caused problems and are listed in " << @problem_log
  end
end