slt 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8d12a2efb90837bd77d7de37cdd729ff7c344717
4
+ data.tar.gz: b73a60fd3ae54505356632a3ec8e90b08462d80b
5
+ SHA512:
6
+ metadata.gz: d2e8331dee284936cf242f4fbf536df81050d1c5426b983b66c0c205d8269948a62af62a916fa32c94b1b005d1dcf8379a21d40ab9c2c7f51517b8a9f3de3452
7
+ data.tar.gz: fe2e070b8d95c2ca953a2f5346d91b2d57f7ec57aa15e1ee27c9b73843fbe36d84123274a560492fbb9996300a11c3134eee297e4128360abbc67f0c51a36edc
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,3 @@
1
+ [submodule "simple_layer_tag"]
2
+ path = simple_layer_tag
3
+ url = git@github.com:liveralmask/simple_layer_tag.git
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slt.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 liveralmask
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # SLT
2
+
3
+ simple layer tag to xml/html tag generator.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'slt'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install slt
18
+
19
+ ## Usage
20
+
21
+ require "slt"
22
+
23
+ slt = SimpleLayerTag.new
24
+
25
+ text = "html\n head\n body\n br"
26
+
27
+ puts slt.tag( text ) # TAG
28
+ =begin
29
+ <html>
30
+ <head></head>
31
+ <body>
32
+ <br></br></body></html>
33
+ =end
34
+
35
+ puts slt.html( text ) # HTML
36
+ =begin
37
+ <html>
38
+ <head></head>
39
+ <body>
40
+ <br></body></html>
41
+ =end
42
+
43
+ ## Try
44
+
45
+ [Try SimpleLayerTag](http://slt.heroku.com)
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Update Files."
4
+ task :update do
5
+ sh "git submodule foreach 'git pull'"
6
+
7
+ sh 'cp ./simple_layer_tag/simple_layer_tag.rb ./lib/slt/simple_layer_tag.rb'
8
+ end
9
+
10
+ desc "Build Gems."
11
+ task :build do
12
+ sh "gem build slt.gemspec"
13
+ end
@@ -0,0 +1,2 @@
1
+ require "slt/version"
2
+ require "slt/simple_layer_tag"
@@ -0,0 +1,259 @@
1
+ # Author:: liveralmask
2
+ # Copyright:: Copyright (c) 2013 liveralmask
3
+ # License:: MIT
4
+
5
+ class SimpleLayerTag
6
+ def initialize
7
+ @singleTagList = []
8
+
9
+ @singleHtmlTagList = [
10
+ "br",
11
+ "img",
12
+ "hr",
13
+ "meta",
14
+ "input",
15
+ "embed",
16
+ "area",
17
+ "base",
18
+ "col",
19
+ "keygen",
20
+ "link",
21
+ "param",
22
+ "source"
23
+ ]
24
+ end
25
+
26
+ private
27
+ KEYWORD_COPY = "[copy]"
28
+ KEYWORD_COMMENT = "#"
29
+
30
+ public
31
+ def tag( str, singleTagList = [] )
32
+ @singleTagList = singleTagList
33
+
34
+ # 処理しやすい形式に変換しておく
35
+ list = parse( str )
36
+
37
+ tagList = []
38
+ unfinishedTagList = []
39
+ list.each{|hash|
40
+ # 処理している階層より前の終了タグを出力
41
+ tagList.concat( extract_end_tag_list( unfinishedTagList, hash[ "level" ] ) )
42
+
43
+ case hash[ "keyword" ]
44
+ when KEYWORD_COPY # コピー
45
+ tagList.push( hash[ "value" ] )
46
+
47
+ # 階層の順序を維持するために必要
48
+ hash[ "keyword" ] = ""
49
+ unfinishedTagList.push( hash )
50
+ when KEYWORD_COMMENT # コメント
51
+ tagList.push( comment_tag( hash[ "value" ] ) )
52
+
53
+ # 階層の順序を維持するために必要
54
+ hash[ "keyword" ] = ""
55
+ unfinishedTagList.push( hash )
56
+ else # タグ
57
+ tag = start_tag( hash )
58
+ if tag.empty?
59
+ next if hash[ "value" ].strip.empty? # 空白行は無視
60
+
61
+ tagList.push( comment_tag( "Error => #{hash[ 'number' ]}:#{hash[ 'value' ]}" ) )
62
+ next
63
+ end
64
+
65
+ tagList.push( tag )
66
+ unfinishedTagList.push( hash )
67
+ end
68
+ }
69
+
70
+ # 残りの終了タグ
71
+ tagList.concat( extract_end_tag_list( unfinishedTagList ) )
72
+
73
+ tagText = "#{tagList.join.strip}"
74
+
75
+ return tagText
76
+ end
77
+
78
+ def html( str )
79
+ self.tag( str, @singleHtmlTagList )
80
+ end
81
+
82
+ private
83
+ KEYWORD_SINGLE_COPY = "-"
84
+ KEYWORD_MULTI_COPY_BEGIN = "<<<"
85
+ KEYWORD_MULTI_COPY_END = ">>>"
86
+
87
+ COPY_UNLIMITED = -1
88
+ COPY_END = 0
89
+
90
+ def parse( str )
91
+ list = []
92
+ copyHash = {
93
+ "copyableCount" => 0, # 0:コピーしない/-1:無制限/1以上:残りコピー可能回数
94
+ "level" => -1,
95
+ "number" => 0,
96
+ "list" => []
97
+ }
98
+ str.split( "\n" ).each_with_index{|line, i|
99
+ indent, keyword, value = parse_line( line )
100
+
101
+ hash = {
102
+ "level" => indent.length,
103
+ "keyword" => keyword,
104
+ "value" => value,
105
+ "number" => i + 1
106
+ }
107
+
108
+ # コピー情報
109
+ case hash[ "keyword" ]
110
+ when KEYWORD_SINGLE_COPY # 前後の空白なしの単一行コピー
111
+ if 0 == copyHash[ "copyableCount" ]
112
+ copyHash[ "copyableCount" ] = 1
113
+ copyHash[ "level" ] = hash[ "level" ]
114
+ copyHash[ "number" ] = hash[ "number" ]
115
+ end
116
+ when KEYWORD_MULTI_COPY_BEGIN # 前後の空白ありの複数行コピー開始
117
+ if 0 == copyHash[ "copyableCount" ]
118
+ copyHash[ "copyableCount" ] = COPY_UNLIMITED
119
+ copyHash[ "level" ] = hash[ "level" ]
120
+ copyHash[ "number" ] = hash[ "number" ]
121
+ next
122
+ end
123
+ when KEYWORD_MULTI_COPY_END # 前後の空白ありの複数行コピー終了
124
+ if hash[ "level" ] == copyHash[ "level" ]
125
+ copyHash[ "copyableCount" ] = COPY_END
126
+ copyHash[ "number" ] = "#{copyHash[ 'number' ]}-#{hash[ 'number' ]}"
127
+ end
128
+ when ""
129
+ next if 0 == copyHash[ "copyableCount" ] && hash[ "value" ].empty?
130
+ end
131
+
132
+ if 0 != copyHash[ "copyableCount" ]
133
+ # コピー中
134
+ hash[ "value" ] = line if copyHash[ "copyableCount" ] < 0
135
+
136
+ copyHash[ "list" ].push( hash[ "value" ] )
137
+
138
+ copyHash[ "copyableCount" ] -= 1 if 0 < copyHash[ "copyableCount" ]
139
+ end
140
+
141
+ if 0 == copyHash[ "copyableCount" ]
142
+ if 0 <= copyHash[ "level" ]
143
+ # コピー終了
144
+ hash = {
145
+ "level" => copyHash[ "level" ],
146
+ "keyword" => KEYWORD_COPY,
147
+ "value" => copyHash[ "list" ].join( "\n" ),
148
+ "number" => copyHash[ "number" ]
149
+ }
150
+
151
+ copyHash[ "level" ] = -1
152
+ copyHash[ "list" ] = []
153
+ end
154
+ else
155
+ next
156
+ end
157
+
158
+ list.push( hash )
159
+ }
160
+
161
+ list
162
+ end
163
+
164
+ RGX_INDENT = "[ \\t]*"
165
+ RGX_ALL = ".*"
166
+ RGX_KEYWORD = ".+?"
167
+ RGX_VALUE = ".*"
168
+ RGX_BLANK_SPACE = "\\s+"
169
+
170
+ def parse_line( line )
171
+ indent = ""
172
+ keyword = ""
173
+ value = ""
174
+
175
+ begin
176
+ # インデントの取得
177
+ str = line
178
+ break if /^(#{RGX_INDENT})(#{RGX_ALL})/ !~ str
179
+
180
+ indent = $~[1]
181
+ str = $~[2]
182
+
183
+ # シンプルレイヤータグでは、
184
+ # キーワードと値を空白で区切ったほうが美しい
185
+ if /^(#{RGX_KEYWORD})#{RGX_BLANK_SPACE}(#{RGX_VALUE})$/ =~ str
186
+ keyword = $~[1]
187
+ value = $~[2]
188
+ elsif /^(#{RGX_KEYWORD})$/ =~ str
189
+ keyword = $~[1]
190
+ else
191
+ value = line
192
+ end
193
+ end while false
194
+
195
+ return indent, keyword, value
196
+ end
197
+
198
+ def extract_end_tag_list( unfinishedTagList, base_level = 0 )
199
+ endTagList = []
200
+ while ! unfinishedTagList.empty?
201
+ hash = unfinishedTagList.pop
202
+
203
+ if hash[ "level" ] < base_level
204
+ # 元に戻す
205
+ unfinishedTagList.push( hash )
206
+ break
207
+ end
208
+
209
+ # 基準レベルより深い階層の終了タグ
210
+ endTagList.push( end_tag( hash ) )
211
+ end
212
+
213
+ endTagList
214
+ end
215
+
216
+ STR_INDENT = "\t"
217
+
218
+ def indent( level )
219
+ STR_INDENT * level
220
+ end
221
+
222
+ def comment_tag( str )
223
+ "\n<!-- #{str} -->"
224
+ end
225
+
226
+ def start_tag( hash )
227
+ # 開始タグ名が存在しないのはマズイので、スルー
228
+ return "" if hash[ "keyword" ].empty?
229
+
230
+ if /^!/ =~ hash[ "keyword" ]
231
+ add_single_tag( hash[ "keyword" ] )
232
+ elsif /^\?/ =~ hash[ "keyword" ]
233
+ add_single_tag( hash[ "keyword" ] )
234
+ hash[ "value" ] += "?"
235
+ end
236
+
237
+ tag = "<#{hash[ 'keyword' ]}>"
238
+ tag = "<#{hash[ 'keyword' ]} #{hash[ 'value' ]}>" if ! hash[ "value" ].empty?
239
+
240
+ "\n#{tag}"
241
+ end
242
+
243
+ def end_tag( hash )
244
+ return "" if @singleTagList.include?( hash[ "keyword" ].downcase )
245
+
246
+ # 終了タグが存在しない場合もある
247
+ return "" if hash[ "keyword" ].empty?
248
+
249
+ "</#{hash[ 'keyword' ]}>"
250
+ end
251
+
252
+ def add_single_tag( name )
253
+ name = name.downcase
254
+
255
+ return if @singleTagList.include?( name )
256
+
257
+ @singleTagList.push( name )
258
+ end
259
+ end
@@ -0,0 +1,3 @@
1
+ class SLT
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ require "slt"
2
+
3
+ slt = SimpleLayerTag.new
4
+
5
+ text = "html\n head\n body\n br"
6
+
7
+ puts slt.tag( text ) # TAG
8
+ =begin
9
+ <html>
10
+ <head></head>
11
+ <body>
12
+ <br></br></body></html>
13
+ =end
14
+
15
+ puts slt.html( text ) # HTML
16
+ =begin
17
+ <html>
18
+ <head></head>
19
+ <body>
20
+ <br></body></html>
21
+ =end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slt"
8
+ spec.version = SLT::VERSION
9
+ spec.authors = ["liveralmask"]
10
+ spec.email = ["liveralmask.lisk@gmail.com"]
11
+ spec.description = %q{simple layer tag to xml/html tag generator.}
12
+ spec.summary = %q{xml/html tag generator}
13
+ spec.homepage = "https://github.com/liveralmask/slt"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake", "~> 0"
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slt
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - liveralmask
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: simple layer tag to xml/html tag generator.
42
+ email:
43
+ - liveralmask.lisk@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".gitmodules"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/slt.rb
55
+ - lib/slt/simple_layer_tag.rb
56
+ - lib/slt/version.rb
57
+ - sample/sample.rb
58
+ - slt.gemspec
59
+ homepage: https://github.com/liveralmask/slt
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.0
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: xml/html tag generator
83
+ test_files: []