ita2 0.1.1 → 0.1.2
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/lib/ita2.rb +23 -18
- metadata +4 -4
data/lib/ita2.rb
CHANGED
@@ -1,33 +1,35 @@
|
|
1
1
|
# ita2.rb -- string to ita2 converter. Written by Oemuer Oezkir.
|
2
|
-
# Usage: require
|
2
|
+
# Usage: require "ita2", it automatically adds 3 new methods to String:
|
3
|
+
|
3
4
|
# String#to_ita2 => returns the ita2 codes for the string in an array
|
4
5
|
# String#to_punchcode => returns the string as ita2 5 bit punchcodes
|
6
|
+
# String#invalid_letters? => returns number of unconvertable letters or false
|
5
7
|
module Ita2
|
6
8
|
private
|
7
9
|
|
8
10
|
LTRS = "\x00e\na\ssiu\rdrjnfcktzlwhypqobg\x1bmxv\x1f".split(//)
|
9
11
|
FIGS = "\x003\n-\s'87\r\x054\a,!:(5+)2$6019?&\x1b./;\x1f".split(//)
|
10
|
-
|
12
|
+
|
11
13
|
# Ita2.pretty_string(text, on, off) => text in a "punched tape" style. on and off characters are optional.
|
12
14
|
def pretty_string(text, on = "#", off = "\s")
|
13
|
-
convert(text, on, off).map{ |
|
15
|
+
convert(text, on, off).map{ |str| str.to_s }.join("\n")
|
14
16
|
end
|
15
|
-
|
17
|
+
|
16
18
|
def convert(text, on = "#", off = "\s")
|
17
19
|
converted = codes(text)
|
18
|
-
|
20
|
+
matrix = Array.new(5) {Array.new(converted.size)}
|
19
21
|
converted.each_with_index do |char, i|
|
20
22
|
j = 0
|
21
23
|
sprintf("%5b", char).gsub(/\s/, "0").reverse.each_char do |bin|
|
22
|
-
|
23
|
-
|
24
|
+
matrix[j][i] = on if bin == "1"
|
25
|
+
matrix[j][i] = off if bin == "0"
|
24
26
|
j += 1
|
25
27
|
end
|
26
28
|
end
|
27
|
-
|
29
|
+
matrix
|
28
30
|
end
|
29
|
-
|
30
|
-
# Ita2.codes(text) => returns the ita2 codes for text in an array. Skips any unknown letters.
|
31
|
+
|
32
|
+
# Ita2.codes(text) => returns the ita2 codes for text in an array. Skips any unknown letters.
|
31
33
|
def codes(text)
|
32
34
|
figs = false
|
33
35
|
converted = []
|
@@ -41,7 +43,7 @@ module Ita2
|
|
41
43
|
converted << 0x1b << is_figs?(char)
|
42
44
|
figs = true
|
43
45
|
end
|
44
|
-
|
46
|
+
|
45
47
|
else # we are in FIGS mode
|
46
48
|
if is_figs?(char)
|
47
49
|
converted << is_figs?(char)
|
@@ -51,13 +53,13 @@ module Ita2
|
|
51
53
|
end
|
52
54
|
end
|
53
55
|
end
|
54
|
-
|
56
|
+
|
55
57
|
converted
|
56
58
|
end
|
57
|
-
|
58
|
-
|
59
|
+
|
60
|
+
|
59
61
|
def is_ltrs?(char)
|
60
|
-
if l = LTRS.index(char)
|
62
|
+
if ( l = LTRS.index(char) )
|
61
63
|
return l
|
62
64
|
else
|
63
65
|
false
|
@@ -65,27 +67,30 @@ module Ita2
|
|
65
67
|
end
|
66
68
|
|
67
69
|
def is_figs?(char)
|
68
|
-
if l = FIGS.index(char)
|
70
|
+
if ( l = FIGS.index(char) )
|
69
71
|
return l
|
70
72
|
else
|
71
73
|
false
|
72
74
|
end
|
73
75
|
end
|
74
|
-
|
76
|
+
|
75
77
|
end
|
76
78
|
|
77
79
|
# mixin => String
|
78
80
|
class String
|
79
81
|
include Ita2
|
80
82
|
|
83
|
+
# returns an array of letters converted to ITA2 codes
|
81
84
|
def to_ita2
|
82
85
|
codes(self)
|
83
86
|
end
|
84
|
-
|
87
|
+
|
88
|
+
# returns a string showing the ITA2 codes in a 5bit punchcode ASCII graphic
|
85
89
|
def to_punchcode(on = "#", off = "\s")
|
86
90
|
pretty_string(self, on, off)
|
87
91
|
end
|
88
92
|
|
93
|
+
# returns the number of letters that can't be converted to ITA2 or false if every letter can be converted
|
89
94
|
def invalid_letters?
|
90
95
|
invalid = 0
|
91
96
|
self.downcase.each_char do |char|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- !binary |
|
@@ -16,11 +16,11 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-03-
|
19
|
+
date: 2010-03-30 00:00:00 +02:00
|
20
20
|
default_executable:
|
21
21
|
dependencies: []
|
22
22
|
|
23
|
-
description: Converts strings to
|
23
|
+
description: Converts strings to ITA2 codes or to a punchcode ASCII graphic in 5-bit ITA2
|
24
24
|
email: darkoem@gmail.com
|
25
25
|
executables: []
|
26
26
|
|