parsergem 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb26a9c2d1591965320607c103b70fcfc231fcbb67532224c0affea0cba59443
4
- data.tar.gz: 644bffc80b1f88dec4f3523eac8bd96213b6f6dd80090b8693965347b2970849
3
+ metadata.gz: 7f4b5dcf97aec9f152486fa05c4264780bb9f4b2b9a890c4dd1ac66458cba31a
4
+ data.tar.gz: c073c2e151d5521f1870b8f94daa4068ceb618d991fb8ce5503d8c3f936b026a
5
5
  SHA512:
6
- metadata.gz: 82e0b832ff9df26b5ba0fd65894a6eccf1d0605d176f23c5643883680d4231d740db4fca5cdf0680ffe0d78860cdf69533fdc08d05d59b44e5e9de42102af87a
7
- data.tar.gz: 9d91c3fb09f112327897c7ef38217edef2f5a1e20158454113d07125ff383ef12896a97586dd442d3d6ae0ac14f200a6083c9507170702e66fabb35aa2e537dd
6
+ metadata.gz: 225ddfb601c1102ab8af2f9acef55007e6a93df5aa59e31cee827398f692ea0e9010f229363bdbe978f9117160c96ce0b57800a23c13b04955e3010a61dab717
7
+ data.tar.gz: 4dbc5f4fd706778bd438d099165b06922c6d19dfad50a23a1412eac152b6abb0f91bb00973dbd0b42012a3bfef7dde62db452292d2c93720c22a0538d1b1264d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- parsergem (0.1.2)
4
+ parsergem (0.1.3)
5
5
  activesupport (>= 5.0.0)
6
6
  aws-sdk-translate
7
7
  nokogiri
data/README.md CHANGED
@@ -42,7 +42,7 @@ To create language versions (You must fill in all parameters): <br />
42
42
  `--aws_public_key` - The public key of your AWS TRANSLATE API <br />
43
43
  `--aws_secret_key` - The secret key of your AWS TRANSLATE API <br />
44
44
 
45
- After the generator has finished its work, you will most likely need to modify the header and footer parshals to fix the links.
45
+ After the generator has finished its work, you will most likely need to modify the header and footer parshals to fix the links. And in current version some media files includes in css may be broken.
46
46
 
47
47
  ## Contributing
48
48
 
@@ -92,7 +92,7 @@ Rails 7.0.4.3
92
92
  `--aws_public_key` - Публичный ключ вашего AWS TRANSLATE API <br />
93
93
  `--aws_secret_key` - Секретный ключ вашего AWS TRANSLATE API <br />
94
94
 
95
- После того как генератор закончит свою работу, вам, скорее всего, придётся доработать паршалы хедеров и футеров для исправления ссылок.
95
+ После того как генератор закончит свою работу, вам, скорее всего, придётся доработать паршалы хедеров и футеров для исправления ссылок. Так же в текущей версии некоторые ссылки на подключение фотографий в css файлах могут не работать.
96
96
 
97
97
  ## Содействие
98
98
 
@@ -17,12 +17,21 @@ module ParserGem
17
17
  class_option :aws_region, type: :string, default: nil # Set aws region for translate API
18
18
  class_option :aws_public_key, type: :string, default: nil # Set aws api public key
19
19
  class_option :aws_secret_key, type: :string, default: nil # Set aws api secret key
20
+ class_option :to_partials_blocks_ids, type: :array, default: [] # Set blocks ids which must be partial
21
+ class_option :media_to_ignore, type: :array, default: [] # Set media filenames which must be ignored
22
+ class_option :media_to_ignore_for_langs, type: :array, default: [] # Set media filenames which must be ignored in lang versions
23
+ class_option :create_meta_og, type: :boolean, default: false # Create meta og: tags from standart meta tags
24
+ class_option :inline_styles_to_files, type: :boolean, default: false
25
+ class_option :inline_scripts_to_files, type: :boolean, default: false
26
+ class_option :meta_tags_names_to_ignore, type: :array, default: []
20
27
 
21
28
  def clone # Main method
22
29
  sitemap = Nokogiri::XML(URI.open("http://#{options[:target_url]}/sitemap.xml")) # Load sitemap.xml
