html2index 1.2.1 → 1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/html2index +4 -10
- data/doc/html/html2index.html +430 -582
- data/doc/man/html2index.1.gz +0 -0
- data/doc/pdf/html2index.pdf +0 -0
- data/doc/rst/html2index.rst +30 -14
- data/html2index.gemspec +11 -7
- data/lib/argparser.rb +9 -19
- data/lib/basic_logging.rb +202 -0
- data/lib/config +27 -0
- data/lib/configuration.rb +48 -45
- data/lib/constants.rb +8 -14
- data/lib/definition.rb +5 -10
- data/lib/dictionary.rb +12 -10
- data/lib/html2index.rb +125 -91
- data/lib/template.rb +20 -29
- data/lib/translating.rb +31 -28
- data/lib/user_input.rb +4 -12
- data/lib/version.rb +16 -12
- metadata +21 -23
- data/lib/file_checking.rb +0 -103
- data/lib/log.conf +0 -56
- data/lib/logging.rb +0 -206
data/lib/definition.rb
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
#encoding: UTF-8
|
|
2
2
|
=begin
|
|
3
3
|
/***************************************************************************
|
|
4
|
-
* ©2015-
|
|
4
|
+
* ©2015-2026, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
|
5
5
|
* *
|
|
6
6
|
* This program is free software; you can redistribute it and/or modify *
|
|
7
|
-
* it under the terms of the
|
|
8
|
-
*
|
|
9
|
-
* (at your option) any later version. *
|
|
7
|
+
* it under the terms of the WTFPL, version 2 or later, as published on *
|
|
8
|
+
* https://www.wtfpl.net/about/. *
|
|
10
9
|
* *
|
|
11
10
|
* This program is distributed in the hope that it will be useful, *
|
|
12
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
13
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
14
|
-
* GNU General Public License for more details. *
|
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
15
13
|
* *
|
|
16
|
-
* You should have received a copy of the GNU General Public License *
|
|
17
|
-
* along with this program; if not, write to the *
|
|
18
|
-
* Free Software Foundation, Inc., *
|
|
19
|
-
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
20
14
|
***************************************************************************/
|
|
21
15
|
=end
|
|
16
|
+
|
|
22
17
|
require 'constants'
|
|
23
18
|
require_relative 'translating'
|
|
24
19
|
|
data/lib/dictionary.rb
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
#encoding: UTF-8
|
|
2
2
|
=begin
|
|
3
3
|
/***************************************************************************
|
|
4
|
-
* ©2015-
|
|
4
|
+
* ©2015-2026, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
|
5
5
|
* *
|
|
6
6
|
* This program is free software; you can redistribute it and/or modify *
|
|
7
|
-
* it under the terms of the
|
|
8
|
-
*
|
|
9
|
-
* (at your option) any later version. *
|
|
7
|
+
* it under the terms of the WTFPL, version 2 or later, as published on *
|
|
8
|
+
* https://www.wtfpl.net/about/. *
|
|
10
9
|
* *
|
|
11
10
|
* This program is distributed in the hope that it will be useful, *
|
|
12
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
13
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
14
|
-
* GNU General Public License for more details. *
|
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
15
13
|
* *
|
|
16
|
-
* You should have received a copy of the GNU General Public License *
|
|
17
|
-
* along with this program; if not, write to the *
|
|
18
|
-
* Free Software Foundation, Inc., *
|
|
19
|
-
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
20
14
|
***************************************************************************/
|
|
21
15
|
=end
|
|
16
|
+
|
|
22
17
|
require 'constants'
|
|
23
18
|
require_relative 'translating'
|
|
24
19
|
|
|
@@ -32,6 +27,13 @@ class Dictionary
|
|
|
32
27
|
@xpath = xpath
|
|
33
28
|
@color = color
|
|
34
29
|
end
|
|
30
|
+
|
|
31
|
+
def valid?
|
|
32
|
+
return (@name && ! @name.empty?
|
|
33
|
+
&& @url && ! @url.empty?
|
|
34
|
+
&& @xpath && ! @xpath.empty?
|
|
35
|
+
&& @color && ! @color.empty?)
|
|
36
|
+
end
|
|
35
37
|
end
|
|
36
38
|
|
|
37
39
|
##### TEST
|
data/lib/html2index.rb
CHANGED
|
@@ -1,27 +1,20 @@
|
|
|
1
1
|
#encoding: UTF-8
|
|
2
2
|
=begin
|
|
3
3
|
/***************************************************************************
|
|
4
|
-
* ©2015-
|
|
4
|
+
* ©2015-2026, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
|
5
5
|
* *
|
|
6
6
|
* This program is free software; you can redistribute it and/or modify *
|
|
7
|
-
* it under the terms of the
|
|
8
|
-
*
|
|
9
|
-
* (at your option) any later version. *
|
|
7
|
+
* it under the terms of the WTFPL, version 2 or later, as published on *
|
|
8
|
+
* https://www.wtfpl.net/about/. *
|
|
10
9
|
* *
|
|
11
10
|
* This program is distributed in the hope that it will be useful, *
|
|
12
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
13
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
14
|
-
* GNU General Public License for more details. *
|
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
15
13
|
* *
|
|
16
|
-
* You should have received a copy of the GNU General Public License *
|
|
17
|
-
* along with this program; if not, write to the *
|
|
18
|
-
* Free Software Foundation, Inc., *
|
|
19
|
-
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
20
14
|
***************************************************************************/
|
|
21
15
|
=end
|
|
22
|
-
|
|
23
16
|
require_relative 'argparser'
|
|
24
|
-
require_relative '
|
|
17
|
+
require_relative 'basic_logging'
|
|
25
18
|
require_relative 'translating'
|
|
26
19
|
require_relative 'user_input'
|
|
27
20
|
require_relative 'constants'
|
|
@@ -31,43 +24,46 @@ require_relative 'dictionary'
|
|
|
31
24
|
require_relative 'template'
|
|
32
25
|
require 'open-uri'
|
|
33
26
|
require 'nokogiri'
|
|
34
|
-
require '
|
|
35
|
-
require_relative 'file_checking'
|
|
27
|
+
require 'marcel'
|
|
36
28
|
require_relative 'configuration'
|
|
37
29
|
|
|
38
30
|
class HTML2Index
|
|
39
|
-
include
|
|
31
|
+
include BasicLogging
|
|
40
32
|
include Nokogiri
|
|
41
33
|
|
|
34
|
+
|
|
35
|
+
@@log_level = :debug
|
|
42
36
|
=begin
|
|
43
37
|
initialize the HTML2Index-object
|
|
44
38
|
=end
|
|
45
39
|
|
|
46
40
|
def initialize(*argv)
|
|
47
|
-
init_logger()
|
|
48
41
|
# ensure the configuration-file exist.
|
|
49
42
|
options = ArgParser.parse(argv)
|
|
50
43
|
$configuration = Configuration.new(options)
|
|
51
44
|
$configuration.user_conf
|
|
52
|
-
|
|
45
|
+
set_level $log_level
|
|
53
46
|
@file = $configuration.source
|
|
54
47
|
|
|
55
|
-
|
|
56
|
-
msg =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
48
|
+
debug("read #{@file}, write #{$configuration.target}")
|
|
49
|
+
msg = ''
|
|
50
|
+
[:exist?, :readable?, :file?].each do |cond|
|
|
51
|
+
if !File.send(cond, @file)
|
|
52
|
+
error("Error: unsuitable file " <<@file << ": NOT " << cond.to_s.split('?')[0] << '!')
|
|
53
|
+
exit false
|
|
54
|
+
end
|
|
60
55
|
end
|
|
61
56
|
|
|
62
|
-
ftype =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
57
|
+
ftype = Marcel::Magic.by_path(@file).subtype
|
|
58
|
+
|
|
59
|
+
debug('ftype for ' << @file << ' is ' << ftype.to_s)
|
|
60
|
+
if(ftype && !ftype.empty? && !ftype.downcase.match(/(html|xml)/) )
|
|
61
|
+
error(@file.dup << ' does not look like a valid file to scan: ' << ftype)
|
|
66
62
|
exit false
|
|
67
63
|
end
|
|
68
64
|
|
|
69
65
|
@problem_log = File.join(temp_dir, PROBLEM_LOG)
|
|
70
|
-
|
|
66
|
+
debug('calling generate() ')
|
|
71
67
|
generate()
|
|
72
68
|
end
|
|
73
69
|
|
|
@@ -77,7 +73,7 @@ class HTML2Index
|
|
|
77
73
|
$configuration.dicts.each do |d|
|
|
78
74
|
list << '<li>' << d.name << ' : ' << '<a href="' << d.url << '">' << d.url << '</a></li>' << "\n"
|
|
79
75
|
end
|
|
80
|
-
|
|
76
|
+
debug('list is ' << list.to_s)
|
|
81
77
|
list << "</ul>\n"
|
|
82
78
|
end
|
|
83
79
|
|
|
@@ -85,19 +81,19 @@ class HTML2Index
|
|
|
85
81
|
Parses the html-file and generates the glossary.
|
|
86
82
|
=end
|
|
87
83
|
def generate()
|
|
88
|
-
|
|
84
|
+
info('Generating... PSE wait')
|
|
89
85
|
begin
|
|
90
86
|
@doc = HTML::Document.parse(File.open(@file ) )
|
|
91
87
|
write_html()
|
|
92
88
|
rescue SystemExit => ir
|
|
93
|
-
|
|
89
|
+
info "Bye"
|
|
94
90
|
exit true
|
|
95
91
|
rescue Exception => ex
|
|
96
|
-
|
|
92
|
+
error "line #{__LINE__}: " << ex.message
|
|
97
93
|
exit false
|
|
98
94
|
end
|
|
99
95
|
if(File.exist?(@problem_log ))
|
|
100
|
-
|
|
96
|
+
info "Some expressions caused problems and are listed in " << @problem_log
|
|
101
97
|
end
|
|
102
98
|
end
|
|
103
99
|
private
|
|
@@ -109,41 +105,62 @@ class HTML2Index
|
|
|
109
105
|
=end
|
|
110
106
|
def dict_definition(expression)
|
|
111
107
|
definitions = Array.new
|
|
112
|
-
$configuration.dicts
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
rescue Exception => ex
|
|
119
|
-
url_b = d.url.dup << URI.encode_www_form_component(expression.gsub(/\s/,'_'))
|
|
120
|
-
@log.warn('WARNING, accessing ' << url << ': ' << ex.message)
|
|
121
|
-
if(url_b != url)
|
|
122
|
-
@log.warn("\twill try " << url_b)
|
|
108
|
+
if $configuration.dicts && ! $configuration.dicts.empty?
|
|
109
|
+
$configuration.dicts.each do |d|
|
|
110
|
+
if d && d.valid?
|
|
111
|
+
debug 'dict: ' << (d ? d.inspect : ' N I L ')
|
|
112
|
+
text_array = Array.new
|
|
113
|
+
page = nil
|
|
123
114
|
begin
|
|
124
|
-
|
|
115
|
+
url = d.url.dup << URI.encode_www_form_component(expression.gsub(/\s/,'_').gsub("'", ''))
|
|
116
|
+
page = Nokogiri::HTML(URI.open(url))
|
|
125
117
|
rescue Exception => ex
|
|
126
|
-
|
|
118
|
+
url_b = d.url.dup << URI.encode_www_form_component(expression.gsub(/\s/,'_'))
|
|
119
|
+
warn('WARNING, accessing ' << url << ': ' << ex.message)
|
|
120
|
+
if(url_b != url)
|
|
121
|
+
warn("\twill try " << url_b)
|
|
122
|
+
begin
|
|
123
|
+
page = Nokogiri::HTML(URI.open(url_b, "User-Agent"=>"#{APPNAME} #{VERSION}", "From"=>"Function Test <bat.guano@don.blech.e4ward.com>"))
|
|
124
|
+
rescue Exception => ex
|
|
125
|
+
warn("\tWARNING, accessing " << url_b << ': ' << ex.message)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
else
|
|
129
|
+
warn 'invalid dictionary definition'
|
|
130
|
+
|
|
127
131
|
end
|
|
128
132
|
end
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
133
|
+
if(page)
|
|
134
|
+
nodes = page.xpath(d.xpath)
|
|
135
|
+
if(nodes)
|
|
136
|
+
text_array = nodes.collect {|n| n.inner_text.to_s.strip}
|
|
137
|
+
if(!text_array.empty?)
|
|
138
|
+
definition = Definition.new(d.name, expression.gsub('_', ' '), text_array)
|
|
139
|
+
definition.color = d.color
|
|
140
|
+
definitions.push(definition)
|
|
141
|
+
end
|
|
138
142
|
end
|
|
143
|
+
else
|
|
144
|
+
File.open(@problem_log, 'a'){|f| f.write(expression + "\n") }
|
|
139
145
|
end
|
|
140
|
-
else
|
|
141
|
-
File.open(@problem_log, 'a'){|f| f.write(expression + "\n") }
|
|
142
146
|
end
|
|
147
|
+
else
|
|
148
|
+
error 'no dictionaries defined!'
|
|
149
|
+
exit false
|
|
143
150
|
end
|
|
144
151
|
return definitions
|
|
145
152
|
end
|
|
146
153
|
|
|
154
|
+
def verify_strings(strs)
|
|
155
|
+
debug 'strings to verify: ' << strs.to_s
|
|
156
|
+
if strs.respond_to?(:to_ary)
|
|
157
|
+
strs.each {|s| return false if(!s || s.strip.empty?) }
|
|
158
|
+
else
|
|
159
|
+
return strs && !(strs.strip).empy?()
|
|
160
|
+
end
|
|
161
|
+
return true
|
|
162
|
+
end
|
|
163
|
+
|
|
147
164
|
=begin
|
|
148
165
|
Creates and returns an Array of Definitions.
|
|
149
166
|
=end
|
|
@@ -152,43 +169,57 @@ Creates and returns an Array of Definitions.
|
|
|
152
169
|
#TODO: Hash
|
|
153
170
|
list = Array.new
|
|
154
171
|
def_list = Array.new
|
|
172
|
+
|
|
155
173
|
tag = $configuration.html_tag
|
|
156
174
|
attr = $configuration.html_attribute
|
|
157
175
|
value = $configuration.attr_value
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
|
|
177
|
+
if verify_strings([tag,attr,value])
|
|
178
|
+
xpath = ".//#{tag}[contains(@#{attr}, '#{value}')]"
|
|
179
|
+
# xpath = './/' << value << '[@' << attr << '="' << value << '"]'
|
|
180
|
+
debug('xpath is ' << xpath)
|
|
181
|
+
tags = @doc.xpath(xpath)
|
|
182
|
+
if(tags && !tags.empty?)
|
|
183
|
+
tags.each do |t|
|
|
184
|
+
expression = t.attribute('title').to_s
|
|
185
|
+
unless (expression && !expression.strip.empty? )
|
|
186
|
+
expression = t.text.to_s
|
|
187
|
+
end
|
|
188
|
+
if(expression)
|
|
189
|
+
expression.gsub!(/\s+/, " ")
|
|
190
|
+
expression.strip!
|
|
191
|
+
if(!expression.empty? && !def_list.include?(expression) )
|
|
192
|
+
if(!list.include?expression.downcase)
|
|
193
|
+
list.push expression.downcase
|
|
194
|
+
definition = dict_definition(expression)
|
|
195
|
+
if(definition && !definition.empty?)
|
|
196
|
+
# debug('definition is ' << definition.to_s)
|
|
197
|
+
def_list.push(format_index(definition) )
|
|
198
|
+
def_list.sort!
|
|
199
|
+
end
|
|
200
|
+
end
|
|
178
201
|
end
|
|
179
202
|
end
|
|
180
203
|
end
|
|
204
|
+
else
|
|
205
|
+
error 'Cannot find any marked tags in the source-file. Aborting!'
|
|
206
|
+
raise SystemExit
|
|
207
|
+
#exit false
|
|
181
208
|
end
|
|
209
|
+
else
|
|
210
|
+
error 'Configuration incomplete. Verify tag, attribute and attribute value! Aborting!'
|
|
211
|
+
raise SystemExit
|
|
212
|
+
#exit false
|
|
182
213
|
end
|
|
183
|
-
=begin
|
|
184
|
-
if(@log.level == Logger::DEBUG)
|
|
185
|
-
@log.debug('def_list is ')
|
|
186
|
-
def_list.each_with_index{|t, i| @log.debug( "%i %s"%[i, t]) }
|
|
187
|
-
end
|
|
188
|
-
=end
|
|
189
214
|
rescue Interrupt => ex
|
|
190
|
-
|
|
215
|
+
warn "--------------\n\tIndex generation interrupted by user\n---------------"
|
|
216
|
+
rescue SystemExit => sx
|
|
217
|
+
raise sx
|
|
218
|
+
rescue Exception => ex
|
|
219
|
+
error( "line #{__LINE__} " << ex.message << ' (' << ex.class.name << ')')
|
|
220
|
+
exit false
|
|
191
221
|
end
|
|
222
|
+
|
|
192
223
|
return def_list
|
|
193
224
|
end
|
|
194
225
|
|
|
@@ -212,11 +243,11 @@ Formats the definition text.
|
|
|
212
243
|
# def temp_dir options = {:remove => true}
|
|
213
244
|
def temp_dir
|
|
214
245
|
@temp_dir ||= begin
|
|
215
|
-
|
|
246
|
+
debug('creating temp_dir')
|
|
216
247
|
require 'tmpdir'
|
|
217
248
|
require 'fileutils'
|
|
218
249
|
path = File.join(Dir::tmpdir, "HTML2Index_#{Time.now.to_i}_#{rand(1000)}")
|
|
219
|
-
|
|
250
|
+
debug('temp-dir path is ' << path)
|
|
220
251
|
Dir.mkdir(path)
|
|
221
252
|
# at_exit {FileUtils.rm_rf(path) if File.exists?(path)} if options[:remove]
|
|
222
253
|
File.new path
|
|
@@ -227,10 +258,12 @@ Formats the definition text.
|
|
|
227
258
|
tempfile = nil
|
|
228
259
|
out = nil
|
|
229
260
|
out_file = $configuration.target
|
|
261
|
+
debug 'out_file is ' << out_file
|
|
230
262
|
template = Template.new
|
|
263
|
+
debug 'template is ' << template.to_s
|
|
231
264
|
if out_file
|
|
232
|
-
if(File.exist?(out_file))
|
|
233
|
-
if(File.writable?(out_file))
|
|
265
|
+
if(out_file != '<STDOUT>' && File.exist?(out_file) )
|
|
266
|
+
if( File.writable?(out_file) )
|
|
234
267
|
puts "\nWARNING! File " << out_file << " exists!"
|
|
235
268
|
print "Do you want to overwrite it? (Y/n) "
|
|
236
269
|
res = wait_for_user
|
|
@@ -245,9 +278,10 @@ Formats the definition text.
|
|
|
245
278
|
end
|
|
246
279
|
end
|
|
247
280
|
else
|
|
248
|
-
|
|
281
|
+
info('Writing to ' << out_file)
|
|
249
282
|
end
|
|
250
283
|
begin
|
|
284
|
+
debug 'begin'
|
|
251
285
|
html = template.to_s
|
|
252
286
|
|
|
253
287
|
# create the definitions in index()
|
|
@@ -258,15 +292,15 @@ Formats the definition text.
|
|
|
258
292
|
fdelim = $configuration.fdelim
|
|
259
293
|
placeholders.each_pair do |k, v|
|
|
260
294
|
repl = fdelim.dup << v << fdelim.reverse
|
|
261
|
-
|
|
295
|
+
debug('try to replace ' << repl)
|
|
262
296
|
html.gsub!(repl, data[k])
|
|
263
297
|
end
|
|
264
|
-
|
|
298
|
+
debug('html is now ' << html)
|
|
265
299
|
html.gsub!(/\<head\>/, "<head>\n\t#{GeneratorMeta}")
|
|
266
300
|
File.write(out_file, html) if out_file
|
|
267
301
|
puts html if !out_file
|
|
268
302
|
rescue Exception => ex
|
|
269
|
-
|
|
303
|
+
error( "line #{__LINE__} " << ex.message << ' (' << ex.class.name << ')')
|
|
270
304
|
ensure
|
|
271
305
|
out.close if out && !out.closed?
|
|
272
306
|
File.unlink(out) if out
|
data/lib/template.rb
CHANGED
|
@@ -1,53 +1,44 @@
|
|
|
1
1
|
#encoding: UTF-8
|
|
2
|
+
|
|
2
3
|
=begin
|
|
3
4
|
/***************************************************************************
|
|
4
|
-
* ©2017-
|
|
5
|
+
* ©2017-2026, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
|
5
6
|
* *
|
|
6
7
|
* This program is free software; you can redistribute it and/or modify *
|
|
7
|
-
* it under the terms of the
|
|
8
|
-
*
|
|
9
|
-
* (at your option) any later version. *
|
|
8
|
+
* it under the terms of the WTFPL, version 2 or later, as published on *
|
|
9
|
+
* https://www.wtfpl.net/about/. *
|
|
10
10
|
* *
|
|
11
11
|
* This program is distributed in the hope that it will be useful, *
|
|
12
12
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
13
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
14
|
-
* GNU General Public License for more details. *
|
|
13
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
15
14
|
* *
|
|
16
|
-
* You should have received a copy of the GNU General Public License *
|
|
17
|
-
* along with this program; if not, write to the *
|
|
18
|
-
* Free Software Foundation, Inc., *
|
|
19
|
-
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
20
15
|
***************************************************************************/
|
|
21
16
|
=end
|
|
22
|
-
|
|
17
|
+
|
|
23
18
|
require 'date'
|
|
24
|
-
require_relative '
|
|
19
|
+
require_relative 'basic_logging'
|
|
25
20
|
require_relative 'dictionary'
|
|
26
21
|
require_relative 'constants'
|
|
27
|
-
require_relative 'file_checking'
|
|
28
22
|
|
|
29
23
|
class Template
|
|
30
|
-
|
|
31
|
-
@@log = init_logger
|
|
24
|
+
include BasicLogging
|
|
32
25
|
|
|
33
26
|
def initialize()
|
|
34
|
-
|
|
35
|
-
@log.level = $log_level
|
|
27
|
+
set_level = $log_level
|
|
36
28
|
template_file = $configuration.template
|
|
37
29
|
@template = nil
|
|
38
30
|
if template_file
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
31
|
+
error = nil
|
|
32
|
+
[:file?, :readable?].each do |cond|
|
|
33
|
+
if(!File.send(cond, template_file) )
|
|
34
|
+
error = true
|
|
35
|
+
warn('Cannot use template ' << template_file << ': NOT ' << cond.to_s.split('?')[0] << ',' << "\n\tusing default template")
|
|
44
36
|
end
|
|
45
37
|
end
|
|
46
|
-
|
|
38
|
+
|
|
39
|
+
if(!error)
|
|
47
40
|
@template = File.read(template_file)
|
|
48
|
-
|
|
49
|
-
else
|
|
50
|
-
@log.warn('Cannot use template ' << template_file << ': ' << msg << "\n\tusing default template")
|
|
41
|
+
debug('using template ' << template_file)
|
|
51
42
|
end
|
|
52
43
|
end
|
|
53
44
|
end
|
|
@@ -57,7 +48,7 @@ class Template
|
|
|
57
48
|
if @template && @template.respond_to?(:to_str)
|
|
58
49
|
str = @template
|
|
59
50
|
else
|
|
60
|
-
|
|
51
|
+
warn('Empty template, using default!')
|
|
61
52
|
str = @@def_template
|
|
62
53
|
end
|
|
63
54
|
str
|
|
@@ -74,8 +65,8 @@ class Template
|
|
|
74
65
|
when :def_template
|
|
75
66
|
@@def_template
|
|
76
67
|
else
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
error('Do not know ' << name.to_s)
|
|
69
|
+
error('Aborting. Bye!')
|
|
79
70
|
end
|
|
80
71
|
end
|
|
81
72
|
|
data/lib/translating.rb
CHANGED
|
@@ -1,23 +1,16 @@
|
|
|
1
1
|
#encoding: UTF-8
|
|
2
2
|
=begin
|
|
3
3
|
/***************************************************************************
|
|
4
|
-
* ©2011-
|
|
5
|
-
* <michael.uplawski@uplawski.eu> *
|
|
4
|
+
* ©2011-2026, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
|
6
5
|
* *
|
|
7
6
|
* This program is free software; you can redistribute it and/or modify *
|
|
8
|
-
* it under the terms of the
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* *
|
|
7
|
+
* it under the terms of the WTFPL, version 2 or later, as published on *
|
|
8
|
+
* https://www.wtfpl.net/about/. *
|
|
9
|
+
* *
|
|
12
10
|
* This program is distributed in the hope that it will be useful, *
|
|
13
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
14
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
15
|
-
* GNU General Public License for more details. *
|
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
16
13
|
* *
|
|
17
|
-
* You should have received a copy of the GNU General Public License *
|
|
18
|
-
* along with this program; if not, write to the *
|
|
19
|
-
* Free Software Foundation, Inc., *
|
|
20
|
-
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
21
14
|
***************************************************************************/
|
|
22
15
|
=end
|
|
23
16
|
|
|
@@ -25,7 +18,6 @@
|
|
|
25
18
|
RD = File.expand_path(File.dirname(__FILE__) ) + File::SEPARATOR if !defined?(RD)
|
|
26
19
|
|
|
27
20
|
require 'yaml'
|
|
28
|
-
require_relative 'file_checking'
|
|
29
21
|
|
|
30
22
|
|
|
31
23
|
# A way to produce translated text in a ruby-program.
|
|
@@ -38,21 +30,32 @@ module Translating
|
|
|
38
30
|
|
|
39
31
|
@@lang = nil
|
|
40
32
|
@@lang_file = format("%s%s", RD, 'LANG')
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
33
|
+
@@tr = YAML::load_file("#{RD}translations")
|
|
34
|
+
|
|
35
|
+
# find the current language-setting and return it.
|
|
36
|
+
def self.language()
|
|
37
|
+
if @@lang == nil
|
|
38
|
+
r = ENV['LANG']
|
|
39
|
+
if(r)
|
|
40
|
+
@@lang = r[0, 2]
|
|
41
|
+
else
|
|
42
|
+
error = nil
|
|
43
|
+
[:exist?, :readable?].each do |cond|
|
|
44
|
+
if(! File.send(cond, @@lang_File))
|
|
45
|
+
error = cond.to_s.split('?')[0]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
if(!error && File::size(@@lang_file) >= 2)
|
|
49
|
+
File::open(@@lang_file, 'r') {|f| @@lang = f.readline}
|
|
50
|
+
@@lang.chomp!.downcase! if @@lang
|
|
51
|
+
else
|
|
52
|
+
warn 'Cannot use ' << @@lang_file << '; file unuseable'
|
|
53
|
+
info 'Language will be English'
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
@@lang = 'en' if !@@lang
|
|
58
|
+
end
|
|
56
59
|
|
|
57
60
|
# Translate a string to the currently set langage.
|
|
58
61
|
# The args parameter may contain replacement-text which
|
data/lib/user_input.rb
CHANGED
|
@@ -1,26 +1,18 @@
|
|
|
1
1
|
#encoding: UTF-8
|
|
2
|
-
|
|
3
2
|
=begin
|
|
4
3
|
/***************************************************************************
|
|
5
|
-
*
|
|
4
|
+
* ©2011-2026, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
|
6
5
|
* *
|
|
7
6
|
* This program is free software; you can redistribute it and/or modify *
|
|
8
|
-
* it under the terms of the
|
|
9
|
-
*
|
|
10
|
-
* (at your option) any later version. *
|
|
7
|
+
* it under the terms of the WTFPL, version 2 or later, as published on *
|
|
8
|
+
* https://www.wtfpl.net/about/. *
|
|
11
9
|
* *
|
|
12
10
|
* This program is distributed in the hope that it will be useful, *
|
|
13
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
14
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
15
|
-
* GNU General Public License for more details. *
|
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
16
13
|
* *
|
|
17
|
-
* You should have received a copy of the GNU General Public License *
|
|
18
|
-
* along with this program; if not, write to the *
|
|
19
|
-
* Free Software Foundation, Inc., *
|
|
20
|
-
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
21
14
|
***************************************************************************/
|
|
22
15
|
=end
|
|
23
|
-
|
|
24
16
|
require 'io/wait'
|
|
25
17
|
require 'io/console'
|
|
26
18
|
|