bade 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f328f0e5c63c627084b539186d88cf638e1f74a04b0176e21b50fa9e44744cd
4
- data.tar.gz: fea38cc7b66f3a5104b3f981d2194d60131cc111240767b67ac5bcd225add4e5
3
+ metadata.gz: f0e26e77d2d1be1c0ef31a906d025a3f13417be44f0f7377e0228a2a38b25cc9
4
+ data.tar.gz: d9a9b30ef8235cef2f15491b739b61afacc8b4b015d22154434fb3debc6bbacb
5
5
  SHA512:
6
- metadata.gz: c79cbb86a9d154c1df45d0aabca8131e62051c2cac257f533e13e1866291fa78d655d4fd6a894d36e042ac4f690f2e74082c3868022c8e4607156be0abdcfab8
7
- data.tar.gz: 4c411d64a549987eba7fc04b72f110025fd5ccccf6b05bcbed79ba8526f177faaf2c3661184f02b9b91f8bf5a1d04044758067050cdc348ddd027e95f6e625dd
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")
@@ -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.5'.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.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Kříž
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-04 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
@@ -72,7 +72,7 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '1.14'
75
- description:
75
+ description:
76
76
  email:
77
77
  - samnung@gmail.com
78
78
  executables: []
@@ -119,7 +119,7 @@ licenses:
119
119
  - MIT
120
120
  metadata:
121
121
  rubygems_mfa_required: 'true'
122
- post_install_message:
122
+ post_install_message:
123
123
  rdoc_options: []
124
124
  require_paths:
125
125
  - lib
@@ -134,8 +134,8 @@ 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.5
138
- signing_key:
137
+ rubygems_version: 3.4.20
138
+ signing_key:
139
139
  specification_version: 4
140
140
  summary: Minimalistic template engine for Ruby.
141
141
  test_files: []