23
30
  controller_name = options[:target_url].split('/').last.delete('.').delete('-') # Set controller name from clear domain name
24
- actions = set_actions(sitemap) # Set actions for controller
31
+ actions = set_actions(sitemap)[0..1] # Set actions for controller
25
32
 
33
+ puts options[:to_partials_blocks_ids].to_s
34
+
26
35
  actions.each do |action| # Create routes
27
36
  if action[:rails_route] == '/'
28
37
  route "root \"#{controller_name}##{action[:action_name]}\""
@@ -51,12 +60,21 @@ module ParserGem
51
60
  client = Aws::Translate::Client.new( region: options[:aws_region],
52
61
  credentials: credentials)
53
62
  end
63
+
64
+ inline_styles = []
65
+ inline_scripts = []
54
66
 
55
67
  actions.each do |action|
56
68
  file_path = File.join('app/views', controller_name, "#{action[:action_name]}.html.erb") # Set view
57
69
  File.open(file_path, 'wb') do |file|
58
70
  page = Nokogiri::HTML(URI.open("#{action[:native_url]}")) # Parse page from url in sitemap
59
71
 
72
+ page.css('meta').each do |meta|
73
+ if options[:meta_tags_names_to_ignore].include?(meta['name'].to_s)
74
+ meta.replace('')
75
+ end
76
+ end
77
+
60
78
  if action[:lang]
61
79
  page.css('title').each do |title|
62
80
  if title.inner_html.length > 1
@@ -75,12 +93,12 @@ module ParserGem
75
93
  media['alt'] = translate(media['alt'], action[:lang], client)
76
94
  end
77
95
  end
96
+ end
78
97
 
79
- page.traverse do |node|
80
- if node.text? && !node.parent.name.in?(%w[script style])
81
- if node.text.length > 1
82
- node.content = translate(node.text, action[:lang], client)
83
- end
98
+ if options[:media_to_ignore_for_langs] && action[:lang]
99
+ page.css('img, video').each do |media|
100
+ if options[:media_to_ignore_for_langs].include?(File.basename(media['src']))
101
+ media.replace('')
84
102
  end
85
103
  end
86
104
  end
@@ -97,6 +115,12 @@ module ParserGem
97
115
  file.puts meta
98
116
  end
99
117
 
118
+ if options[:create_meta_og]
119
+ file.puts "<meta property=\"og:titile\" content=\"#{head.css('title').first.inner_html}\" >"
120
+ file.puts "<meta property=\"og:description\" content=\"#{head.css('meta[name=\"description\"]').first['content']}\" >"
121
+ file.puts "<meta property=\"og:image\" content=\" \" >"
122
+ end
123
+
100
124
  page.css('link[rel="stylesheet"]').each do |link| # Repair css including for Rails
101
125
  css_file_path = download_css(path_to_download(link['href']), controller_name)
102
126
  file.puts "<%= stylesheet_link_tag '#{css_file_path}' %>"
@@ -109,26 +133,94 @@ module ParserGem
109
133
  end
110
134
  end
111
135
 
112
- page.css('style').each do |style_node| # Move all style tags to head
113
- file.puts style_node
136
+ if options[:inline_styles_to_files]
137
+ page.css('style').each do |style_node|
138
+ node_hash = { filename: "inline_style_#{inline_styles.size + 1}.css",
139
+ content: style_node.content }
140
+
141
+ if inline_styles.size > 0
142
+ existing_hash = inline_styles.find { |elem| elem[:content] == node_hash[:content] }
143
+ if existing_hash
144
+ file.puts "<%= stylesheet_link_tag '#{existing_hash[:filename]}' %>"
145
+ else
146
+ inline_styles << node_hash
147
+ file.puts "<%= stylesheet_link_tag '#{node_hash[:filename]}' %>"
148
+ end
149
+ else
150
+ inline_styles << node_hash
151
+ file.puts "<%= stylesheet_link_tag '#{node_hash[:filename]}' %>"
152
+ end
153
+ end
154
+ else
155
+ page.css('style').each do |style_node| # Move all style tags to head
156
+ file.puts style_node
157
+ end
114
158
  end
115
159
 
