haml_to_star 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE.txt +21 -0
- data/README.md +23 -0
- data/haml_to_star.gemspec +17 -0
- data/lib/haml_to_star/code_node.rb +29 -0
- data/lib/haml_to_star/compiler.rb +120 -31
- metadata +22 -6
- data/lib/haml_to_star/code_element.rb +0 -15
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright 2012 Sebastien Drouyer
|
2
|
+
https://github.com/sdrdis/haml_to_star
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
haml_to_*
|
2
|
+
=========
|
3
|
+
|
4
|
+
haml_to_star is a ruby library that purpose is to allow you to transform haml to any language.
|
5
|
+
|
6
|
+
The compiler class handle common processing tasks as generating the code tree and html tags, but functions that are specific to languages such as how the code in the source code must be implemented on extensions.
|
7
|
+
|
8
|
+
Please take a look at known extensions and at the documentation if you want to create your own extension.
|
9
|
+
|
10
|
+
Known extensions
|
11
|
+
----------------
|
12
|
+
|
13
|
+
[haml_to_js](https://github.com/sdrdis/haml_to_js) converts haml to javascript
|
14
|
+
|
15
|
+
Contributions
|
16
|
+
-------------
|
17
|
+
|
18
|
+
This project is hosted on [github](https://github.com/sdrdis/haml_to_star), so don't hesitate to contribute and raise issues.
|
19
|
+
|
20
|
+
License
|
21
|
+
-------
|
22
|
+
|
23
|
+
This project is under MIT License.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'haml_to_star'
|
3
|
+
s.version = '0.3'
|
4
|
+
s.date = '2012-12-25'
|
5
|
+
s.summary = "haml_to_star is a ruby library that purpose is to allow you to transform haml to any language."
|
6
|
+
s.description =<<eos
|
7
|
+
haml_to_star is a ruby library that purpose is to allow you to transform haml to any language.
|
8
|
+
|
9
|
+
The compiler class handle common processing tasks as generating the code tree and html tags, but functions that are specific to languages such as how the code in the source code must be implemented on extensions.
|
10
|
+
|
11
|
+
Please take a look at known extensions and at the documentation if you want to create your own extension.
|
12
|
+
eos
|
13
|
+
s.authors = ["Sébastien Drouyer"]
|
14
|
+
s.email = 'sdrdis@hotmail.com'
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.homepage = 'https://github.com/sdrdis/haml_to_star'
|
17
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module HamlToStar
|
2
|
+
|
3
|
+
# This class is used only internally. It allows the compiler to construct the tree
|
4
|
+
# associated to the haml code. A code element is equivalent to a line in haml.
|
5
|
+
class CodeNode
|
6
|
+
|
7
|
+
# Current line in haml
|
8
|
+
#
|
9
|
+
# @return [String]
|
10
|
+
attr_accessor :line
|
11
|
+
|
12
|
+
# Children of current line in haml
|
13
|
+
#
|
14
|
+
# @return [Array<CodeNode>]
|
15
|
+
attr_accessor :children
|
16
|
+
|
17
|
+
# The line number of the code element
|
18
|
+
#
|
19
|
+
# @return [Integer]
|
20
|
+
attr_accessor :line_number
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@line = ''
|
24
|
+
@line_number = 0
|
25
|
+
@children = []
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -1,15 +1,35 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'json'
|
3
3
|
require 'cgi'
|
4
|
-
require 'haml_to_star/
|
4
|
+
require 'haml_to_star/code_node'
|
5
5
|
|
6
6
|
module HamlToStar
|
7
7
|
|
8
|
+
# The compiler class transform an haml code to executable code.
|
8
9
|
class Compiler
|
10
|
+
# Variable name in which we save resulting html.
|
11
|
+
#
|
12
|
+
# @return [String]
|
9
13
|
attr_accessor :variable_name
|
14
|
+
|
15
|
+
# Variable name in which we save current haml line code (debugging)
|
16
|
+
#
|
17
|
+
# @return [String]
|
10
18
|
attr_accessor :variable_line_name
|
19
|
+
|
20
|
+
# Self closing html tags (like meta or img)
|
21
|
+
#
|
22
|
+
# @return [Array<String>]
|
11
23
|
attr_accessor :self_closing
|
24
|
+
|
25
|
+
# Shortcuts for doc types (ex: 'xml' => '<?xml version="1.0" encoding="utf-8" ?>')
|
26
|
+
#
|
27
|
+
# @return [{String => String}]
|
12
28
|
attr_accessor :doc_types
|
29
|
+
|
30
|
+
# Current line number
|
31
|
+
#
|
32
|
+
# @return [Integer]
|
13
33
|
attr_accessor :line_number
|
14
34
|
|
15
35
|
def initialize
|
@@ -37,29 +57,41 @@ module HamlToStar
|
|
37
57
|
}
|
38
58
|
end
|
39
59
|
|
40
|
-
|
60
|
+
# Converts haml code to executable code.
|
61
|
+
# Internally, the function first convert it to a {CodeNode} and the calls convert_from_node
|
62
|
+
#
|
63
|
+
# @param str [String] The haml code
|
64
|
+
#
|
65
|
+
# @return [String]
|
41
66
|
def convert_from_string(str)
|
42
|
-
|
67
|
+
nb_char_per_indentation = get_indentation_spacing_from_string(str)
|
43
68
|
|
44
69
|
@line_number = 0
|
45
|
-
|
46
|
-
|
47
|
-
return
|
70
|
+
code_node = CodeNode.new
|
71
|
+
code_node.children = get_code_children_from_string(str, nb_char_per_indentation, 0)
|
72
|
+
return convert_from_node(code_node)
|
48
73
|
end
|
49
74
|
|
50
|
-
|
75
|
+
# Converts haml code into an array of {CodeNode}.
|
76
|
+
#
|
77
|
+
# @param str [String] The haml code
|
78
|
+
# @param nb_char_per_indentation [Integer] Number of spaces defining one unit indentation
|
79
|
+
# @param nb_indentation_depth [Integer] Depth of indentation we want to analyse (used by the recursivity)
|
80
|
+
#
|
81
|
+
# @return [Array<CodeNode>]
|
82
|
+
def get_code_children_from_string(str, nb_char_per_indentation, nb_indentation_depth)
|
51
83
|
temp_str = ''
|
52
84
|
children = []
|
53
85
|
str.each_line do |line|
|
54
|
-
nb_indentations = get_nb_indentation_from_line(line,
|
55
|
-
if (nb_indentations ==
|
56
|
-
code_element =
|
57
|
-
code_element.line = line[
|
86
|
+
nb_indentations = get_nb_indentation_from_line(line, nb_char_per_indentation)
|
87
|
+
if (nb_indentations == nb_indentation_depth)
|
88
|
+
code_element = CodeNode.new
|
89
|
+
code_element.line = line[nb_indentation_depth * nb_char_per_indentation..line.size]
|
58
90
|
if code_element.line[code_element.line.size - 1] == "\n"
|
59
91
|
code_element.line = code_element.line[0..code_element.line.size - 2]
|
60
92
|
end
|
61
93
|
if (temp_str != '')
|
62
|
-
children.last.children = get_code_children_from_string(temp_str,
|
94
|
+
children.last.children = get_code_children_from_string(temp_str, nb_char_per_indentation, nb_indentation_depth + 1)
|
63
95
|
temp_str = ''
|
64
96
|
end
|
65
97
|
@line_number += 1
|
@@ -70,13 +102,19 @@ module HamlToStar
|
|
70
102
|
end
|
71
103
|
end
|
72
104
|
if (temp_str != '')
|
73
|
-
children.last.children = get_code_children_from_string(temp_str,
|
105
|
+
children.last.children = get_code_children_from_string(temp_str, nb_char_per_indentation, nb_indentation_depth + 1)
|
74
106
|
temp_str = ''
|
75
107
|
end
|
76
108
|
return children
|
77
109
|
end
|
78
110
|
|
79
|
-
|
111
|
+
# From a line in the haml code, determines the number of indentation unit
|
112
|
+
#
|
113
|
+
# @param line [String] The haml code line
|
114
|
+
# @param nb_char_per_indentation [Integer] Number of spaces defining one unit indentation
|
115
|
+
#
|
116
|
+
# @return [Integer]
|
117
|
+
def get_nb_indentation_from_line(line, nb_char_per_indentation)
|
80
118
|
nb_char = 0
|
81
119
|
line.each_char do |char|
|
82
120
|
if is_spacing_character(char)
|
@@ -85,12 +123,17 @@ module HamlToStar
|
|
85
123
|
break;
|
86
124
|
end
|
87
125
|
end
|
88
|
-
if (nb_char %
|
126
|
+
if (nb_char % nb_char_per_indentation != 0)
|
89
127
|
raise "Bad indentation"
|
90
128
|
end
|
91
|
-
return nb_char /
|
129
|
+
return nb_char / nb_char_per_indentation
|
92
130
|
end
|
93
131
|
|
132
|
+
# From a haml code, determines number of spaces / tabs composes one indentation unit
|
133
|
+
#
|
134
|
+
# @param str [String] The haml code
|
135
|
+
#
|
136
|
+
# @return [Integer]
|
94
137
|
def get_indentation_spacing_from_string(str)
|
95
138
|
str.each_line do |line|
|
96
139
|
nb_char = 0
|
@@ -109,21 +152,31 @@ module HamlToStar
|
|
109
152
|
return 1
|
110
153
|
end
|
111
154
|
|
155
|
+
# Returns true if the character is a space or tab, false otherwise
|
156
|
+
#
|
157
|
+
# @param char [String] Character to be analysed
|
158
|
+
#
|
159
|
+
# @return [Boolean]
|
112
160
|
def is_spacing_character(char)
|
113
161
|
return char == ' ' || char == "\t";
|
114
162
|
end
|
115
163
|
|
116
|
-
|
164
|
+
# Converts a {CodeNode} to executable code.
|
165
|
+
#
|
166
|
+
# @param code_node [CodeNode] The code tree
|
167
|
+
#
|
168
|
+
# @return [String]
|
169
|
+
def convert_from_node(code_node, indentation = -1)
|
117
170
|
str = []
|
118
171
|
|
119
172
|
inside = []
|
120
|
-
|
121
|
-
inside <<
|
173
|
+
code_node.children.each do |child|
|
174
|
+
inside << convert_from_node(child, indentation + 1)
|
122
175
|
end
|
123
176
|
if (indentation > -1)
|
124
|
-
line =
|
177
|
+
line = code_node.line
|
125
178
|
if (line[0] != '-')
|
126
|
-
|
179
|
+
process_code_line_number(str, code_node.line_number)
|
127
180
|
end
|
128
181
|
if (line[0] == '%' || line[0] == '.' || line[0] == '#')
|
129
182
|
dom = convert_dom_element(line)
|
@@ -153,7 +206,12 @@ module HamlToStar
|
|
153
206
|
|
154
207
|
return str.join("\n")
|
155
208
|
end
|
156
|
-
|
209
|
+
|
210
|
+
# Converts a line in the haml code into a valid dom element.
|
211
|
+
#
|
212
|
+
# @param line [String] The haml code line
|
213
|
+
#
|
214
|
+
# @return [String]
|
157
215
|
def convert_dom_element(line)
|
158
216
|
div_base_informations = line.gsub(%r{[^\{ =]*}).first
|
159
217
|
infos = div_base_informations.gsub(%r{[%.#][\w-]*})
|
@@ -185,7 +243,7 @@ module HamlToStar
|
|
185
243
|
elsif (char == '}')
|
186
244
|
num_brackets -= 1
|
187
245
|
if (num_brackets == 0)
|
188
|
-
params[:
|
246
|
+
params[:dom_params] = process_dom_params(rest_of_line[start_brackets..char_num])
|
189
247
|
end
|
190
248
|
elsif (num_brackets == 0)
|
191
249
|
remaining = rest_of_line[char_num..rest_of_line.size - 1].strip
|
@@ -205,11 +263,16 @@ module HamlToStar
|
|
205
263
|
return construct_dom(params)
|
206
264
|
end
|
207
265
|
|
266
|
+
# Converts object sent by convert_dom_element into a valid dom element.
|
267
|
+
#
|
268
|
+
# @param params [Object] Params sent by convert_dom_element
|
269
|
+
#
|
270
|
+
# @return [String]
|
208
271
|
def construct_dom(params)
|
209
272
|
dom = {}
|
210
273
|
dom[:self_closing] = @self_closing.index(params[:tag])
|
211
274
|
dom[:begin] = '\'<' + params[:tag] + ' '
|
212
|
-
if (params[:
|
275
|
+
if (params[:dom_params])
|
213
276
|
extend = {}
|
214
277
|
if (params[:id])
|
215
278
|
extend[:id] = params[:id]
|
@@ -217,7 +280,7 @@ module HamlToStar
|
|
217
280
|
if (params[:class])
|
218
281
|
extend[:class] = params[:class]
|
219
282
|
end
|
220
|
-
dom[:begin] += '\' + attrs(' + params[:
|
283
|
+
dom[:begin] += '\' + attrs(' + params[:dom_params] + ', ' + extend.to_json + ') + \''
|
221
284
|
else
|
222
285
|
if (params[:id])
|
223
286
|
dom[:begin] += 'id="' + params[:id] + '" '
|
@@ -237,34 +300,60 @@ module HamlToStar
|
|
237
300
|
return dom
|
238
301
|
end
|
239
302
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
303
|
+
# Process dom parameters
|
304
|
+
#
|
305
|
+
# @param dom_params [String] Dom parameters
|
306
|
+
#
|
307
|
+
# @return [String]
|
308
|
+
def process_dom_params(dom_params)
|
245
309
|
raise 'To be defined'
|
246
310
|
end
|
247
311
|
|
312
|
+
# What should be on the header of generated code
|
313
|
+
#
|
314
|
+
# @param str [String] Result string
|
315
|
+
# @param content [String] Generated code
|
248
316
|
def initialize_content(str, content)
|
249
317
|
raise 'To be defined'
|
250
318
|
end
|
251
319
|
|
320
|
+
# How do we add html content to the result
|
321
|
+
#
|
322
|
+
# @param str [String] Result string
|
323
|
+
# @param content [String] Generated code
|
252
324
|
def add_content(str, content)
|
253
325
|
raise 'To be defined'
|
254
326
|
end
|
255
327
|
|
328
|
+
# How do we add code content to the result
|
329
|
+
#
|
330
|
+
# @param str [String] Result string
|
331
|
+
# @param line [String] Current line
|
332
|
+
# @param inside [String] Children lines
|
256
333
|
def add_code(str, line, inside)
|
257
334
|
raise 'To be defined'
|
258
335
|
end
|
259
336
|
|
337
|
+
# How do we what is after = or !=
|
338
|
+
#
|
339
|
+
# @param line [String] Line to be processed
|
340
|
+
#
|
341
|
+
# @return [String]
|
260
342
|
def evaluate(line)
|
261
343
|
raise 'To be defined'
|
262
344
|
end
|
263
345
|
|
264
|
-
|
346
|
+
# How do we add the current line number into the resulted string
|
347
|
+
#
|
348
|
+
# @param str [String] Result string
|
349
|
+
# @param code_line_number [String] Current line number
|
350
|
+
def process_code_line_number(str, code_line_number)
|
265
351
|
raise 'To be defined'
|
266
352
|
end
|
267
353
|
|
354
|
+
# How do we process lines begining with = or !=
|
355
|
+
#
|
356
|
+
# @param line [String] Line to be processed
|
268
357
|
def process_inline_code(str, content)
|
269
358
|
raise 'To be defined'
|
270
359
|
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.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,17 +9,32 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-25 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
|
-
description: haml_to_star
|
14
|
+
description: ! 'haml_to_star is a ruby library that purpose is to allow you to transform
|
15
|
+
haml to any language.
|
16
|
+
|
17
|
+
|
18
|
+
The compiler class handle common processing tasks as generating the code tree and
|
19
|
+
html tags, but functions that are specific to languages such as how the code in
|
20
|
+
the source code must be implemented on extensions.
|
21
|
+
|
22
|
+
|
23
|
+
Please take a look at known extensions and at the documentation if you want to create
|
24
|
+
your own extension.
|
25
|
+
|
26
|
+
'
|
15
27
|
email: sdrdis@hotmail.com
|
16
28
|
executables: []
|
17
29
|
extensions: []
|
18
30
|
extra_rdoc_files: []
|
19
31
|
files:
|
32
|
+
- MIT-LICENSE.txt
|
33
|
+
- README.md
|
34
|
+
- haml_to_star.gemspec
|
35
|
+
- lib/haml_to_star/code_node.rb
|
20
36
|
- lib/haml_to_star/compiler.rb
|
21
|
-
|
22
|
-
homepage: http://rubygems.org/gems/haml_to_star
|
37
|
+
homepage: https://github.com/sdrdis/haml_to_star
|
23
38
|
licenses: []
|
24
39
|
post_install_message:
|
25
40
|
rdoc_options: []
|
@@ -42,5 +57,6 @@ rubyforge_project:
|
|
42
57
|
rubygems_version: 1.8.17
|
43
58
|
signing_key:
|
44
59
|
specification_version: 3
|
45
|
-
summary:
|
60
|
+
summary: haml_to_star is a ruby library that purpose is to allow you to transform
|
61
|
+
haml to any language.
|
46
62
|
test_files: []
|