bbcode 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +1 -1
- data/Rakefile +1 -1
- data/bbcode.gemspec +2 -2
- data/lib/bbcode.rb +168 -175
- metadata +2 -2
data/Manifest
CHANGED
data/Rakefile
CHANGED
data/bbcode.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{bbcode}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.4"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["eiffel qiu"]
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.description = %q{A tool to beautify code.}
|
11
11
|
s.email = %q{eiffelqiu@gmail.com}
|
12
12
|
s.extra_rdoc_files = ["lib/bbcode.rb", "LICENSE", "TODO", "README"]
|
13
|
-
s.files = ["lib/bbcode.rb", "Manifest", "LICENSE", "
|
13
|
+
s.files = ["lib/bbcode.rb", "Manifest", "LICENSE", "TODO", "README", "Rakefile", "bbcode.gemspec"]
|
14
14
|
s.has_rdoc = true
|
15
15
|
s.homepage = %q{http://www.likenote.com}
|
16
16
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bbcode", "--main", "README"]
|
data/lib/bbcode.rb
CHANGED
@@ -1,186 +1,179 @@
|
|
1
1
|
module Bbcode
|
2
2
|
|
3
|
-
|
3
|
+
# user-customizable values
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
TabStr = " "
|
6
|
+
TabSize = 4
|
7
7
|
|
8
|
-
|
8
|
+
# indent regexp tests
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
10
|
+
IndentExp = [
|
11
|
+
/^module\b/,
|
12
|
+
/^class\b/,
|
13
|
+
/^if\b/,
|
14
|
+
/(=\s*|^)until\b/,
|
15
|
+
/(=\s*|^)for\b/,
|
16
|
+
/^unless\b/,
|
17
|
+
/(=\s*|^)while\b/,
|
18
|
+
/(=\s*|^)begin\b/,
|
19
|
+
/(^| )case\b/,
|
20
|
+
/\bthen\b/,
|
21
|
+
/^rescue\b/,
|
22
|
+
/^def\b/,
|
23
|
+
/\bdo\b/,
|
24
|
+
/^else\b/,
|
25
|
+
/^elsif\b/,
|
26
|
+
/^ensure\b/,
|
27
|
+
/\bwhen\b/,
|
28
|
+
/\{[^\}]*$/,
|
29
|
+
/\[[^\]]*$/
|
30
|
+
]
|
31
31
|
|
32
|
-
|
32
|
+
# outdent regexp tests
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
34
|
+
OutdentExp = [
|
35
|
+
/^rescue\b/,
|
36
|
+
/^ensure\b/,
|
37
|
+
/^elsif\b/,
|
38
|
+
/^end\b/,
|
39
|
+
/^else\b/,
|
40
|
+
/\bwhen\b/,
|
41
|
+
/^[^\{]*\}/,
|
42
|
+
/^[^\[]*\]/
|
43
|
+
]
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
def self.rb_make_tab(tab)
|
46
|
+
return (tab < 0) ? "" : TabStr * TabSize * tab
|
47
|
+
end
|
48
48
|
|
49
|
-
|
49
|
+
def self.rb_add_line(line,tab)
|
50
|
+
line.strip!
|
51
|
+
line = rb_make_tab(tab) + line if line.length > 0
|
52
|
+
return line
|
53
|
+
end
|
50
54
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
# detect program end mark
|
74
|
-
if(line =~ /^__END__$/)
|
75
|
-
program_end = true
|
76
|
-
else
|
77
|
-
# combine continuing lines
|
78
|
-
if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
|
79
|
-
multiLine_array.push line
|
80
|
-
multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
|
81
|
-
next
|
82
|
-
end
|
83
|
-
|
84
|
-
# add final line
|
85
|
-
if(multiLine_str.length > 0)
|
86
|
-
multiLine_array.push line
|
87
|
-
multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
|
88
|
-
end
|
89
|
-
|
90
|
-
tline = ((multiLine_str.length > 0)?multiLine_str:line).strip
|
91
|
-
if(tline =~ /^=begin/)
|
92
|
-
comment_block = true
|
93
|
-
end
|
94
|
-
if(in_here_doc)
|
95
|
-
in_here_doc = false if tline =~ %r{\s*#{here_doc_term}\s*}
|
96
|
-
else # not in here_doc
|
97
|
-
if tline =~ %r{=\s*<<}
|
98
|
-
here_doc_term = tline.sub(%r{.*=\s*<<-?\s*([_|\w]+).*},"\\1")
|
99
|
-
in_here_doc = here_doc_term.size > 0
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
if(comment_block || program_end || in_here_doc)
|
105
|
-
# add the line unchanged
|
106
|
-
output << line
|
107
|
-
else
|
108
|
-
comment_line = (tline =~ /^#/)
|
109
|
-
if(!comment_line)
|
110
|
-
# throw out sequences that will
|
111
|
-
# only sow confusion
|
112
|
-
while tline.gsub!(/\{[^\{]*?\}/,"")
|
113
|
-
end
|
114
|
-
while tline.gsub!(/\[[^\[]*?\]/,"")
|
115
|
-
end
|
116
|
-
while tline.gsub!(/'.*?'/,"")
|
117
|
-
end
|
118
|
-
while tline.gsub!(/".*?"/,"")
|
119
|
-
end
|
120
|
-
while tline.gsub!(/\`.*?\`/,"")
|
121
|
-
end
|
122
|
-
while tline.gsub!(/\([^\(]*?\)/,"")
|
123
|
-
end
|
124
|
-
while tline.gsub!(/\/.*?\//,"")
|
125
|
-
end
|
126
|
-
while tline.gsub!(/%r(.).*?\1/,"")
|
127
|
-
end
|
128
|
-
# delete end-of-line comments
|
129
|
-
tline.sub!(/#[^\"]+$/,"")
|
130
|
-
# convert quotes
|
131
|
-
tline.gsub!(/\\\"/,"'")
|
132
|
-
OutdentExp.each do |re|
|
133
|
-
if(tline =~ re)
|
134
|
-
tab -= 1
|
135
|
-
break
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
if (multiLine_array.length > 0)
|
140
|
-
multiLine_array.each do |ml|
|
141
|
-
output << rb_add_line(ml,tab)
|
142
|
-
end
|
143
|
-
multiLine_array.clear
|
144
|
-
multiLine_str = ""
|
145
|
-
else
|
146
|
-
output << rb_add_line(line,tab)
|
147
|
-
end
|
148
|
-
if(!comment_line)
|
149
|
-
IndentExp.each do |re|
|
150
|
-
if(tline =~ re && !(tline =~ /\s+end\s*$/))
|
151
|
-
tab += 1
|
152
|
-
break
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
if(tline =~ /^=end/)
|
158
|
-
comment_block = false
|
159
|
-
end
|
160
|
-
end
|
161
|
-
error = (tab != 0)
|
162
|
-
STDERR.puts "Error: indent/outdent mismatch: #{tab}." if error
|
163
|
-
return output.join("\n") + "\n",error
|
164
|
-
end # beautify_string
|
165
|
-
|
166
|
-
def beautify_file(path)
|
167
|
-
error = false
|
168
|
-
if(path == '-') # stdin source
|
169
|
-
source = STDIN.read
|
170
|
-
dest,error = beautify_string(source,"stdin")
|
171
|
-
print dest
|
172
|
-
else # named file source
|
173
|
-
source = File.read(path)
|
174
|
-
dest,error = beautify_string(source,path)
|
175
|
-
if(source != dest)
|
176
|
-
# make a backup copy
|
177
|
-
File.open(path + "~","w") { |f| f.write(source) }
|
178
|
-
# overwrite the original
|
179
|
-
File.open(path,"w") { |f| f.write(dest) }
|
180
|
-
end
|
181
|
-
end
|
182
|
-
return error
|
183
|
-
end # beautify_file
|
184
|
-
end
|
55
|
+
def self.beautify_string(source, path = "")
|
56
|
+
comment_block = false
|
57
|
+
in_here_doc = false
|
58
|
+
here_doc_term = ""
|
59
|
+
program_end = false
|
60
|
+
multiLine_array = []
|
61
|
+
multiLine_str = ""
|
62
|
+
tab = 0
|
63
|
+
output = []
|
64
|
+
source.each do |line|
|
65
|
+
line.chomp!
|
66
|
+
if(!program_end)
|
67
|
+
# detect program end mark
|
68
|
+
if(line =~ /^__END__$/)
|
69
|
+
program_end = true
|
70
|
+
else
|
71
|
+
# combine continuing lines
|
72
|
+
if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
|
73
|
+
multiLine_array.push line
|
74
|
+
multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
|
75
|
+
next
|
76
|
+
end
|
185
77
|
|
186
|
-
|
78
|
+
# add final line
|
79
|
+
if(multiLine_str.length > 0)
|
80
|
+
multiLine_array.push line
|
81
|
+
multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
|
82
|
+
end
|
83
|
+
|
84
|
+
tline = ((multiLine_str.length > 0)?multiLine_str:line).strip
|
85
|
+
if(tline =~ /^=begin/)
|
86
|
+
comment_block = true
|
87
|
+
end
|
88
|
+
if(in_here_doc)
|
89
|
+
in_here_doc = false if tline =~ %r{\s*#{here_doc_term}\s*}
|
90
|
+
else # not in here_doc
|
91
|
+
if tline =~ %r{=\s*<<}
|
92
|
+
here_doc_term = tline.sub(%r{.*=\s*<<-?\s*([_|\w]+).*},"\\1")
|
93
|
+
in_here_doc = here_doc_term.size > 0
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
if(comment_block || program_end || in_here_doc)
|
99
|
+
# add the line unchanged
|
100
|
+
output << line
|
101
|
+
else
|
102
|
+
comment_line = (tline =~ /^#/)
|
103
|
+
if(!comment_line)
|
104
|
+
# throw out sequences that will
|
105
|
+
# only sow confusion
|
106
|
+
while tline.gsub!(/\{[^\{]*?\}/,"")
|
107
|
+
end
|
108
|
+
while tline.gsub!(/\[[^\[]*?\]/,"")
|
109
|
+
end
|
110
|
+
while tline.gsub!(/'.*?'/,"")
|
111
|
+
end
|
112
|
+
while tline.gsub!(/".*?"/,"")
|
113
|
+
end
|
114
|
+
while tline.gsub!(/\`.*?\`/,"")
|
115
|
+
end
|
116
|
+
while tline.gsub!(/\([^\(]*?\)/,"")
|
117
|
+
end
|
118
|
+
while tline.gsub!(/\/.*?\//,"")
|
119
|
+
end
|
120
|
+
while tline.gsub!(/%r(.).*?\1/,"")
|
121
|
+
end
|
122
|
+
# delete end-of-line comments
|
123
|
+
tline.sub!(/#[^\"]+$/,"")
|
124
|
+
# convert quotes
|
125
|
+
tline.gsub!(/\\\"/,"'")
|
126
|
+
OutdentExp.each do |re|
|
127
|
+
if(tline =~ re)
|
128
|
+
tab -= 1
|
129
|
+
break
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
if (multiLine_array.length > 0)
|
134
|
+
multiLine_array.each do |ml|
|
135
|
+
output << rb_add_line(ml,tab)
|
136
|
+
end
|
137
|
+
multiLine_array.clear
|
138
|
+
multiLine_str = ""
|
139
|
+
else
|
140
|
+
output << rb_add_line(line,tab)
|
141
|
+
end
|
142
|
+
if(!comment_line)
|
143
|
+
IndentExp.each do |re|
|
144
|
+
if(tline =~ re && !(tline =~ /\s+end\s*$/))
|
145
|
+
tab += 1
|
146
|
+
break
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
if(tline =~ /^=end/)
|
152
|
+
comment_block = false
|
153
|
+
end
|
154
|
+
end
|
155
|
+
error = (tab != 0)
|
156
|
+
STDERR.puts "Error: indent/outdent mismatch: #{tab}." if error
|
157
|
+
return output.join("\n") + "\n",error
|
158
|
+
end # beautify_string
|
159
|
+
|
160
|
+
def self.beautify_file(path)
|
161
|
+
error = false
|
162
|
+
if(path == '-') # stdin source
|
163
|
+
source = STDIN.read
|
164
|
+
dest,error = beautify_string(source,"stdin")
|
165
|
+
print dest
|
166
|
+
else # named file source
|
167
|
+
source = File.read(path)
|
168
|
+
dest,error = beautify_string(source,path)
|
169
|
+
if(source != dest)
|
170
|
+
# make a backup copy
|
171
|
+
File.open(path + "~","w") { |f| f.write(source) }
|
172
|
+
# overwrite the original
|
173
|
+
File.open(path,"w") { |f| f.write(dest) }
|
174
|
+
end
|
175
|
+
end
|
176
|
+
return error
|
177
|
+
end # beautify_file
|
178
|
+
|
179
|
+
end # module Bbcode
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bbcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- eiffel qiu
|
@@ -28,9 +28,9 @@ files:
|
|
28
28
|
- lib/bbcode.rb
|
29
29
|
- Manifest
|
30
30
|
- LICENSE
|
31
|
-
- Rakefile
|
32
31
|
- TODO
|
33
32
|
- README
|
33
|
+
- Rakefile
|
34
34
|
- bbcode.gemspec
|
35
35
|
has_rdoc: true
|
36
36
|
homepage: http://www.likenote.com
|