opulent 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/benchmark/benchmark.rb +18 -7
  3. data/benchmark/cases/node/node.haml +6 -16
  4. data/benchmark/cases/node/node.op +6 -13
  5. data/benchmark/cases/node/node.slim +6 -18
  6. data/docs/comments.md +5 -0
  7. data/docs/filters.md +31 -0
  8. data/docs/includes.md +0 -0
  9. data/docs/nodes.md +88 -0
  10. data/docs/reference.md +7 -6
  11. data/lib/opulent.rb +2 -1
  12. data/lib/opulent/compiler.rb +19 -18
  13. data/lib/opulent/compiler/buffer.rb +260 -0
  14. data/lib/opulent/compiler/comment.rb +4 -9
  15. data/lib/opulent/compiler/control.rb +65 -67
  16. data/lib/opulent/compiler/define.rb +91 -63
  17. data/lib/opulent/compiler/doctype.rb +1 -5
  18. data/lib/opulent/compiler/eval.rb +1 -1
  19. data/lib/opulent/compiler/node.rb +10 -194
  20. data/lib/opulent/compiler/root.rb +1 -1
  21. data/lib/opulent/compiler/text.rb +3 -40
  22. data/lib/opulent/compiler/yield.rb +15 -0
  23. data/lib/opulent/context.rb +2 -2
  24. data/lib/opulent/engine.rb +56 -57
  25. data/lib/opulent/parser.rb +10 -10
  26. data/lib/opulent/parser/control.rb +2 -3
  27. data/lib/opulent/parser/expression.rb +1 -1
  28. data/lib/opulent/parser/{require.rb → include.rb} +17 -15
  29. data/lib/opulent/parser/node.rb +22 -17
  30. data/lib/opulent/parser/root.rb +3 -4
  31. data/lib/opulent/parser/text.rb +3 -7
  32. data/lib/opulent/parser/yield.rb +23 -0
  33. data/lib/opulent/settings.rb +5 -4
  34. data/lib/opulent/template.rb +12 -30
  35. data/lib/opulent/tokens.rb +5 -9
  36. data/lib/opulent/utils.rb +41 -0
  37. data/lib/opulent/version.rb +1 -1
  38. metadata +9 -5
  39. data/lib/opulent/compiler/block.rb +0 -31
  40. data/lib/opulent/parser/block.rb +0 -56
@@ -28,7 +28,7 @@ module Opulent
28
28
  value = value[1..-1] if value[0] == '\\'
29
29
 
30
30
  # Create the text node using input data
31
- text_node = [:plain, type, {value: value.strip, escaped: escaped, evaluate: false}, nil, indent]
31
+ text_node = [:plain, type, {value: value.strip, escaped: escaped}, nil, indent]
32
32
 
33
33
  # If we have a multiline node, get all the text which has higher
34
34
  # indentation than our indentation node.
@@ -42,10 +42,6 @@ module Opulent
42
42
  return nil
43
43
  end
44
44
 
45
- if text_node[@options][:value] =~ Settings::InterpolationCheck
46
- text_node[@options][:evaluate] = true
47
- end
48
-
49
45
  # Increase indentation if this is an inline text node
50
46
  text_node[@indent] += Settings[:indent] unless multiline_or_print
51
47
 
@@ -58,7 +54,7 @@ module Opulent
58
54
  def html_text(parent, indent)
59
55
  if (text_feed = accept :html_text)
60
56
  text_node = [:plain, :text, {value: text_feed.strip, escaped: false}, nil, indent]
61
-
57
+
62
58
  parent[@children] << text_node
63
59
  end
64
60
  end
@@ -107,7 +103,7 @@ module Opulent
107
103
  # Add next line feed, prepend the indent and append the newline
108
104
  buffer += " " * next_line_indent if next_line_indent > 0
109
105
  buffer += accept_stripped(:line_feed) || ""
110
- buffer += accept_stripped(:newline) || ""
106
+ buffer += accept(:newline) || ""
111
107
 
