opulent 1.5.5 → 1.6.0

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.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CODE_OF_CONDUCT.md +13 -0
  3. data/{LICENSE → LICENSE.md} +0 -0
  4. data/bin/opulent +0 -0
  5. data/lib/opulent.rb +10 -10
  6. data/lib/opulent/compiler.rb +15 -9
  7. data/lib/opulent/compiler/buffer.rb +123 -62
  8. data/lib/opulent/compiler/comment.rb +3 -4
  9. data/lib/opulent/compiler/control.rb +20 -26
  10. data/lib/opulent/compiler/define.rb +88 -36
  11. data/lib/opulent/compiler/doctype.rb +20 -22
  12. data/lib/opulent/compiler/eval.rb +23 -2
  13. data/lib/opulent/compiler/filter.rb +4 -5
  14. data/lib/opulent/compiler/node.rb +18 -12
  15. data/lib/opulent/compiler/root.rb +4 -5
  16. data/lib/opulent/compiler/text.rb +7 -2
  17. data/lib/opulent/compiler/yield.rb +2 -3
  18. data/lib/opulent/engine.rb +21 -20
  19. data/lib/opulent/exec.rb +21 -63
  20. data/lib/opulent/logger.rb +230 -3
  21. data/lib/opulent/parser.rb +14 -76
  22. data/lib/opulent/parser/comment.rb +45 -34
  23. data/lib/opulent/parser/control.rb +132 -111
  24. data/lib/opulent/parser/define.rb +15 -12
  25. data/lib/opulent/parser/doctype.rb +15 -15
  26. data/lib/opulent/parser/eval.rb +16 -6
  27. data/lib/opulent/parser/expression.rb +87 -84
  28. data/lib/opulent/parser/filter.rb +31 -25
  29. data/lib/opulent/parser/include.rb +38 -42
  30. data/lib/opulent/parser/node.rb +136 -118
  31. data/lib/opulent/parser/root.rb +24 -18
  32. data/lib/opulent/parser/text.rb +150 -123
  33. data/lib/opulent/parser/yield.rb +23 -23
  34. data/lib/opulent/settings.rb +70 -51
  35. data/lib/opulent/tokens.rb +17 -15
  36. data/lib/opulent/utils.rb +5 -4
  37. data/lib/opulent/version.rb +1 -1
  38. metadata +4 -43
  39. data/.libold/opulent.rb +0 -96
  40. data/.libold/opulent/context.rb +0 -80
  41. data/.libold/opulent/engine.rb +0 -88
  42. data/.libold/opulent/filter.rb +0 -101
  43. data/.libold/opulent/logger.rb +0 -67
  44. data/.libold/opulent/nodes.rb +0 -13
  45. data/.libold/opulent/nodes/block.rb +0 -29
  46. data/.libold/opulent/nodes/comment.rb +0 -35
  47. data/.libold/opulent/nodes/control.rb +0 -230
  48. data/.libold/opulent/nodes/define.rb +0 -42
  49. data/.libold/opulent/nodes/eval.rb +0 -41
  50. data/.libold/opulent/nodes/expression.rb +0 -28
  51. data/.libold/opulent/nodes/filter.rb +0 -70
  52. data/.libold/opulent/nodes/helper.rb +0 -69
  53. data/.libold/opulent/nodes/node.rb +0 -101
  54. data/.libold/opulent/nodes/root.rb +0 -62
  55. data/.libold/opulent/nodes/text.rb +0 -54
  56. data/.libold/opulent/nodes/theme.rb +0 -36
  57. data/.libold/opulent/parser.rb +0 -252
  58. data/.libold/opulent/parser/block.rb +0 -70
  59. data/.libold/opulent/parser/comment.rb +0 -32
  60. data/.libold/opulent/parser/control.rb +0 -83
  61. data/.libold/opulent/parser/define.rb +0 -39
  62. data/.libold/opulent/parser/eval.rb +0 -33
  63. data/.libold/opulent/parser/expression.rb +0 -350
  64. data/.libold/opulent/parser/filter.rb +0 -41
  65. data/.libold/opulent/parser/node.rb +0 -232
  66. data/.libold/opulent/parser/root.rb +0 -96
  67. data/.libold/opulent/parser/text.rb +0 -114
  68. data/.libold/opulent/parser/theme.rb +0 -36
  69. data/.libold/opulent/preprocessor.rb +0 -102
  70. data/.libold/opulent/runtime.rb +0 -144
  71. data/.libold/opulent/template.rb +0 -43
  72. data/.libold/opulent/tokens.rb +0 -276
  73. data/.libold/opulent/version.rb +0 -5
  74. data/.travis.yml +0 -4
  75. data/benchmark/benchmark.rb +0 -57
  76. data/benchmark/cases/node/node.haml +0 -7
  77. data/benchmark/cases/node/node.op +0 -7
  78. data/benchmark/cases/node/node.slim +0 -7
