opulent 1.1.0 → 1.1.5

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: fb989f9b5f0876f4af2c6951f7ddb013f03f2b83
4
- data.tar.gz: 56271859c6703789c2b56157817fca15044256a9
3
+ metadata.gz: fbda9fcfae11aa802dd630503537fe4ad6e68208
4
+ data.tar.gz: 7d9f569b65d0a043c6fdb831992eb17860cd9d01
5
5
  SHA512:
6
- metadata.gz: bb801aa49723ef35738eff275ca1b96edc3db5c2080292b13c9e0ff96c9600cb5f5ebe9cb0a63ece21824814b2700be25d348565fbcc63f44e728dc855cac59b
7
- data.tar.gz: 82cffc61c9c99d51583b7050ef37bb394358de4ae94927b6b595edea121efb50cb76fbab18e0cbc7430ac423465af137654a59e78db2f86ff6e99d90abd18394
6
+ metadata.gz: 48323906c8fd7463558c599b7bdb2aa3d4799b6544fb51d5ad284903d11869bd00f24b96ade47edaf90a34c13fe0b36dedd8b8a43ca855ee5d65586ba76bd242
7
+ data.tar.gz: 10cee6d1c1153a20d15acbcf591a7404722ee7dde668d40d7d0cb118719b8360f1061bc013411cf345c5fabfafc7fe2131714a39e88a3059959e2a15c3046e8c
@@ -2,6 +2,7 @@ require_relative 'compiler/block.rb'
2
2
  require_relative 'compiler/comment.rb'
3
3
  require_relative 'compiler/control.rb'
4
4
  require_relative 'compiler/define.rb'
5
+ require_relative 'compiler/doctype.rb'
5
6
  require_relative 'compiler/eval.rb'
6
7
  require_relative 'compiler/filter.rb'
7
8
  require_relative 'compiler/node.rb'
@@ -11,9 +11,10 @@ module Opulent
11
11
  def comment(node, indent, context)
12
12
  indentation = " " * indent
13
13
 
14
- value = context.evaluate "\"#{node[@value]}\""
14
+ # Escaping double quotes is required in order to avoid any conflicts with the eval quotes.
15
+ value = indent_lines context.evaluate('"' + node[@value].gsub('"', '\\"') + '"'), " " * indent
15
16
 
16
- comment_tag = "#{indentation}<!-- #{value} -->\n"
17
+ comment_tag = "#{"\n" if node[@options][:newline]}#{indentation}<!-- #{value.strip} -->\n"
17
18
 
18
19
  @node_stack << :comment
19
20
  @code += comment_tag
@@ -23,6 +23,27 @@ module Opulent
23
23
  end
24
24
  end
25
25
 
26
+ # Generate the code for a unless-else control structure
27
+ #
28
+ # @param node [Array] Node code generation data
29
+ # @param indent [Fixnum] Size of the indentation to be added
30
+ # @param context [Context] Processing environment data
31
+ #
32
+ def unless_node(node, indent, context)
33
+ # Check if we have any condition met, or an else branch
34
+ index = node[@value].index do |value|
35
+ value.empty? || !context.evaluate(value)
36
+ end
37
+
38
+ # If we have a branch that meets the condition, generate code for the
39
+ # children related to that specific branch
40
+ if index
41
+ node[@children][index].each do |child|
42
+ root child, indent, context
43
+ end
44
+ end
45
+ end
46
+
26
47
  # Generate the code for a case-when-else control structure
27
48
  #
28
49
  # @param node [Array] Node code generation data
@@ -126,11 +147,12 @@ module Opulent
126
147
 
127
148
  # Create a new context based on the parent context and progressively update
128
149
  # variables in the new context
129
- each_context = Context.new Hash.new, context.binding.clone
150
+ block = context.block.clone if context.block
151
+ each_context = Context.new Hash.new, &block
130
152
  each_context.parent = context
131
153
 
132
154
  # Evaluate the iterable object
133
- enumerable = each_context.evaluate(node[@value][1])
155
+ enumerable = context.evaluate(node[@value][1])
134
156
 
135
157
  # Check if input can be iterated
136
158
  self.error :enumerable, node[@value][1] unless enumerable.respond_to? :each
@@ -71,7 +71,6 @@ module Opulent
71
71
  end
72
72
 
73
73
  # Set variable to determine available blocks
74
- #
75
74
  arguments[:blocks] = Hash[@block_stack[-1].keys.map{|key| [key, true]}]
76
75
 
77
76
  # Create local variables from argument variables
