opulent 1.0.3 → 1.0.4

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
  SHA1:
3
- metadata.gz: a41bc661663219aac5c2cbdbc6def8eedbe6abeb
4
- data.tar.gz: 68d65112e22329817b60acdf1aa6aad48ad68653
3
+ metadata.gz: e02913ca7a92447940158c0b4ff5dee49a28c32c
4
+ data.tar.gz: 97be181ec7177499387884db7dee3acaa644f7c4
5
5
  SHA512:
6
- metadata.gz: fd1a7efd68cd841e5085ffc2b14f5158f8e8a4a4b0ca07ace88f400857797fc0cd7ba695822b33ad1638c884cf93c8816a70f2dd15021356c88e2d83db2f9446
7
- data.tar.gz: 07249aab1bf67424862905d9f643b4dd1f80a95c4d59423bb61b9c09ee925d87ca91da6f5f54f80cf0edfc7c054254cbc35b1ede2fc888f2c321e003de1546d5
6
+ metadata.gz: c2fea0f9dc244784ccd505f290c53c6c1b22518c280f010be00d589ebcb496422f50f680e4085f708622929bbd0fd297e1612bc5ff6726cce9ba60c12d7bf071
7
+ data.tar.gz: 2112d92298f2752e573d15633594f1425ae9bf718658b1d66759d9602a0e05d37d18a04830c13abf6a491ca348a4d9eff56a95609cac6da474fc65978bedc6af
@@ -5,7 +5,6 @@ require_relative 'compiler/define.rb'
5
5
  require_relative 'compiler/eval.rb'
6
6
  require_relative 'compiler/filter.rb'
7
7
  require_relative 'compiler/node.rb'
8
- require_relative 'compiler/require.rb'
9
8
  require_relative 'compiler/root.rb'
10
9
  require_relative 'compiler/text.rb'
11
10
 
@@ -22,7 +21,7 @@ module Opulent
22
21
  #
23
22
  # @param path [String] Current file path needed for require nodes
24
23
  #
25
- def initialize(settings = {})
24
+ def initialize
26
25
  # Setup convention accessors
27
26
  @type = 0
28
27
  @value = 1
@@ -30,12 +29,6 @@ module Opulent
30
29
  @children = 3
31
30
  @indent = 4
32
31
 
33
- # Set current compiled file
34
- @path = File.dirname settings.delete :path
35
-
36
- # Extract definitions for require directives
37
- @definitions = settings.delete :definitions
38
-
39
32
  # Create the HTML Entities encoder/decoder
40
33
  @entities = HTMLEntities.new
41
34
 
@@ -99,7 +92,7 @@ module Opulent
99
92
  # @param text [String] Input text to be indented
100
93
  # @param indent [String] Indentation string to be appended
101
94
  #
102
- def indent_lines text, indent
95
+ def indent_lines(text, indent)
103
96
  text ||= ""
104
97
  text.lines.inject("") do |result, line|
105
98
  result += indent + line
@@ -130,8 +123,6 @@ module Opulent
130
123
  "The \"#{data[0]}\" filter could not be recognized by Opulent."
131
124
  when :filter_load
132
125
  "The gem required for the \"#{data[0]}\" filter is not installed. You can install it by running:\n\n#{data[1]}"
133
- when :require
134
- "The required file #{data[0]} does not exist or an incorrect path has been specified."
135
126
  end
136
127
 
137
128
  # Reconstruct lines to display where errors occur
@@ -15,10 +15,10 @@ module Opulent
15
15
  # @param locals [Hash] Binding extension
16
16
  # @param bind [Binding] Call environment binding
17
17
  #
18
- def initialize(locals = {}, override = false, &block)
19
- @block = block || Proc.new
18
+ def initialize(locals = {}, &block)
19
+ @block = block
20
20
  @binding = if @block
21
- override ? @block.binding : @block.binding.clone
21
+ @block.binding.clone
22
22
  else
23
23
  Binding.new
24
24
  end
@@ -8,7 +8,7 @@ module Opulent
8
8
 
