lingohub 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,130 @@
1
+ module Lingohub
2
+ module Models
3
+ require 'lingohub/models/resource'
4
+ require 'lingohub/models/collaborator'
5
+
6
+ class Project
7
+ def self.lazy_attr_accessor(*params)
8
+ params.each do |sym|
9
+ define_method(sym) do
10
+ unless defined? @fetched
11
+ fetch
12
+ end
13
+ self.instance_variable_get("@#{sym}")
14
+ end
15
+ define_method("#{sym}=") do |value|
16
+ self.instance_variable_set("@#{sym}", value)
17
+ end
18
+ end
19
+ end
20
+
21
+ lazy_attr_accessor(:title, :link, :weburl, :resources_url, :collaborators_url, :invitations_url, :translations_url, :search_url, :translations_count, :owner)
22
+
23
+ def initialize(client, link)
24
+ @client = client
25
+ @link = link
26
+ end
27
+
28
+ def create!(attributes={})
29
+ self.title = attributes[:title]
30
+ end
31
+
32
+ def destroy
33
+ @client.delete self.link
34
+ end
35
+
36
+ def update(attributes={})
37
+ @client.put self.link, {:project => attributes}
38
+ end
39
+
40
+ def invite_collaborator(email)
41
+ @client.post(self.invitations_url, :invitation => {:email => email})
42
+ end
43
+
44
+ def resources
45
+ unless defined? @resources
46
+ @resources = {}
47
+ response = @client.get(self.resources_url)
48
+ resource_hash = JSON.parse(response)
49
+ members = resource_hash["members"]
50
+ members.each do |member|
51
+ @resources[member["name"]] = Lingohub::Models::Resource.new(@client, member["links"][0]["href"])
52
+ end
53
+ end
54
+ @resources
55
+ end
56
+
57
+ def collaborators
58
+ unless defined? @collaborators
59
+ @collaborators = []
60
+ response = @client.get(self.collaborators_url)
61
+ resource_hash = JSON.parse(response)
62
+ members = resource_hash["members"]
63
+ members.each do |member|
64
+ link = member["link"]["href"] rescue ""
65
+ collaborator = Lingohub::Models::Collaborator.new(@client, link)
66
+ collaborator.email = member["email"]
67
+ collaborator.display_name = member["display_name"]
68
+ collaborator.roles = member["roles"]
69
+ @collaborators << collaborator
70
+ end
71
+ end
72
+ @collaborators
73
+ end
74
+
75
+ def pull_resource(directory, filename)
76
+ raise "Project does not contain that file." unless self.resources.has_key?(filename)
77
+ save_to_file(File.join(directory, filename), self.resources[filename].content)
78
+ end
79
+
80
+ def push_resource(path, locale)
81
+ raise "Path #{path} does not exists" unless File.exists?(path)
82
+ request = { :file => File.new(path, "rb") }
83
+ request.merge!({ :iso2_slug => locale }) if locale
84
+ @client.post(self.resources_url, request)
85
+ end
86
+
87
+ def pull_search_results(directory, filename, query, locale = nil)
88
+ parameters = {:filename => filename, :query => query}
89
+ parameters.merge!({:iso2_slug => locale}) unless locale.nil? or locale.strip.empty?
90
+
91
+ content = @client.get(search_url, parameters)
92
+ save_to_file(File.join(directory, filename), content)
93
+ end
94
+
95
+ private
96
+
97
+ def fetch
98
+ @fetched = true
99
+ response = @client.get @link
100
+ project_hash = JSON.parse(response)
101
+ links = project_hash["links"]
102
+ link = links[0]["href"]
103
+ weburl = links[1]["href"]
104
+ translations_url = links[2]["href"]
105
+ resources_url = links[3]["href"]
106
+ collaborators_url = links[4]["href"]
107
+ invitations_url = links[5]["href"]
108
+ search_url = links[6]["href"]
109
+ init_attributes :title => project_hash["title"], :link => link, :weburl => weburl,
110
+ :owner => project_hash["owner_email"], :translations_count => project_hash["translations_count"],
111
+ :translations_url => translations_url, :resources_url => resources_url,
112
+ :collaborators_url => collaborators_url, :invitations_url => invitations_url,
113
+ :search_url => search_url
114
+ end
115
+
116
+ def init_attributes(attributes)
117
+ attributes.each_pair do |key, value|
118
+ unless self.instance_variable_get("@#{key}")
119
+ self.send "#{key}=", value
120
+ end
121
+ end
122
+ end
123
+
124
+ def save_to_file(path, content)
125
+ File.open(path, 'w+') { |f| f.write(content) }
126
+ end
127
+
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,34 @@
1
+ module Lingohub
2
+ module Models
3
+ require 'lingohub/models/project'
4
+
5
+ class Projects
6
+
7
+ PROJECT_URL = '/projects'
8
+
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ def create(title)
14
+ @client.post(PROJECT_URL, :project => {:title => title})
15
+ end
16
+
17
+ def all
18
+ return @projects if defined? @projects
19
+ @projects = {}
20
+ response = JSON.parse @client.get(PROJECT_URL).to_s
21
+ response["members"].each do |member|
22
+ project = Lingohub::Models::Project.new(@client, member["links"][0]["href"])
23
+ @projects[member["title"]] = project
24
+ end
25
+ @projects
26
+ end
27
+
28
+ def [](project_title)
29
+ return all[project_title]
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ module Lingohub
2
+ module Models
3
+ class Resource
4
+ def initialize(client, link)
5
+ @client = client
6
+ @link = link
7
+ end
8
+
9
+ # Downloads the resource content
10
+ def content
11
+ @content ||= @client.get(@link)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ class User
2
+ # To change this template use File | Settings | File Templates.
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+
3
+ module Lingohub
4
+ class Railtie < ::Rails::Railtie
5
+ config.after_initialize do
6
+ Lingohub.environments ||= [:development]
7
+ Lingohub.protocol ||= "https"
8
+ Lingohub.host ||= "lingohub.com"
9
+ Lingohub.username ||= ":username"
10
+ Lingohub.project ||= ":project"
11
+ Dir[File.join(File.dirname(__FILE__), "../../patches/**/*.rb")].each { |f| require f }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module Lingohub
2
+ VERSION = '0.0.4'
3
+ API_VERSION = 'v1'
4
+ end
@@ -0,0 +1,66 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "stringex"
3
+
4
+ module I18n
5
+ class << self
6
+ alias :base_translate :translate
7
+ alias :base_localize :localize
8
+
9
+ def translate(key, options={ })
10
+ result = base_translate(key, options)
11
+ wysiwyt_enabled = options.has_key?(:wysiwyt) ? options.delete(:wysiwyt) : true
12
+ if wysiwyt_enabled && enabled?
13
+ result = wrap_with_wysiwyt(key, result)
14
+ result.html_safe
15
+ else
16
+ result
17
+ end
18
+ end
19
+
20
+ def localize(object, options = { })
21
+ result = base_localize(object, options)
22
+ wysiwyt_enabled = options.has_key?(:wysiwyt) ? options.delete(:wysiwyt) : true
23
+ if wysiwyt_enabled && enabled?
24
+ result = wrap_with_wysiwyt(object, result)
25
+ result.html_safe
26
+ else
27
+ result
28
+ end
29
+ end
30
+
31
+ alias :t :translate
32
+ alias :l :localize
33
+
34
+ private
35
+
36
+ def enabled?
37
+ Lingohub.environments.include?(current_env) rescue false
38
+ end
39
+
40
+ def current_env
41
+ defined?(Rails) ? Rails.env.to_sym : nil
42
+ end
43
+
44
+ def wrap_with_wysiwyt(translation_title, translation_phrase)
45
+ "<span data-translation_url=\"#{link_to_translation_phrase(translation_title)}\" >#{translation_phrase}</span>"
46
+ end
47
+
48
+ def link_to_translation_phrase(translation_title)
49
+ username = option_to_url(Lingohub.username)
50
+ project = option_to_url(Lingohub.project)
51
+ translation_title = translation_title.to_s.to_url
52
+
53
+ "#{Lingohub.protocol}://#{Lingohub.host}/#{username}/#{project}/translations/#{translation_title}/phrases/#{locale}"
54
+ end
55
+
56
+ def option_to_url(option)
57
+ if option.nil?
58
+ ""
59
+ elsif Lingohub.default_value?(option)
60
+ option
61
+ else
62
+ option.to_url
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,556 @@
1
+ # Copyright 2011 Keith Rarick
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ # See https://github.com/kr/okjson for updates.
22
+
23
+ require 'stringio'
24
+
25
+ # Some parts adapted from
26
+ # http://golang.org/src/pkg/json/decode.go and
27
+ # http://golang.org/src/pkg/utf8/utf8.go
28
+ module OkJson
29
+ extend self
30
+
31
+ class ParserError < ::StandardError; end
32
+
33
+ # Decodes a json document in string s and
34
+ # returns the corresponding ruby value.
35
+ # String s must be valid UTF-8. If you have
36
+ # a string in some other encoding, convert
37
+ # it first.
38
+ #
39
+ # String values in the resulting structure
40
+ # will be UTF-8.
41
+ def decode(s)
42
+ ts = lex(s)
43
+ v, ts = textparse(ts)
44
+ if ts.length > 0
45
+ raise OkJson::ParserError, 'trailing garbage'
46
+ end
47
+ v
48
+ end
49
+
50
+
51
+ # Parses a "json text" in the sense of RFC 4627.
52
+ # Returns the parsed value and any trailing tokens.
53
+ # Note: this is almost the same as valparse,
54
+ # except that it does not accept atomic values.
55
+ def textparse(ts)
56
+ if ts.length < 0
57
+ raise OkJson::ParserError, 'empty'
58
+ end
59
+
60
+ typ, _, val = ts[0]
61
+ case typ
62
+ when '{' then objparse(ts)
63
+ when '[' then arrparse(ts)
64
+ else
65
+ raise OkJson::ParserError, "unexpected #{val.inspect}"
66
+ end
67
+ end
68
+
69
+
70
+ # Parses a "value" in the sense of RFC 4627.
71
+ # Returns the parsed value and any trailing tokens.
72
+ def valparse(ts)
73
+ if ts.length < 0
74
+ raise OkJson::ParserError, 'empty'
75
+ end
76
+
77
+ typ, _, val = ts[0]
78
+ case typ
79
+ when '{' then objparse(ts)
80
+ when '[' then arrparse(ts)
81
+ when :val,:str then [val, ts[1..-1]]
82
+ else
83
+ raise OkJson::ParserError, "unexpected #{val.inspect}"
84
+ end
85
+ end
86
+
87
+
88
+ # Parses an "object" in the sense of RFC 4627.
89
+ # Returns the parsed value and any trailing tokens.
90
+ def objparse(ts)
91
+ ts = eat('{', ts)
92
+ obj = {}
93
+
94
+ if ts[0][0] == '}'
95
+ return obj, ts[1..-1]
96
+ end
97
+
98
+ k, v, ts = pairparse(ts)
99
+ obj[k] = v
100
+
101
+ if ts[0][0] == '}'
102
+ return obj, ts[1..-1]
103
+ end
104
+
105
+ loop do
106
+ ts = eat(',', ts)
107
+
108
+ k, v, ts = pairparse(ts)
109
+ obj[k] = v
110
+
111
+ if ts[0][0] == '}'
112
+ return obj, ts[1..-1]
113
+ end
114
+ end
115
+ end
116
+
117
+
118
+ # Parses a "member" in the sense of RFC 4627.
119
+ # Returns the parsed value and any trailing tokens.
120
+ def pairparse(ts)
121
+ (typ, _, k), ts = ts[0], ts[1..-1]
122
+ if typ != :str
123
+ raise OkJson::ParserError, "unexpected #{k.inspect}"
124
+ end
125
+ ts = eat(':', ts)
126
+ v, ts = valparse(ts)
127
+ [k, v, ts]
128
+ end
129
+
130
+
131
+ # Parses an "array" in the sense of RFC 4627.
132
+ # Returns the parsed value and any trailing tokens.
133
+ def arrparse(ts)
134
+ ts = eat('[', ts)
135
+ arr = []
136
+
137
+ if ts[0][0] == ']'
138
+ return arr, ts[1..-1]
139
+ end
140
+
141
+ v, ts = valparse(ts)
142
+ arr << v
143
+
144
+ if ts[0][0] == ']'
145
+ return arr, ts[1..-1]
146
+ end
147
+
148
+ loop do
149
+ ts = eat(',', ts)
150
+
151
+ v, ts = valparse(ts)
152
+ arr << v
153
+
154
+ if ts[0][0] == ']'
155
+ return arr, ts[1..-1]
156
+ end
157
+ end
158
+ end
159
+
160
+
161
+ def eat(typ, ts)
162
+ if ts[0][0] != typ
163
+ raise OkJson::ParserError, "expected #{typ} (got #{ts[0].inspect})"
164
+ end
165
+ ts[1..-1]
166
+ end
167
+
168
+
169
+ # Sans s and returns a list of json tokens,
170
+ # excluding white space (as defined in RFC 4627).
171
+ def lex(s)
172
+ ts = []
173
+ while s.length > 0
174
+ typ, lexeme, val = tok(s)
175
+ if typ == nil
176
+ raise OkJson::ParserError, "invalid character at #{s[0,10].inspect}"
177
+ end
178
+ if typ != :space
179
+ ts << [typ, lexeme, val]
180
+ end
181
+ s = s[lexeme.length..-1]
182
+ end
183
+ ts
184
+ end
185
+
186
+
187
+ # Scans the first token in s and
188
+ # returns a 3-element list, or nil
189
+ # if no such token exists.
190
+ #
191
+ # The first list element is one of
192
+ # '{', '}', ':', ',', '[', ']',
193
+ # :val, :str, and :space.
194
+ #
195
+ # The second element is the lexeme.
196
+ #
197
+ # The third element is the value of the
198
+ # token for :val and :str, otherwise
199
+ # it is the lexeme.
200
+ def tok(s)
201
+ case s[0]
202
+ when ?{ then ['{', s[0,1], s[0,1]]
203
+ when ?} then ['}', s[0,1], s[0,1]]
204
+ when ?: then [':', s[0,1], s[0,1]]
205
+ when ?, then [',', s[0,1], s[0,1]]
206
+ when ?[ then ['[', s[0,1], s[0,1]]
207
+ when ?] then [']', s[0,1], s[0,1]]
208
+ when ?n then nulltok(s)
209
+ when ?t then truetok(s)
210
+ when ?f then falsetok(s)
211
+ when ?" then strtok(s)
212
+ when Spc then [:space, s[0,1], s[0,1]]
213
+ when ?\t then [:space, s[0,1], s[0,1]]
214
+ when ?\n then [:space, s[0,1], s[0,1]]
215
+ when ?\r then [:space, s[0,1], s[0,1]]
216
+ else numtok(s)
217
+ end
218
+ end
219
+
220
+
221
+ def nulltok(s); s[0,4] == 'null' && [:val, 'null', nil] end
222
+ def truetok(s); s[0,4] == 'true' && [:val, 'true', true] end
223
+ def falsetok(s); s[0,5] == 'false' && [:val, 'false', false] end
224
+
225
+
226
+ def numtok(s)
227
+ m = /-?([1-9][0-9]+|[0-9])([.][0-9]+)?([eE][+-]?[0-9]+)?/.match(s)
228
+ if m && m.begin(0) == 0
229
+ if m[3] && !m[2]
230
+ [:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
231
+ elsif m[2]
232
+ [:val, m[0], Float(m[0])]
233
+ else
234
+ [:val, m[0], Integer(m[0])]
235
+ end
236
+ end
237
+ end
238
+
239
+
240
+ def strtok(s)
241
+ m = /"([^"\\]|\\["\/\\bfnrt]|\\u[0-9a-fA-F]{4})*"/.match(s)
242
+ if ! m
243
+ raise OkJson::ParserError, "invalid string literal at #{abbrev(s)}"
244
+ end
245
+ [:str, m[0], unquote(m[0])]
246
+ end
247
+
248
+
249
+ def abbrev(s)
250
+ t = s[0,10]
251
+ p = t['`']
252
+ t = t[0,p] if p
253
+ t = t + '...' if t.length < s.length
254
+ '`' + t + '`'
255
+ end
256
+
257
+
258
+ # Converts a quoted json string literal q into a UTF-8-encoded string.
259
+ # The rules are different than for Ruby, so we cannot use eval.
260
+ # Unquote will raise OkJson::ParserError, an error if q contains control characters.
261
+ def unquote(q)
262
+ q = q[1...-1]
263
+ a = q.dup # allocate a big enough string
264
+ r, w = 0, 0
265
+ while r < q.length
266
+ c = q[r]
267
+ case true
268
+ when c == ?\\
269
+ r += 1
270
+ if r >= q.length
271
+ raise OkJson::ParserError, "string literal ends with a \"\\\": \"#{q}\""
272
+ end
273
+
274
+ case q[r]
275
+ when ?",?\\,?/,?'
276
+ a[w] = q[r]
277
+ r += 1
278
+ w += 1
279
+ when ?b,?f,?n,?r,?t
280
+ a[w] = Unesc[q[r]]
281
+ r += 1
282
+ w += 1
283
+ when ?u
284
+ r += 1
285
+ uchar = begin
286
+ hexdec4(q[r,4])
287
+ rescue RuntimeError => e
288
+ raise OkJson::ParserError, "invalid escape sequence \\u#{q[r,4]}: #{e}"
289
+ end
290
+ r += 4
291
+ if surrogate? uchar
292
+ if q.length >= r+6
293
+ uchar1 = hexdec4(q[r+2,4])
294
+ uchar = subst(uchar, uchar1)
295
+ if uchar != Ucharerr
296
+ # A valid pair; consume.
297
+ r += 6
298
+ end
299
+ end
300
+ end
301
+ w += ucharenc(a, w, uchar)
302
+ else
303
+ raise OkJson::ParserError, "invalid escape char #{q[r]} in \"#{q}\""
304
+ end
305
+ when c == ?", c < Spc
306
+ raise OkJson::ParserError, "invalid character in string literal \"#{q}\""
307
+ else
308
+ # Copy anything else byte-for-byte.
309
+ # Valid UTF-8 will remain valid UTF-8.
310
+ # Invalid UTF-8 will remain invalid UTF-8.
311
+ a[w] = c
312
+ r += 1
313
+ w += 1
314
+ end
315
+ end
316
+ a[0,w]
317
+ end
318
+
319
+
320
+ def hexdec4(s)
321
+ if s.length != 4
322
+ raise OkJson::ParserError, 'short'
323
+ end
324
+ (nibble(s[0])<<12) | (nibble(s[1])<<8) | (nibble(s[2])<<4) | nibble(s[3])
325
+ end
326
+
327
+
328
+ def subst(u1, u2)
329
+ if Usurr1 <= u1 && u1 < Usurr2 && Usurr2 <= u2 && u2 < Usurr3
330
+ return ((u1-Usurr1)<<10) | (u2-Usurr2) + Usurrself
331
+ end
332
+ return Ucharerr
333
+ end
334
+
335
+
336
+ def unsubst(u)
337
+ if u < Usurrself || u > Umax || surrogate?(u)
338
+ return Ucharerr, Ucharerr
339
+ end
340
+ u -= Usurrself
341
+ [Usurr1 + ((u>>10)&0x3ff), Usurr2 + (u&0x3ff)]
342
+ end
343
+
344
+
345
+ def surrogate?(u)
346
+ Usurr1 <= u && u < Usurr3
347
+ end
348
+
349
+
350
+ def nibble(c)
351
+ case true
352
+ when ?0 <= c && c <= ?9 then c.ord - ?0.ord
353
+ when ?a <= c && c <= ?z then c.ord - ?a.ord + 10
354
+ when ?A <= c && c <= ?Z then c.ord - ?A.ord + 10
355
+ else
356
+ raise OkJson::ParserError, "invalid hex code #{c}"
357
+ end
358
+ end
359
+
360
+
361
+ # Encodes x into a json text. It may contain only
362
+ # Array, Hash, String, Numeric, true, false, nil.
363
+ # (Note, this list excludes Symbol.)
364
+ # Strings contained in x must be valid UTF-8.
365
+ # Values that cannot be represented, such as
366
+ # Nan, Infinity, Symbol, and Proc, are encoded
367
+ # as null, in accordance with ECMA-262, 5th ed.
368
+ def encode(x)
369
+ case x
370
+ when Hash then objenc(x)
371
+ when Array then arrenc(x)
372
+ when String then strenc(x)
373
+ when Numeric then numenc(x)
374
+ when Symbol then strenc(x.to_s)
375
+ when true then "true"
376
+ when false then "false"
377
+ when nil then "null"
378
+ else "null"
379
+ end
380
+ end
381
+
382
+
383
+ def objenc(x)
384
+ '{' + x.map{|k,v| encode(k) + ':' + encode(v)}.join(',') + '}'
385
+ end
386
+
387
+
388
+ def arrenc(a)
389
+ '[' + a.map{|x| encode(x)}.join(',') + ']'
390
+ end
391
+
392
+
393
+ def strenc(s)
394
+ t = StringIO.new
395
+ t.putc(?")
396
+ r = 0
397
+ while r < s.length
398
+ case s[r]
399
+ when ?" then t.print('\\"')
400
+ when ?\\ then t.print('\\\\')
401
+ when ?\b then t.print('\\b')
402
+ when ?\f then t.print('\\f')
403
+ when ?\n then t.print('\\n')
404
+ when ?\r then t.print('\\r')
405
+ when ?\t then t.print('\\t')
406
+ else
407
+ c = s[r]
408
+ case true
409
+ when Spc <= c && c <= ?~
410
+ t.putc(c)
411
+ when true
412
+ u, size = uchardec(s, r)
413
+ r += size - 1 # we add one more at the bottom of the loop
414
+ if u < 0x10000
415
+ t.print('\\u')
416
+ hexenc4(t, u)
417
+ else
418
+ u1, u2 = unsubst(u)
419
+ t.print('\\u')
420
+ hexenc4(t, u1)
421
+ t.print('\\u')
422
+ hexenc4(t, u2)
423
+ end
424
+ else
425
+ # invalid byte; skip it
426
+ end
427
+ end
428
+ r += 1
429
+ end
430
+ t.putc(?")
431
+ t.string
432
+ end
433
+
434
+
435
+ def hexenc4(t, u)
436
+ t.putc(Hex[(u>>12)&0xf])
437
+ t.putc(Hex[(u>>8)&0xf])
438
+ t.putc(Hex[(u>>4)&0xf])
439
+ t.putc(Hex[u&0xf])
440
+ end
441
+
442
+
443
+ def numenc(x)
444
+ if x.nan? || x.infinite?
445
+ return 'null'
446
+ end rescue nil
447
+ "#{x}"
448
+ end
449
+
450
+
451
+ # Decodes unicode character u from UTF-8
452
+ # bytes in string s at position i.
453
+ # Returns u and the number of bytes read.
454
+ def uchardec(s, i)
455
+ n = s.length - i
456
+ return [Ucharerr, 1] if n < 1
457
+
458
+ c0 = s[i].ord
459
+
460
+ # 1-byte, 7-bit sequence?
461
+ if c0 < Utagx
462
+ return [c0, 1]
463
+ end
464
+
465
+ # unexpected continuation byte?
466
+ return [Ucharerr, 1] if c0 < Utag2
467
+
468
+ # need continuation byte
469
+ return [Ucharerr, 1] if n < 2
470
+ c1 = s[i+1].ord
471
+ return [Ucharerr, 1] if c1 < Utagx || Utag2 <= c1
472
+
473
+ # 2-byte, 11-bit sequence?
474
+ if c0 < Utag3
475
+ u = (c0&Umask2)<<6 | (c1&Umaskx)
476
+ return [Ucharerr, 1] if u <= Uchar1max
477
+ return [u, 2]
478
+ end
479
+
480
+ # need second continuation byte
481
+ return [Ucharerr, 1] if n < 3
482
+ c2 = s[i+2].ord
483
+ return [Ucharerr, 1] if c2 < Utagx || Utag2 <= c2
484
+
485
+ # 3-byte, 16-bit sequence?
486
+ if c0 < Utag4
487
+ u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
488
+ return [Ucharerr, 1] if u <= Uchar2max
489
+ return [u, 3]
490
+ end
491
+
492
+ # need third continuation byte
493
+ return [Ucharerr, 1] if n < 4
494
+ c3 = s[i+3].ord
495
+ return [Ucharerr, 1] if c3 < Utagx || Utag2 <= c3
496
+
497
+ # 4-byte, 21-bit sequence?
498
+ if c0 < Utag5
499
+ u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
500
+ return [Ucharerr, 1] if u <= Uchar3max
501
+ return [u, 4]
502
+ end
503
+
504
+ return [Ucharerr, 1]
505
+ end
506
+
507
+
508
+ # Encodes unicode character u as UTF-8
509
+ # bytes in string a at position i.
510
+ # Returns the number of bytes written.
511
+ def ucharenc(a, i, u)
512
+ case true
513
+ when u <= Uchar1max
514
+ a[i] = (u & 0xff).chr
515
+ 1
516
+ when u <= Uchar2max
517
+ a[i+0] = (Utag2 | ((u>>6)&0xff)).chr
518
+ a[i+1] = (Utagx | (u&Umaskx)).chr
519
+ 2
520
+ when u <= Uchar3max
521
+ a[i+0] = (Utag3 | ((u>>12)&0xff)).chr
522
+ a[i+1] = (Utagx | ((u>>6)&Umaskx)).chr
523
+ a[i+2] = (Utagx | (u&Umaskx)).chr
524
+ 3
525
+ else
526
+ a[i+0] = (Utag4 | ((u>>18)&0xff)).chr
527
+ a[i+1] = (Utagx | ((u>>12)&Umaskx)).chr
528
+ a[i+2] = (Utagx | ((u>>6)&Umaskx)).chr
529
+ a[i+3] = (Utagx | (u&Umaskx)).chr
530
+ 4
531
+ end
532
+ end
533
+
534
+ Utagx = 0x80 # 1000 0000
535
+ Utag2 = 0xc0 # 1100 0000
536
+ Utag3 = 0xe0 # 1110 0000
537
+ Utag4 = 0xf0 # 1111 0000
538
+ Utag5 = 0xF8 # 1111 1000
539
+ Umaskx = 0x3f # 0011 1111
540
+ Umask2 = 0x1f # 0001 1111
541
+ Umask3 = 0x0f # 0000 1111
542
+ Umask4 = 0x07 # 0000 0111
543
+ Uchar1max = (1<<7) - 1
544
+ Uchar2max = (1<<11) - 1
545
+ Uchar3max = (1<<16) - 1
546
+ Ucharerr = 0xFFFD # unicode "replacement char"
547
+ Usurrself = 0x10000
548
+ Usurr1 = 0xd800
549
+ Usurr2 = 0xdc00
550
+ Usurr3 = 0xe000
551
+ Umax = 0x10ffff
552
+
553
+ Spc = ' '[0]
554
+ Unesc = {?b=>?\b, ?f=>?\f, ?n=>?\n, ?r=>?\r, ?t=>?\t}
555
+ Hex = '0123456789abcdef'
556
+ end