116
- page.css('script').each do |script_node| # Move all script tags to head
117
- file.puts script_node
160
+ if options[:inline_scripts_to_files]
161
+ page.css('script').each do |script_node|
162
+ if script_node.content && script_node.content.length > 1
163
+ node_hash = { filename: "inline_script_#{inline_scripts.size + 1}.js",
164
+ content: script_node.content }
165
+
166
+ if inline_scripts.size > 0
167
+ existing_hash = inline_scripts.find { |elem| elem[:content] == node_hash[:content] }
168
+ if existing_hash
169
+ file.puts "<%= javascript_tag '#{existing_hash[:filename]}' %>"
170
+ else
171
+ inline_scripts << node_hash
172
+ file.puts "<%= javascript_tag '#{node_hash[:filename]}' %>"
173
+ end
174
+ else
175
+ inline_scripts << node_hash
176
+ file.puts "<%= javascript_tag '#{node_hash[:filename]}' %>"
177
+ end
178
+ end
179
+ end
180
+ else
181
+ page.css('script').each do |script_node| # Move all script tags to head
182
+ file.puts script_node
183
+ end
118
184
  end
119
185
 
120
186
  file.puts "<% end %>"
121
187
 
122
- if !File.exist?("app/views/layouts/_#{action[:lang]}header.html.erb") || !File.exist?("app/views/layouts/_#{action[:lang]}footer.html.erb") # Create footer and header partials
188
+ if !File.exist?("app/views/layouts/_#{action[:lang]}header.html.erb") # Create footer and header partials
123
189
  if options[:header_class_name] # Find and save header to partial
124
190
  File.open(File.join('app/views/layouts/', "_#{action[:lang]}header.html.erb"), 'w') do |file|
125
- file.puts body.css("div[id=\"#{options[:header_class_name]}\"]").to_html
191
+ if action[:lang]
192
+ translated_block = body.css("div[id=\"#{options[:header_class_name]}\"]").first
193
+ translated_block.traverse do |node|
194
+ if node.text? && !node.parent.name.in?(%w[script style])
195
+ if node.text.length > 1
196
+ node.content = translate(node.text, action[:lang], client)
197
+ end
198
+ end
199
+ end
200
+ file.puts translated_block.to_html
201
+ else
202
+ file.puts body.css("div[id=\"#{options[:header_class_name]}\"]").first.to_html
203
+ end
126
204
  end
127
205
  end
206
+ end
128
207
 
208
+ if !File.exist?("app/views/layouts/_#{action[:lang]}footer.html.erb")
129
209
  if options[:footer_class_name] # Find and save footer to partial
130
210
  File.open(File.join('app/views/layouts/', "_#{action[:lang]}footer.html.erb"), 'w') do |file|
131
- file.puts body.css("div[id=\"#{options[:footer_class_name]}\"]").to_html
211
+ if action[:lang]
212
+ translated_block = body.css("div[id=\"#{options[:footer_class_name]}\"]").first
213
+ translated_block.traverse do |node|
214
+ if node.text? && !node.parent.name.in?(%w[script style])
215
+ if node.text.length > 1
216
+ node.content = translate(node.text, action[:lang], client)
217
+ end
218
+ end
219
+ end
220
+ file.puts translated_block.to_html
221
+ else
222
+ file.puts body.css("div[id=\"#{options[:footer_class_name]}\"]").first.to_html
223
+ end
132
224
  end
133
225
  end
134
226
  end
@@ -158,16 +250,63 @@ module ParserGem
158
250
  end
159
251
  end
160
252
 
253
+ options[:to_partials_blocks_ids].each do |block_id|
254
+ body.css("div[id=\"#{block_id}\"]").each do |block|
255
+ if !File.exist?("app/views/layouts/_#{action[:lang]}#{block_id}.html.erb")
256
+ File.open(File.join('app/views/layouts/', "_#{action[:lang]}#{block_id}.html.erb"), 'w') do |file|
257
+ if action[:lang]
258
+ translated_block = body.css("div[id=\"#{block_id}\"]").first
259
+ translated_block.traverse do |node|
260
+ if node.text? && !node.parent.name.in?(%w[script style])
261
+ if node.text.length > 1
262
+ node.content = translate(node.text, action[:lang], client)
263
+ end
264
+ end
265
+ end
266
+ file.puts translated_block.to_html
267
+ else
268
+ file.puts body.css("div[id=\"#{block_id}\"]").first.to_html
269
+ end
270
+ end
271
+ end
272
+ block.replace("<%= render \"layouts/#{action[:lang]}#{block_id}\" %>")
273
+ end
274
+ end
275
+
161
276
  if action[:lang]