@@ -82,6 +81,7 @@ module Opulent
82
81
  root child, indent, definition_context
83
82
  end
84
83
 
84
+ # Remove last set of blocks from the block stack
85
85
  @block_stack.pop
86
86
  end
87
87
  end
@@ -0,0 +1,42 @@
1
+
2
+ # @Opulent
3
+ module Opulent
4
+ # @Compiler
5
+ class Compiler
6
+ # Generate the code for a while control structure
7
+ #
8
+ # @param node [Array] Node code generation data
9
+ # @param indent [Fixnum] Size of the indentation to be added
10
+ # @param context [Context] Processing environment data
11
+ #
12
+ def doctype_node(node, indent, context)
13
+ indentation = " " * indent
14
+
15
+ value = case node[@value]
16
+ when :"", :"html", :"5"
17
+ "!DOCTYPE html"
18
+ when :"1.1"
19
+ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"'
20
+ when :strict
21
+ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"'
22
+ when :frameset
23
+ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"'
24
+ when :mobile
25
+ '!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"'
26
+ when :basic
27
+ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"'
28
+ when :transitional
29
+ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"'
30
+ when :xml
31
+ '?xml version="1.0" encoding="utf-8" ?'
32
+ when :'xml ISO-8859-1'
33
+ '?xml version="1.0" encoding="iso-8859-1" ?'
34
+ end
35
+
36
+ doctype_tag = "#{indentation}<#{value}>\n"
37
+
38
+ @node_stack << :doctype
39
+ @code += doctype_tag
40
+ end
41
+ end
42
+ end
@@ -210,19 +210,21 @@ module Opulent
210
210
  attribute_value = value.join '_'
211
211
  end
212
212
 
213
- attribute_code += " #{key}"
214
- attribute_code += "=\"#{attribute_value}\"" unless attribute_value.empty?
213
+ unless attribute_value.empty?
214
+ attribute_code += " #{key}"
215
+ attribute_code += "=\"#{attribute_value.strip}\""
216
+ end
215
217
  when Hash
216
218
  value.each do |k,v|
217
219
  if v
218
220
  attribute_code += " #{key}-#{k}"
219
- attribute_code += "=\"#{v.to_s}\"" unless v == true
221
+ attribute_code += "=\"#{v.to_s.strip}\"" unless v == true
220
222
  end
221
223
  end
222
224
  else
223
225
  if value
224
226
  attribute_code += " #{key}"
225
- attribute_code += "=\"#{value.to_s}\"" unless value == true
227
+ attribute_code += "=\"#{value.to_s.strip}\"" unless value == true
226
228
  end
227
229
  end
228
230
 
@@ -20,7 +20,7 @@ module Opulent
20
20
  @binding = if @block
21
21
  @block.binding.clone
22
22
  else
23
- Binding.new
23
+ Binding.new.get
24
24
  end
25
25
 
26
26
  extend_locals locals
@@ -80,7 +80,7 @@ module Opulent
80
80
 
81
81
  # @Binding
82
82
  class Binding
83
- def self.new
83
+ def get
84
84
  return binding
85
85
  end
86
86
  end
@@ -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[:definitions] || {}
20
+ @definitions = {}
21
21
  @overwrite = settings.delete :overwrite
22
22
 
23
23
  Settings.update_settings settings unless settings.empty?
@@ -48,7 +48,7 @@ module Opulent
48
48
  @code = read input
49
49
 
50
50
  # Get the nodes tree
51
- @nodes = Parser.new(@file).parse @code
51
+ @nodes, @definitions = Parser.new(@file, @definitions).parse @code
52
52
 
53
53
  # @TODO
54
54
  # Implement precompiled template handling
@@ -2,6 +2,7 @@ require_relative 'parser/block.rb'
2
2
  require_relative 'parser/comment.rb'
3
3
  require_relative 'parser/control.rb'
4
4
  require_relative 'parser/define.rb'
5
+ require_relative 'parser/doctype.rb'
5
6
  require_relative 'parser/eval.rb'
6
7
  require_relative 'parser/expression.rb'
7
8
  require_relative 'parser/filter.rb'
@@ -21,7 +22,7 @@ module Opulent
21
22
  #
22
23
  # [:node_type, :value, :attributes, :children, :indent]
23
24
  #
24
- def initialize(file)
25
+ def initialize(file, definitions)
25
26
  # Convention accessors
26
27
  @type = 0
27
28
  @value = 1
@@ -34,7 +35,7 @@ module Opulent
34
35
  @dir = File.dirname @file
35
36
 
36
37
  # Initialize definitions for the parser
