nineteen-eighty-two 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23550cc3b6c5889f1a37f56cc1c24770083ee75a
4
- data.tar.gz: d13ee480608b109d220ee6fcd281748c041c2f80
3
+ metadata.gz: c8adb225e0b27b75def30e8cbcf6cfaf4d93ec5b
4
+ data.tar.gz: 437eb48a19094395b0a30193727bbd36784d3ac2
5
5
  SHA512:
6
- metadata.gz: b57bb53b971aba3b8f5884726943b0df031e7914752e9cce65de97d28918808ab939e80465f2f118ff21aeb2caeb742e6677326b42b311d55e585fc5135a48ec
7
- data.tar.gz: 430b491bb1a20d84eff6c36b478c1a0d13af4f15bf370b33cfd2851674f4844ad6447df760a2ac9041ff5807e792ccc074f8d8b6ee1788fc55f6a01f4911ed27
6
+ metadata.gz: 1853d80dc42dfec864e4e9fde5803aebcd90842d027ec8d7ecb84eb44d8744b236422b0666255e0ee0053f5972f4986c14650bc962fc55e65f1f475a22c588a7
7
+ data.tar.gz: d319135567cc68edb911f1ce80a9e68afb329e41d8b2887b389f7087077c29cbee9a6098c7196e711f2ca504ddaa7b3ed9696946dd23d91160a0f39c1f142506
data/README.md CHANGED
@@ -5,8 +5,190 @@
5
5
  [![Gem Version](http://img.shields.io/gem/v/nineteen-eighty-two.svg?style=flat-square)](https://rubygems.org/gems/nineteen-eighty-two)
6
6
  [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://pikesley.mit-license.org)
7
7
 
8
- #1982
8
+ # 1982
9
9
 
10
- ##Sinclair Spectrum font as a Rubygem
10
+ ## Sinclair Spectrum font as a Rubygem
11
11
 
12
- See it in action [here](https://uncle-clive.herokuapp.com)
12
+ A massively over-engineered solution to a problem that I'm almost 100% certain does not exist - rendering text in the 1982 Sinclair Spectrum character set, in a variety of formats
13
+
14
+ git clone https://github.com/pikesley/nineteen-eighty-two
15
+ cd nineteen-eighty-two
16
+ bundle
17
+ rake
18
+
19
+ Or just
20
+
21
+ gem install nineteen-eighty-two
22
+
23
+ ## API
24
+
25
+ From the [specs](https://github.com/pikesley/nineteen-eighty-two/tree/master/spec/nineteen/eighty/two):
26
+
27
+ ### Just the data
28
+
29
+ #### Transform a string into an array-of-arrays of bits
30
+
31
+ require 'nineteen/eighty/two'
32
+
33
+ module Nineteen::Eighty::Two
34
+ describe Spectrum do
35
+ it 'returns an array of arrays' do
36
+ expect(described_class['ab']).to eq [
37
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
38
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
39
+ [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
40
+ [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
41
+ [0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
42
+ [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
43
+ [0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
44
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
45
+ ]
46
+ end
47
+ end
48
+ end
49
+
50
+ ### Specific output formats
51
+
52
+ #### HTML table
53
+
54
+ require 'nineteen/eighty/two'
55
+
56
+ module Nineteen::Eighty::Two
57
+ module Formats
58
+ describe HTMLTable do
59
+ it 'makes a table' do
60
+ expect(described_class.format 'a').to eq (
61
+ """<!-- a -->
62
+ <table class='sinclair'>
63
+ <tr><td class='off'> </td><td class='off'> </td><td class='off'> </td><td class='off'> </td><td class='off'> </td><td class='off'> </td><td class='off'> </td><td class='off'> </td></tr>
64
+ <tr><td class='off' colspan='8'> </td></tr>
65
+ <tr><td class='off' colspan='8'> </td></tr>
66
+ <tr><td class='off' colspan='2'> </td><td class='on' colspan='3'> </td><td class='off' colspan='3'> </td></tr>
67
+ <tr><td class='off' colspan='5'> </td><td class='on'> </td><td class='off' colspan='2'> </td></tr>
68
+ <tr><td class='off' colspan='2'> </td><td class='on' colspan='4'> </td><td class='off' colspan='2'> </td></tr>
69
+ <tr><td class='off'> </td><td class='on'> </td><td class='off' colspan='3'> </td><td class='on'> </td><td class='off' colspan='2'> </td></tr>
70
+ <tr><td class='off' colspan='2'> </td><td class='on' colspan='4'> </td><td class='off' colspan='2'> </td></tr>
71
+ <tr><td class='off' colspan='8'> </td></tr>
72
+ </table>"""
73
+ )
74
+ end
75
+ end
76
+ end
77
+
78
+ #### JSON
79
+
80
+ require 'nineteen/eighty/two'
81
+
82
+ module Nineteen::Eighty::Two
83
+ module Formats
84
+ describe JSON do
85
+ expect(described_class.format 'ab').to eq ({
86
+ :id => 'ab',
87
+ :data => [
88
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
89
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
90
+ [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
91
+ [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
92
+ [0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
93
+ [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
94
+ [0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
95
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
96
+ ]
97
+ }.to_json)
98
+ end
99
+ end
100
+ end
101
+
102
+ #### SVG
103
+
104
+ require 'nineteen/eighty/two'
105
+
106
+ module Nineteen::Eighty::Two
107
+ module Formats
108
+ describe SVG do
109
+ it 'returns an SVG' do
110
+ expect(described_class.format '/').to eq (
111
+ """<svg viewBox='0 0 8 8' xmlns='http://www.w3.org/2000/svg'>
112
+ <style type='text/css'>
113
+ <![CDATA[
114
+ rect.on {
115
+ fill: #000000;
116
+ }
117
+ ]]>
118
+ </style>
119
+ <g>
120
+ <rect x='6' y='2' width='1' height='1' class='on' />
121
+ <rect x='5' y='3' width='1' height='1' class='on' />
122
+ <rect x='4' y='4' width='1' height='1' class='on' />
123
+ <rect x='3' y='5' width='1' height='1' class='on' />
124
+ <rect x='2' y='6' width='1' height='1' class='on' />
125
+ </g>
126
+ </svg>
127
+ """)
128
+ end
129
+
130
+ it 'sets the colour' do
131
+ expect(described_class.format '/', {colour: '#fa8100'}).to match /fill: #fa8100;/
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+ #### Text
138
+
139
+ require 'nineteen/eighty/two'
140
+
141
+ module Nineteen::Eighty::Two
142
+ module Formats
143
+ describe Text do
144
+ it 'gives the correct string' do
145
+ expect(described_class.format 'ab').to eq (
146
+ """
147
+ 0000000000000000
148
+ 0000000000100000
149
+ 0011100000100000
150
+ 0000010000111100
151
+ 0011110000100010
152
+ 0100010000100010
153
+ 0011110000111100
154
+ 0000000000000000
155
+ """
156
+ ).strip
157
+ end
158
+
159
+ it 'returns characters other than zeroes and ones' do
160
+ expect(described_class.format 'Sam', {on: 'X', off: '.'}).to eq (
161
+ """
162
+ ........................
163
+ ..XXXX..................
164
+ .X........XXX....XX.X...
165
+ ..XXXX.......X...X.X.X..
166
+ ......X...XXXX...X.X.X..
167
+ .X....X..X...X...X.X.X..
168
+ ..XXXX....XXXX...X.X.X..
169
+ ........................
170
+ """
171
+ ).strip
172
+ end
173
+ end
174
+ end
175
+
176
+ ## FAQ
177
+
178
+ Your questions, answered
179
+
180
+ ### Why the stupid module structure?
181
+
182
+ I realise that `Nineteen::Eighty::Two::Formats::HTMLTable.format` is a ludicrously long method name, but it turns out if you do `bundle gem nineteen-eighty-two`, this module hierarchy is what you're presented with, and I actually kind of like it (I did try to call it `1982` but apparently that's not a kosher Gem name)
183
+
184
+ ### Why doesn't it output in _${other_format}_
185
+
186
+ If you can think of another format I could support, let me know and I'll have a go. I am also [open to PRs](https://github.com/pikesley/nineteen-eighty-two/pulls), of course
187
+
188
+ ### Where can I play with this?
189
+
190
+ I've wrapped this Gem in a RESTful API [over here](http://uncleclive.herokuapp.com/)
191
+
192
+ ### Why have you done this?
193
+
194
+ Have you met me? Also, because I'm [moderately obsessed with this font](https://www.youtube.com/watch?v=Qt_J0jNqtZg)
@@ -6,7 +6,7 @@ module Nineteen
6
6
  def self.format text
7
7
  lines = Spectrum[text]
8
8
 
9
- t = File.read File.open File.join templates_dir, 'table.eruby'
9
+ t = File.read File.open File.join Nineteen::Eighty::Two.templates_dir, 'html', 'table', 'table.eruby'
10
10
  context = {
11
11
  title: text,
12
12
  blanks: blanks(lines.first),
@@ -16,7 +16,7 @@ module Nineteen
16
16
  end
17
17
 
18
18
  def self.blanks lines
19
- t = File.read File.open File.join templates_dir, 'row.eruby'
19
+ t = File.read File.open File.join Nineteen::Eighty::Two.templates_dir, 'html', 'table', 'row.eruby'
20
20
  context = {
21
21
  cells: lines.map { |b| cell Span.new(0, 1) }.join
22
22
  }
@@ -24,7 +24,7 @@ module Nineteen
24
24
  end
25
25
 
26
26
  def self.cell span
27
- t = File.read File.open File.join templates_dir, 'cell.eruby'
27
+ t = File.read File.open File.join Nineteen::Eighty::Two.templates_dir, 'html', 'table', 'cell.eruby'
28
28
  context = {
29
29
  style: span.type == 1 ? 'on' : 'off',
30
30
  }
@@ -33,16 +33,12 @@ module Nineteen
33
33
  end
34
34
 
35
35
  def self.row list
36
- t = File.read File.open File.join templates_dir, 'row.eruby'
36
+ t = File.read File.open File.join Nineteen::Eighty::Two.templates_dir, 'html', 'table', 'row.eruby'
37
37
  context = {
38
38
  cells: Decorators::RunLengthEncoder.encode(list).map { |i| cell i }.join
39
39
  }
40
40
  Erubis::Eruby.new(t).evaluate(context).strip
41
41
  end
42
-
43
- def self.templates_dir
44
- File.join File.dirname(__FILE__),'..', 'templates', 'html', 'table'
45
- end
46
42
  end
47
43
  end
48
44
  end
@@ -4,10 +4,12 @@ module Nineteen
4
4
  module Formats
5
5
  class SVG
6
6
  def self.format text, options = {}
7
- t = File.read File.open File.join templates_dir, 'document.eruby'
7
+ text = [text] unless text.is_a? Array
8
+
9
+ t = File.read File.open File.join Nineteen::Eighty::Two.templates_dir, 'svg', 'document.eruby'
8
10
  context = {
9
- width: text.length * 8,
10
- height: 8,
11
+ width: text.longest * 8,
12
+ height: text.count * 8,
11
13
  fill_colour: options.fetch(:colour, '#000000'),
12
14
  body: body(text)
13
15
  }
@@ -15,11 +17,16 @@ module Nineteen
15
17
  end
16
18
 
17
19
  def self.body text
18
- rows = []
19
- Spectrum[text].each_with_index do |line, index|
20
- rows.push row(line, index)
20
+ s = ''
21
+ text.each_with_index do |line, count|
22
+ rows = []
23
+ Spectrum[line].each_with_index do |line, index|
24
+ rows.push row(line, index + (count * 8))
25
+ end
26
+ s << rows.join("\n")
21
27
  end
22
- rows.join("\n").strip
28
+
29
+ s.strip
23
30
  end
24
31
 
25
32
  def self.row list, index = 0
@@ -27,7 +34,7 @@ module Nineteen
27
34
  cells = []
28
35
  Decorators::RunLengthEncoder.encode(list).each do |item|
29
36
  if item.type == 1
30
- t = File.read File.open File.join templates_dir, 'cell.eruby'
37
+ t = File.read File.open File.join Nineteen::Eighty::Two.templates_dir, 'svg', 'cell.eruby'
31
38
  context = {
32
39
  x: x,
33
40
  y: index,
@@ -43,8 +50,8 @@ module Nineteen
43
50
  cells.join("\n").strip
44
51
  end
45
52
 
46
- def self.templates_dir
47
- File.join File.dirname(__FILE__),'..', 'templates', 'svg'
53
+ def self.longest list
54
+ list.map { |i| i.length }.max
48
55
  end
49
56
  end
50
57
  end
@@ -1,7 +1,7 @@
1
1
  module Nineteen
2
2
  module Eighty
3
3
  module Two
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
6
6
  end
7
7
  end
@@ -12,16 +12,32 @@ require 'nineteen/eighty/two/formatters/json_formatter'
12
12
  require 'nineteen/eighty/two/formatters/html_table_formatter'
13
13
  require 'nineteen/eighty/two/formatters/svg_formatter'
14
14
 
15
- class Span < Hash
16
- def initialize type, width
17
- self[type] = width
18
- end
15
+ module Nineteen
16
+ module Eighty
17
+ module Two
18
+ class Span < Hash
19
+ def initialize type, width
20
+ self[type] = width
21
+ end
22
+
23
+ def type
24
+ self.keys.first
25
+ end
19
26
 
20
- def type
21
- self.keys.first
27
+ def width
28
+ self.values.first
29
+ end
30
+ end
31
+
32
+ def self.templates_dir
33
+ File.join File.dirname(__FILE__), 'two', 'templates'
34
+ end
35
+ end
22
36
  end
37
+ end
23
38
 
24
- def width
25
- self.values.first
39
+ class Array
40
+ def longest
41
+ self.map { |i| i.length }.max
26
42
  end
27
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nineteen-eighty-two
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - pikesley