opulent 1.8.1 → 1.8.2

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: eb391e9dc419cbe3860f6439e74f4af66fb0bb2f
4
- data.tar.gz: 7a6314283b3e23907a04b6f3ed36669f26431c05
3
+ metadata.gz: cd8db65730c87a9220e739b5863cc7b1054129f3
4
+ data.tar.gz: 25c0887712f3ac678719b13487368781a2854c6b
5
5
  SHA512:
6
- metadata.gz: ad945c888025afb1ecfff5bc45e71de36bfdf13602386a636f6bde25b4fa6b524954e460f56be94f3d740da986fb86d44b7c229b772b1160b1c213895d351e08
7
- data.tar.gz: b8c67846a876db18702f33b80c9f1832c37d1321571e86d3dcd45fbafce58dfa4fbcc5d0df2320dcebc2cde5e45a8a24a4608248ac38e50d915f8c154663076c
6
+ metadata.gz: 61534b13b508dd60b494d3e5ddf7254f86830a6c2dbd223790e59c1fcdad3a8b9c11778ae18d96cb1f07110d49750287f8a1ea597da67b39f81cdea9f160faed
7
+ data.tar.gz: a37b62028da6f5dfab4064899305b0492271c53eed205773c33b34d7680cee1d3f8d46e54cdedfe2815a03c93761af5e55f20b271a367711a662df43579d1ebb
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ ## v1.8.2 26/07/2016
4
+ - Added pretty print to elements inside definitions
@@ -63,6 +63,9 @@ module Opulent
63
63
 
64
64
  # Set parent node, required for pretty printing
65
65
  @parent_stack = []
66
+
67
+ # Flag to determine whether we're inside a definition
68
+ @in_definition = false
66
69
  end
67
70
 
68
71
  # Compile input nodes, replace them with their definitions and
@@ -64,12 +64,20 @@ module Opulent
64
64
  # @param type [Symbol] Remove only if last buffer part is of this type
65
65
  # @param n [Fixnum] Number of characters to be removed
66
66
  #
67
- def buffer_remove_trailing_whitespace(type = :freeze)
67
+ def buffer_remove_trailing_newline(type = :freeze)
68
68
  if @template[-1][0] == type
69
- n = @template[-1][1][/\s$/]
70
- return if n.nil?
69
+ @template[-1][1].gsub! /\s*\n+\s*$/, ''
70
+ end
71
+ end
71
72
 
72
- @template[-1][1] = @template[-1][1][0..-1 - n.size]
73
+ # Remove last n characters from the most recent template item
74
+ #
75
+ # @param type [Symbol] Remove only if last buffer part is of this type
76
+ # @param n [Fixnum] Number of characters to be removed
77
+ #
78
+ def buffer_remove_trailing_whitespace(type = :freeze)
79
+ if @template[-1][0] == type
80
+ @template[-1][1].rstrip!
73
81
  end
74
82
  end
75
83
 
@@ -9,7 +9,13 @@ module Opulent
9
9
  #
10
10
  def comment(node, indent)
11
11
  buffer_freeze "\n" if node[@options][:newline]
12
- buffer_freeze " " * indent if @settings[:pretty]
12
+ if @settings[:pretty]
13
+ if @in_definition
14
+ buffer "' ' * (indent + #{indent})"
15
+ else
16
+ buffer_freeze " " * indent
17
+ end
18
+ end
13
19
  buffer_freeze '<!-- '
14
20
  buffer_split_by_interpolation node[@value].strip, false
15
21
  buffer_freeze ' -->'
@@ -16,15 +16,18 @@ module Opulent
16
16
  parameters << "#{key} = #{value[@value]}"
17
17
  end
18
18
  parameters << 'attributes = {}'
19
+ parameters << 'indent'
19
20
  parameters << '&block'
20
21
  definition += '(' + parameters.join(', ') + ')'
21
22
 
22
23
  buffer_eval 'instance_eval do'
23
24
  buffer_eval definition
24
25
 
26
+ @in_definition = true
25
27
  node[@children].each do |child|
26
28
  root child, 0
27
29
  end
30
+ @in_definition = false
28
31
 
29
32
  buffer_eval 'end'
30
33
  buffer_eval 'end'
@@ -56,6 +59,11 @@ module Opulent
56
59
  end
57
60
  arguments << '{}'
58
61
 
62
+ if @in_definition
63
+ arguments << "(indent ? indent + #{indent} : #{indent})"
64
+ else
65
+ arguments << indent
66
+ end
59
67
  method_call += '(' + arguments.join(', ') + ')'
60
68
  method_call += ' do' unless node[@children].empty?
61
69
 
@@ -105,7 +113,7 @@ module Opulent
105
113
 
106
114
  arguments << call_attributes
107
115
 