162
277
  body.css('a').each do |link| # Repair links for lang versions
163
278
  if link['href']
164
- if !link['href'].include?('http')
165
- if link['href'][0] == '/'
279
+ if !link['href'].include?('http') && !link['href'].include?('mailto:') && !link['href'].include?('tel:')
280
+ if link['href'][0] == '/' && ( !link['href'].include?('.jpg') || !link['href'].include?('.jpeg') || !link['href'].include?('.png') || !link['href'].include?('.webp'))
166
281
  link['href'] = "/#{action[:lang]}#{link['href']}"
167
282
  else
168
283
  link['href'] = "/#{action[:lang]}/#{link['href']}"
169
284
  end
170
285
  end
286
+ if link['title']
287
+ link['title'] = translate(link['title'], action[:lang], client)
288
+ end
289
+ end
290
+ end
291
+ end
292
+
293
+ body.css('a').each do |link|
294
+ if link['href']
295
+ if link['href'][0] != '/'
296
+ link['href'] = "/#{link['href']}"
297
+ end
298
+ link['href'] = link['href'].gsub('/..', '')
299
+ end
300
+ end
301
+
302
+ if action[:lang]
303
+ body.traverse do |node|
304
+ if node.text? && !node.parent.name.in?(%w[script style])
305
+ if node.text.length > 1
306
+ if !node.content.include?('<%= render')
307
+ node.content = translate(node.text, action[:lang], client)
308
+ end
309
+ end
171
310
  end
172
311
  end
173
312
  end
@@ -177,7 +316,27 @@ module ParserGem
177
316
  repair_css(file_path, controller_name) # Repair links in inline css
178
317
  progress.increment
179
318
  end
180
-
319
+
320
+ if options[:inline_styles_to_files]
321
+ inline_styles.each do |style|
322
+ File.open(File.join('app/assets/stylesheets', style[:filename]), 'wb') do |file|
323
+ file.puts style[:content]
324
+ end
325
+ repair_css("app/assets/stylesheets/#{style[:filename]}", controller_name)
326
+ end
327
+ end
328
+
329
+ if options[:inline_scripts_to_files]
330
+ inline_scripts.each do |script|
331
+ File.open(File.join('app/javascript/', script[:filename]), 'wb') do |file|
332
+ file.puts script[:content]
333
+ end
334
+ File.open('app/javascript/application.js', 'a') do |file|
335
+ file.puts("import \"#{script[:filename]}\"") # add downloaded js file to js including file
336
+ end
337
+ end
338
+ end
339
+
181
340
  # editing app/views/layouts/application.html.erb template
182
341
  File.open("app/views/layouts/application.html.erb", 'w') do |file|
183
342
  new_layout = "<!DOCTYPE html><html><head><%= yield :head %></head><body><%= yield %></body></html>"
@@ -190,10 +349,10 @@ module ParserGem
190
349
  private
191
350
 
192
351
  def translate(text_to_translate, target_lang, client)
193
- client.translate_text({ text: "#{text_to_translate}", # required
194
- source_language_code: "#{options[:target_site_language]}", # required
195
- target_language_code: "#{target_lang}", # required
196
- settings: { formality: "FORMAL" } }).translated_text
352
+ client.translate_text({ text: "#{text_to_translate}", # required
353
+ source_language_code: "#{options[:target_site_language]}", # required
354
+ target_language_code: "#{target_lang}", # required
355
+ settings: { formality: "FORMAL" } }).translated_text
197
356
  end
198
357
 
199
358
  def download_css(css_url, controller_name) # Method for save css table to file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Parser
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsergem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Дмитрий Герасименко
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-12 00:00:00.000000000 Z
11
+ date: 2023-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor