erb_to_slim 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +45 -0
- data/bin/erb_to_slim +27 -0
- data/lib/erb_to_slim.rb +4 -0
- data/lib/erb_to_slim/engine.rb +57 -0
- data/lib/erb_to_slim/html_tag_list.txt +80 -0
- data/lib/erb_to_slim/pattern.rb +55 -0
- data/lib/erb_to_slim/version.rb +5 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2b43f654f30aaa455b055c0049a878d1dfbb0c94
|
4
|
+
data.tar.gz: a1d9512f1a36cf8ffe7e50374564292514bee974
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9ad3c8143a373e671c91c7cdb0239bc5b08e35a1ba9c9b50af02dd6b2718dbf1c7a9c23b78010f44083f1845062feb4f7d27c509cf7de273be48c366bfe7bd6e
|
7
|
+
data.tar.gz: eb3f3a7fbff8b257c54c66d9c9596ad64412c57c33acb830eb48c8f1c70088d54cbbba855323fdea39ed6be06cd2052fc8ba623d039891a29558c92da3003ab7
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# ErbToSlim
|
2
|
+
|
3
|
+
The most stupid ERB to Slim Converter.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
```sh
|
7
|
+
$ gem install erb_to_slim
|
8
|
+
```
|
9
|
+
|
10
|
+
Goto ERB template directory:
|
11
|
+
|
12
|
+
```sh
|
13
|
+
$ erb_to_slim
|
14
|
+
```
|
15
|
+
|
16
|
+
It will convert all `*.html.erb` files in current directory and subdirectory to `*.html.slim`,
|
17
|
+
and rename original file to `*.html.erb.bak`.
|
18
|
+
|
19
|
+
## How it works
|
20
|
+
|
21
|
+
[ERbToSlim](https://github.com/zw963/erb_to_slim) convert ERB to Slim directly with REGEXP.
|
22
|
+
You need Ruby 1.9+ to support the newest regular expression syntax.
|
23
|
+
|
24
|
+
## Feature
|
25
|
+
* Minimum modified, not change indentation/outline.
|
26
|
+
* No any gem dependency, YEAH!
|
27
|
+
|
28
|
+
## Limitations
|
29
|
+
* Only support html.erb format.
|
30
|
+
* This gem just do match and replace with REGEXP, not do any syntactic analysis or
|
31
|
+
indent detection, so, before process, please format ERB file with your's favorate editor.
|
32
|
+
* This gem is not test fully, consider all cases is not possible.
|
33
|
+
So, before you try, ensure about you well understood Slim,
|
34
|
+
and can fix any unexpected error manually.
|
35
|
+
|
36
|
+
## Author
|
37
|
+
|
38
|
+
Billy.zheng (zw963)
|
39
|
+
|
40
|
+
## OFFICIAL REPO
|
41
|
+
https://github.com/zw963/erb_to_slim
|
42
|
+
|
43
|
+
## Contact
|
44
|
+
If you found any bug, please file an issue at [github](https://github.com/zw963/erb_to_slim/issues).
|
45
|
+
|
data/bin/erb_to_slim
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
require_relative '../lib/erb_to_slim'
|
5
|
+
|
6
|
+
Dir.chdir Dir.pwd do
|
7
|
+
Dir['**/*.{html.erb}'].each do |file|
|
8
|
+
include ErbToSlim::Engine
|
9
|
+
|
10
|
+
stream = File.read(file)
|
11
|
+
|
12
|
+
stream.markup_js_tag
|
13
|
+
stream.cleanup_close_tag
|
14
|
+
stream.replace_html_tag
|
15
|
+
stream.replace_erb_exec_tag
|
16
|
+
stream.replace_erb_eval_tag
|
17
|
+
stream.replace_string_literal
|
18
|
+
stream.replace_tag_with_id
|
19
|
+
stream.replace_tag_with_class
|
20
|
+
stream.finally_clean_up
|
21
|
+
|
22
|
+
File.open("#{File.basename(file, '.erb')}.slim", "w") do |f|
|
23
|
+
f.write(stream)
|
24
|
+
end
|
25
|
+
File.rename(file, "#{file}.bak")
|
26
|
+
end
|
27
|
+
end
|
data/lib/erb_to_slim.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require_relative 'pattern'
|
4
|
+
|
5
|
+
module ErbToSlim
|
6
|
+
module Engine
|
7
|
+
include Pattern
|
8
|
+
|
9
|
+
def markup_js_tag
|
10
|
+
gsub!(js_tag) { $&.gsub(/\n[\s\t]*/, '\&@ ') }
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def cleanup_close_tag
|
15
|
+
gsub!(close_tag, '')
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def replace_html_tag
|
20
|
+
gsub!(html_tag) { $4.empty? ? "#$1#$2#$3" : "#$1#$2#$3 #$4" }
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def replace_erb_exec_tag
|
25
|
+
gsub!(erb_exec_tag) { "- #$1".gsub(/\n[\s\t]*/, '\&- ') }
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def replace_erb_eval_tag
|
30
|
+
gsub!(erb_eval_tag) do
|
31
|
+
$2.nil? ? "#$1= #{$3.gsub(/\n[\s\t]*/, '\&= ')}" : "#$1#$2 = #{$3.gsub(/\n[\s\t]*/, '\&= ')}"
|
32
|
+
end
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_string_literal
|
37
|
+
gsub!(string_literal) { $2.nil? ? "#$1| #$2#$3" : $& }
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def replace_tag_with_id
|
42
|
+
gsub!(tag_with_id) { $1 == 'div' ? "\##$4#$2#$5" : "#$1\##$4#$2#$5" }
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def replace_tag_with_class
|
47
|
+
gsub!(tag_with_class) { $1 == 'div' ? ".#{$4.tr(' ', '.')}#$2#$5" : "#$1.#{$4.tr(' ', '.')}#$2#$5" }
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def finally_clean_up
|
52
|
+
gsub!('@ ', '')
|
53
|
+
gsub!(/<%= *(.*?) *-?%>/, '#{\1}')
|
54
|
+
self
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# get from http://htmldog.com/reference/htmltags/
|
2
|
+
|
3
|
+
a
|
4
|
+
abbr
|
5
|
+
acronym
|
6
|
+
address
|
7
|
+
area
|
8
|
+
b
|
9
|
+
base
|
10
|
+
bdo
|
11
|
+
big
|
12
|
+
blockquote
|
13
|
+
body
|
14
|
+
br
|
15
|
+
button
|
16
|
+
caption
|
17
|
+
cite
|
18
|
+
code
|
19
|
+
col
|
20
|
+
colgroup
|
21
|
+
dd
|
22
|
+
del
|
23
|
+
dfn
|
24
|
+
div
|
25
|
+
dl
|
26
|
+
DOCTYPE
|
27
|
+
dt
|
28
|
+
em
|
29
|
+
fieldset
|
30
|
+
form
|
31
|
+
h1
|
32
|
+
h2
|
33
|
+
h3
|
34
|
+
h4
|
35
|
+
h5
|
36
|
+
h6
|
37
|
+
head
|
38
|
+
html
|
39
|
+
hr
|
40
|
+
i
|
41
|
+
img
|
42
|
+
input
|
43
|
+
ins
|
44
|
+
kbd
|
45
|
+
label
|
46
|
+
legend
|
47
|
+
li
|
48
|
+
link
|
49
|
+
map
|
50
|
+
meta
|
51
|
+
noscript
|
52
|
+
object
|
53
|
+
ol
|
54
|
+
optgroup
|
55
|
+
option
|
56
|
+
p
|
57
|
+
param
|
58
|
+
pre
|
59
|
+
q
|
60
|
+
samp
|
61
|
+
script
|
62
|
+
select
|
63
|
+
small
|
64
|
+
span
|
65
|
+
strong
|
66
|
+
style
|
67
|
+
sub
|
68
|
+
sup
|
69
|
+
table
|
70
|
+
tbody
|
71
|
+
td
|
72
|
+
textarea
|
73
|
+
tfoot
|
74
|
+
th
|
75
|
+
thead
|
76
|
+
title
|
77
|
+
tr
|
78
|
+
tt
|
79
|
+
ul
|
80
|
+
var
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module ErbToSlim
|
4
|
+
module Pattern
|
5
|
+
def html_tags_array
|
6
|
+
@html_tags_array ||= File.readlines(File.expand_path('../html_tag_list.txt', __FILE__))
|
7
|
+
.reject {|line| line =~ /^#|^$/ }.map(&:chomp)
|
8
|
+
end
|
9
|
+
|
10
|
+
def html_tags_list
|
11
|
+
@html_tags_list ||= html_tags_array.join('|')
|
12
|
+
end
|
13
|
+
|
14
|
+
def indentation
|
15
|
+
/[\s\t]*+/
|
16
|
+
end
|
17
|
+
|
18
|
+
def multiline_string_which_not_match(str)
|
19
|
+
str_escaped = str.gsub(/[!<>=?]/, '\\\\\&')
|
20
|
+
/(?:.(?!#{str_escaped}))*?/m
|
21
|
+
end
|
22
|
+
|
23
|
+
def js_tag
|
24
|
+
/<script *(type="text\/javascript")? *>(#{multiline_string_which_not_match('<script')})<\/script>/
|
25
|
+
end
|
26
|
+
|
27
|
+
def erb_exec_tag
|
28
|
+
/<%(?!=)\s*((?m:.*?))\s*-?%>/
|
29
|
+
end
|
30
|
+
|
31
|
+
def close_tag
|
32
|
+
/\s*(<% *end *-?%>|(@ )?<\/\w+>)$/
|
33
|
+
end
|
34
|
+
|
35
|
+
def html_tag
|
36
|
+
/( *)<(\w+)(.*?) *(?<!%)\/?> *(.*)?/
|
37
|
+
end
|
38
|
+
|
39
|
+
def erb_eval_tag
|
40
|
+
/^(#{indentation})(#{html_tags_list})? *<%=\s*(#{multiline_string_which_not_match('<%=')})\s*-?%>$/
|
41
|
+
end
|
42
|
+
|
43
|
+
def string_literal
|
44
|
+
/^(#{indentation})((?:#{html_tags_list}|\||-|=|\#|@))?\b *(.*)$/
|
45
|
+
end
|
46
|
+
|
47
|
+
def tag_with_id
|
48
|
+
/(\w+)(.*?) *id *= *(["'])(.*?)\3(.*?) *$/
|
49
|
+
end
|
50
|
+
|
51
|
+
def tag_with_class
|
52
|
+
/([\w#]+)(.*?) *class *= *(["'])(.*?)\3(.*?) *$/
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: erb_to_slim
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Billy(zw963)
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Erb to Slim converter
|
28
|
+
email:
|
29
|
+
- zw963@163.com
|
30
|
+
executables:
|
31
|
+
- erb_to_slim
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- README.md
|
36
|
+
- bin/erb_to_slim
|
37
|
+
- lib/erb_to_slim.rb
|
38
|
+
- lib/erb_to_slim/engine.rb
|
39
|
+
- lib/erb_to_slim/html_tag_list.txt
|
40
|
+
- lib/erb_to_slim/pattern.rb
|
41
|
+
- lib/erb_to_slim/version.rb
|
42
|
+
homepage: http://github.com/zw963/erb_to_slim
|
43
|
+
licenses: []
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.9.1
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.2.2
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Converts Erb templates to Slim templates
|
65
|
+
test_files: []
|
66
|
+
has_rdoc:
|