9
9
  # @Engine
10
10
  class Engine
11
- attr_reader :nodes, :preamble, :buffer
11
+ attr_reader :nodes, :definitions, :parser, :file, :preamble, :buffer
12
12
 
13
13
  # Update render settings
14
14
  #
@@ -17,7 +17,7 @@ module Opulent
17
17
  # @param overwrite [Boolean] Write changes directly to the parent binding
18
18
  #
19
19
  def initialize(settings = {})
20
- @definitions = settings.delete(:definitions) || {}
20
+ @definitions = settings[:definitions] || {}
21
21
  @overwrite = settings.delete :overwrite
22
22
 
23
23
  Settings.update_settings settings unless settings.empty?
@@ -48,17 +48,17 @@ module Opulent
48
48
  @code = read input
49
49
 
50
50
  # Get the nodes tree
51
- @nodes, @definitions = Parser.new(@definitions).parse @code
51
+ @nodes = Parser.new(@file).parse @code
52
52
 
53
53
  # @TODO
54
54
  # Implement precompiled template handling
55
55
  @preamble = @nodes.inspect.inspect
56
56
 
57
57
  # Create a new context based on our rendering environment
58
- @context = Context.new locals, @overwrite, &block
58
+ @context = Context.new locals, &block
59
59
 
60
60
  # Compile our syntax tree using input context
61
- @output = Compiler.new({path: @file, definitions: @definitions}).compile @nodes, @context
61
+ @output = Compiler.new.compile @nodes, @context
62
62
 
63
63
  # puts "Nodes\n---\n"
64
64
  # pp @nodes
@@ -21,7 +21,7 @@ module Opulent
21
21
  #
22
22
  # [:node_type, :value, :attributes, :children, :indent]
23
23
  #
24
- def initialize(definitions = {})
24
+ def initialize(file)
25
25
  # Convention accessors
26
26
  @type = 0
27
27
  @value = 1
@@ -29,8 +29,12 @@ module Opulent
29
29
  @children = 3
30
30
  @indent = 4
31
31
 
32
- # Node definitions encountered up to the current point
33
- @definitions = definitions
32
+ # Set current compiled file
33
+ @file = file
34
+ @dir = File.dirname @file
35
+
36
+ # Initialize definitions for the parser
37
+ @definitions = {}
34
38
  end
35
39
 
36
40
  # Initialize the parsing process by splitting the code into lines and
@@ -52,7 +56,7 @@ module Opulent
52
56
 
53
57
  # Get all nodes starting from the root element and return output
54
58
  # nodes and definitions
55
- [root(@root), @definitions]
59
+ root @root
56
60
  end
57
61
 
58
62
  # Check and accept or reject a given token as long as we have tokens
@@ -137,6 +141,18 @@ module Opulent
137
141
  end
138
142
  end
139
143
 
144
+ # Indent all lines of the input text using give indentation
145
+ #
146
+ # @param text [String] Input text to be indented
147
+ # @param indent [String] Indentation string to be appended
148
+ #
149
+ def indent_lines(text, indent)
150
+ text ||= ""
151
+ text.lines.inject("") do |result, line|
152
+ result += indent + line
153
+ end
154
+ end
155
+
140
156
  # Give an explicit error report where an unexpected sequence of tokens
141
157
  # appears and give indications on how to solve it
142
158
  #
@@ -148,7 +164,7 @@ module Opulent
148
164
  "An unknown node type has been encountered at:\n\n#{Logger.red @line}"
149
165
  when :expected
150
166
  data[0] = "#{Tokens.bracket data[0]}" if [:'(', :'{', :'[', :'<'].include? data[0]
151
- "Expected to find a :#{data[0]} token at: \n\n#{@line[0..@offset-1]}#{Logger.red @line[@offset..-1].rstrip}"
167
+ "Expected to find a :#{data[0]} token on line #{@i+1} of input at: \n\n#{@line[0..@offset-1]}#{Logger.red @line[@offset..-1].rstrip}"
152
168
  when :root
