clbustos-rtf 0.4.2 → 0.5.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.
- data/.gemtest +0 -0
- data/History.txt +53 -0
- data/{LICENSE → LICENSE.txt} +0 -0
- data/Manifest.txt +52 -0
- data/README.rdoc +60 -63
- data/README.txt +201 -0
- data/Rakefile +18 -34
- data/examples/example01.rb +5 -1
- data/examples/example02.rb +1 -0
- data/examples/example03.rb +1 -0
- data/examples/example03.rtf +0 -0
- data/examples/example04.rb +1 -0
- data/examples/rubyrtf.bmp +0 -0
- data/examples/rubyrtf.jpg +0 -0
- data/examples/rubyrtf.png +0 -0
- data/ifad-rtf.gemspec +120 -0
- data/lib/rtf.rb +1 -0
- data/lib/rtf/colour.rb +0 -0
- data/lib/rtf/converters.rb +5 -0
- data/lib/rtf/converters/html.rb +123 -0
- data/lib/rtf/font.rb +0 -0
- data/lib/rtf/information.rb +0 -0
- data/lib/rtf/node.rb +136 -109
- data/lib/rtf/paper.rb +0 -0
- data/lib/rtf/style.rb +0 -0
- data/test/fixtures/bitmap1.bmp +0 -0
- data/test/fixtures/bitmap2.bmp +0 -0
- data/test/fixtures/jpeg1.jpg +0 -0
- data/test/fixtures/jpeg2.jpg +0 -0
- data/test/fixtures/png1.png +0 -0
- data/test/fixtures/png2.png +0 -0
- data/test/{test_helper.rb → helper_tests.rb} +1 -0
- data/test/{character_style_test.rb → test_character_style.rb} +1 -1
- data/test/{colour_test.rb → test_colour.rb} +2 -2
- data/test/{colour_table_test.rb → test_colour_table.rb} +2 -2
- data/test/{command_node_test.rb → test_command_node.rb} +1 -1
- data/test/{container_node_test.rb → test_container_node.rb} +2 -1
- data/test/{document_test.rb → test_document.rb} +1 -1
- data/test/{document_style_test.rb → test_document_style.rb} +1 -1
- data/test/{font_test.rb → test_font.rb} +2 -2
- data/test/{font_table_test.rb → test_font_table.rb} +2 -2
- data/test/{footer_node_test.rb → test_footer_node.rb} +1 -1
- data/test/{header_node_test.rb → test_header_node.rb} +1 -1
- data/test/{image_node_test.rb → test_image_node.rb} +10 -3
- data/test/{information_test.rb → test_information.rb} +1 -1
- data/test/{node_test.rb → test_node.rb} +1 -1
- data/test/{paragraph_style_test.rb → test_paragraph_style.rb} +1 -1
- data/test/{style_test.rb → test_style.rb} +1 -1
- data/test/{table_cell_node_test.rb → test_table_cell_node.rb} +1 -1
- data/test/{table_node_test.rb → test_table_node.rb} +2 -1
- data/test/{table_row_node_test.rb → test_table_row_node.rb} +1 -1
- data/test/text_node_test.rb +35 -10
- metadata +137 -97
- data/CHANGES +0 -23
- data/VERSION.yml +0 -5
data/.gemtest
ADDED
File without changes
|
data/History.txt
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
=== 0.5.0 / 2012-01-05
|
2
|
+
|
3
|
+
* Added test for utf8. Test load implementation of rtf library on same path first [Claudio Bustos]
|
4
|
+
* Cleanup: remove executable bit [Marcello Barnaba]
|
5
|
+
* Converters: HTML: add H4 support [Marcello Barnaba]
|
6
|
+
* Converters: HTML: add tidy cleaning (and the tidy dependency of course) [Marcello Barnaba]
|
7
|
+
* Converters: HTML: carriage returns are not spaces in RTF [Marcello Barnaba]
|
8
|
+
* Converters: HTML: fix excess space bug [Marcello Barnaba]
|
9
|
+
* Converters: HTML: fixed a traversing bug [Marcello Barnaba]
|
10
|
+
* Converters: HTML: imported @panmind source previously published on gist:583061 [Marcello Barnaba]
|
11
|
+
* Converters: HTML: reduce headings font sizes - should be parametrized [Marcello Barnaba]
|
12
|
+
* Converters: HTML: rewrote without polluting nokogiri [Marcello Barnaba]
|
13
|
+
* Converters: HTML: whitespace [Marcello Barnaba]
|
14
|
+
* Converters: require them via require "rtf/converters" [Marcello Barnaba]
|
15
|
+
* Fix gemspec to reflect correct homepage [Chris O'Sullivan]
|
16
|
+
* Fixing rtf files to work with tempfiles opened from s3 [Dan Herrera]
|
17
|
+
* Gem: add nokogiri dependency (sucks, I know) [Marcello Barnaba]
|
18
|
+
* Gem: changed name and generated gemspec [Marcello Barnaba]
|
19
|
+
* Gem: update authors list [Marcello Barnaba]
|
20
|
+
* Rename README to have rdoc extension so it looks pretty on github [Chris O'Sullivan]
|
21
|
+
* Some pictures are not properly scaled without \picwgoal and \pichgoal [Marcin Urbanski]
|
22
|
+
* Update readme to reflect correct copyright [Chris O'Sullivan]
|
23
|
+
* Updated CHANGES with new version details [Chris O'Sullivan]
|
24
|
+
* Updated README; Use name-spaced class invocations instead of including RTF module; Open file with 'w'I thought adding the 'w' to your `File.open` was a bit [Jay Hayes]e doing it exactly as you've shown will
|
25
|
+
* Updated test file names [clbustos]
|
26
|
+
* add '\lastrow' tag to the lastrow. It's a fix for iOS. [Samuel Mullen]
|
27
|
+
* block syntax for ImageNode#open_file [Cygnus]
|
28
|
+
* improve speed and memory use of ImageNode#to_rtf [Cygnus]
|
29
|
+
* refactor to close and open ImageNode file only when needed [Cygnus]
|
30
|
+
* refactor to use a Constant for ImageNode dimensions reading rather than passing values around [Cygnus]
|
31
|
+
* usually fail since in all likelihood the user doesn't have an empty file by that name in their current dir. [Jay Hayes]
|
32
|
+
|
33
|
+
=== 0.4.2
|
34
|
+
* Fix bug causing TextNode.to_rtf to return nil under 1.8.7[Jason Langenauer]
|
35
|
+
* Tests run under Ruby 1.9.2 and later [clbustos]
|
36
|
+
=== 0.4.1
|
37
|
+
* Links: implemented hyperlinks[Marcello Barnaba]
|
38
|
+
* Lists: add a newline before every new list[Marcello Barnaba]
|
39
|
+
=== 0.4.0
|
40
|
+
* Lists: Decimal: quick&dirty implementation of sequential numbering[Marcello Barnaba]
|
41
|
+
* Lists: Decimal: fixed marker name generation: it must contain the dot[Marcello Barnaba]
|
42
|
+
* Lists: removed last StringIO occurrences in new code[Marcello Barnaba]
|
43
|
+
* Lists: wrote minimal object model test[Marcello Barnaba]
|
44
|
+
* Lists: refactored and cleaned up the API a bit[Marcello Barnaba]
|
45
|
+
* Lists: wrote the API bridge between Nodes and Lists[Marcello Barnaba]
|
46
|
+
* Lists: initial implementation of listtable header generation[Marcello Barnaba]
|
47
|
+
* Node: cleaned up the generic to_rtf, added the .wrap option to the constructor[Marcello Barnaba]
|
48
|
+
* Node: stubbed out paragraph generation into a new ParagraphNode[Marcello Barnaba]
|
49
|
+
=== 0.3.1
|
50
|
+
* Added the #subscript helper to the CommandNode class [vjt]
|
51
|
+
=== 0.3.0
|
52
|
+
* Resolve problems on Ruby 1.9.1 with ImageNode#read_source. Peter uses IO#getc, which returns a String on Ruby 1.9.1, not a Integer, so I replaced with getbyte. [clbustos]
|
53
|
+
* Deleted duplicated definition of ImageNode#style= with attr_writer and def. [clbustos]
|
data/{LICENSE → LICENSE.txt}
RENAMED
File without changes
|
data/Manifest.txt
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
History.txt
|
2
|
+
LICENSE.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.rdoc
|
5
|
+
README.txt
|
6
|
+
Rakefile
|
7
|
+
examples/example01.rb
|
8
|
+
examples/example02.rb
|
9
|
+
examples/example03.rb
|
10
|
+
examples/example03.rtf
|
11
|
+
examples/example04.rb
|
12
|
+
examples/rubyrtf.bmp
|
13
|
+
examples/rubyrtf.jpg
|
14
|
+
examples/rubyrtf.png
|
15
|
+
ifad-rtf.gemspec
|
16
|
+
lib/rtf.rb
|
17
|
+
lib/rtf/colour.rb
|
18
|
+
lib/rtf/converters.rb
|
19
|
+
lib/rtf/converters/html.rb
|
20
|
+
lib/rtf/font.rb
|
21
|
+
lib/rtf/information.rb
|
22
|
+
lib/rtf/list.rb
|
23
|
+
lib/rtf/node.rb
|
24
|
+
lib/rtf/paper.rb
|
25
|
+
lib/rtf/style.rb
|
26
|
+
test/fixtures/bitmap1.bmp
|
27
|
+
test/fixtures/bitmap2.bmp
|
28
|
+
test/fixtures/jpeg1.jpg
|
29
|
+
test/fixtures/jpeg2.jpg
|
30
|
+
test/fixtures/png1.png
|
31
|
+
test/fixtures/png2.png
|
32
|
+
test/helper_tests.rb
|
33
|
+
test/test_character_style.rb
|
34
|
+
test/test_colour.rb
|
35
|
+
test/test_colour_table.rb
|
36
|
+
test/test_command_node.rb
|
37
|
+
test/test_container_node.rb
|
38
|
+
test/test_document.rb
|
39
|
+
test/test_document_style.rb
|
40
|
+
test/test_font.rb
|
41
|
+
test/test_font_table.rb
|
42
|
+
test/test_footer_node.rb
|
43
|
+
test/test_header_node.rb
|
44
|
+
test/test_image_node.rb
|
45
|
+
test/test_information.rb
|
46
|
+
test/test_node.rb
|
47
|
+
test/test_paragraph_style.rb
|
48
|
+
test/test_style.rb
|
49
|
+
test/test_table_cell_node.rb
|
50
|
+
test/test_table_node.rb
|
51
|
+
test/test_table_row_node.rb
|
52
|
+
test/text_node_test.rb
|
data/README.rdoc
CHANGED
@@ -1,15 +1,3 @@
|
|
1
|
-
== Purpose of this fork
|
2
|
-
|
3
|
-
* [DONE] Add support for ordered and unordered lists
|
4
|
-
* [DONE] Add support for hyperlinks
|
5
|
-
* [TODO] Write comprehensive tests for OL and UL
|
6
|
-
* [TODO] Clean up the API
|
7
|
-
* [TODO] DRY the code
|
8
|
-
|
9
|
-
Please, please, please: if you come along this library and would lend me an
|
10
|
-
hand to complete tests, please help. Thank you!
|
11
|
-
|
12
|
-
|
13
1
|
== Ruby Rich Text Format (RTF) Library
|
14
2
|
The RTF library provides a pure Ruby set of functionality that can be used to
|
15
3
|
programmatically create RTF documents. The main aim in developing this library
|
@@ -59,7 +47,6 @@ the code.
|
|
59
47
|
|
60
48
|
* Check into RTF image handling with a view to adding support for the insertion
|
61
49
|
of images into a Document.
|
62
|
-
|
63
50
|
* Provide a complete implementation for the headers and footers.
|
64
51
|
|
65
52
|
=== Some Examples
|
@@ -103,69 +90,74 @@ some code into the document. We want the code to appear in the document slightly
|
|
103
90
|
indented on the left hand side, in a non-proportionately space font and we want
|
104
91
|
it in bold text. Heres the code that shows how to do that...
|
105
92
|
|
106
|
-
01
|
107
|
-
02
|
108
|
-
03
|
109
|
-
04
|
110
|
-
05
|
111
|
-
06
|
112
|
-
07
|
113
|
-
08
|
114
|
-
09
|
115
|
-
10
|
116
|
-
11
|
117
|
-
12
|
118
|
-
13
|
119
|
-
14
|
120
|
-
15
|
121
|
-
16
|
122
|
-
17
|
123
|
-
18
|
124
|
-
19
|
125
|
-
20
|
93
|
+
01 styles = {}
|
94
|
+
02 styles['PS_CODE'] = ParagraphStyle.new
|
95
|
+
03 styles['CS_CODE'] = CharacterStyle.new
|
96
|
+
04
|
97
|
+
05 styles['PS_CODE'].left_indent = 200
|
98
|
+
06 styles['CS_CODE'].font = Font.new(Font::MODERN, 'Courier')
|
99
|
+
07 styles['CS_CODE'].bold = true
|
100
|
+
08
|
101
|
+
09 document.paragraph(styles['PS_CODE']) do |n1|
|
102
|
+
10 n1.apply(styles['CS_CODE']) do |n2|
|
103
|
+
11 n2 << "count = 0"
|
104
|
+
12 n2.line_break
|
105
|
+
13 n2 << "File.open('file.txt', 'r') do |file|"
|
106
|
+
14 n2.line_break
|
107
|
+
15 n2 << " file.each_line {|line| count += 1}"
|
108
|
+
16 n2.line_break
|
109
|
+
17 n2 << "end"
|
110
|
+
18 n2.line_break
|
111
|
+
19 n2 << "puts \"File contains \#{count} lines.\""
|
112
|
+
20 end
|
113
|
+
21 end
|
126
114
|
|
127
115
|
This is a much larger piece of code and covers a number of topics that need to
|
128
116
|
be addressed. I have included line numbers with code so that individual elements
|
129
|
-
can be referenced. Lines 1 to
|
117
|
+
can be referenced. Lines 1 to 3 are the first new elements. Here we create a
|
118
|
+
Hash and assign it to a variable called styles. On the next two lines we create
|
130
119
|
two style objects, one that can be applied to paragraphs and one that applies
|
131
120
|
to characters.
|
132
121
|
|
133
|
-
On
|
134
|
-
|
135
|
-
|
136
|
-
|
122
|
+
On lines 5 to 7 we update settings on the style objects we've created. We set
|
123
|
+
the left indentation value of the paragraph style to 200. The 200 in this case
|
124
|
+
refers to a measurement of twips. A twip is a type setting measurement that
|
125
|
+
equates to one twentieth of a point (about a fifty seventh of a millimetre or
|
126
|
+
one seventy second of an inch). This is the measurement scale used by RTF
|
127
|
+
documents so it is also employed in the library.
|
137
128
|
|
138
|
-
On lines
|
139
|
-
apply bold styling to the text.
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
using the one we prepared earlier.
|
129
|
+
On lines 6 and 7 we update the character style to use a courier font and to
|
130
|
+
apply bold styling to the text. On line 9 we start a new paragraph in our
|
131
|
+
document. This differs from our previous use of this method in that we specify
|
132
|
+
a style that will be applied to the paragraph created, the paragraph style we
|
133
|
+
had prepared earlier.
|
144
134
|
|
145
135
|
The block accompanying the paragraph method takes the single parameter that we
|
146
136
|
have seen previously. At this point its probably a good idea to point out that
|
147
137
|
the elements that make up an RTF document created with the library are all
|
148
|
-
stored as nodes in a tree.
|
149
|
-
|
150
|
-
Within the block, the +apply+ method applies a character style, and we're using
|
151
|
-
the one we prepared earlier.
|
138
|
+
stored as nodes in a tree. We've named the one passed to the paragraph method as
|
139
|
+
n1 to reflect this.
|
152
140
|
|
141
|
+
Within the block we've called a method on the paragraph node called apply. This
|
142
|
+
method applies a character style and we're using the one we prepared earlier.
|
153
143
|
Like the call to the paragraph method, the apply method is passed a block. All
|
154
|
-
text added to the blocks node will have the styling we've
|
155
|
-
font) applied to it.
|
144
|
+
text added to the blocks node (n2 in this case) will have the styling we've
|
145
|
+
defined (bold courier font) applied to it.
|
156
146
|
|
157
|
-
Note, that within the apply block we could still use the
|
158
|
-
|
159
|
-
|
160
|
-
becomes part of the document when the apply block completes.
|
147
|
+
Note, that within the apply block we could still use the n1 node. Any text we
|
148
|
+
added to this would appear in the paragraph but wouldn't be styled and, it
|
149
|
+
should be noted, will appear before any text added to n2 (as the n2 node only
|
150
|
+
becomes part of the document when the apply block completes).
|
161
151
|
|
162
|
-
Within the apply method block we add some lines of text to the
|
152
|
+
Within the apply method block we add some lines of text to the n2 node. Note
|
163
153
|
that, as this is all encompassed within the parapgraph block, all the text is
|
164
154
|
part of a single paragraph. To get each of the lines of code to appear on a
|
165
155
|
line of their own we have used the line_break method which inserts a carriage
|
166
156
|
return into the document. Note you should use this method to insert line breaks
|
167
157
|
rather than trying to add a string containing "\n". RTF is a text based standard
|
168
|
-
and won't treat "\n" as you're expecting.
|
158
|
+
and won't treat "\n" as you're expecting. You should note also that we've had to
|
159
|
+
escape the '#' in one of the code lines to stop Ruby considering it as an
|
160
|
+
interpolated value.
|
169
161
|
|
170
162
|
Okay, so we've seen have the basics of creating a document and adding elements
|
171
163
|
to that document. How do we get what we've created to a file. Well thats
|
@@ -173,7 +165,7 @@ actually quite straight forward. As was mentioned previously, RTF is a text
|
|
173
165
|
based standard so you simply generate the RTF and write it to a file. Heres an
|
174
166
|
example...
|
175
167
|
|
176
|
-
File.open('my_document.rtf') {|file| file.write(document.to_rtf)}
|
168
|
+
File.open('my_document.rtf', 'w') {|file| file.write(document.to_rtf)} # existing file by this name will be overwritten
|
177
169
|
|
178
170
|
There you have it. You've been given a quick overview of the basics of using
|
179
171
|
the library. For more information consult the HTML based API documentation that
|
@@ -181,11 +173,16 @@ is installed with the library gem (if you're reading this you may already be
|
|
181
173
|
looking at this documentation). Another source of information is the examples
|
182
174
|
directory, so check that out too.
|
183
175
|
|
184
|
-
CONTRIBUTORS
|
185
|
-
Claudio Bustos
|
186
|
-
Chris O'Sullivan
|
176
|
+
== CONTRIBUTORS
|
187
177
|
|
188
|
-
|
189
|
-
|
178
|
+
=== Individuals
|
179
|
+
* Marcello Barnaba
|
180
|
+
* Claudio Bustos
|
181
|
+
* Sam Mullen
|
182
|
+
* Chris O'Sullivan
|
190
183
|
|
191
|
-
|
184
|
+
===Organizations
|
185
|
+
* IFAD
|
186
|
+
|
187
|
+
COPYRIGHT
|
188
|
+
Copyright (c) 2009-2012 Peter Wood. See LICENSE for details.
|
data/README.txt
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
= rtf
|
2
|
+
|
3
|
+
* https://github.com/clbustos/rtf
|
4
|
+
|
5
|
+
== Purpose of this fork
|
6
|
+
|
7
|
+
* [DONE] Add support for ordered and unordered lists
|
8
|
+
* [DONE] Add support for hyperlinks
|
9
|
+
* [DONE] Support for UTF8 text
|
10
|
+
* [TODO] Write comprehensive tests for OL and UL
|
11
|
+
* [TODO] Clean up the API
|
12
|
+
* [TODO] DRY the code
|
13
|
+
* [TODO] Add an HTML-to-RTF converter
|
14
|
+
|
15
|
+
Please, please, please: if you come along this library and would lend me an
|
16
|
+
hand to complete tests, please help. Thank you!
|
17
|
+
|
18
|
+
== DESCRIPTION:
|
19
|
+
|
20
|
+
The RTF library provides a pure Ruby set of functionality that can be used to
|
21
|
+
programmatically create RTF documents. The main aim in developing this library
|
22
|
+
is to ease the complexity involved in assembling RTF documents although some
|
23
|
+
consideration has also been given to generating documents that are easier to
|
24
|
+
manually interpret too.
|
25
|
+
|
26
|
+
This library does not include functionality for parsing RTF documents. Nor does
|
27
|
+
the library claim to provide extensive coverage of the RTF specification. The
|
28
|
+
library was developed mostly with reference to the RTF Pocket Guide by Sean M.
|
29
|
+
Burke and some reference to the RTF specification itself. The introduction to
|
30
|
+
the RTF Pocket Guide states that the book covers version 1.7 of the RTF
|
31
|
+
specification so I guess, as this was the primary source, that this is the
|
32
|
+
version that the library covers too. Finally, no consideration was given to
|
33
|
+
making the functionality within the library thread safe.
|
34
|
+
|
35
|
+
In creating this library I set out to make it reasonably easy to create RTF
|
36
|
+
documents in code. Having said that I'm certain that it is possible to generate
|
37
|
+
invalid RTF documents with this library.
|
38
|
+
|
39
|
+
=== FEATURES/PROBLEMS:
|
40
|
+
|
41
|
+
I've tried to assemble a reasonably extensive (although I won't claim
|
42
|
+
exhaustive) unit test for the library. Despite that, this is an early release of
|
43
|
+
the code and I'm sure there will be issues with it given the complexity inherent
|
44
|
+
in RTF. The following are issues that I'm already aware of with the library...
|
45
|
+
|
46
|
+
* The implementation of headers and footers is incomplete. Stick to using
|
47
|
+
universal headers and footers for the time being.
|
48
|
+
|
49
|
+
* The library makes no attempt to split large chunks of text into separate
|
50
|
+
lines. This can make editing the resulting document in a text editor a little
|
51
|
+
awkward.
|
52
|
+
|
53
|
+
* RTF is, when it comes down to it, a Microsoft standard. As a result I have
|
54
|
+
taken Word and Wordpad to be definitive when it comes to displaying the RTF
|
55
|
+
documents generated by the library. I have tried things like Abiword and
|
56
|
+
Open Office with varying degrees of success. I'm certainly not saying that
|
57
|
+
this is due to deficencies in these particular applications as it could
|
58
|
+
equally be a lack of my understanding of the RTF standard or problems with my
|
59
|
+
implementation. Still, I think I should mention the point that I don't get
|
60
|
+
consistent appearance across all of the RTF viewers I've tried.
|
61
|
+
|
62
|
+
=== To do
|
63
|
+
This section details that areas where I feel the library is currently lacking
|
64
|
+
or incomplete. I hope to address the things detailed here in later releases of
|
65
|
+
the code.
|
66
|
+
|
67
|
+
* Check into RTF image handling with a view to adding support for the insertion
|
68
|
+
of images into a Document.
|
69
|
+
|
70
|
+
* Provide a complete implementation for the headers and footers.
|
71
|
+
|
72
|
+
=== Some Examples
|
73
|
+
Okay, so how is the library used. Well lets look at some examples to see if we
|
74
|
+
can cast a little light on the matter. The examples provided here assume that
|
75
|
+
you are already familiar with the Ruby language. So, for a start, consider...
|
76
|
+
|
77
|
+
require 'rubygems'
|
78
|
+
require 'rtf'
|
79
|
+
|
80
|
+
The first thing to look at here at the are the first two lines. The RTF library
|
81
|
+
is provided as a Ruby gem and these two lines load the libraries functionality
|
82
|
+
into the script. The third line of code includes the RTF module into the current
|
83
|
+
name space. This is a convenience mechanism that saves on specifically having
|
84
|
+
to refer to the module when accessing the RTF library. Next we want to create
|
85
|
+
an RTF document and that is done like this...
|
86
|
+
|
87
|
+
document = RTF::Document.new(RTF::Font.new(RTF::Font::ROMAN, 'Times New Roman'))
|
88
|
+
|
89
|
+
This line of code creates a new Document object, specifying that the default
|
90
|
+
font for the document will the the Times New Roman font. So we have a document,
|
91
|
+
what can we do with it. Well, lets add a short paragraph of text...
|
92
|
+
|
93
|
+
document.paragraph << "This is a short paragraph of text."
|
94
|
+
|
95
|
+
That's fine, but what if we wanted to extend that paragraph or we simply wanted
|
96
|
+
to add more text than we've added here? Well, the paragraph method accepts a
|
97
|
+
block to which it passes the actual paragraph object, so we could do something
|
98
|
+
like the following...
|
99
|
+
|
100
|
+
document.paragraph do |p|
|
101
|
+
p << "This is the first sentence in the paragraph. "
|
102
|
+
p << "This is the second sentence in the paragraph. "
|
103
|
+
p << "And this is the third sentence in the paragraph."
|
104
|
+
end
|
105
|
+
|
106
|
+
This is a common approach used by the RTF library, allowing a block to define
|
107
|
+
the scope of a document element. Lets see a more complicated example of this
|
108
|
+
in which we apply a number of document effects. Lets say that we want to insert
|
109
|
+
some code into the document. We want the code to appear in the document slightly
|
110
|
+
indented on the left hand side, in a non-proportionately space font and we want
|
111
|
+
it in bold text. Heres the code that shows how to do that...
|
112
|
+
|
113
|
+
01 code_para = ParagraphStyle.new
|
114
|
+
02 code_para.left_indent = 200
|
115
|
+
03
|
116
|
+
04 code_char = CharacterStyle.new
|
117
|
+
05 code_char.font = RTF::Font::MODERN
|
118
|
+
06 code_char.bold = true
|
119
|
+
07
|
120
|
+
08 document.paragraph(code_para) do |p|
|
121
|
+
09 p.apply(code_char) do |c|
|
122
|
+
10 c << "count = 0"
|
123
|
+
11 c.line_break
|
124
|
+
12 c << "File.open('file.txt', 'r') do |file|"
|
125
|
+
13 c.line_break
|
126
|
+
14 c << " file.each_line {|line| count += 1}"
|
127
|
+
15 c.line_break
|
128
|
+
16 c << "end"
|
129
|
+
17 c.line_break
|
130
|
+
18 c << "puts \"File contains \#{count} lines.\""
|
131
|
+
19 end
|
132
|
+
20 end
|
133
|
+
|
134
|
+
This is a much larger piece of code and covers a number of topics that need to
|
135
|
+
be addressed. I have included line numbers with code so that individual elements
|
136
|
+
can be referenced. Lines 1 to 6 are the first new elements. Here we create
|
137
|
+
two style objects, one that can be applied to paragraphs and one that applies
|
138
|
+
to characters.
|
139
|
+
|
140
|
+
On line 2 we set the left indentation value of the paragraph style to 200 *twips*.
|
141
|
+
A twip is a type setting measurement that equates to one twentieth of a point
|
142
|
+
(about a fifty seventh of a millimetre or one seventy second of an inch). This is
|
143
|
+
the measurement scale used by RTF documents.
|
144
|
+
|
145
|
+
On lines 5 and 6 we update the character style to use a courier font and to
|
146
|
+
apply bold styling to the text.
|
147
|
+
|
148
|
+
On line 8 we start a new paragraph in our document. This differs from our previous
|
149
|
+
use of this method in that we specify a style that will be applied to the paragraph
|
150
|
+
using the one we prepared earlier.
|
151
|
+
|
152
|
+
The block accompanying the paragraph method takes the single parameter that we
|
153
|
+
have seen previously. At this point its probably a good idea to point out that
|
154
|
+
the elements that make up an RTF document created with the library are all
|
155
|
+
stored as nodes in a tree.
|
156
|
+
|
157
|
+
Within the block, the +apply+ method applies a character style, and we're using
|
158
|
+
the one we prepared earlier.
|
159
|
+
|
160
|
+
Like the call to the paragraph method, the apply method is passed a block. All
|
161
|
+
text added to the blocks node will have the styling we've defined (bold courier
|
162
|
+
font) applied to it.
|
163
|
+
|
164
|
+
Note, that within the apply block we could still use the previous (p) node. Any
|
165
|
+
text we added to this would appear in the paragraph but wouldn't be styled and,
|
166
|
+
it should be noted, will appear before any text added to c, as the c node only
|
167
|
+
becomes part of the document when the apply block completes.
|
168
|
+
|
169
|
+
Within the apply method block we add some lines of text to the c node. Note
|
170
|
+
that, as this is all encompassed within the parapgraph block, all the text is
|
171
|
+
part of a single paragraph. To get each of the lines of code to appear on a
|
172
|
+
line of their own we have used the line_break method which inserts a carriage
|
173
|
+
return into the document. Note you should use this method to insert line breaks
|
174
|
+
rather than trying to add a string containing "\n". RTF is a text based standard
|
175
|
+
and won't treat "\n" as you're expecting.
|
176
|
+
|
177
|
+
Okay, so we've seen have the basics of creating a document and adding elements
|
178
|
+
to that document. How do we get what we've created to a file. Well thats
|
179
|
+
actually quite straight forward. As was mentioned previously, RTF is a text
|
180
|
+
based standard so you simply generate the RTF and write it to a file. Heres an
|
181
|
+
example...
|
182
|
+
|
183
|
+
File.open('my_document.rtf') {|file| file.write(document.to_rtf)}
|
184
|
+
|
185
|
+
There you have it. You've been given a quick overview of the basics of using
|
186
|
+
the library. For more information consult the HTML based API documentation that
|
187
|
+
is installed with the library gem (if you're reading this you may already be
|
188
|
+
looking at this documentation). Another source of information is the examples
|
189
|
+
directory, so check that out too.
|
190
|
+
|
191
|
+
== CONTRIBUTORS
|
192
|
+
|
193
|
+
* Marcello Barnaba
|
194
|
+
* Claudio Bustos
|
195
|
+
* Sam Mullen
|
196
|
+
* Chris O'Sullivan
|
197
|
+
|
198
|
+
|
199
|
+
== COPYRIGHT
|
200
|
+
|
201
|
+
Copyright (c) 2009-2012 Peter Wood. See LICENSE for details.
|