fancy_writer 1.0.0
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.
- checksums.yaml +15 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +165 -0
- data/README.md +306 -0
- data/Rakefile +1 -0
- data/fancy_writer.gemspec +24 -0
- data/lib/fancy_writer.rb +198 -0
- data/lib/fancy_writer/version.rb +26 -0
- data/spec/fancy_writer/fancy_writer_spec.rb +144 -0
- data/spec/spec_helper.rb +27 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OTIxMzllODc0MzhhMzM0M2Y0YWI0ZDFiYzAzMmQyZWI5NjhlMDQ1Ng==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YmM5NjZjMWNhY2ZhMzJhNWQxZjQwMjI4OTkwZjg0ZmQ0ZGZjZGY5YQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZGZiMTM3NDk4MzkxMTYzNGQyOTBmMzkzMzkwODcyNGFlMmM0OTY4ZTc4MWFl
|
10
|
+
NmMzY2VhZjQ5NjVhYjEyNjg1ZmYxYmM3Yjk2YzE2OGQ1NzVmNWM3ZTc5NmEx
|
11
|
+
ZGE3MWJjYzk4YjI4YmFjZjVjMDUzYTA2NjRjYzIzNTA1Mzg5MWI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YjU1MjIzNjE0ZTgwYjM5ZjAxMGIyODM5MjRjNmQzMGNhYmNiYjQ0Yzg2NmUy
|
14
|
+
OTI3MjQyMjMyOGVlNjg3MWE1OWZlOGU3MDY5MTM1OTkzY2MyYzUxYzg5ODRl
|
15
|
+
ODllN2Q4NDEwMjBkNTIxMjg5NTFhMzJkMTA3MWZlZmJlYTg4MTA=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.md
ADDED
@@ -0,0 +1,306 @@
|
|
1
|
+
# FancyWriter
|
2
|
+
|
3
|
+
FancyWriter is a wrapper around an IO object that allows you to augment text
|
4
|
+
blocks with whitespace indentation and comment symbols, and to format simple
|
5
|
+
CSV data series. It uses a simple DSL for defining indentations, comment
|
6
|
+
sections, and rules for symbol-separated value lists out of Enumerables.
|
7
|
+
|
8
|
+
This gem originated from the need to create files in a simple plain-text
|
9
|
+
format in which indentation is important: The TextGrid file format of Praat,
|
10
|
+
a software used for linguistic and phonetic analyses
|
11
|
+
(see [http://www.fon.hum.uva.nl/praat/](http://www.fon.hum.uva.nl/praat/)).
|
12
|
+
|
13
|
+
After creating the code for indentation, I added the other methods, since
|
14
|
+
they could easily benefit from the code that was already produced.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
gem 'fancy_writer'
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install fancy_writer
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
### Basic usage and writing lines
|
33
|
+
|
34
|
+
Given an IO object that is suitable for writing (such as a file or stdout),
|
35
|
+
FancyWriter can use this IO object for creating formatted output:
|
36
|
+
|
37
|
+
```` Ruby
|
38
|
+
io = File.open('/foo/bar', 'w') # or any other writable IO
|
39
|
+
FancyWriter::FancyIO.new(io) do
|
40
|
+
w 'Hello World!'
|
41
|
+
end
|
42
|
+
````
|
43
|
+
|
44
|
+
produces
|
45
|
+
|
46
|
+
````
|
47
|
+
Hello World!
|
48
|
+
````
|
49
|
+
|
50
|
+
The methods `write`, `w`, and `line` can be used to your liking to create
|
51
|
+
plain output. You can also pass multiple lines at once to such a method.
|
52
|
+
Newlines are added automatically between each line. So,
|
53
|
+
|
54
|
+
```` Ruby
|
55
|
+
FancyWriter::FancyIO.new(io) do
|
56
|
+
w 'one', 'two', 'three'
|
57
|
+
end
|
58
|
+
````
|
59
|
+
|
60
|
+
produces
|
61
|
+
|
62
|
+
````
|
63
|
+
one
|
64
|
+
two
|
65
|
+
three
|
66
|
+
````
|
67
|
+
|
68
|
+
### Ading commented sections
|
69
|
+
|
70
|
+
You can mark a section of your document with comment symbols by wrapping the
|
71
|
+
section in a block given to the `comment` method (short version: `c`):
|
72
|
+
|
73
|
+
```` Ruby
|
74
|
+
FancyWriter::FancyIO.new(io) do
|
75
|
+
comment do
|
76
|
+
w 'This line is commented out.', 'This one, too.'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
````
|
80
|
+
|
81
|
+
````
|
82
|
+
# This line is commented out.
|
83
|
+
# This one, too.
|
84
|
+
````
|
85
|
+
|
86
|
+
As you can see, Ruby-style comments are used by default (including a single
|
87
|
+
trailing space to keep thind readable). To use another comment symbol, simply
|
88
|
+
pass it as a parameter to the method:
|
89
|
+
|
90
|
+
```` Ruby
|
91
|
+
FancyWriter::FancyIO.new(io) do
|
92
|
+
comment('//') do
|
93
|
+
w 'This is a C style comment.'
|
94
|
+
end
|
95
|
+
comment('%') do
|
96
|
+
w 'This is a LaTeX style comment.'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
````
|
100
|
+
|
101
|
+
````
|
102
|
+
// This is a C style comment.
|
103
|
+
% This is a LaTeX style comment.
|
104
|
+
````
|
105
|
+
|
106
|
+
You can suppress the space separator by passing `false` as a second parameter.
|
107
|
+
In this case, you must specify the comment symbol parameter in any case.
|
108
|
+
|
109
|
+
```` Ruby
|
110
|
+
FancyWriter::FancyIO.new(io) do
|
111
|
+
comment('#', false) do
|
112
|
+
w 'This line is commented out.'
|
113
|
+
end
|
114
|
+
end
|
115
|
+
````
|
116
|
+
|
117
|
+
````
|
118
|
+
#This line is commented out.
|
119
|
+
````
|
120
|
+
|
121
|
+
### Indentation
|
122
|
+
|
123
|
+
Similar to comment blocks you can mark sections that should be indented by a
|
124
|
+
certain amount of spaces (for tabs see below) with the `indent` method
|
125
|
+
(short version: `i`):
|
126
|
+
|
127
|
+
```` Ruby
|
128
|
+
FancyWriter::FancyIO.new(io) do
|
129
|
+
w 'one'
|
130
|
+
indent do
|
131
|
+
w 'two'
|
132
|
+
indent do
|
133
|
+
w 'three'
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
````
|
138
|
+
|
139
|
+
````
|
140
|
+
one
|
141
|
+
two
|
142
|
+
three
|
143
|
+
````
|
144
|
+
|
145
|
+
You can see in this example that indentation (and also commenting) can be
|
146
|
+
nested. Multiple indentations will be added / concatenated.
|
147
|
+
By default, indentation uses two spaces. You can specify another amount
|
148
|
+
of spaces by giving a number as a parameter.
|
149
|
+
If you prefer tabs, you can use the method `tab_indent` (short version: `t`)
|
150
|
+
– it works similar, but uses tabs instead of spaces and defaults to *one*
|
151
|
+
tab symbol instead of *two* spaces.
|
152
|
+
|
153
|
+
```` Ruby
|
154
|
+
FancyWriter::FancyIO.new(io) do
|
155
|
+
w 'one'
|
156
|
+
tab_indent do
|
157
|
+
w 'I'm indented with one tab symbol.'
|
158
|
+
indent(8) do
|
159
|
+
w 'Additionally, eight spaces.'
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
````
|
164
|
+
|
165
|
+
````
|
166
|
+
I'm indented with one tab symbol.
|
167
|
+
Additionally, eight spaces.
|
168
|
+
````
|
169
|
+
|
170
|
+
### Symbol-separated values
|
171
|
+
|
172
|
+
If you pass an Enumerable to `write_enum` (short version: `e`), the underlying
|
173
|
+
collection will be formatted as a symbol-separated list. By default, a comma
|
174
|
+
will be used to separate values, and the values themselves will be used as-is:
|
175
|
+
|
176
|
+
```` Ruby
|
177
|
+
FancyWriter::FancyIO.new(io) do
|
178
|
+
write_enum [1,2,3,4]
|
179
|
+
end
|
180
|
+
````
|
181
|
+
|
182
|
+
````
|
183
|
+
1,2,3,4
|
184
|
+
````
|
185
|
+
|
186
|
+
You can specify another separator and a symbol used for quoting the single values
|
187
|
+
by passing them to the writer object during initialization. So, in order to use
|
188
|
+
a semicolon and single quotes, you can write:
|
189
|
+
|
190
|
+
```` Ruby
|
191
|
+
FancyWriter::FancyIO.new(io, {enum_quote: "'", enum_separator: ";"}) do
|
192
|
+
write_enum [1,2,3,4]
|
193
|
+
end
|
194
|
+
````
|
195
|
+
|
196
|
+
````
|
197
|
+
'1';'2';'3';'4'
|
198
|
+
````
|
199
|
+
|
200
|
+
Note that also for this method, surrounding `comment` or indentation methods
|
201
|
+
are evaluated, meaning that also formatted number sequences will be indented
|
202
|
+
or commented out.
|
203
|
+
|
204
|
+
## An exhaustive example
|
205
|
+
|
206
|
+
This is a somewhat artifical example, but it contains most of the methods
|
207
|
+
in action.
|
208
|
+
|
209
|
+
```` Ruby
|
210
|
+
FancyWriter::FancyIO.new(io) do
|
211
|
+
comment '#' do
|
212
|
+
line 'This is an example file.'
|
213
|
+
line 'These comments explain the contents.'
|
214
|
+
end
|
215
|
+
line 'config:'
|
216
|
+
indent 4 do
|
217
|
+
line 'setting:'
|
218
|
+
indent 4 do
|
219
|
+
line 'some_key: some_value.'
|
220
|
+
comment do
|
221
|
+
line 'This is an inside comment.'
|
222
|
+
end
|
223
|
+
end
|
224
|
+
line 'data:'
|
225
|
+
indent 4 do
|
226
|
+
write_enum %w(1,2,3,4)
|
227
|
+
write_enum %w(5,6,7,8)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
````
|
232
|
+
|
233
|
+
````
|
234
|
+
# This is an example file.
|
235
|
+
# These comments explain the contents.
|
236
|
+
config:
|
237
|
+
setting:
|
238
|
+
some_key: some_value.
|
239
|
+
# This is an inside comment.
|
240
|
+
data:
|
241
|
+
1,2,3,4
|
242
|
+
5,6,7,8
|
243
|
+
````
|
244
|
+
|
245
|
+
|
246
|
+
## Issues
|
247
|
+
|
248
|
+
### Context and scope
|
249
|
+
|
250
|
+
It is not possible at the moment to access member variables
|
251
|
+
inside a `FancyIO.new` block. This is due to the way
|
252
|
+
`instance_eval` works (which is used internally to evaluate
|
253
|
+
the formatting blocks). If you need access to the context
|
254
|
+
of the calling object,
|
255
|
+
|
256
|
+
1. use accessor methods instead of variables,
|
257
|
+
2. add `caller: self` to the options passed during creation
|
258
|
+
of the fancy writer object:
|
259
|
+
|
260
|
+
```` Ruby
|
261
|
+
FancyWriter::FancyIO.new(io, {..., caller: self}) do
|
262
|
+
...
|
263
|
+
end
|
264
|
+
````
|
265
|
+
|
266
|
+
Then, the writer will know about the context of the object.
|
267
|
+
Internally, it will try to send method calls to the origin
|
268
|
+
object whenever `method_missing` reports a method call for
|
269
|
+
which the writer does not have a counterpart.
|
270
|
+
|
271
|
+
Found another problem? Is something not working? Contact me,
|
272
|
+
ideally by creating an issue on Github.
|
273
|
+
|
274
|
+
[https://github.com/pmenke/fancy_writer/issues](https://github.com/pmenke/fancy_writer/issues)
|
275
|
+
|
276
|
+
Please browse existing issues first, to avoid double postings.
|
277
|
+
|
278
|
+
## Contributing
|
279
|
+
|
280
|
+
Any ideas are welcome! Either
|
281
|
+
|
282
|
+
1. Fork it ( http://github.com/<my-github-username>/fancy_writer/fork )
|
283
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
284
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
285
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
286
|
+
5. Create new Pull Request
|
287
|
+
|
288
|
+
or create an issue on github:
|
289
|
+
|
290
|
+
[https://github.com/pmenke/fancy_writer/issues](https://github.com/pmenke/fancy_writer/issues)
|
291
|
+
|
292
|
+
Please browse existing issues first, to avoid double postings.
|
293
|
+
|
294
|
+
## Copyright and License
|
295
|
+
|
296
|
+
FancyWriter is free software: you can redistribute it and/or modify
|
297
|
+
it under the terms of the **GNU Lesser General Public License** as
|
298
|
+
published by the Free Software Foundation, either version 3 of
|
299
|
+
the License, or (at your option) any later version.
|
300
|
+
|
301
|
+
FancyWriter is distributed in the hope that it will be useful,
|
302
|
+
but **WITHOUT ANY WARRANTY**; without even the implied warranty of
|
303
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
304
|
+
GNU Lesser General Public License for more details.
|
305
|
+
|
306
|
+
See LICENSE.txt for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fancy_writer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fancy_writer"
|
8
|
+
spec.version = FancyWriter::VERSION
|
9
|
+
spec.authors = ["Peter Menke"]
|
10
|
+
spec.email = ["pmenke@googlemail.com"]
|
11
|
+
spec.summary = %q{A simple IO wrapper for easier comment blocks, indentation and CSV output.}
|
12
|
+
spec.description = %q{FancyWriter is a wrapper around an IO object that allows you to augment text blocks with whitespace indentation and comment symbols, and to format simple CSV data series. }
|
13
|
+
spec.homepage = "https://github.com/pmenke/fancy_writer"
|
14
|
+
spec.license = "GNU LGPL 3"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/fancy_writer.rb
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# This file is part of the fancy_writer gem.
|
3
|
+
# Copyright (c) 2014 Peter Menke.
|
4
|
+
# http://www.petermenke.de
|
5
|
+
#
|
6
|
+
# fancy_writer is free software: you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation, either
|
9
|
+
# version 3 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# fancy_writer is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with fancy_writer. If not, see
|
18
|
+
# <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
require "fancy_writer/version"
|
21
|
+
|
22
|
+
module FancyWriter
|
23
|
+
|
24
|
+
# FancyIO is the main class used for creating formatted writers.
|
25
|
+
# See README.md for an exhaustive usage description.
|
26
|
+
class FancyIO
|
27
|
+
|
28
|
+
# This hash holds some default options that are used when
|
29
|
+
# no other options are passed to the constructor.
|
30
|
+
DEFAULT_OPTIONS = {
|
31
|
+
enum_separator: ',',
|
32
|
+
enum_quote: ''
|
33
|
+
}
|
34
|
+
|
35
|
+
# The internal stream (IO) to write formatted output to.
|
36
|
+
attr_reader :stream
|
37
|
+
|
38
|
+
# The stack for strings to be prepended to each line.
|
39
|
+
attr_reader :prefix_stack
|
40
|
+
|
41
|
+
# The separator string to use for printing enumerables.
|
42
|
+
attr_reader :enum_separator
|
43
|
+
|
44
|
+
# The quote symbol to use for printing enumerables.
|
45
|
+
attr_reader :enum_quote
|
46
|
+
|
47
|
+
# An attribute holding the caller object.
|
48
|
+
attr_reader :caller
|
49
|
+
|
50
|
+
|
51
|
+
# Initializes a new fancy writer instance that wraps
|
52
|
+
# around the given io object +p_stream+.
|
53
|
+
# The block contains the code for writing to that
|
54
|
+
# stream.
|
55
|
+
# @param p_stream [IO] The stream to write into.
|
56
|
+
# @option opts [String] :enum_separator The symbol to
|
57
|
+
# be used to separate
|
58
|
+
# values in enums.
|
59
|
+
# @option opts [String] :enum_quote The symbol for quoting
|
60
|
+
# values in enums.
|
61
|
+
# @option opts [Object] :caller The calling object, useful
|
62
|
+
# when a method of it must
|
63
|
+
# be called inside FancyWriter's
|
64
|
+
# blocks.
|
65
|
+
# @param @block [Block] A block containing the
|
66
|
+
# fancy writer code.
|
67
|
+
def initialize(p_stream, opts={}, &block)
|
68
|
+
@stream = p_stream
|
69
|
+
@prefix_stack = []
|
70
|
+
effective_opts = DEFAULT_OPTIONS.merge(opts)
|
71
|
+
@enum_separator = effective_opts[:enum_separator]
|
72
|
+
@enum_quote = effective_opts[:enum_quote]
|
73
|
+
@caller = effective_opts[:caller]
|
74
|
+
if block_given?
|
75
|
+
instance_eval &block
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Adds a new string to the prepend stack. These strings
|
80
|
+
# will be added to each output line until the end of the
|
81
|
+
# block is reached.
|
82
|
+
# @param prepend_string [String] The string to be prepended
|
83
|
+
# to each line in the given
|
84
|
+
# block
|
85
|
+
def prepend(prepend_string=' ', &block)
|
86
|
+
@prefix_stack << prepend_string
|
87
|
+
yield # &block
|
88
|
+
@prefix_stack.pop
|
89
|
+
end
|
90
|
+
|
91
|
+
# Prepends each line in the given block with Ruby-style
|
92
|
+
# comments ("# "). Another comment character can be passed
|
93
|
+
# as a parameter.
|
94
|
+
# @param comment_string [String] The comment character(s) to
|
95
|
+
# be prepended to each line in
|
96
|
+
# the given block.
|
97
|
+
def comment(comment_string='#', space_sep=true, &block)
|
98
|
+
if block_given?
|
99
|
+
if space_sep
|
100
|
+
prepend("#{comment_string} ", &block)
|
101
|
+
else
|
102
|
+
prepend("#{comment_string}", &block)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Indents each line in the given block with spaces. The default
|
108
|
+
# amount is 2, another number of spaces can be given as a
|
109
|
+
# parameter.
|
110
|
+
# @param number [Integer] The number of spaces to be used for
|
111
|
+
# indentation in the given block.
|
112
|
+
def indent(number=2, &block)
|
113
|
+
prepend(' '*number, &block)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Indents each line in the given block with tabs. The default
|
117
|
+
# amount is 1, another number of tabs can be given as a
|
118
|
+
# parameter.
|
119
|
+
# @param number [Integer] The number of tabs to be used for
|
120
|
+
# indentation in the given block.
|
121
|
+
def tab_indent(number=1, &block)
|
122
|
+
prepend("\t"*number, &block)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Writes one or more lines to the output object, taking into
|
126
|
+
# account all strings on the prefix stack (such as comment
|
127
|
+
# symbols or indentations).
|
128
|
+
# @param line [Object] The object(s) to be formatted and written
|
129
|
+
# to the underlying writer.
|
130
|
+
def write(*line)
|
131
|
+
lines = lines==[] ? [''] : line
|
132
|
+
lines.each do |l|
|
133
|
+
write_line(l)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# This is a helper method for writing a single enumerable.
|
138
|
+
# @param p_enum [Enumerable] the enumerable to format and write.
|
139
|
+
def write_enum(p_enum)
|
140
|
+
write_line(p_enum)
|
141
|
+
end
|
142
|
+
|
143
|
+
# Adds an alias +w+ for the +write+ method.
|
144
|
+
alias :w :write
|
145
|
+
|
146
|
+
# Adds an alias +line+ for the +write+ method.
|
147
|
+
alias :line :write
|
148
|
+
|
149
|
+
# Adds an alias +c+ for the +comment+ method.
|
150
|
+
alias :c :comment
|
151
|
+
|
152
|
+
# Adds an alias +i+ for the +indent+ method.
|
153
|
+
alias :i :indent
|
154
|
+
|
155
|
+
# Adds an alias +t+ for the +tab_indent+ method.
|
156
|
+
alias :t :tab_indent
|
157
|
+
|
158
|
+
# Adds an alias +e+ for the +write_enum+ method.
|
159
|
+
alias :e :write_enum
|
160
|
+
|
161
|
+
|
162
|
+
private
|
163
|
+
|
164
|
+
# This method redirects failed message calls to the
|
165
|
+
# caller object, if present, in order to provide the
|
166
|
+
# user with method calls inside FancyWriter's blocks.
|
167
|
+
def method_missing(meth, *args, &block)
|
168
|
+
if caller.respond_to?(meth)
|
169
|
+
caller.send(meth, *args, &block)
|
170
|
+
else
|
171
|
+
super
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
# Internal method that performs the actual writing.
|
176
|
+
# @param line [Object] the line to be written.
|
177
|
+
def write_line(line)
|
178
|
+
if line.kind_of? String
|
179
|
+
formatted_line = line
|
180
|
+
elsif line.kind_of? Enumerable
|
181
|
+
formatted_line = join_enumerable(line)
|
182
|
+
else
|
183
|
+
formatted_line = line.to_s
|
184
|
+
end
|
185
|
+
stream << "%s%s%s" %[@prefix_stack.join(''),formatted_line,"\n"]
|
186
|
+
end
|
187
|
+
|
188
|
+
# Joins together an enumerable, using configuration options
|
189
|
+
# +enum_quote+ to quote the single values, and +enum_separator+ to
|
190
|
+
# glue them together.
|
191
|
+
# @param enum [Enumerable] the enumerable to format.
|
192
|
+
def join_enumerable(enum)
|
193
|
+
enum.collect{|e| "#{@enum_quote}#{e}#{@enum_quote}"}.join(@enum_separator)
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# This file is part of the fancy_writer gem.
|
3
|
+
# Copyright (c) 2014 Peter Menke.
|
4
|
+
# http://www.petermenke.de
|
5
|
+
#
|
6
|
+
# fancy_writer is free software: you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation, either
|
9
|
+
# version 3 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# fancy_writer is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with fancy_writer. If not, see
|
18
|
+
# <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
# The FancyWriter module contains a single class at the moment,
|
21
|
+
# which is used for creating formatted output.
|
22
|
+
# See README.md for usage details.
|
23
|
+
module FancyWriter
|
24
|
+
# The current version of the library.
|
25
|
+
VERSION = "1.0.0"
|
26
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# This file is part of the fancy_writer gem.
|
3
|
+
# Copyright (c) 2014 Peter Menke.
|
4
|
+
# http://www.petermenke.de
|
5
|
+
#
|
6
|
+
# fancy_writer is free software: you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation, either
|
9
|
+
# version 3 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# fancy_writer is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with fancy_writer. If not, see
|
18
|
+
# <http://www.gnu.org/licenses/>.
|
19
|
+
require 'spec_helper'
|
20
|
+
|
21
|
+
include FancyWriter
|
22
|
+
|
23
|
+
describe FancyWriter::FancyIO do
|
24
|
+
|
25
|
+
# set up an initial corpus representation from the example file
|
26
|
+
before(:each) do
|
27
|
+
@string = String.new
|
28
|
+
@enum = [1,2,3,4]
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'FancyWriter' do
|
32
|
+
|
33
|
+
context 'Basics' do
|
34
|
+
it 'writes lines without method as they are' do
|
35
|
+
@writer = FancyIO.new(@string) do
|
36
|
+
w "Foo"
|
37
|
+
end
|
38
|
+
@string.should eq "Foo\n"
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'also works with the aliases of write' do
|
42
|
+
@writer = FancyIO.new(@string) do
|
43
|
+
w "a"
|
44
|
+
write "b"
|
45
|
+
line "c"
|
46
|
+
end
|
47
|
+
@string.should eq "a\nb\nc\n"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'Comments' do
|
52
|
+
it "should write comments without options as '#'" do
|
53
|
+
@writer = FancyIO.new(@string) do
|
54
|
+
comment do
|
55
|
+
w "Foo"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
@string.should eq "# Foo\n"
|
59
|
+
end
|
60
|
+
it 'should write comments with options correctly' do
|
61
|
+
@writer = FancyIO.new(@string) do
|
62
|
+
comment '//' do
|
63
|
+
w "Foo"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
@string.should eq "// Foo\n"
|
67
|
+
end
|
68
|
+
it 'should write comments without indentation correctly' do
|
69
|
+
@writer = FancyIO.new(@string) do
|
70
|
+
comment '#', false do
|
71
|
+
w "Foo"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
@string.should eq "#Foo\n"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'Enumerables' do
|
79
|
+
|
80
|
+
it 'produces the correct output for default enum config' do
|
81
|
+
test_enum = @enum
|
82
|
+
@writer = FancyIO.new(@string) do
|
83
|
+
write_enum(test_enum)
|
84
|
+
end
|
85
|
+
@string.should eq "1,2,3,4\n"
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'produces the correct output for a custom separator' do
|
89
|
+
test_enum = @enum
|
90
|
+
separator = ';'
|
91
|
+
@writer = FancyIO.new(@string, enum_separator: separator) do
|
92
|
+
write_enum(test_enum)
|
93
|
+
end
|
94
|
+
@string.should eq "1;2;3;4\n"
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'produces the correct output for a custom quote symbol' do
|
98
|
+
test_enum = @enum
|
99
|
+
quote = "'"
|
100
|
+
@writer = FancyIO.new(@string, enum_quote: quote) do
|
101
|
+
write_enum(test_enum)
|
102
|
+
end
|
103
|
+
@string.should eq "'1','2','3','4'\n"
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'produces the correct output for custom quote symbol and separator' do
|
107
|
+
test_enum = @enum
|
108
|
+
quote = "'"
|
109
|
+
separator = ';'
|
110
|
+
@writer = FancyIO.new(@string, enum_quote: quote, enum_separator: separator) do
|
111
|
+
write_enum(test_enum)
|
112
|
+
end
|
113
|
+
@string.should eq "'1';'2';'3';'4'\n"
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
it 'exports a complex example correctly' do
|
120
|
+
@writer = FancyIO.new(@string) do
|
121
|
+
comment '# ' do
|
122
|
+
line 'This is an example file.'
|
123
|
+
line 'These comments explain the contents.'
|
124
|
+
end
|
125
|
+
line 'config:'
|
126
|
+
indent 4 do
|
127
|
+
line 'setting:'
|
128
|
+
indent 4 do
|
129
|
+
line 'some_key: some_value.'
|
130
|
+
comment do
|
131
|
+
line 'This is an inside comment.'
|
132
|
+
end
|
133
|
+
end
|
134
|
+
line 'data:'
|
135
|
+
indent 4 do
|
136
|
+
write_enum %w(1,2,3,4)
|
137
|
+
write_enum %w(5,6,7,8)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
puts @string
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# This file is part of the fancy_writer gem.
|
3
|
+
# Copyright (c) 2014 Peter Menke.
|
4
|
+
# http://www.petermenke.de
|
5
|
+
#
|
6
|
+
# fancy_writer is free software: you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation, either
|
9
|
+
# version 3 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# fancy_writer is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with fancy_writer. If not, see
|
18
|
+
# <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
require 'bundler/setup'
|
21
|
+
Bundler.setup
|
22
|
+
|
23
|
+
require 'fancy_writer' # and any other gems you need
|
24
|
+
|
25
|
+
RSpec.configure do |config|
|
26
|
+
# some (optional) config here
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fancy_writer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Menke
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-28 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: ! 'FancyWriter is a wrapper around an IO object that allows you to augment
|
56
|
+
text blocks with whitespace indentation and comment symbols, and to format simple
|
57
|
+
CSV data series. '
|
58
|
+
email:
|
59
|
+
- pmenke@googlemail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- fancy_writer.gemspec
|
70
|
+
- lib/fancy_writer.rb
|
71
|
+
- lib/fancy_writer/version.rb
|
72
|
+
- spec/fancy_writer/fancy_writer_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: https://github.com/pmenke/fancy_writer
|
75
|
+
licenses:
|
76
|
+
- GNU LGPL 3
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: A simple IO wrapper for easier comment blocks, indentation and CSV output.
|
98
|
+
test_files:
|
99
|
+
- spec/fancy_writer/fancy_writer_spec.rb
|
100
|
+
- spec/spec_helper.rb
|