@@ -1,13 +1,15 @@
1
1
  # @Opulent
2
2
  module Opulent
3
3
  # Opulent KEYWORDS
4
- KEYWORDS = %i(def yield include if else elsif unless case when each while until doctype)
4
+ KEYWORDS = %i(
5
+ def yield include if else elsif unless case when each while until doctype
6
+ )
5
7
 
6
8
  # @Tokens
7
9
  class Tokens
8
10
  # All tokens available within Opulent
9
11
  #
10
- @@tokens = {
12
+ @tokens = {
11
13
  # Indentation
12
14
  indent: /\A\s*/,
13
15
 
@@ -20,12 +22,11 @@ module Opulent
20
22
  shorthand_lookahead: /\A[\.\#\&][a-zA-Z\_\(\"]/,
21
23
 
22
24
  # Leading and trailing whitespace
23
- leading_whitespace: /\A(\<\-)/,
24
- leading_trailing_whitespace: /\A(\>)/,
25
- trailing_whitespace: /\A(\-\>)/,
25
+ leading_whitespace: /\A(\')/,
26
+ trailing_whitespace: /\A(\")/,
26
27
 
27
28
  # Self enclosing node
28
- self_enclosing: /\A\/(.*)/,
29
+ self_enclosing: %r{\A\/(.*)},
29
30
 
30
31
  # Definition
31
32
  def: /\Adef +/,
@@ -86,11 +87,11 @@ module Opulent
86
87
 
87
88
  # Receive matching brackets for allowing multiple bracket types for
88
89
  # element attributes
89
- :brackets => /\A([\(\[\{])/,
90
- :'(' => /\A(\))/,
91
- :'[' => /\A(\])/,
92
- :'{' => /\A(\})/,
93
- :'<' => /\A(\>)/,
90
+ brackets: /\A([\(\[\{])/,
91
+ '(': /\A(\))/,
92
+ '[': /\A(\])/,
93
+ '{': /\A(\})/,
94
+ '<': /\A(\>)/,
94
95
 
95
96
  # Terminators
96
97
  comma: /\A(\s*\,\s*)/,
@@ -106,6 +107,7 @@ module Opulent
106
107
  exp_operation: /\A( *(\+|\-|\*\*|\*|\/|\<\<|\>\>|\.\.|\%|\<\=\>|\<\=|\^|\<|\>\=|\>|\=\~|\!\~|\=\=\=|\=\=|\!|not|\&\&|\&|and|\|\||\||or) *)/,
107
108
  exp_regex: /\A(\/((?:[^\/\\]|\\.)*?)\/)/,
108
109
  exp_string: /\A(("((?:[^"\\]|\\.)*?)")|('(?:[^'\\]|\\.)*?'))/,
110
+ exp_string_match: /\A(("((?:[^"\\]|\\.)*?)")|('(?:[^'\\]|\\.)*?'))\Z/,
109
111
  exp_percent: /\A(\%[wWqQrxsiI]?.)/,
110
112
  exp_double: /\A([0-9]+\.[0-9]+([eE][-+]?[0-9]+)?)/,
111
113
  exp_fixnum: /\A([0-9]+)/,
@@ -115,6 +117,7 @@ module Opulent
115
117
  exp_ternary_else: /\A( *\: *)/,
116
118
 
117
119
  exp_identifier_lookahead: /\A[a-zA-Z\_][a-zA-Z0-9\_]*[\!\?]?/,
120
+ exp_identifier_stripped_lookahead: /\A *[a-zA-Z\_][a-zA-Z0-9\_]*[\!\?]?/,
118
121
 
119
122
  # Hash
120
123
  hash_terminator: /\A(\s*(\,)\s*)/,
@@ -125,8 +128,7 @@ module Opulent
125
128
  whitespace: /\A\s+/,
126
129
 
127
130
  # Evaluation
128
- eval: /\A\-(.*)/,
129
- eval_multiline: /\A\+(.*)/,
131
+ eval: /\A\-/,
130
132
 
131
133
  # Whitespace
132
134
  newline: /\A(\n+)/,
@@ -154,7 +156,7 @@ module Opulent
154
156
  # @param name [Symbol] Token requested by the parser accept method
155
157
  #
156
158
  def self.[](name)
157
- @@tokens[name]
159
+ @tokens[name]
158
160
  end
159
161
 
160
162
  # Set a new token at runtime
@@ -163,7 +165,7 @@ module Opulent
163
165
  # @param token [Token] Token data to be set
164
166
  #
165
167
  def self.[]=(name, token)
166
- @@tokens[name] = token
168
+ @tokens[name] = token
167
169
  end
168
170
  end
169
171
  end
@@ -1,9 +1,10 @@
1
1
  # @Opulent
2
2
  module Opulent
3
+ # @Utils
3
4
  module Utils
4
5
  # Used by escape_html
5
6
  #
6
- EscapeHTML = {
7
+ ESCAPE_HTML = {
7
8
  '&' => '&amp;',
8
9
  '"' => '&quot;',
9
10
  '\'' => '&#39;',
@@ -12,10 +13,10 @@ module Opulent
12
13
  }.freeze
13
14
 
14
15
  # Pattern matching for html escape characters
15
- EscapeHTMLPattern = Regexp.union(*EscapeHTML.keys)
16
+ ESCAPE_HTML_PATTERN = Regexp.union(*ESCAPE_HTML.keys)
16
17
 
17
18
  # Ruby interpolation pattern
18
- InterpolationPattern = /\#\{([^}]+)\}/
19
+ INTERPOLATION_PATTERN = /(\#\{[^}]+\})/
19
20
 
20
21
  # @Utils
21
22
  class << self
@@ -33,7 +34,7 @@ module Opulent
33
34
  # @param html [String] The string to escape
34
35
  # @return [String] The escaped string
35
36
  def escape(html)
36
- html.to_s.chomp.gsub EscapeHTMLPattern, EscapeHTML
37
+ html.to_s.chomp.gsub ESCAPE_HTML_PATTERN, ESCAPE_HTML
37
38
  end
38
39
  end
39
40
  end
@@ -1,4 +1,4 @@
1
1
  # @Opulent
2
2
  module Opulent
3
- VERSION = '1.5.5'
3
+ VERSION = '1.6.0'
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.5.5
4
+ version: 1.6.0
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-10-27 00:00:00.000000000 Z
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,50 +90,11 @@ extensions: []
90
90
  extra_rdoc_files: []
91
91
  files:
92
92
  - ".gitignore"
93
- - ".libold/opulent.rb"
94
- - ".libold/opulent/context.rb"
95
- - ".libold/opulent/engine.rb"
96
- - ".libold/opulent/filter.rb"
97
- - ".libold/opulent/logger.rb"
98
- - ".libold/opulent/nodes.rb"
99
- - ".libold/opulent/nodes/block.rb"
100
- - ".libold/opulent/nodes/comment.rb"
101
- - ".libold/opulent/nodes/control.rb"
102
- - ".libold/opulent/nodes/define.rb"
103
- - ".libold/opulent/nodes/eval.rb"
104
- - ".libold/opulent/nodes/expression.rb"
105
- - ".libold/opulent/nodes/filter.rb"
106
- - ".libold/opulent/nodes/helper.rb"
107
- - ".libold/opulent/nodes/node.rb"
108
- - ".libold/opulent/nodes/root.rb"
109
- - ".libold/opulent/nodes/text.rb"
110
- - ".libold/opulent/nodes/theme.rb"
111
- - ".libold/opulent/parser.rb"
112
- - ".libold/opulent/parser/block.rb"
113
- - ".libold/opulent/parser/comment.rb"
114
- - ".libold/opulent/parser/control.rb"
115
- - ".libold/opulent/parser/define.rb"
116
- - ".libold/opulent/parser/eval.rb"
117
- - ".libold/opulent/parser/expression.rb"
118
- - ".libold/opulent/parser/filter.rb"
119
- - ".libold/opulent/parser/node.rb"
120
- - ".libold/opulent/parser/root.rb"
121
- - ".libold/opulent/parser/text.rb"
122
- - ".libold/opulent/parser/theme.rb"
123
- - ".libold/opulent/preprocessor.rb"
124
- - ".libold/opulent/runtime.rb"
125
- - ".libold/opulent/template.rb"
126
- - ".libold/opulent/tokens.rb"
127
- - ".libold/opulent/version.rb"
128
- - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
129
94
  - Gemfile
130
- - LICENSE
95
+ - LICENSE.md
131
96
  - README.md
132
97
  - Rakefile
133
- - benchmark/benchmark.rb
134
- - benchmark/cases/node/node.haml
135
- - benchmark/cases/node/node.op
136
- - benchmark/cases/node/node.slim
137
98
  - bin/opulent
138
99
  - docs/attributes.md
139
100
  - docs/comments.md
@@ -1,96 +0,0 @@
1
- require 'htmlentities'
2
- require 'tilt'
3
- require_relative 'opulent/engine'
4
- require_relative 'opulent/logger'
5
- require_relative 'opulent/preprocessor'
6
- require_relative 'opulent/tokens'
7
- require_relative 'opulent/nodes'
8
- require_relative 'opulent/parser'
9
- require_relative 'opulent/context'
10
- require_relative 'opulent/runtime'
11
- require_relative 'opulent/template'
12
- require_relative 'opulent/filter'
13
- require 'pp'
14
-
15
- # @Opulent
16
- module Opulent
17
- # Wrapper method for creating a new Opulent instance
18
- #
19
- def Opulent.new
20
- return Opulent.new
21
- end
22
-
23
- # @Opulent
24
- class Opulent
25
- # Analyze the input code and check for matching tokens. In case no match was
26
- # found, throw an exception. In special cases, modify the token hash.
27
- #
28
- # @param file [String] The file that needs to be analyzed
29
- # @param locals [Hash] Render call local variables
30
- # @param block [Proc] Processing environment data
31
- #
32
- def render_file(file, locals = {}, &block)
33
- # Temporarily set file render mode for using it in the Preprocessor
34
- @mode = :file
35
-
36
- # Render the file
37
- render file, locals, &block
38
- end
39
-
40
- # Analyze the input code and check for matching tokens. In case no match was
41
- # found, throw an exception. In special cases, modify the token hash.
42
- #
43
- # @param file [String] The file that needs to be analyzed
44
- # @param locals [Hash] Render call local variables
45
- # @param block [Proc] Processing environment data
46
- #
47
- def render(code, locals = {}, &block)
48
- # Get the code from the input file
49
- @code = PreProcessor.process code, @mode, &block
50
-
51
- # Reset rendering mode to code. The mode variable is used to specify
52
- # whether we're using render file or render code. When using render code,
53
- # the preprocessor can be used only when a block is also passed
54
- @mode = :code
55
-
56
- # Instantiate required language components
57
- @syntax = Parser.parse @code
58
-
59
- # Create a new context based on our rendering environment
60
- #bind = block.binding if block
61
- #@context = Context.new locals, bind
62
-
63
- # Instantiate required language components
64
- #@model = Runtime.remodel @syntax, @context
65
-
66
- #puts "\n\nModel\n---"
67
- #Logger.pretty_print @model
68
- end
69
-
70
- # Update the engine options with the required option changes
71
- #
72
- def update_options(opts)
73
- Engine.update_options(opts)
74
- end
75
-
76
- private
77
-
78
- # Give an explicit error report where an unexpected sequence of tokens
79
- # appears and give indications on how to solve it
80
- #
81
- # @param context [Symbol] Context name in which the error happens
82
- # @param data [Array] Additional error information
83
- #
84
- def error(context, *data)
85
- message = case context
86
- when :options_key
87
- "The input \"#{data[0]}\" is not a valid option name."
88
- end
89
-
90
- # Reconstruct lines to display where errors occur
91
- fail "\n\nOpulent " + Logger.red("[Engine Error]") + "\n---\n" +
92
- "An error has been encountered when updating the engine options.\n" +
93
- "#{message}"
94
- end
95
- end
96
- end
@@ -1,80 +0,0 @@
1
- # @Opulent
2
- module Opulent
3
- # @Context
4
- #
5
- # The context class is used to differentiate local, instance and class variables
6
- # and to define the current working environment. Each class, method and instance
7
- # has its own context
8
- #
9
- class Context
10
- attr_accessor :locals, :binding, :parent, :name
11
-
12
- # Create a context from the environment binding, extended with the locals
13
- # given as arguments
14
- #
15
- # @param locals [Hash] Binding extension
16
- # @param bind [Binding] Call environment binding
17
- #
18
- def initialize(locals = {}, bind = nil)
19
- @binding = if bind
20
- bind.clone
21
- else
22
- Binding.new
23
- end
24
-
25
- extend_locals locals
26
- end
27
-
28
- # Evaluate ruby code in current context
29
- #
30
- # @param code [String] Code to be evaluated
31
- #
32
- def evaluate(code)
33
- begin
34
- eval code, @binding
35
- rescue NameError => variable
36
- Runtime.error :binding, variable, code
37
- end
38
- end
39
-
40
- # Extend the call context with a Hash, String or other Object
41
- #
42
- # @param context [Object] Extension object
43
- #
44
- def extend_locals(locals)
45
- # Create new local variables from the input hash
46
- locals.each do |key, value|
47
- begin
48
- @binding.local_variable_set(key.to_sym, value)
49
- rescue NameError => variable
50
- Runtime.error :variable_name, variable, key
51
- end
52
- end
53
- end
54
-
55
- # Extend instance, class and global variables for use in definitions
56
- #
57
- # @param bind [Binding] Binding to extend current context binding
58
- #
59
- def extend_nonlocals(bind)
60
- bind.eval('instance_variables').each do |var|
61
- @binding.eval('self').instance_variable_set var, bind.eval(var.to_s)
62
- end
63
-
64
- bind.eval('self.class.class_variables').each do |var|
65
- @binding.eval('self').class_variable_set var, bind.eval(var.to_s)
66
- end
67
-
68
- bind.eval('self.class.constants').each do |var|
69
- @binding.eval('self').const_set var, bind.eval(var.to_s)
70
- end
71
- end
72
- end
73
-
74
- # @Binding
75
- class Binding
76
- def self.new
77
- binding
78
- end
79
- end
80
- end
@@ -1,88 +0,0 @@
1
- # @SugarCube
2
- module Opulent
3
- # @Engine
4
- module Engine
5
- # Default (root) theme name which can be accesed using the root definitions
6
- DEFAULT_THEME = :default
7
-
8
- # Default yield target which is used for child block replacements
9
- DEFAULT_YIELD = :children
10
-
11
- # List of self enclosing node elements
12
- SELF_ENCLOSING = %w(img link input meta br hr area base col command embed keygen param source track wbr)
13
-
14
- # @Singleton
15
- class << self
16
- attr_accessor :filters, :options
17
-
18
- # Opulent runtime options
19
- @@defaults = {
20
- pretty: true,
21
- indent: 2,
22
- dependency_manager: true,
23
- shorthand: {
24
- class: /\./,
25
- id: /\#/,
26
- name: /\&/
27
- },
28
- each: {
29
- :default_key => :key,
30
- :default_value => :value
31
- }
32
- }
33
-
34
- # Set defaults as initial options
35
- @@options = @@defaults.clone
36
-
37
- # Get an option at runtime
38
- #
39
- # @param name [Symbol] Identifier for the option
40
- #
41
- def [](name)
42
- @@options[name]
43
- end
44
-
45
- # Set a new option at runtime
46
- #
47
- # @param name [Symbol] Identifier for the option
48
- # @param value Option value to be set
49
- #
50
- def []=(name, value)
51
- @@options[name] = value
52
- end
53
-
54
- # Add a new Opulent filter to the filters knowledgebase
55
- #
56
- # @param class [Class] Class to be used for filter instance
57
- # @param name [Symbol] Identifier in the filters hash
58
- # @param options [Hash] Filter engine instance options
59
- #
60
- def register(klass, name, options)
61
- @filters ||= {}
62
- @filters[name] = klass.new name, options
63
- end
64
-
65
- # Check if the chosen filter name is registed within our knowledgebase
66
- #
67
- def filter?(name)
68
- @filters.has_key? name
69
- end
70
-
71
- # Update the engine options with the required option changes
72
- #
73
- # @param opts [Hash] Option extension
74
- #
75
- def update_options(opts)
76
- @@options = @@defaults.clone
77
-
78
- opts.each do |key, value|
79
- if @@options[key]
80
- @@options[key] = value
81
- else
82
- error :options_key, key
83
- end
84
- end
85
- end
86
- end
87
- end
88
- end