112
108
  # Get blank lines until we match something
113
109
  blank_lines[]
@@ -0,0 +1,23 @@
1
+ # @Opulent
2
+ module Opulent
3
+ # @Parser
4
+ class Parser
5
+ # Match a yield with a explicit or implicit target
6
+ #
7
+ # yield target
8
+ #
9
+ # @param parent [Node] Parent node to which we append the definition
10
+ #
11
+ def block_yield(parent, indent)
12
+ if accept :yield
13
+ # Consume the newline from the end of the element
14
+ error :yield unless accept(:line_feed).strip.empty?
15
+
16
+ # Create a new node
17
+ yield_node = [:yield, nil, {}, [], indent]
18
+
19
+ parent[@children] << yield_node
20
+ end
21
+ end
22
+ end
23
+ end
@@ -2,11 +2,11 @@
2
2
  module Opulent
3
3
  # @Settings
4
4
  module Settings
5
- # Default Opulent allowed file extensions
6
- Extensions = %w(.op .opl .opulent)
5
+ # Set buffer variable name
6
+ Buffer = :@_opulent_buffer
7
7
 
8
- # Default yield target which is used for child block replacements
9
- DefaultYield = :children
8
+ # Default Opulent file extension
9
+ FileExtension = '.op'
10
10
 
11
11
  # Default yield target which is used for child block replacements
12
12
  DefaultEachKey = :key
@@ -45,6 +45,7 @@ module Opulent
45
45
  #dependency_manager: true, # Soon to be implemented
46
46
  indent: 2,
47
47
  layouts: false,
48
+ pretty: false, # Under work
48
49
  default_layout: :'views/layouts/application'
49
50
  }
50
51
 
@@ -2,6 +2,10 @@
2
2
  module Opulent
3
3
  # @OpulentTemplate
4
4
  class OpulentTemplate < ::Tilt::Template
5
+ # Allow accessing engine definitions
6
+ attr_reader :def
7
+
8
+ # Set default mime type
5
9
  self.default_mime_type = 'text/html'
6
10
 
7
11
  # Do whatever preparation is necessary to setup the underlying template
@@ -11,17 +15,11 @@ module Opulent
11
15
  # Subclasses must provide an implementation of this method.
12
16
  #
13
17
  def prepare
14
- # Set the file which is being evaluated
15
- @options[:file] = eval_file
16
-
17
- # Enable caching for the current rendered file
18
- @options[:cache] = true
19
-
20
- # Enable layouts so that we can render with symbols
21
- #@options[:layouts] = true
22
-
23
18
  # Set up the rendering engine
24
- @engine = ::Opulent.new @options
19
+ @engine = ::Opulent.new eval_file.to_sym, @options
20
+
21
+ # Set reusable template definitions
22
+ @def = @engine.def
25
23
  end
26
24
 
27
25
  # Execute the compiled template and return the result string. Template
@@ -32,17 +30,8 @@ module Opulent
32
30
  # override render() may not support all features.
33
31
  #
34
32
  def evaluate(scope, locals, &block)
35
- # if @engine.respond_to?(:precompiled_method_return_value, true)
36
- # super
37
- # else
38
- # @engine.render(data, locals, &block)
39
- # end
40
- if @engine.preamble
41
- super
42
- else
43
- locals[:scope] = scope
44
- @engine.render(data, locals, &block)
45
- end
33
+ raise ArgumentError, 'Invalid scope: must not be frozen.' if scope.frozen?
34
+ super
46
35
  end
47
36
 
48
37
  # A string containing the (Ruby) source code for the template. The
@@ -52,15 +41,8 @@ module Opulent
52
41
  # support, custom scopes, proper encoding, and support for template
53
42
  # compilation.
54
43
  #
55
- def precompiled_template(locals)
56
- # This here should be evaluated in order to return the precompiled code
57
- # as text to the user.
58
- # For example:
59
- # _buff = [] # This should be in preamble
60
- # _buff << "<html>",
61
- # _buff << compile('a * b')
62
- # _buff << "</html>"
63
- @engine.preamble
44
+ def precompiled_template(locals = {})
45
+ @engine.template
64
46
  end
