haml_to_js 0.1
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.
- data/MIT-LICENSE.txt +21 -0
- data/README.md +46 -0
- data/Rakefile +8 -0
- data/bin/haml_to_js +6 -0
- data/haml_to_js.gemspec +24 -0
- data/js/haml_to_js.js +35 -0
- data/lib/haml_to_js/compiler.rb +66 -0
- data/test/integration_test.rb +19 -0
- data/test/integration_test_cases/input/001_empty.jshaml +0 -0
- data/test/integration_test_cases/input/002_div.jshaml +1 -0
- data/test/integration_test_cases/input/003_div_with_class.jshaml +1 -0
- data/test/integration_test_cases/input/004_div_with_class_and_id.jshaml +1 -0
- data/test/integration_test_cases/input/005_span_with_class_and_id.jshaml +1 -0
- data/test/integration_test_cases/input/006_span_with_id_and_two_classes.jshaml +1 -0
- data/test/integration_test_cases/input/007_div_with_id_parameter.jshaml +1 -0
- data/test/integration_test_cases/input/008_div_with_class_and_class_parameter.jshaml +1 -0
- data/test/integration_test_cases/input/009_div_with_class_and_javascript_class.jshaml +1 -0
- data/test/integration_test_cases/input/010_div_with_class_and_multiple_parameters.jshaml +1 -0
- data/test/integration_test_cases/input/011_div_with_class_and_inside.jshaml +2 -0
- data/test/integration_test_cases/input/012_div_with_class_and_inside_and_inside.jshaml +3 -0
- data/test/integration_test_cases/input/013_div_with_class_and_conditionnal_inside.jshaml +5 -0
- data/test/integration_test_cases/input/014_div_with_dynamic_escaped_content.jshaml +2 -0
- data/test/integration_test_cases/input/015_div_with_dynamic_non_escaped_content.jshaml +2 -0
- data/test/integration_test_cases/input/016_div_with_simple_each.jshaml +3 -0
- data/test/integration_test_cases/input/017_div_with_key_value_each.jshaml +3 -0
- data/test/integration_test_cases/output/001_empty.js +2 -0
- data/test/integration_test_cases/output/002_div.js +5 -0
- data/test/integration_test_cases/output/003_div_with_class.js +5 -0
- data/test/integration_test_cases/output/004_div_with_class_and_id.js +5 -0
- data/test/integration_test_cases/output/005_span_with_class_and_id.js +5 -0
- data/test/integration_test_cases/output/006_span_with_id_and_two_classes.js +5 -0
- data/test/integration_test_cases/output/007_div_with_id_parameter.js +5 -0
- data/test/integration_test_cases/output/008_div_with_class_and_class_parameter.js +5 -0
- data/test/integration_test_cases/output/009_div_with_class_and_javascript_class.js +5 -0
- data/test/integration_test_cases/output/010_div_with_class_and_multiple_parameters.js +5 -0
- data/test/integration_test_cases/output/011_div_with_class_and_inside.js +8 -0
- data/test/integration_test_cases/output/012_div_with_class_and_inside_and_inside.js +11 -0
- data/test/integration_test_cases/output/013_div_with_class_and_conditionnal_inside.js +17 -0
- data/test/integration_test_cases/output/014_div_with_dynamic_escaped_content.js +7 -0
- data/test/integration_test_cases/output/015_div_with_dynamic_non_escaped_content.js +7 -0
- data/test/integration_test_cases/output/016_div_with_simple_each.js +10 -0
- data/test/integration_test_cases/output/017_div_with_key_value_each.js +10 -0
- metadata +107 -0
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright 2012 Sebastien Drouyer
|
2
|
+
https://github.com/sdrdis/haml_to_js
|
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,46 @@
|
|
1
|
+
haml_to_js
|
2
|
+
=========
|
3
|
+
|
4
|
+
haml_to_js is a ruby library and command line that converts haml to javascript.
|
5
|
+
|
6
|
+
Why an other haml to js converter (see similar utilities) ?
|
7
|
+
* It is a extension of [haml_to_star](https://github.com/sdrdis/haml_to_star) which purpose is to convert haml into any language
|
8
|
+
* This converter is intended for front usage
|
9
|
+
* It simplifies debugging (the generated code is readable, a variable hold haml line number so you can know exactly where the problem is)
|
10
|
+
* It support features that other don't support (don't hesitate to raise an issue if I am no longer right):
|
11
|
+
* if...else statements
|
12
|
+
* Declarations such as `.first{class: 'second'}` will generate `<div class="first second"></div>`
|
13
|
+
* Support for brackets inside parameters (ex: `%div{key: 'value with bracket}'}`)
|
14
|
+
|
15
|
+
Installation
|
16
|
+
------------
|
17
|
+
|
18
|
+
`gem install haml_to_js`
|
19
|
+
|
20
|
+
Usage
|
21
|
+
-----
|
22
|
+
|
23
|
+
`haml_to_js file` or `haml_to_js < file`
|
24
|
+
|
25
|
+
Take a look at integration test cases in order to see what generated code looks like.
|
26
|
+
|
27
|
+
The generated code has dependencies; it needs two functions `attrs` and `escape`. It is recommended to include [haml_to_js.js](https://github.com/sdrdis/haml_to_js/blob/master/js/haml_to_js.js).
|
28
|
+
|
29
|
+
Contributions
|
30
|
+
-------------
|
31
|
+
|
32
|
+
This project is hosted on [github](https://github.com/sdrdis/haml_to_js), so don't hesitate to contribute and raise issues.
|
33
|
+
|
34
|
+
License
|
35
|
+
-------
|
36
|
+
|
37
|
+
This project is under MIT License.
|
38
|
+
|
39
|
+
Similar utilities
|
40
|
+
-----------------
|
41
|
+
|
42
|
+
[haml-js](https://github.com/creationix/haml-js)
|
43
|
+
|
44
|
+
[client-side-js](https://github.com/uglyog/clientside-haml-js)
|
45
|
+
|
46
|
+
[ruby-haml-js](https://github.com/dnagir/ruby-haml-js)
|
data/Rakefile
ADDED
data/bin/haml_to_js
ADDED
data/haml_to_js.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'haml_to_js'
|
3
|
+
s.version = '0.1'
|
4
|
+
s.date = '2012-12-27'
|
5
|
+
s.summary = "haml_to_js is a ruby library and command line that converts haml to javascript."
|
6
|
+
s.add_runtime_dependency('haml_to_star')
|
7
|
+
s.description = <<eos
|
8
|
+
haml_to_js is a ruby library and command line that converts haml to javascript.
|
9
|
+
|
10
|
+
Why an other haml to js converter (see similar utilities) ?
|
11
|
+
* It is a extension of [haml_to_star](https://github.com/sdrdis/haml_to_star) which purpose is to convert haml into any language
|
12
|
+
* This converter is intended for front usage
|
13
|
+
* It simplifies debugging (the generated code is readable, a variable hold haml line number so you can know exactly where the problem is)
|
14
|
+
* It support features that other don't support (don't hesitate to raise an issue if I am no longer right):
|
15
|
+
* if...else statements
|
16
|
+
* Declarations such as `.first{class: 'second'}` will generate `<div class="first second"></div>`
|
17
|
+
* Support for brackets inside parameters (ex: `%div{key: 'value with bracket}'}`)
|
18
|
+
eos
|
19
|
+
s.authors = ["Sébastien Drouyer"]
|
20
|
+
s.email = 'sdrdis@hotmail.com'
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.executables = ['haml_to_js']
|
23
|
+
s.homepage = 'https://github.com/sdrdis/haml_to_js/'
|
24
|
+
end
|
data/js/haml_to_js.js
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
function escape(str) {
|
2
|
+
return String(str)
|
3
|
+
.replace(/&/g, '&')
|
4
|
+
.replace(/>/g, '>')
|
5
|
+
.replace(/</g, '<')
|
6
|
+
.replace(/"/g, '"')
|
7
|
+
}
|
8
|
+
|
9
|
+
function attrs(attrs, extended) {
|
10
|
+
if (extended) {
|
11
|
+
if (extended['class']) {
|
12
|
+
if (!attrs['class']) {
|
13
|
+
attrs['class'] = '';
|
14
|
+
}
|
15
|
+
attrs['class'] += extended['class'];
|
16
|
+
}
|
17
|
+
if (extended['id']) {
|
18
|
+
if (!attrs['id']) {
|
19
|
+
attrs['id'] = '';
|
20
|
+
}
|
21
|
+
attrs['id'] = extended['id'] + attrs['id'];
|
22
|
+
}
|
23
|
+
}
|
24
|
+
var buf = []
|
25
|
+
for (var key in attrs)
|
26
|
+
if (typeof attrs[key] === 'boolean') {
|
27
|
+
if (attrs[key] === true)
|
28
|
+
buf.push(key + '="' + key + '"')
|
29
|
+
} else if ($.isPlainObject(attrs[key]) || $.isArray(attrs[key])) {
|
30
|
+
buf.push(key + '="' + escape($.toJSON(attrs[key])) + '"')
|
31
|
+
} else if (attrs[key]) {
|
32
|
+
buf.push(key + '="' + escape(attrs[key]) + '"')
|
33
|
+
}
|
34
|
+
return buf.join(' ')
|
35
|
+
}
|
@@ -0,0 +1,66 @@
|
|
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
|
+
require 'haml_to_star/compiler'
|
13
|
+
|
14
|
+
class Compiler < HamlToStar::Compiler
|
15
|
+
|
16
|
+
def process_dom_params(dom_params)
|
17
|
+
return dom_params.gsub(%r{(for|name|id|class) *:}, '\'\1\':')
|
18
|
+
end
|
19
|
+
|
20
|
+
def process_code_line(line)
|
21
|
+
processed_line = line;
|
22
|
+
processed_line = processed_line.gsub(%r{each *([^ ]*) *: *([^ ]*) *in *([^ ]*)}, 'for (var \1 in \3) { var \2 = \3[\1];')
|
23
|
+
processed_line = processed_line.gsub(%r{each *([^ ]*) *in *([^ ]*)}, 'for (var _$key in \2) { var \1 = \2[_$key];')
|
24
|
+
return {:processed_line => processed_line, :changed => line != processed_line}
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize_content(str, content)
|
28
|
+
str << "var #{@variable_name} = '';"
|
29
|
+
str << "var #{@variable_line_name} = 0;"
|
30
|
+
str << content.join("\n")
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_content(str, content)
|
34
|
+
str << "#{@variable_name} += " + content + ';'
|
35
|
+
end
|
36
|
+
|
37
|
+
def add_code(str, line, inside)
|
38
|
+
processed_line = process_code_line(line[1..line.size - 1])
|
39
|
+
str << processed_line[:processed_line] + (inside.size > 0 ? '' : ';')
|
40
|
+
if (inside.size > 0)
|
41
|
+
if (!processed_line[:changed])
|
42
|
+
str << '{'
|
43
|
+
end
|
44
|
+
str << inside.join("\n")
|
45
|
+
str << '}'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def evaluate(line)
|
50
|
+
if (line[0] == '=')
|
51
|
+
return 'escape(' + line[1..line.size - 1] + ')'
|
52
|
+
elsif (line[0..1] == '!=')
|
53
|
+
return line[2..line.size - 1]
|
54
|
+
else
|
55
|
+
return line
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def process_code_line_number(str, code_line_number)
|
60
|
+
str << "#{@variable_line_name} = #{code_line_number};";
|
61
|
+
end
|
62
|
+
|
63
|
+
def process_inline_code(str, content)
|
64
|
+
str << "#{@variable_name} += " + evaluate(content) + ';'
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'haml_to_js/compiler'
|
3
|
+
|
4
|
+
class IntegrationTest < Test::Unit::TestCase
|
5
|
+
def test_integration_test_cases
|
6
|
+
input_dir = "integration_test_cases/input/"
|
7
|
+
input_ext = ".jshaml"
|
8
|
+
output_dir = "integration_test_cases/output/"
|
9
|
+
output_ext = ".js"
|
10
|
+
input_files = Dir.glob("#{input_dir}*#{input_ext}")
|
11
|
+
inst = Compiler.new
|
12
|
+
|
13
|
+
input_files.each do |input_file|
|
14
|
+
filename = input_file[input_dir.length..(input_file.length - input_ext.length - 1)]
|
15
|
+
output_file = "#{output_dir}#{filename}#{output_ext}"
|
16
|
+
assert_equal(inst.convert_from_string(File.read(input_file)).strip, File.read(output_file).strip)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
%div
|
@@ -0,0 +1 @@
|
|
1
|
+
.class
|
@@ -0,0 +1 @@
|
|
1
|
+
#id.class
|
@@ -0,0 +1 @@
|
|
1
|
+
%span#id.class
|
@@ -0,0 +1 @@
|
|
1
|
+
%span#id.class.second
|
@@ -0,0 +1 @@
|
|
1
|
+
%div{id: 'id'}
|
@@ -0,0 +1 @@
|
|
1
|
+
.class{class: 'second'}
|
@@ -0,0 +1 @@
|
|
1
|
+
.class{class: variable ? 'true' : 'false'}
|
@@ -0,0 +1 @@
|
|
1
|
+
.class{class: 'second', 'data-enabled': true}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
var _$output = '';
|
2
|
+
var _$line = 0;
|
3
|
+
_$line = 1;
|
4
|
+
_$output += '<div class=" class" >';
|
5
|
+
_$line = 2;
|
6
|
+
_$output += '<div class=" inside" >';
|
7
|
+
_$line = 3;
|
8
|
+
_$output += '<div class=" deep" >';
|
9
|
+
_$output += '</div>';
|
10
|
+
_$output += '</div>';
|
11
|
+
_$output += '</div>';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
var _$output = '';
|
2
|
+
var _$line = 0;
|
3
|
+
_$line = 1;
|
4
|
+
_$output += '<div class=" class" >';
|
5
|
+
if (condition)
|
6
|
+
{
|
7
|
+
_$line = 3;
|
8
|
+
_$output += '<div class=" true" >';
|
9
|
+
_$output += '</div>';
|
10
|
+
}
|
11
|
+
else
|
12
|
+
{
|
13
|
+
_$line = 5;
|
14
|
+
_$output += '<div class=" false" >';
|
15
|
+
_$output += '</div>';
|
16
|
+
}
|
17
|
+
_$output += '</div>';
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: haml_to_js
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sébastien Drouyer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-27 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: haml_to_star
|
16
|
+
requirement: &76616240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *76616240
|
25
|
+
description: ! "haml_to_js is a ruby library and command line that converts haml to
|
26
|
+
javascript.\n\nWhy an other haml to js converter (see similar utilities) ?\n* It
|
27
|
+
is a extension of [haml_to_star](https://github.com/sdrdis/haml_to_star) which purpose
|
28
|
+
is to convert haml into any language\n* This converter is intended for front usage\n*
|
29
|
+
It simplifies debugging (the generated code is readable, a variable hold haml line
|
30
|
+
number so you can know exactly where the problem is)\n* It support features that
|
31
|
+
other don't support (don't hesitate to raise an issue if I am no longer right):\n
|
32
|
+
\ * if...else statements\n * Declarations such as `.first{class: 'second'}` will
|
33
|
+
generate `<div class=\"first second\"></div>`\n * Support for brackets inside parameters
|
34
|
+
(ex: `%div{key: 'value with bracket}'}`)\n"
|
35
|
+
email: sdrdis@hotmail.com
|
36
|
+
executables:
|
37
|
+
- haml_to_js
|
38
|
+
extensions: []
|
39
|
+
extra_rdoc_files: []
|
40
|
+
files:
|
41
|
+
- MIT-LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- bin/haml_to_js
|
45
|
+
- haml_to_js.gemspec
|
46
|
+
- js/haml_to_js.js
|
47
|
+
- lib/haml_to_js/compiler.rb
|
48
|
+
- test/integration_test.rb
|
49
|
+
- test/integration_test_cases/input/001_empty.jshaml
|
50
|
+
- test/integration_test_cases/input/002_div.jshaml
|
51
|
+
- test/integration_test_cases/input/003_div_with_class.jshaml
|
52
|
+
- test/integration_test_cases/input/004_div_with_class_and_id.jshaml
|
53
|
+
- test/integration_test_cases/input/005_span_with_class_and_id.jshaml
|
54
|
+
- test/integration_test_cases/input/006_span_with_id_and_two_classes.jshaml
|
55
|
+
- test/integration_test_cases/input/007_div_with_id_parameter.jshaml
|
56
|
+
- test/integration_test_cases/input/008_div_with_class_and_class_parameter.jshaml
|
57
|
+
- test/integration_test_cases/input/009_div_with_class_and_javascript_class.jshaml
|
58
|
+
- test/integration_test_cases/input/010_div_with_class_and_multiple_parameters.jshaml
|
59
|
+
- test/integration_test_cases/input/011_div_with_class_and_inside.jshaml
|
60
|
+
- test/integration_test_cases/input/012_div_with_class_and_inside_and_inside.jshaml
|
61
|
+
- test/integration_test_cases/input/013_div_with_class_and_conditionnal_inside.jshaml
|
62
|
+
- test/integration_test_cases/input/014_div_with_dynamic_escaped_content.jshaml
|
63
|
+
- test/integration_test_cases/input/015_div_with_dynamic_non_escaped_content.jshaml
|
64
|
+
- test/integration_test_cases/input/016_div_with_simple_each.jshaml
|
65
|
+
- test/integration_test_cases/input/017_div_with_key_value_each.jshaml
|
66
|
+
- test/integration_test_cases/output/001_empty.js
|
67
|
+
- test/integration_test_cases/output/002_div.js
|
68
|
+
- test/integration_test_cases/output/003_div_with_class.js
|
69
|
+
- test/integration_test_cases/output/004_div_with_class_and_id.js
|
70
|
+
- test/integration_test_cases/output/005_span_with_class_and_id.js
|
71
|
+
- test/integration_test_cases/output/006_span_with_id_and_two_classes.js
|
72
|
+
- test/integration_test_cases/output/007_div_with_id_parameter.js
|
73
|
+
- test/integration_test_cases/output/008_div_with_class_and_class_parameter.js
|
74
|
+
- test/integration_test_cases/output/009_div_with_class_and_javascript_class.js
|
75
|
+
- test/integration_test_cases/output/010_div_with_class_and_multiple_parameters.js
|
76
|
+
- test/integration_test_cases/output/011_div_with_class_and_inside.js
|
77
|
+
- test/integration_test_cases/output/012_div_with_class_and_inside_and_inside.js
|
78
|
+
- test/integration_test_cases/output/013_div_with_class_and_conditionnal_inside.js
|
79
|
+
- test/integration_test_cases/output/014_div_with_dynamic_escaped_content.js
|
80
|
+
- test/integration_test_cases/output/015_div_with_dynamic_non_escaped_content.js
|
81
|
+
- test/integration_test_cases/output/016_div_with_simple_each.js
|
82
|
+
- test/integration_test_cases/output/017_div_with_key_value_each.js
|
83
|
+
homepage: https://github.com/sdrdis/haml_to_js/
|
84
|
+
licenses: []
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.17
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: haml_to_js is a ruby library and command line that converts haml to javascript.
|
107
|
+
test_files: []
|