haml_to_star 0.1 → 0.2

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.
@@ -0,0 +1,15 @@
1
+ module HamlToStar
2
+
3
+ class CodeElement
4
+ attr_accessor :line
5
+ attr_accessor :children
6
+ attr_accessor :line_number
7
+
8
+ def initialize
9
+ @line = ''
10
+ @line_number = 0
11
+ @children = []
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,273 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+ require 'cgi'
4
+ require 'haml_to_star/code_element'
5
+
6
+ module HamlToStar
7
+
8
+ class Compiler
9
+ attr_accessor :variable_name
10
+ attr_accessor :variable_line_name
11
+ attr_accessor :self_closing
12
+ attr_accessor :doc_types
13
+ attr_accessor :line_number
14
+
15
+ def initialize
16
+ @variable_name = '_$output'
17
+ @variable_line_name = '_$line'
18
+ @self_closing = [
19
+ 'meta',
20
+ 'img',
21
+ 'link',
22
+ 'br',
23
+ 'hr',
24
+ 'input',
25
+ 'area',
26
+ 'base']
27
+
28
+ @doc_types = {
29
+ '5' => '<!DOCTYPE html>',
30
+ 'xml' => '<?xml version="1.0" encoding="utf-8" ?>',
31
+ 'default' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
32
+ 'strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
33
+ 'frameset' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
34
+ '1.1' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
35
+ 'basic' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">',
36
+ 'mobile' => '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">'
37
+ }
38
+ end
39
+
40
+
41
+ def convert_from_string(str)
42
+ nb_char_spacing = get_indentation_spacing_from_string(str)
43
+
44
+ @line_number = 0
45
+ code_tree = CodeElement.new
46
+ code_tree.children = get_code_children_from_string(str, nb_char_spacing, 0)
47
+ return convert_from_tree(code_tree)
48
+ end
49
+
50
+ def get_code_children_from_string(str, nb_char_spacing, nb_indentation_objective)
51
+ temp_str = ''
52
+ children = []
53
+ str.each_line do |line|
54
+ nb_indentations = get_nb_indentation_from_line(line, nb_char_spacing)
55
+ if (nb_indentations == nb_indentation_objective)
56
+ code_element = CodeElement.new
57
+ code_element.line = line[nb_indentation_objective * nb_char_spacing..line.size]
58
+ if code_element.line[code_element.line.size - 1] == "\n"
59
+ code_element.line = code_element.line[0..code_element.line.size - 2]
60
+ end
61
+ if (temp_str != '')
62
+ children.last.children = get_code_children_from_string(temp_str, nb_char_spacing, nb_indentation_objective + 1)
63
+ temp_str = ''
64
+ end
65
+ @line_number += 1
66
+ code_element.line_number = @line_number
67
+ children << code_element
68
+ else
69
+ temp_str += line
70
+ end
71
+ end
72
+ if (temp_str != '')
73
+ children.last.children = get_code_children_from_string(temp_str, nb_char_spacing, nb_indentation_objective + 1)
74
+ temp_str = ''
75
+ end
76
+ return children
77
+ end
78
+
79
+ def get_nb_indentation_from_line(line, nb_char_spacing)
80
+ nb_char = 0
81
+ line.each_char do |char|
82
+ if is_spacing_character(char)
83
+ nb_char += 1
84
+ else
85
+ break;
86
+ end
87
+ end
88
+ if (nb_char % nb_char_spacing != 0)
89
+ raise "Bad indentation"
90
+ end
91
+ return nb_char / nb_char_spacing
92
+ end
93
+
94
+ def get_indentation_spacing_from_string(str)
95
+ str.each_line do |line|
96
+ nb_char = 0
97
+ line.each_char do |char|
98
+ if is_spacing_character(char)
99
+ nb_char += 1
100
+ else
101
+ break
102
+ end
103
+ end
104
+ if (nb_char != 0)
105
+ return nb_char
106
+ end
107
+ end
108
+
109
+ return 1
110
+ end
111
+
112
+ def is_spacing_character(char)
113
+ return char == ' ' || char == "\t";
114
+ end
115
+
116
+ def convert_from_tree(code_tree, indentation = -1)
117
+ str = []
118
+
119
+ inside = []
120
+ code_tree.children.each do |child|
121
+ inside << convert_from_tree(child, indentation + 1)
122
+ end
123
+ if (indentation > -1)
124
+ line = code_tree.line
125
+ if (line[0] != '-')
126
+ process_code_number(str, code_tree.line_number)
127
+ end
128
+ if (line[0] == '%' || line[0] == '.' || line[0] == '#')
129
+ dom = convert_dom_element(line)
130
+ if (dom[:self_closing])
131
+ add_content(str, dom[:begin])
132
+ else
133
+ add_content(str, dom[:begin])
134
+ if (inside.size > 0)
135
+ str << inside.join("\n")
136
+ else
137
+ if (dom[:inside])
138
+ add_content(str, dom[:inside])
139
+ end
140
+ end
141
+ add_content(str, dom[:end])
142
+ end
143
+ elsif (line[0] == '=' || line[0] == '!')
144
+ process_inline_code(str, line)
145
+ elsif (line[0] == '-')
146
+ add_code(str, line, inside)
147
+ else
148
+ add_content(str, line.to_json)
149
+ end
150
+ else
151
+ initialize_content(str, inside)
152
+ end
153
+
154
+ return str.join("\n")
155
+ end
156
+
157
+ def convert_dom_element(line)
158
+ div_base_informations = line.gsub(%r{[^\{ =]*}).first
159
+ infos = div_base_informations.gsub(%r{[%.#][\w-]*})
160
+ params = {:tag => 'div'}
161
+ infos.each do |info|
162
+ if (info[0] == '%')
163
+ params[:tag] = info[1..info.size - 1]
164
+ elsif (info[0] == '#')
165
+ params[:id] = info[1..info.size - 1]
166
+ elsif (info[0] == '.')
167
+ unless params[:class]
168
+ params[:class] = ''
169
+ end
170
+ params[:class] += ' ' + info[1..info.size - 1]
171
+ end
172
+ end
173
+
174
+ rest_of_line = line[div_base_informations.size..line.size - 1]
175
+
176
+ num_brackets = 0
177
+ char_num = 0
178
+ start_brackets = 0
179
+ rest_of_line.each_char do |char|
180
+ if (char == '{')
181
+ if (num_brackets == 0)
182
+ start_brackets = char_num
183
+ end
184
+ num_brackets += 1
185
+ elsif (char == '}')
186
+ num_brackets -= 1
187
+ if (num_brackets == 0)
188
+ params[:additionnals] = process_additionnals(rest_of_line[start_brackets..char_num])
189
+ end
190
+ elsif (num_brackets == 0)
191
+ remaining = rest_of_line[char_num..rest_of_line.size - 1].strip
192
+ if (remaining.size > 0)
193
+ if (char == ' ')
194
+ params[:inside] = remaining
195
+ break
196
+ elsif (char == '=' || char == '!')
197
+ params[:inside_code] = evaluate(remaining)
198
+ break
199
+ end
200
+ end
201
+ end
202
+ char_num += 1
203
+ end
204
+
205
+ return construct_dom(params)
206
+ end
207
+
208
+ def construct_dom(params)
209
+ dom = {}
210
+ dom[:self_closing] = @self_closing.index(params[:tag])
211
+ dom[:begin] = '\'<' + params[:tag] + ' '
212
+ if (params[:additionnals])
213
+ extend = {}
214
+ if (params[:id])
215
+ extend[:id] = params[:id]
216
+ end
217
+ if (params[:class])
218
+ extend[:class] = params[:class]
219
+ end
220
+ dom[:begin] += '\' + attrs(' + params[:additionnals] + ', ' + extend.to_json + ') + \''
221
+ else
222
+ if (params[:id])
223
+ dom[:begin] += 'id="' + params[:id] + '" '
224
+ end
225
+ if (params[:class])
226
+ dom[:begin] += 'class="' + params[:class] + '" '
227
+ end
228
+ end
229
+ dom[:begin] += (dom[:self_closing] ? '/' : '') + '>\''
230
+ if (params[:inside])
231
+ dom[:inside] = CGI::escapeHTML(params[:inside])
232
+ elsif (params[:inside_code])
233
+ dom[:inside] = params[:inside_code]
234
+ end
235
+
236
+ dom[:end] = '\'</' + params[:tag] + '>\''
237
+ return dom
238
+ end
239
+
240
+ def process_additionnals(additionnals)
241
+ raise 'To be defined'
242
+ end
243
+
244
+ def process_code_line(line)
245
+ raise 'To be defined'
246
+ end
247
+
248
+ def initialize_content(str, content)
249
+ raise 'To be defined'
250
+ end
251
+
252
+ def add_content(str, content)
253
+ raise 'To be defined'
254
+ end
255
+
256
+ def add_code(str, line, inside)
257
+ raise 'To be defined'
258
+ end
259
+
260
+ def evaluate(line)
261
+ raise 'To be defined'
262
+ end
263
+
264
+ def process_code_number(str, code_number)
265
+ raise 'To be defined'
266
+ end
267
+
268
+ def process_inline_code(str, content)
269
+ raise 'To be defined'
270
+ end
271
+ end
272
+
273
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haml_to_star
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -17,7 +17,8 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - lib/haml_to_star.rb
20
+ - lib/haml_to_star/compiler.rb
21
+ - lib/haml_to_star/code_element.rb
21
22
  homepage: http://rubygems.org/gems/haml_to_star
22
23
  licenses: []
23
24
  post_install_message:
data/lib/haml_to_star.rb DELETED
@@ -1,292 +0,0 @@
1
- # HAML-TO-*
2
-
3
- # MIT - Sebastien Drouyer
4
-
5
- # Why an other haml converter (especially for JS) ?
6
- # - others are using global variables, except if you prefix all your variables with this.
7
- # - they don't support brackets in parameters : try {value: "test}"}
8
- # - we are giving line number so that you can debug your code more easily
9
- # - class can't be used on prefix and params on others
10
- # - this one is intended to be used in various languages : you can easily use it for others languages as Ruby or C
11
-
12
-
13
- require 'rubygems'
14
- require 'json'
15
- require 'cgi'
16
-
17
- class CodeElement
18
- attr_accessor :line
19
- attr_accessor :children
20
- attr_accessor :line_number
21
-
22
- def initialize
23
- @line = ''
24
- @line_number = 0
25
- @children = []
26
- end
27
- end
28
-
29
- class HamlToStar
30
- attr_accessor :variable_name
31
- attr_accessor :variable_line_name
32
- attr_accessor :self_closing
33
- attr_accessor :doc_types
34
- attr_accessor :line_number
35
-
36
- def initialize
37
- @variable_name = '_$output'
38
- @variable_line_name = '_$line'
39
- @self_closing = [
40
- 'meta',
41
- 'img',
42
- 'link',
43
- 'br',
44
- 'hr',
45
- 'input',
46
- 'area',
47
- 'base']
48
-
49
- @doc_types = {
50
- '5' => '<!DOCTYPE html>',
51
- 'xml' => '<?xml version="1.0" encoding="utf-8" ?>',
52
- 'default' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
53
- 'strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
54
- 'frameset' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
55
- '1.1' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
56
- 'basic' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">',
57
- 'mobile' => '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">'
58
- }
59
- end
60
-
61
-
62
- def convert_from_string(str)
63
- nb_char_spacing = get_indentation_spacing_from_string(str)
64
-
65
- @line_number = 0
66
- code_tree = CodeElement.new
67
- code_tree.children = get_code_children_from_string(str, nb_char_spacing, 0)
68
- return convert_from_tree(code_tree)
69
- end
70
-
71
- def get_code_children_from_string(str, nb_char_spacing, nb_indentation_objective)
72
- temp_str = ''
73
- children = []
74
- str.each_line do |line|
75
- nb_indentations = get_nb_indentation_from_line(line, nb_char_spacing)
76
- if (nb_indentations == nb_indentation_objective)
77
- code_element = CodeElement.new
78
- code_element.line = line[nb_indentation_objective * nb_char_spacing..line.size]
79
- if code_element.line[code_element.line.size - 1] == "\n"
80
- code_element.line = code_element.line[0..code_element.line.size - 2]
81
- end
82
- if (temp_str != '')
83
- children.last.children = get_code_children_from_string(temp_str, nb_char_spacing, nb_indentation_objective + 1)
84
- temp_str = ''
85
- end
86
- @line_number += 1
87
- code_element.line_number = @line_number
88
- children << code_element
89
- else
90
- temp_str += line
91
- end
92
- end
93
- if (temp_str != '')
94
- children.last.children = get_code_children_from_string(temp_str, nb_char_spacing, nb_indentation_objective + 1)
95
- temp_str = ''
96
- end
97
- return children
98
- end
99
-
100
- def get_nb_indentation_from_line(line, nb_char_spacing)
101
- nb_char = 0
102
- line.each_char do |char|
103
- if is_spacing_character(char)
104
- nb_char += 1
105
- else
106
- break;
107
- end
108
- end
109
- if (nb_char % nb_char_spacing != 0)
110
- raise "Bad indentation"
111
- end
112
- return nb_char / nb_char_spacing
113
- end
114
-
115
- def get_indentation_spacing_from_string(str)
116
- str.each_line do |line|
117
- nb_char = 0
118
- line.each_char do |char|
119
- if is_spacing_character(char)
120
- nb_char += 1
121
- else
122
- break
123
- end
124
- end
125
- if (nb_char != 0)
126
- return nb_char
127
- end
128
- end
129
-
130
- return 1
131
- end
132
-
133
- def is_spacing_character(char)
134
- return char == ' ' || char == "\t";
135
- end
136
-
137
- def convert_from_tree(code_tree, indentation = -1)
138
- str = []
139
-
140
- inside = []
141
- code_tree.children.each do |child|
142
- inside << convert_from_tree(child, indentation + 1)
143
- end
144
- if (indentation > -1)
145
- line = code_tree.line
146
- if (line[0] != '-')
147
- process_code_number(str, code_tree.line_number)
148
- end
149
- if (line[0] == '%' || line[0] == '.' || line[0] == '#')
150
- dom = convert_dom_element(line)
151
- if (dom[:self_closing])
152
- add_content(str, dom[:begin])
153
- else
154
- add_content(str, dom[:begin])
155
- if (inside.size > 0)
156
- str << inside.join("\n")
157
- else
158
- if (dom[:inside])
159
- add_content(str, dom[:inside])
160
- end
161
- end
162
- add_content(str, dom[:end])
163
- end
164
- elsif (line[0] == '=' || line[0] == '!')
165
- process_inline_code(str, line)
166
- elsif (line[0] == '-')
167
- add_code(str, line, inside)
168
- else
169
- add_content(str, line.to_json)
170
- end
171
- else
172
- initialize_content(str, inside)
173
- end
174
-
175
- return str.join("\n")
176
- end
177
-
178
- def convert_dom_element(line)
179
- div_base_informations = line.gsub(%r{[^\{ =]*}).first
180
- infos = div_base_informations.gsub(%r{[%.#][\w-]*})
181
- params = {:tag => 'div'}
182
- infos.each do |info|
183
- if (info[0] == '%')
184
- params[:tag] = info[1..info.size - 1]
185
- elsif (info[0] == '#')
186
- params[:id] = info[1..info.size - 1]
187
- elsif (info[0] == '.')
188
- unless params[:class]
189
- params[:class] = ''
190
- end
191
- params[:class] += ' ' + info[1..info.size - 1]
192
- end
193
- end
194
-
195
- rest_of_line = line[div_base_informations.size..line.size - 1]
196
-
197
- num_brackets = 0
198
- char_num = 0
199
- start_brackets = 0
200
- rest_of_line.each_char do |char|
201
- if (char == '{')
202
- if (num_brackets == 0)
203
- start_brackets = char_num
204
- end
205
- num_brackets += 1
206
- elsif (char == '}')
207
- num_brackets -= 1
208
- if (num_brackets == 0)
209
- params[:additionnals] = process_additionnals(rest_of_line[start_brackets..char_num])
210
- end
211
- elsif (num_brackets == 0)
212
- remaining = rest_of_line[char_num..rest_of_line.size - 1].strip
213
- if (remaining.size > 0)
214
- if (char == ' ')
215
- params[:inside] = remaining
216
- break
217
- elsif (char == '=' || char == '!')
218
- params[:inside_code] = evaluate(remaining)
219
- break
220
- end
221
- end
222
- end
223
- char_num += 1
224
- end
225
-
226
- return construct_dom(params)
227
- end
228
-
229
- def construct_dom(params)
230
- dom = {}
231
- dom[:self_closing] = @self_closing.index(params[:tag])
232
- dom[:begin] = '\'<' + params[:tag] + ' '
233
- if (params[:additionnals])
234
- extend = {}
235
- if (params[:id])
236
- extend[:id] = params[:id]
237
- end
238
- if (params[:class])
239
- extend[:class] = params[:class]
240
- end
241
- dom[:begin] += '\' + attrs(' + params[:additionnals] + ', ' + extend.to_json + ') + \''
242
- else
243
- if (params[:id])
244
- dom[:begin] += 'id="' + params[:id] + '" '
245
- end
246
- if (params[:class])
247
- dom[:begin] += 'class="' + params[:class] + '" '
248
- end
249
- end
250
- dom[:begin] += (dom[:self_closing] ? '/' : '') + '>\''
251
- if (params[:inside])
252
- dom[:inside] = CGI::escapeHTML(params[:inside])
253
- elsif (params[:inside_code])
254
- dom[:inside] = params[:inside_code]
255
- end
256
-
257
- dom[:end] = '\'</' + params[:tag] + '>\''
258
- return dom
259
- end
260
-
261
- def process_additionnals(additionnals)
262
- raise 'To be defined'
263
- end
264
-
265
- def process_code_line(line)
266
- raise 'To be defined'
267
- end
268
-
269
- def initialize_content(str, content)
270
- raise 'To be defined'
271
- end
272
-
273
- def add_content(str, content)
274
- raise 'To be defined'
275
- end
276
-
277
- def add_code(str, line, inside)
278
- raise 'To be defined'
279
- end
280
-
281
- def evaluate(line)
282
- raise 'To be defined'
283
- end
284
-
285
- def process_code_number(str, code_number)
286
- raise 'To be defined'
287
- end
288
-
289
- def process_inline_code(str, content)
290
- raise 'To be defined'
291
- end
292
- end