65
47
  end
66
48
 
@@ -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 doctype)
3
+ Keywords = %i(def yield include if else elsif unless case when each while until doctype)
4
4
 
5
5
  # @Tokens
6
6
  class Tokens
@@ -30,10 +30,10 @@ module Opulent
30
30
  def: /\Adef +/,
31
31
 
32
32
  # Definition
33
- doctype: /\Adoctype +/,
33
+ doctype: /\Adoctype */,
34
34
 
35
- # Require file
36
- require: /\Arequire +/,
35
+ # Include file
36
+ include: /\Ainclude +/,
37
37
 
38
38
  # Node Attributes
39
39
  attributes_bracket: /\A\(\[\{/,
@@ -69,14 +69,10 @@ module Opulent
69
69
 
70
70
  # Yield
71
71
  yield: /\A(yield)/,
72
- yield_identifier: /\A[a-zA-Z]([\_]?[a-zA-Z0-9]+)*/,
73
-
74
- # Yield
75
- block: /\A(block)/,
76
72
 
77
73
  # Conditional Structures
78
74
  control: /\A(if|elsif|else|unless|case|when|each|while|until)/,
79
- each_pattern: /\A(\w+( *, *\w+)?)? +in +.+/,
75
+ each_pattern: /\A(.+) +do *\|(.+)\|/,
80
76
 
81
77
  # Text
82
78
  text: /\A\|/,
@@ -0,0 +1,41 @@
1
+ # @Opulent
2
+ module Opulent
3
+ module Utils
4
+ # Used by escape_html
5
+ #
6
+ EscapeHTML = {
7
+ '&' => '&amp;',
8
+ '"' => '&quot;',
9
+ '\'' => '&#39;',
10
+ '<' => '&lt;',
11
+ '>' => '&gt;'
12
+ }.freeze
13
+
14
+ # Pattern matching for html escape characters
15
+ EscapeHTMLPattern = Regexp.union(*EscapeHTML.keys)
16
+
17
+ # Ruby interpolation pattern
18
+ InterpolationPattern = /\#\{([^}]+)\}/
19
+
20
+ # @Utils
21
+ class << self
22
+ if defined?(EscapeUtils)
23
+ # Returns an escaped copy of `html`.
24
+ #
25
+ # @param html [String] The string to escape
26
+ # @return [String] The escaped string
27
+ def escape(html)
28
+ EscapeUtils.escape_html html.to_s, false
29
+ end
30
+ else
31
+ # Returns an escaped copy of `html`.
32
+ #
33
+ # @param html [String] The string to escape
34
+ # @return [String] The escaped string
35
+ def escape(html)
36
+ html.to_s.gsub EscapeHTMLPattern, EscapeHTML
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,4 +1,4 @@
1
1
  # @Opulent
2
2
  module Opulent
3
- VERSION = "1.4.0"
3
+ VERSION = "1.4.1"
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.4.0
4
+ version: 1.4.1
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-09-02 00:00:00.000000000 Z
11
+ date: 2015-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -140,12 +140,14 @@ files:
140
140
  - docs/control-structures.md
141
141
  - docs/doctype.md
142
142
  - docs/expressions.md
143
+ - docs/filters.md
144
+ - docs/includes.md
143
145
  - docs/nodes.md
144
146
  - docs/reference.md
145
147
  - docs/usage.md
146
148
  - lib/opulent.rb
147
149
  - lib/opulent/compiler.rb
148
- - lib/opulent/compiler/block.rb
150
+ - lib/opulent/compiler/buffer.rb
149
151
  - lib/opulent/compiler/comment.rb
150
152
  - lib/opulent/compiler/control.rb
151
153
  - lib/opulent/compiler/define.rb
@@ -155,13 +157,13 @@ files:
155
157
  - lib/opulent/compiler/node.rb
156
158
  - lib/opulent/compiler/root.rb
157
159
  - lib/opulent/compiler/text.rb
160
+ - lib/opulent/compiler/yield.rb
158
161
  - lib/opulent/context.rb
159
162
  - lib/opulent/engine.rb
160
163
  - lib/opulent/exec.rb
161
164
  - lib/opulent/filters.rb
162
165
  - lib/opulent/logger.rb
163
166
  - lib/opulent/parser.rb
164
- - lib/opulent/parser/block.rb
165
167
  - lib/opulent/parser/comment.rb
166
168
  - lib/opulent/parser/control.rb
167
169
  - lib/opulent/parser/define.rb
@@ -169,13 +171,15 @@ files:
169
171
  - lib/opulent/parser/eval.rb
170
172
  - lib/opulent/parser/expression.rb
171
173
  - lib/opulent/parser/filter.rb
174
+ - lib/opulent/parser/include.rb
172
175
  - lib/opulent/parser/node.rb
173
- - lib/opulent/parser/require.rb
174
176
  - lib/opulent/parser/root.rb
175
177
  - lib/opulent/parser/text.rb
178
+ - lib/opulent/parser/yield.rb
176
179
  - lib/opulent/settings.rb
177
180
  - lib/opulent/template.rb
178
181
  - lib/opulent/tokens.rb
182
+ - lib/opulent/utils.rb
179
183
  - lib/opulent/version.rb
180
184
  - opulent.gemspec
181
185
  homepage: http://opulent.io
@@ -1,31 +0,0 @@
1
- # @Opulent
2
- module Opulent
3
- # @Compiler
4
- class Compiler
5
- # Generate the code for a while control structure
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 yield_node(node, indent, context)
12
- if @block_stack[-1].has_key? node[@value]
13
- @block_stack[-1][node[@value]].each do |child|
14
- root child, indent, context.parent
15
- end
16
- end
17
- end
18
-
19
- # Generate the code for a while control structure
20
- #
21
- # @param node [Array] Node code generation data
22
- # @param indent [Fixnum] Size of the indentation to be added
23
- # @param context [Context] Processing environment data
24
- #
25
- def block_node(node, indent, context)
26
- node[@children].each do |child|
27
- root child, indent, context
28
- end
29
- end
30
- end
31
- end
@@ -1,56 +0,0 @@
1
- # @Opulent
2
- module Opulent
3
- # @Parser
4
- class Parser
5
- # Match a yield with a explicit or implicit target
6
- #
7
- # yield target
8
- #
9
- # @param parent [Node] Parent node to which we append the definition
10
- #
11
- def block_yield(parent, indent)
12
- if accept :yield
13
- # Get definition name
14
- if(yield_name = accept_stripped(:yield_identifier))
15
- yield_name = yield_name.strip.to_sym
16
- else
17
- yield_name = Settings::DefaultYield
18
- end
19
-
20
- # Consume the newline from the end of the element
21
- error :yield unless accept(:line_feed).strip.empty?
22
-
23
- # Create a new node
24
- yield_node = [:yield, yield_name, {}, [], indent]
25
-
26
- parent[@children] << yield_node
27
- end
28
- end
29
-
30
- # Match a block with a explicit target
31
- #
32
- # block target
33
- #
34
- # @param parent [Node] Parent node to which we append the definition
35
- #
36
- def block(parent, indent)
37
- if accept :block
38
- # Get definition name
39
- if(block_name = accept_stripped(:yield_identifier))
40
- block_name = block_name.strip.to_sym
41
- else
42
- block_name = Settings::DefaultYield
43
- end
44
-
45
- # Consume the newline from the end of the element
46
- error :block unless accept(:line_feed).strip.empty?
47
-
48
- # Create a new node
49
- block_node = [:block, block_name, {}, [], indent]
50
- root block_node, indent
51
-
52
- parent[@children] << block_node
53
- end
54
- end
55
- end
56
- end