non-haml 1.0.1 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,11 +4,27 @@
4
4
 
5
5
  # Example usage:
6
6
  # require 'non-haml'
7
- # NonHaml.generate 'output/source.c', 'source.c', binding
7
+ # NonHaml.generate source, local_vars|context
8
+ # NonHaml.generate_file 'output/source.c', 'source.c', local_vars|context
8
9
 
9
10
  module NonHaml
10
11
  class << self
12
+ def generate out_name, in_name, context_or_vars={}, base_dir='./', verbose=false
13
+ NonHamlParser.new.generate out_name, in_name, context_or_vars, base_dir, verbose
14
+ end
15
+ end
16
+
17
+ class IndentError < StandardError
18
+ end
19
+
20
+ class ParseError < StandardError
21
+ end
22
+
23
+ class NonHamlParser
11
24
  attr_accessor :last_ok_line, :out, :base_dir
25
+ def initialize
26
+ self.out = ""
27
+ end
12
28
 
13
29
  def concat spaces=nil, text=nil
14
30
  if spaces.nil? and text.nil?
@@ -20,10 +36,6 @@ module NonHaml
20
36
  end
21
37
  end
22
38
 
23
- def generate out_name, in_name, context, base_dir='./', verbose=false
24
- NonHamlParser.new.generate out_name, in_name, context, base_dir, verbose
25
- end
26
-
27
39
  def filename
28
40
  # Retrieves the current filename.
29
41
  @filenames.last
@@ -39,17 +51,9 @@ module NonHaml
39
51
  end
40
52
 
41
53
  def current_filename
42
- "#{base_dir}#{NonHaml.filename}"
54
+ "#{base_dir}#{filename}"
43
55
  end
44
- end
45
56
 
46
- class IndentError < StandardError
47
- end
48
-
49
- class ParseError < StandardError
50
- end
51
-
52
- class NonHamlParser
53
57
  def parse text, base_control_indent=0, base_indent=0
54
58
  @s = ""
55
59
  def store text
@@ -88,11 +92,11 @@ module NonHaml
88
92
  # Entering a block.
89
93
 
90
94
  if %w{elsif else}.include? $2
91
- store control_indent + "NonHaml.last_ok_line = #{i}"
95
+ store control_indent + "self.last_ok_line = #{i}"
92
96
  dedent indent, %w{elsif else}.include?($2)
93
97
  else
94
98
  dedent indent, %w{elsif else}.include?($2)
95
- store control_indent + "NonHaml.last_ok_line = #{i}"
99
+ store control_indent + "self.last_ok_line = #{i}"
96
100
  end
97
101
 
98
102
  store control_indent + $1
@@ -101,28 +105,28 @@ module NonHaml
101
105
  # Output should have same indent as block.
102
106
  concat_indent = indent