37
- @definitions = {}
38
+ @definitions = definitions
38
39
  end
39
40
 
40
41
  # Initialize the parsing process by splitting the code into lines and
@@ -57,6 +58,8 @@ module Opulent
57
58
  # Get all nodes starting from the root element and return output
58
59
  # nodes and definitions
59
60
  root @root
61
+
62
+ return @root, @definitions
60
63
  end
61
64
 
62
65
  # Check and accept or reject a given token as long as we have tokens
@@ -17,7 +17,14 @@ module Opulent
17
17
  # create a new comment element. Otherwise, we ignore the current
18
18
  # gathered text and we simply begin the root parsing again
19
19
  if buffer[0] == '!'
20
- parent[@children] << [:comment, buffer[1..-1].strip, {}, nil, indent]
20
+ offset = 1; options = {}
21
+
22
+ # Allow leading comment newline
23
+ if buffer[1] == '^'
24
+ offset = 2; options[:newline] = true
25
+ end
26
+
27
+ parent[@children] << [:comment, buffer[offset..-1].strip, options, nil, indent]
21
28
  end
22
29
 
23
30
  return parent
@@ -0,0 +1,15 @@
1
+ # @Opulent
2
+ module Opulent
3
+ # @Parser
4
+ class Parser
5
+ # Match one line or multiline comments
6
+ #
7
+ def doctype(parent, indent)
8
+ if (accept :doctype)
9
+ buffer = accept(:line_feed)
10
+
11
+ parent[@children] << [:doctype, buffer.strip.to_sym, {}, nil, indent]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -78,8 +78,11 @@ module Opulent
78
78
  error :self_enclosing_children, line
79
79
  end
80
80
 
81
+ # Create a clone of the definition model. Cloning the options is also
82
+ # necessary because it's a shallow copy
81
83
  if @definitions.keys.include? node_name
82
84
  model = @definitions[node_name].clone
85
+ model[@options] = {}.merge model[@options]
83
86
  model[@options][:call] = current_node
84
87
 
85
88
  parent[@children] << model
@@ -37,7 +37,8 @@ module Opulent
37
37
  filter(parent, indent) ||
38
38
  block_yield(parent, indent) ||
39
39
  block(parent, indent) ||
40
- require_file(parent, indent)
40
+ require_file(parent, indent)||
41
+ doctype(parent, indent)
41
42
 
42
43
  # Throw an error if we couldn't find a valid node
43
44
  error :unknown_node_type unless current_node
@@ -1,6 +1,6 @@
1
1
  module Opulent
2
2
  # Opulent Keywords
3
- Keywords = %i(def block yield require if else elsif unless case when each while until)
3
+ Keywords = %i(def block yield require if else elsif unless case when each while until doctype)
4
4
 
5
5
  # @Tokens
6
6
  class Tokens
@@ -29,6 +29,9 @@ module Opulent
29
29
  # Definition
30
30
  def: /\Adef +/,
31
31
 
32
+ # Definition
33
+ doctype: /\Adoctype +/,
34
+
32
35
  # Require file
33
36
  require: /\Arequire +/,
34
37
 
@@ -1,4 +1,4 @@
1
1
  # @Opulent
2
2
  module Opulent
3
- VERSION = "1.1.0"
3
+ VERSION = "1.1.5"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opulent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Grozav
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-12 00:00:00.000000000 Z
11
+ date: 2015-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,6 +143,7 @@ files:
143
143
  - lib/opulent/compiler/comment.rb
144
144
  - lib/opulent/compiler/control.rb
145
145
  - lib/opulent/compiler/define.rb
146
+ - lib/opulent/compiler/doctype.rb
146
147
  - lib/opulent/compiler/eval.rb
147
148
  - lib/opulent/compiler/filter.rb
148
149
  - lib/opulent/compiler/node.rb
@@ -158,6 +159,7 @@ files:
158
159
  - lib/opulent/parser/comment.rb
159
160
  - lib/opulent/parser/control.rb
160
161
  - lib/opulent/parser/define.rb
162
+ - lib/opulent/parser/doctype.rb
161
163
  - lib/opulent/parser/eval.rb
162
164
  - lib/opulent/parser/expression.rb
163
165
  - lib/opulent/parser/filter.rb
@@ -190,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
192
  version: '0'
191
193
  requirements: []
192
194
  rubyforge_project:
193
- rubygems_version: 2.4.5
195
+ rubygems_version: 2.4.6
194
196
  signing_key:
195
197
  specification_version: 4
196
198
  summary: Intelligent Templating Engine for Creative Web Developers.