ita2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/ita2.rb +88 -0
  2. data/test/ita2_test.rb +35 -0
  3. metadata +65 -0
@@ -0,0 +1,88 @@
1
+ # ita2.rb -- string to ita2 converter. Written by Oemuer Oezkir.
2
+ # Usage: require this file, it automatically adds 2 new methods to String:
3
+ # String#to_ita2 => returns the ita2 codes for the string in an array
4
+ # String#to_punchcode => returns the string as ita2 5 bit punchcodes
5
+ module Ita2
6
+ private
7
+
8
+ LTRS = "\x00e\na\ssiu\rdrjnfcktzlwhypqobg\x1bmxv\x1f".split(//)
9
+ FIGS = "\x003\n-\s'87\r\x054\a,!:(5+)2$6019?&\x1b./;\x1f".split(//)
10
+
11
+ # Ita2.pretty_string(text, on, off) => text in a "punched tape" style. on and off characters are optional.
12
+ def pretty_string(text, on = "#", off = "\s")
13
+ convert(text, on, off).map{ |a| a.to_s }.join("\n")
14
+ end
15
+
16
+ def convert(text, on = "#", off = "\s")
17
+ converted = codes(text)
18
+ a = Array.new(5) {Array.new(converted.size)}
19
+ converted.each_with_index do |char, i|
20
+ j = 0
21
+ sprintf("%5b", char).gsub(/\s/, "0").reverse.each_char do |bin|
22
+ a[j][i] = on if bin == "1"
23
+ a[j][i] = off if bin == "0"
24
+ j += 1
25
+ end
26
+ end
27
+ a
28
+ end
29
+
30
+ # Ita2.codes(text) => returns the ita2 codes for text in an array. Skips any unknown letters.
31
+ def codes(text)
32
+ figs = false
33
+ converted = []
34
+ text.downcase.each_char do |char|
35
+ # some characters are in both, figs and ltrs, so some checking is in order to avoid
36
+ # unneccessary figs/ltrs switches
37
+ unless figs # we are in LTRS mode
38
+ if is_ltrs?(char)
39
+ converted << is_ltrs?(char)
40
+ elsif is_figs?(char)
41
+ converted << 0x1b << is_figs?(char)
42
+ figs = true
43
+ end
44
+
45
+ else # we are in FIGS mode
46
+ if is_figs?(char)
47
+ converted << is_figs?(char)
48
+ elsif is_ltrs?(char)
49
+ converted << 0x1f << is_ltrs?(char)
50
+ figs = false
51
+ end
52
+ end
53
+ end
54
+
55
+ converted
56
+ end
57
+
58
+
59
+ def is_ltrs?(char)
60
+ if l = LTRS.index(char)
61
+ return l
62
+ else
63
+ false
64
+ end
65
+ end
66
+
67
+ def is_figs?(char)
68
+ if l = FIGS.index(char)
69
+ return l
70
+ else
71
+ false
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ # mixin => String
78
+ class String
79
+ include Ita2
80
+
81
+ def to_ita2
82
+ codes(self)
83
+ end
84
+
85
+ def to_punchcode(on = "#", off = "\s")
86
+ pretty_string(self, on, off)
87
+ end
88
+ end
@@ -0,0 +1,35 @@
1
+ require "test/unit"
2
+
3
+ require File.dirname(__FILE__) + "/../lib/ita2"
4
+
5
+
6
+ class TestITA2 < Test::Unit::TestCase
7
+ def setup
8
+ @text = "International Telegraphy Alphabet No.2"
9
+ end
10
+
11
+ def test_to_ita2_returns_an_array
12
+ assert @text.to_ita2.is_a? Array
13
+ end
14
+
15
+ def test_to_punchcode_only_returns_on_off_nextline
16
+ assert !(@text.to_punchcode =~ /[^# \n]/)
17
+ end
18
+
19
+ def test_codes_returns
20
+ assert_equal([1, 2], "e\n".to_ita2)
21
+ end
22
+
23
+ def test_is_ltrs_is_private
24
+ assert_raise(NoMethodError) { "a".is_ltrs? }
25
+ end
26
+
27
+ def test_to_ita2_figs
28
+ assert_equal([0x1b, 1, 2, 0x1f, 1], "3\ne".to_ita2)
29
+ end
30
+
31
+ def test_codes_skips_unkown_chars
32
+ assert_equal([1, 2, 1], "e\nüüe".to_ita2)
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ita2
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - !binary |
13
+ w5Ztw7xyIMOWemtpcg==
14
+
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-03-12 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Converts strings to ita2 codes or to a punchcode ascii graphic in 5bit ita2
24
+ email: darkoem@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/ita2.rb
33
+ - test/ita2_test.rb
34
+ has_rdoc: true
35
+ homepage: http://darkno.de
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.6
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: International Telegraphy Alphabet No.2 for Ruby
64
+ test_files: []
65
+