103
107
  elsif line =~ /= ?non_haml ['"](.*)['"]/
104
- store control_indent + "NonHaml.last_ok_line = #{i}"
105
- file = NonHaml.base_dir + $1
108
+ store control_indent + "self.last_ok_line = #{i}"
109
+ file = base_dir + $1
106
110
  if File.readable? file
107
- store control_indent + "NonHaml.push_filename '#{$1}'"
111
+ store control_indent + "push_filename '#{$1}'"
108
112
  @s += parse open(file).read, control_indent.length, indent
109
113
  else
110
114
  store control_indent + "raise Errno::ENOENT, '\"#{$1}\"'"
111
115
  end
112
- store control_indent + "NonHaml.pop_filename"
116
+ store control_indent + "pop_filename"
113
117
  elsif line.strip.length.zero?
114
118
  # Blank line. Output and move on. Don't change indent. Only do this
115
119
  # for if blocks though, so 'if false' doesn't generate optional blank
116
120
  # line and 'if true' does.
117
121
  #if @statements.last == 'if' or @statements.empty?
118
122
  # XXX disabled temporarily because it sucked.
119
- store control_indent + "NonHaml.last_ok_line = #{i}"
123
+ store control_indent + "self.last_ok_line = #{i}"
120
124
  #if @statements.empty?
121
- store "#{control_indent}NonHaml.concat"
125
+ store "#{control_indent}concat"
122
126
  #end
123
127
  else
124
128
  dedented = dedent indent
125
- store control_indent + "NonHaml.last_ok_line = #{i}"
129
+ store control_indent + "self.last_ok_line = #{i}"
126
130
 
127
131
  # Now deal with whatever we have left.
128
132
  if line =~ /^- *(.*)$/
@@ -134,7 +138,7 @@ module NonHaml
134
138
  # Deal with blank lines.
135
139
  content = $1
136
140
  content = '""' if content.empty?
137
- store "#{control_indent}NonHaml.concat(#{target_indent}, (#{content}))"
141
+ store "#{control_indent}concat(#{target_indent}, (#{content}))"
138
142
  elsif dedented and line.strip.empty?
139
143
  puts 'skipping'
140
144
  # Skip up to one blank line after dedenting.
@@ -155,7 +159,7 @@ module NonHaml
155
159
  # functions get the right arguments.
156
160
  subst = " % [#{to_sub.map{|x| "(#{x})"}.join ', '}]"
157
161
  end
158
- store "#{control_indent}NonHaml.concat #{target_indent}, (%q##{line.gsub('#', '\\#')}##{subst})"
162
+ store "#{control_indent}concat #{target_indent}, (%q##{line.gsub('#', '\\#')}##{subst})"
159
163
  end
160
164
  end
161
165
  end
@@ -163,29 +167,49 @@ module NonHaml
163
167
  @s
164
168
  end
165
169
 
166
- def generate out_name, in_name, context, base_dir, verbose
167
- NonHaml.base_dir = base_dir
168
- NonHaml.out = ""
170
+ def evaluate(code)
171
+ eval(code, @context)
172
+ end
173
+
174
+ def prepare_context(_context_or_vars)
175
+ case _context_or_vars
176
+ when Hash
177
+ @context = binding
178
+ _context_or_vars.each do |name, value|
179
+ evaluate("#{name} = nil")
180
+ setter = evaluate("lambda{|v| #{name} = v}")
181
+ setter.call(value)
182
+ end
183
+ else
184
+ raise TypeError, "cannot use context of type #{_context_or_vars.class}"
185
+ end
186
+ evaluate("concat = nil")
187
+ setter = evaluate("lambda{|v| concat = v}")
188
+ end
189
+
190
+ def generate out_name, in_name, context_or_vars, base_dir, verbose
191
+ self.base_dir = base_dir
192
+ push_filename(in_name)
169
193
 
170
- NonHaml.push_filename in_name
194
+ context = prepare_context(context_or_vars)
195
+ source = File.read(current_filename)
196
+ parsed = parse(source)
171
197
 
172
- src = parse open(NonHaml.current_filename).read
173
198
  if verbose
174
- src.lines.each_with_index do |l,i|
199
+ parsed.lines.each_with_index do |l,i|
175
200
  print Color.blue '%3d ' % (i + 1)
176
201
  puts l
177
202
  end
178
203
  end
179
204
 
180
205
  begin
181
- eval src, context
206
+ evaluate(parsed)
182
207
  rescue Exception => e
183
208
  # Interrupt everything, give more info, then dump out the old exception.
184
- $stderr.puts
185
- $stderr.puts "in #{NonHaml.current_filename}:"
209
+ $stderr.puts "In #{current_filename}:"
186
210
  $stderr.puts Color.red " #{e.class.name}: #{Color.blue e.to_s}"
187
- open(NonHaml.current_filename).lines.each_with_index.drop([NonHaml.last_ok_line - 2, 0].max).first(5).each do |line,i|
188
- if i == NonHaml.last_ok_line
211
+ File.read(current_filename).lines.each_with_index.drop([last_ok_line - 2, 0].max).first(5).each do |line,i|
212
+ if i == last_ok_line
189
213
  $stderr.print Color.red ' %3d ' % (i + 1)
190
214
  $stderr.print Color.red line
191
215
  else
@@ -195,8 +219,8 @@ module NonHaml
195
219
  end
196
220
  raise e
197
221
  else
198
- open(out_name, 'w') do |f|
199
- f.puts NonHaml.out
222
+ File.open(out_name, "w") do |f|
223
+ f.write(out)
200
224
  end
201
225
  end
202
226
  end
@@ -1,6 +1,6 @@
1
1
  module NonHaml
2
2
  unless const_defined?('VERSION')
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1"
4
4
  end
5
5
 
6
6
  def self.version
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: non-haml
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: '1.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-16 00:00:00.000000000 Z
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: HAML-like syntax for non-HTML
15
15
  email: jem@ieee.org