108
- call = "#{key}(#{arguments.join ', '})"
116
+ call = "#{key}(#{arguments.join ', '}, #{indent})"
109
117
  call += ' do' unless node[@children].empty?
110
118
 
111
119
  buffer_eval call
@@ -14,18 +14,30 @@ module Opulent
14
14
  indentation = ' ' * indent
15
15
  inline = Settings::INLINE_NODE.include? node[@value]
16
16
 
17
+ indentate = proc do
18
+ if @in_definition
19
+ buffer "' ' * (indent + #{indent})"
20
+ else
21
+ buffer_freeze indentation
22
+ end
23
+ end
24
+
17
25
  if inline
26
+ inline_last_sibling = @sibling_stack[-1][-1] ? Settings::INLINE_NODE.include?(@sibling_stack[-1][-1][1]) : true
27
+
18
28
  if @sibling_stack[-1][-1] && @sibling_stack[-1][-1][0] == :plain
19
- buffer_remove_trailing_whitespace
20
- elsif @sibling_stack[-1].length == 1
21
- buffer_freeze indentation
29
+ buffer_remove_trailing_newline
30
+ end
31
+
32
+ if @in_definition || @sibling_stack[-1].length == 1 || !inline_last_sibling
33
+ indentate[]
22
34
  end
23
35
  else
24
- buffer_freeze indentation
36
+ indentate[]
25
37
  end
26
38
 
27
39
  @sibling_stack[-1] << [node[@type], node[@value]]
28
- @sibling_stack << [ [node[@type], node[@value]] ]
40
+ @sibling_stack << [[node[@type], node[@value]]]
29
41
  end
30
42
 
31
43
  # Add the tag opening, with leading whitespace to the code buffer
@@ -80,17 +92,18 @@ module Opulent
80
92
  root child, indent + @settings[:indent]
81
93
  end
82
94
 
95
+ inline_last_child = @sibling_stack[-1][-2] &&
96
+ (@sibling_stack[-1][-2][0] == :plain ||
97
+ Settings::INLINE_NODE.include?(@sibling_stack[-1][-2][1]))
98
+
83
99
  # Pretty print
84
100
  if @settings[:pretty]
85
- if node[@children].length > 1 &&
86
- @sibling_stack[-1][-1] &&
87
- (@sibling_stack[-1][-1][0] == :plain ||
88
- Settings::INLINE_NODE.include?(@sibling_stack[-1][-1][1]))
101
+ if node[@children].length > 1 && inline_last_child
89
102
  buffer_freeze "\n"
90
103
  end
91
104
 
92
- if node[@children].size > 0 and !inline
93
- buffer_freeze indentation
105
+ if node[@children].size > 0 && !inline
106
+ indentate[]
94
107
  end
95
108
  end
96
109
 
@@ -100,7 +113,7 @@ module Opulent
100
113
 
101
114
  # Pretty print
102
115
  if @settings[:pretty]
103
- buffer_freeze "\n" unless inline
116
+ buffer_freeze "\n" if !inline || @in_definition
104
117
  end
105
118
  end
106
119
 
@@ -15,7 +15,8 @@ module Opulent
15
15
  if @settings[:pretty]
16
16
  indentation = ' ' * indent
17
17
 
18
- inline = @sibling_stack[-1][-1] && @sibling_stack[-1][-1][0] == :node &&
18
+ inline = @sibling_stack[-1][-1] &&
19
+ @sibling_stack[-1][-1][0] == :node &&
19
20
  Settings::INLINE_NODE.include?(@sibling_stack[-1][-1][1])
20
21
 
21
22
  # Add current node to the siblings stack
@@ -27,6 +28,7 @@ module Opulent
27
28
  if !inline
28
29
  value.gsub!(/^(?!$)/, indentation)
29
30
  else
31
+ buffer_remove_trailing_whitespace
30
32
  value.strip!
31
33
  end
32
34
  else
@@ -50,7 +52,7 @@ module Opulent
50
52
 
51
53
  # Pretty print
52
54
  if @settings[:pretty]
53
- buffer_freeze "\n" unless inline
55
+ buffer_freeze "\n" if !inline or node[@value] == :print
54
56
  end
55
57
  end
56
58
  end
@@ -1,4 +1,4 @@
1
1
  # @Opulent
2
2
  module Opulent
3
- VERSION = '1.8.1'
3
+ VERSION = '1.8.2'
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.8.1
4
+ version: 1.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Grozav
@@ -91,6 +91,7 @@ extra_rdoc_files: []
91
91
  files:
92
92
  - ".gitignore"
93
93
  - ".travis.yml"
94
+ - CHANGELOG.md
94
95
  - CODE_OF_CONDUCT.md
95
96
  - Gemfile
96
97
  - LICENSE.md