clbustos-rtf 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +3 -0
- data/{README → README.rdoc} +45 -52
- data/Rakefile +1 -0
- data/VERSION.yml +1 -1
- data/lib/rtf/node.rb +42 -10
- data/test/command_node_test.rb +10 -0
- metadata +20 -20
data/CHANGES
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
== CHANGES
|
2
2
|
|
3
|
+
0.3.1
|
4
|
+
* Added the #subscript helper to the CommandNode class [vjt]
|
5
|
+
|
3
6
|
0.3.0
|
4
7
|
* 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]
|
5
8
|
* Deleted duplicated definition of ImageNode#style= with attr_writer and def. [clbustos]
|
data/{README → README.rdoc}
RENAMED
@@ -58,8 +58,6 @@ you are already familiar with the Ruby language. So, for a start, consider...
|
|
58
58
|
require 'rubygems'
|
59
59
|
require 'rtf'
|
60
60
|
|
61
|
-
include RTF
|
62
|
-
|
63
61
|
The first thing to look at here at the are the first two lines. The RTF library
|
64
62
|
is provided as a Ruby gem and these two lines load the libraries functionality
|
65
63
|
into the script. The third line of code includes the RTF module into the current
|
@@ -67,7 +65,7 @@ name space. This is a convenience mechanism that saves on specifically having
|
|
67
65
|
to refer to the module when accessing the RTF library. Next we want to create
|
68
66
|
an RTF document and that is done like this...
|
69
67
|
|
70
|
-
document = Document.new(Font
|
68
|
+
document = RTF::Document.new(RTF::Font::ROMAN)
|
71
69
|
|
72
70
|
This line of code creates a new Document object, specifying that the default
|
73
71
|
font for the document will the the Times New Roman font. So we have a document,
|
@@ -93,74 +91,69 @@ some code into the document. We want the code to appear in the document slightly
|
|
93
91
|
indented on the left hand side, in a non-proportionately space font and we want
|
94
92
|
it in bold text. Heres the code that shows how to do that...
|
95
93
|
|
96
|
-
01
|
97
|
-
02
|
98
|
-
03
|
99
|
-
04
|
100
|
-
05
|
101
|
-
06
|
102
|
-
07
|
103
|
-
08
|
104
|
-
09
|
105
|
-
10
|
106
|
-
11
|
107
|
-
12
|
108
|
-
13
|
109
|
-
14
|
110
|
-
15
|
111
|
-
16
|
112
|
-
17
|
113
|
-
18
|
114
|
-
19
|
115
|
-
20
|
116
|
-
21 end
|
94
|
+
01 code_para = ParagraphStyle.new
|
95
|
+
02 code_para.left_indent = 200
|
96
|
+
03
|
97
|
+
04 code_char = CharacterStyle.new
|
98
|
+
05 code_char.font = RTF::Font::MODERN
|
99
|
+
06 code_char.bold = true
|
100
|
+
07
|
101
|
+
08 document.paragraph(code_para) do |p|
|
102
|
+
09 p.apply(code_char) do |c|
|
103
|
+
10 c << "count = 0"
|
104
|
+
11 c.line_break
|
105
|
+
12 c << "File.open('file.txt', 'r') do |file|"
|
106
|
+
13 c.line_break
|
107
|
+
14 c << " file.each_line {|line| count += 1}"
|
108
|
+
15 c.line_break
|
109
|
+
16 c << "end"
|
110
|
+
17 c.line_break
|
111
|
+
18 c << "puts \"File contains \#{count} lines.\""
|
112
|
+
19 end
|
113
|
+
20 end
|
117
114
|
|
118
115
|
This is a much larger piece of code and covers a number of topics that need to
|
119
116
|
be addressed. I have included line numbers with code so that individual elements
|
120
|
-
can be referenced. Lines 1 to
|
121
|
-
Hash and assign it to a variable called styles. On the next two lines we create
|
117
|
+
can be referenced. Lines 1 to 6 are the first new elements. Here we create
|
122
118
|
two style objects, one that can be applied to paragraphs and one that applies
|
123
119
|
to characters.
|
124
120
|
|
125
|
-
On
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
121
|
+
On line 2 we set the left indentation value of the paragraph style to 200 *twips*.
|
122
|
+
A twip is a type setting measurement that equates to one twentieth of a point
|
123
|
+
(about a fifty seventh of a millimetre or one seventy second of an inch). This is
|
124
|
+
the measurement scale used by RTF documents.
|
125
|
+
|
126
|
+
On lines 5 and 6 we update the character style to use a courier font and to
|
127
|
+
apply bold styling to the text.
|
131
128
|
|
132
|
-
On
|
133
|
-
|
134
|
-
|
135
|
-
a style that will be applied to the paragraph created, the paragraph style we
|
136
|
-
had prepared earlier.
|
129
|
+
On line 8 we start a new paragraph in our document. This differs from our previous
|
130
|
+
use of this method in that we specify a style that will be applied to the paragraph
|
131
|
+
using the one we prepared earlier.
|
137
132
|
|
138
133
|
The block accompanying the paragraph method takes the single parameter that we
|
139
134
|
have seen previously. At this point its probably a good idea to point out that
|
140
135
|
the elements that make up an RTF document created with the library are all
|
141
|
-
stored as nodes in a tree.
|
142
|
-
|
136
|
+
stored as nodes in a tree.
|
137
|
+
|
138
|
+
Within the block, the +apply+ method applies a character style, and we're using
|
139
|
+
the one we prepared earlier.
|
143
140
|
|
144
|
-
Within the block we've called a method on the paragraph node called apply. This
|
145
|
-
method applies a character style and we're using the one we prepared earlier.
|
146
141
|
Like the call to the paragraph method, the apply method is passed a block. All
|
147
|
-
text added to the blocks node
|
148
|
-
|
142
|
+
text added to the blocks node will have the styling we've defined (bold courier
|
143
|
+
font) applied to it.
|
149
144
|
|
150
|
-
Note, that within the apply block we could still use the
|
151
|
-
added to this would appear in the paragraph but wouldn't be styled and,
|
152
|
-
should be noted, will appear before any text added to
|
153
|
-
becomes part of the document when the apply block completes
|
145
|
+
Note, that within the apply block we could still use the previous (p) node. Any
|
146
|
+
text we added to this would appear in the paragraph but wouldn't be styled and,
|
147
|
+
it should be noted, will appear before any text added to c, as the c node only
|
148
|
+
becomes part of the document when the apply block completes.
|
154
149
|
|
155
|
-
Within the apply method block we add some lines of text to the
|
150
|
+
Within the apply method block we add some lines of text to the c node. Note
|
156
151
|
that, as this is all encompassed within the parapgraph block, all the text is
|
157
152
|
part of a single paragraph. To get each of the lines of code to appear on a
|
158
153
|
line of their own we have used the line_break method which inserts a carriage
|
159
154
|
return into the document. Note you should use this method to insert line breaks
|
160
155
|
rather than trying to add a string containing "\n". RTF is a text based standard
|
161
|
-
and won't treat "\n" as you're expecting.
|
162
|
-
escape the '#' in one of the code lines to stop Ruby considering it as an
|
163
|
-
interpolated value.
|
156
|
+
and won't treat "\n" as you're expecting.
|
164
157
|
|
165
158
|
Okay, so we've seen have the basics of creating a document and adding elements
|
166
159
|
to that document. How do we get what we've created to a file. Well thats
|
@@ -183,4 +176,4 @@ Chris O'Sullivan
|
|
183
176
|
COPYRIGHT
|
184
177
|
=========
|
185
178
|
|
186
|
-
Copyright (c) 2009-2010 Peter Wood. See LICENSE for details.
|
179
|
+
Copyright (c) 2009-2010 Peter Wood. See LICENSE for details.
|
data/Rakefile
CHANGED
data/VERSION.yml
CHANGED
data/lib/rtf/node.rb
CHANGED
@@ -6,14 +6,10 @@ module RTF
|
|
6
6
|
# This class represents an element within an RTF document. The class provides
|
7
7
|
# a base class for more specific node types.
|
8
8
|
class Node
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
#
|
13
|
-
attr_writer :parent
|
14
|
-
|
15
|
-
|
16
|
-
# This is the constructor for the Node class.
|
9
|
+
# Node parent.
|
10
|
+
attr_accessor :parent
|
11
|
+
|
12
|
+
# Constructor for the Node class.
|
17
13
|
#
|
18
14
|
# ==== Parameters
|
19
15
|
# parent:: A reference to the Node that owns the new Node. May be nil
|
@@ -118,7 +114,13 @@ module RTF
|
|
118
114
|
# This method generates the RTF equivalent for a TextNode object. This
|
119
115
|
# method escapes any special sequences that appear in the text.
|
120
116
|
def to_rtf
|
121
|
-
|
117
|
+
rtf=(@text == nil ? '' : @text.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\"))
|
118
|
+
# Encode as Unicode.
|
119
|
+
if RUBY_VERSION>"1.9.0"
|
120
|
+
rtf.encode("UTF-16LE").each_codepoint.map {|cp|
|
121
|
+
cp < 128 ? cp.chr : "\\u#{cp}\\'3f"
|
122
|
+
}.join("")
|
123
|
+
end
|
122
124
|
end
|
123
125
|
end # End of the TextNode class.
|
124
126
|
|
@@ -392,6 +394,21 @@ module RTF
|
|
392
394
|
end
|
393
395
|
end
|
394
396
|
|
397
|
+
# This method provides a short cut means of creating a subscript command
|
398
|
+
# node. The method accepts a block that will be passed a single parameter
|
399
|
+
# which will be a reference to the subscript node created. After the
|
400
|
+
# block is complete the subscript node is appended to the end of the
|
401
|
+
# child nodes on the object that the method is call against.
|
402
|
+
def subscript
|
403
|
+
style = CharacterStyle.new
|
404
|
+
style.subscript = true
|
405
|
+
if block_given?
|
406
|
+
apply(style) {|node| yield node}
|
407
|
+
else
|
408
|
+
apply(style)
|
409
|
+
end
|
410
|
+
end
|
411
|
+
|
395
412
|
# This method provides a short cut means of creating a superscript command
|
396
413
|
# node. The method accepts a block that will be passed a single parameter
|
397
414
|
# which will be a reference to the superscript node created. After the
|
@@ -407,6 +424,21 @@ module RTF
|
|
407
424
|
end
|
408
425
|
end
|
409
426
|
|
427
|
+
# This method provides a short cut means of creating a strike command
|
428
|
+
# node. The method accepts a block that will be passed a single parameter
|
429
|
+
# which will be a reference to the strike node created. After the
|
430
|
+
# block is complete the strike node is appended to the end of the
|
431
|
+
# child nodes on the object that the method is call against.
|
432
|
+
def strike
|
433
|
+
style = CharacterStyle.new
|
434
|
+
style.strike = true
|
435
|
+
if block_given?
|
436
|
+
apply(style) {|node| yield node}
|
437
|
+
else
|
438
|
+
apply(style)
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
410
442
|
# This method provides a short cut means of creating a font command node.
|
411
443
|
# The method accepts a block that will be passed a single parameter which
|
412
444
|
# will be a reference to the font node created. After the block is
|
@@ -1660,4 +1692,4 @@ module RTF
|
|
1660
1692
|
text.string
|
1661
1693
|
end
|
1662
1694
|
end # End of the Document class.
|
1663
|
-
end # End of the RTF module.
|
1695
|
+
end # End of the RTF module.
|
data/test/command_node_test.rb
CHANGED
@@ -156,6 +156,16 @@ class CommandNodeTest < Test::Unit::TestCase
|
|
156
156
|
assert(node.prefix == '\super')
|
157
157
|
assert(node.suffix == nil)
|
158
158
|
assert(node == root[-1])
|
159
|
+
|
160
|
+
node = root.subscript
|
161
|
+
assert(node.prefix == '\sub')
|
162
|
+
assert(node.suffix == nil)
|
163
|
+
assert(node == root[-1])
|
164
|
+
|
165
|
+
node = root.strike
|
166
|
+
assert(node.prefix == '\strike')
|
167
|
+
assert(node.suffix == nil)
|
168
|
+
assert(node == root[-1])
|
159
169
|
end
|
160
170
|
|
161
171
|
# Test text node addition.
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 1
|
9
|
+
version: 0.3.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Peter Wood
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-09-14 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -26,11 +26,11 @@ extensions: []
|
|
26
26
|
|
27
27
|
extra_rdoc_files:
|
28
28
|
- LICENSE
|
29
|
-
- README
|
29
|
+
- README.rdoc
|
30
30
|
files:
|
31
31
|
- CHANGES
|
32
32
|
- LICENSE
|
33
|
-
- README
|
33
|
+
- README.rdoc
|
34
34
|
- Rakefile
|
35
35
|
- VERSION.yml
|
36
36
|
- examples/example01.rb
|
@@ -106,28 +106,28 @@ signing_key:
|
|
106
106
|
specification_version: 3
|
107
107
|
summary: Ruby library to create rich text format documents.
|
108
108
|
test_files:
|
109
|
-
- test/
|
110
|
-
- test/
|
109
|
+
- test/node_test.rb
|
110
|
+
- test/information_test.rb
|
111
|
+
- test/container_node_test.rb
|
112
|
+
- test/table_cell_node_test.rb
|
113
|
+
- test/character_style_test.rb
|
111
114
|
- test/paragraph_style_test.rb
|
112
|
-
- test/document_style_test.rb
|
113
115
|
- test/font_test.rb
|
114
|
-
- test/header_node_test.rb
|
115
|
-
- test/node_test.rb
|
116
|
-
- test/colour_table_test.rb
|
117
|
-
- test/test_helper.rb
|
118
116
|
- test/document_test.rb
|
119
|
-
- test/footer_node_test.rb
|
120
|
-
- test/command_node_test.rb
|
121
117
|
- test/style_test.rb
|
122
|
-
- test/
|
118
|
+
- test/font_table_test.rb
|
123
119
|
- test/text_node_test.rb
|
120
|
+
- test/table_node_test.rb
|
121
|
+
- test/colour_table_test.rb
|
124
122
|
- test/image_node_test.rb
|
123
|
+
- test/document_style_test.rb
|
124
|
+
- test/test_helper.rb
|
125
|
+
- test/footer_node_test.rb
|
126
|
+
- test/table_row_node_test.rb
|
125
127
|
- test/colour_test.rb
|
126
|
-
- test/
|
127
|
-
- test/
|
128
|
-
- test/table_node_test.rb
|
129
|
-
- test/table_cell_node_test.rb
|
130
|
-
- examples/example02.rb
|
128
|
+
- test/header_node_test.rb
|
129
|
+
- test/command_node_test.rb
|
131
130
|
- examples/example01.rb
|
131
|
+
- examples/example02.rb
|
132
132
|
- examples/example03.rb
|
133
133
|
- examples/example04.rb
|