153
169
  "Unknown node type encountered on line #{@i+1} of input at:\n\n" +
154
170
  "#{@line[0..@offset-1]}#{Logger.red @line[@offset..-1].rstrip}"
@@ -186,6 +202,13 @@ module Opulent
186
202
  when :self_enclosing_children
187
203
  "Unexpected child elements found for self enclosing node on line #{data[0]+1} of input at:\n\n" +
188
204
  "#{@code[data[0]]}#{Logger.red @code[data[0] + 1]}"
205
+ when :require
206
+ "The required file #{data[0]} does not exist or an incorrect path has been specified."
207
+ when :require_dir
208
+ "The required file path #{data[0]} is a directory."
209
+ when :require_end
210
+ "Unexpected content found after require on line #{@i+1} of input at:\n\n" +
211
+ "#{@line[0..@offset-1]}#{Logger.red @line[@offset..-1].rstrip}"
189
212
  else
190
213
  "#{@line[0..@offset-1]}#{Logger.red @line[@offset..-1].rstrip}"
191
214
  end
@@ -15,14 +15,40 @@ module Opulent
15
15
  #
16
16
  def require_file(parent, indent)
17
17
  if(match = accept :require)
18
+
18
19
  # Process data
19
- name = accept(:exp_string, :*)
20
+ name = accept :exp_string, :*
21
+
22
+ # Check if there is any string after the require input
23
+ unless (feed = accept(:line_feed) || "").strip.empty?
24
+ undo feed; error :require_end
25
+ end
26
+
27
+ # Get the complete file path based on the current file being compiled
28
+ require_path = File.expand_path name[1..-2], @dir
29
+
30
+ # Throw an error if the file doesn't exist
31
+ error :require, name unless Dir[require_path].any?
32
+
33
+ # Require entire directory tree
34
+ Dir[require_path].each do |file|
35
+ # Skip current file when including from same directory
36
+ next if file == @file
37
+
38
+ # Throw an error if the file doesn't exist
39
+ error :require_dir, file if File.directory? file
40
+
41
+ # Throw an error if the file doesn't exist
42
+ error :require, file unless File.file? file
43
+
44
+ # Indent all lines and prepare them for the parser
45
+ lines = indent_lines File.read(file), " " * indent
20
46
 
21
- # Create node
22
- require_node = [:require, name, {}, [], indent]
47
+ # Indent all the output lines with the current indentation
48
+ @code.insert @i+1, *lines.lines
49
+ end
23
50
 
24
- # Add to parent
25
- parent[@children] << require_node
51
+ return true
26
52
  end
27
53
  end
28
54
  end
@@ -1,4 +1,4 @@
1
1
  # @Opulent
2
2
  module Opulent
3
- VERSION = "1.0.3"
3
+ VERSION = "1.0.4"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opulent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Grozav
@@ -147,7 +147,6 @@ files:
147
147
  - lib/opulent/compiler/eval.rb
148
148
  - lib/opulent/compiler/filter.rb
149
149
  - lib/opulent/compiler/node.rb
150
- - lib/opulent/compiler/require.rb
151
150
  - lib/opulent/compiler/root.rb
152
151
  - lib/opulent/compiler/text.rb
153
152
  - lib/opulent/context.rb
@@ -1,24 +0,0 @@
1
- # @Opulent
2
- module Opulent
3
- # @Compiler
4
- class Compiler
5
- # Compile a new Opulent file using the current page context data
6
- #
7
- # @param node [Array] Node code generation data
8
- # @param indent [Fixnum] Size of the indentation to be added
9
- # @param context [Context] Processing environment data
10
- #
11
- def require_node(node, indent, context)
12
- require_file = File.expand_path context.evaluate(node[@value]), @path
13
- error :require, node[@value] unless File.file? require_file
14
-
15
- data = {
16
- definitions: @definitions,
17
- overwrite: true
18
- }
19
- rendered = Engine.new(data).render_file require_file, &context.block
20
-
21
- @code += indent_lines rendered, " " * indent
22
- end
23
- end
24
- end