bade 0.3.4 → 0.3.6

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: 7a51d23b5cdbf2a6555552edd39164fc3b39b99f381cf454872cce827d5cae77
4
- data.tar.gz: b7a98aaee99316d20f22d45c36430d8ba1c785e9d316615343bbb539e86c8891
3
+ metadata.gz: f0e26e77d2d1be1c0ef31a906d025a3f13417be44f0f7377e0228a2a38b25cc9
4
+ data.tar.gz: d9a9b30ef8235cef2f15491b739b61afacc8b4b015d22154434fb3debc6bbacb
5
5
  SHA512:
6
- metadata.gz: 83a93a132f7b957b6d5cd18b4a83c955fa4cd5c68a68d057fa1fec9643ca1c4b2a965c56f7692a0ee3760fafb4732817697d44ca76d35fefdf421204565fe8c0
7
- data.tar.gz: d59fc4be093b3a2f40f0999e3718f2a0f7bed68956860163cc6920b1cb2afb46459ef155af63fdb8496b9b772055e1d8ffdb956dba173347ab531bb7ca002705
6
+ metadata.gz: 80c968d59631db457a0e2ce7d81fb87da9a2ac66436bb64b4b78a099f3e30ff192fadc2a289b35eec1ea0af254975a936ca39bd1e59becf560a05246713b706e
7
+ data.tar.gz: 72f4888f35b356454dc49653c30a87fd79fb4e25c014414728e4eb3d7ec4f202b885b7c98371df517b841c5538ff2617367a73b698780ef0a762c3b722c25f09
@@ -14,6 +14,8 @@ module Bade
14
14
 
15
15
  DEFAULT_BLOCK_NAME = 'default_block'.freeze
16
16
 
17
+ REQUIRE_RELATIVE_REGEX = /require_relative\s+['"](.+)['"]/
18
+
17
19
  # @param [Document] document
18
20
  #
19
21
  # @return [String]
@@ -28,7 +30,7 @@ module Bade
28
30
  # @return [String] string to parse with Ruby
29
31
  #
30
32
  def generate_lambda_string(document, optimize: false)
31
- @document = document
33
+ @documents = []
32
34
  @buff = []
33
35
  @indent = 0
34
36
  @code_indent = 0
@@ -52,9 +54,6 @@ module Bade
52
54
 
53
55
  buff_code 'end'
54
56
 
55
-
56
- @document = nil
57
-
58
57
  @buff.join("\n")
59
58
  end
60
59
 
@@ -76,12 +75,16 @@ module Bade
76
75
  end
77
76
 
78
77
  def buff_code(text)
78
+ text = _fix_required_relative(text)
79
+
79
80
  @buff << "#{' ' * @code_indent}#{text}"
80
81
  end
81
82
 
82
83
  # @param document [Bade::Document]
83
84
  #
84
85
  def visit_document(document)
86
+ @documents.append(document)
87
+
85
88
  document.sub_documents.each do |sub_document|
86
89
  visit_document(sub_document)
87
90
  end
@@ -97,6 +100,8 @@ module Bade
97
100
  visit_node(new_root)
98
101
 
99
102
  buff_code("# ----- end file #{document.file_path}") unless document.file_path.nil?
103
+
104
+ @documents.pop
100
105
  end
101
106
 
102
107
  # @param current_node [Node]
@@ -162,7 +167,7 @@ module Bade
162
167
  when :newline
163
168
  # no-op
164
169
  when :import
165
- base_path = File.expand_path(current_node.value, File.dirname(@document.file_path))
170
+ base_path = File.expand_path(current_node.value, File.dirname(@documents.last.file_path))
166
171
  load_path = if base_path.end_with?('.rb') && File.exist?(base_path)
167
172
  base_path
168
173
  elsif File.exist?("#{base_path}.rb")
@@ -381,7 +386,7 @@ module Bade
381
386
  when :code
382
387
  value = node.value.strip
383
388
 
384
- %w[end else }].include?(value) || value.match(/^(when|elsif) /)
389
+ %w[end else }].include?(value) || value.match(/^(when|elsif) /) || value.match(/^\./)
385
390
  when :newline
386
391
  true
387
392
  else
@@ -422,6 +427,22 @@ module Bade
422
427
 
423
428
  location(filename: node.filename, lineno: node.lineno, label: label)
424
429
  end
430
+
431
+ # Fix require_relative paths to be relative to the main Bade file (instead of the current file)
432
+ #
433
+ # @param [String] text
434
+ # @return [String]
435
+ #
436
+ def _fix_required_relative(text)
437
+ text.gsub(REQUIRE_RELATIVE_REGEX) do
438
+ relative_path = Regexp.last_match[1]
439
+ abs_path = File.expand_path(relative_path, File.dirname(@documents.last.file_path))
440
+ document_abs_path = Pathname.new(File.expand_path(File.dirname(@documents.first.file_path)))
441
+ new_relative_path = Pathname.new(abs_path).relative_path_from(document_abs_path).to_s
442
+
443
+ "require_relative '#{new_relative_path}'"
444
+ end
445
+ end
425
446
  end
426
447
 
427
448
  # backward compatibility
@@ -94,6 +94,7 @@ module Bade
94
94
  next true if location.nil?
95
95
 
96
96
  path = location.first
97
+ next false if path == false
97
98
  next false if $LOADED_FEATURES.include?(path)
98
99
 
99
100
  next true if constants_location_prefixes.nil?
@@ -99,7 +99,12 @@ module Bade
99
99
  eval(File.read(filename), __get_binding, filename)
100
100
  # rubocop:enable Security/Eval
101
101
  else
102
- Kernel.require_relative(filename)
102
+ context_path = caller_locations(1, 1).first.path
103
+ puts "context_path: #{context_path}"
104
+
105
+ abs_path = File.expand_path(filename, File.dirname(context_path))
106
+
107
+ Kernel.require(abs_path)
103
108
  end
104
109
  end
105
110
 
data/lib/bade/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bade
4
- VERSION = '0.3.4'.freeze
4
+ VERSION = '0.3.6'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Kříž
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-29 00:00:00.000000000 Z
11
+ date: 2023-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: psych
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  - !ruby/object:Gem::Version
135
135
  version: '0'
136
136
  requirements: []
137
- rubygems_version: 3.3.10
137
+ rubygems_version: 3.4.20
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: Minimalistic template engine for Ruby.