Class: RTF::CommandNode
- Inherits:
-
ContainerNode
- Object
- Node
- ContainerNode
- RTF::CommandNode
- Defined in:
- lib/rtf/node.rb
Overview
This class represents a RTF command element within a document. This class is concrete enough to be used on its own but will also be used as the base class for some specific command node types.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#prefix ⇒ Object
String containing the prefix text for the command.
-
#split ⇒ Object
(also: #split?)
A boolean to indicate whether the prefix and suffix should be written to separate lines whether the node is converted to RTF.
-
#suffix ⇒ Object
String containing the suffix text for the command.
Attributes inherited from ContainerNode
Attributes inherited from Node
Instance Method Summary collapse
-
#<<(text) ⇒ Object
(also: #write)
This method adds text to a command node.
-
#apply(style) {|node| ... } ⇒ Object
This method provides a short cut means for applying multiple styles via single command node.
-
#background(colour) ⇒ Object
This method provides a short cut means of creating a background colour command node.
-
#bold ⇒ Object
This method provides a short cut means of creating a bold command node.
-
#colour(fore, back) ⇒ Object
(also: #color)
This method provides a short cut menas of creating a colour node that deals with foreground and background colours.
-
#font(font, size = nil) ⇒ Object
This method provides a short cut means of creating a font command node.
-
#footnote(text) ⇒ Object
This method inserts a footnote at the current position in a node.
-
#foreground(colour) ⇒ Object
This method provides a short cut means of creating a foreground colour command node.
-
#image(source) ⇒ Object
This method inserts a new image at the current position in a node.
-
#initialize(parent, prefix, suffix = nil, split = true) ⇒ CommandNode
constructor
This is the constructor for the CommandNode class.
-
#italic ⇒ Object
This method provides a short cut means of creating an italic command node.
-
#line_break ⇒ Object
This method provides a short cut means of creating a line break command node.
-
#paragraph(style = nil) {|node| ... } ⇒ Object
This method provides a short cut means of creating a paragraph command node.
-
#superscript ⇒ Object
This method provides a short cut means of creating a superscript command node.
-
#table(rows, columns, *widths) {|node| ... } ⇒ Object
This method creates a new table node and returns it.
-
#to_rtf ⇒ Object
This method generates the RTF text for a CommandNode object.
-
#underline ⇒ Object
This method provides a short cut means of creating an underline command node.
Methods inherited from ContainerNode
#[], #each, #first, #last, #size, #store
Methods inherited from Node
#is_root?, #next_node, #previous_node, #root
Constructor Details
#initialize(parent, prefix, suffix = nil, split = true) ⇒ CommandNode
This is the constructor for the CommandNode class.
Parameters
- parent
-
A reference to the node that owns the new node.
- prefix
-
A String containing the prefix text for the command.
- suffix
-
A String containing the suffix text for the command. Defaults to nil.
- split
-
A boolean to indicate whether the prefix and suffix should be written to separate lines whether the node is converted to RTF. Defaults to true.
239 240 241 242 243 244 |
# File 'lib/rtf/node.rb', line 239 def initialize(parent, prefix, suffix=nil, split=true) super(parent) @prefix = prefix @suffix = suffix @split = split end |
Instance Attribute Details
#prefix ⇒ Object
String containing the prefix text for the command
221 222 223 |
# File 'lib/rtf/node.rb', line 221 def prefix @prefix end |
#split ⇒ Object Also known as: split?
A boolean to indicate whether the prefix and suffix should be written to separate lines whether the node is converted to RTF. Defaults to true
227 228 229 |
# File 'lib/rtf/node.rb', line 227 def split @split end |
#suffix ⇒ Object
String containing the suffix text for the command
223 224 225 |
# File 'lib/rtf/node.rb', line 223 def suffix @suffix end |
Instance Method Details
#<<(text) ⇒ Object Also known as: write
This method adds text to a command node. If the last child node of the target node is a TextNode then the text is appended to that. Otherwise a new TextNode is created and append to the node.
Parameters
- text
-
The String of text to be written to the node.
252 253 254 255 256 257 258 |
# File 'lib/rtf/node.rb', line 252 def <<(text) if last != nil and last.respond_to?(:text=) last.append(text) else self.store(TextNode.new(self, text)) end end |
#apply(style) {|node| ... } ⇒ Object
This method provides a short cut means for applying multiple styles via single command node. The method accepts a block that will be passed a reference to the node created. Once the block is complete the new node will be append as the last child of the CommandNode the method is called on.
Parameters
- style
-
A reference to a CharacterStyle object that contains the style settings to be applied.
Exceptions
- RTFError
-
Generated whenever a non-character style is specified to the method.
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/rtf/node.rb', line 347 def apply(style) # Check the input style. if style.is_character_style? == false RTFError.fire("Non-character style specified to the "\ "CommandNode#apply() method.") end # Store fonts and colours. root.colours << style.foreground if style.foreground != nil root.colours << style.background if style.background != nil root.fonts << style.font if style.font != nil # Generate the command node. node = CommandNode.new(self, style.prefix(root.fonts, root.colours)) yield node if block_given? self.store(node) end |
#background(colour) ⇒ Object
This method provides a short cut means of creating a background colour command node. The method accepts a block that will be passed a single parameter which will be a reference to the background colour node created. After the block is complete the background colour node is appended to the end of the child nodes on the object that the method is called against.
Parameters
- colour
-
The background colour to be applied by the command.
477 478 479 480 481 482 483 484 485 486 |
# File 'lib/rtf/node.rb', line 477 def background(colour) style = CharacterStyle.new style.background = colour root.colours << colour if block_given? apply(style) {|node| yield node} else apply(style) end end |
#bold ⇒ Object
This method provides a short cut means of creating a bold command node. The method accepts a block that will be passed a single parameter which will be a reference to the bold node created. After the block is complete the bold node is appended to the end of the child nodes on the object that the method is call against.
370 371 372 373 374 375 376 377 378 |
# File 'lib/rtf/node.rb', line 370 def bold style = CharacterStyle.new style.bold = true if block_given? apply(style) {|node| yield node} else apply(style) end end |
#colour(fore, back) ⇒ Object Also known as: color
This method provides a short cut menas of creating a colour node that deals with foreground and background colours. The method accepts a block that will be passed a single parameter which will be a reference to the colour node created. After the block is complete the colour node is append to the end of the child nodes on the object that the method is called against.
Parameters
- fore
-
The foreground colour to be applied by the command.
- back
-
The background colour to be applied by the command.
498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/rtf/node.rb', line 498 def colour(fore, back) style = CharacterStyle.new style.foreground = fore style.background = back root.colours << fore root.colours << back if block_given? apply(style) {|node| yield node} else apply(style) end end |
#font(font, size = nil) ⇒ Object
This method provides a short cut means of creating a font command node. The method accepts a block that will be passed a single parameter which will be a reference to the font node created. After the block is complete the font node is appended to the end of the child nodes on the object that the method is called against.
Parameters
- font
-
A reference to font object that represents the font to be used within the node.
- size
-
An integer size setting for the font. Defaults to nil to indicate that the current font size should be used.
436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/rtf/node.rb', line 436 def font(font, size=nil) style = CharacterStyle.new style.font = font style.font_size = size root.fonts << font if block_given? apply(style) {|node| yield node} else apply(style) end end |
#footnote(text) ⇒ Object
This method inserts a footnote at the current position in a node.
Parameters
- text
-
A string containing the text for the footnote.
311 312 313 314 315 316 317 318 319 |
# File 'lib/rtf/node.rb', line 311 def footnote(text) if text != nil && text != '' mark = CommandNode.new(self, '\fs16\up6\chftn', nil, false) note = CommandNode.new(self, '\footnote {\fs16\up6\chftn}', nil, false) note.paragraph << text self.store(mark) self.store(note) end end |
#foreground(colour) ⇒ Object
This method provides a short cut means of creating a foreground colour command node. The method accepts a block that will be passed a single parameter which will be a reference to the foreground colour node created. After the block is complete the foreground colour node is appended to the end of the child nodes on the object that the method is called against.
Parameters
- colour
-
The foreground colour to be applied by the command.
457 458 459 460 461 462 463 464 465 466 |
# File 'lib/rtf/node.rb', line 457 def foreground(colour) style = CharacterStyle.new style.foreground = colour root.colours << colour if block_given? apply(style) {|node| yield node} else apply(style) end end |
#image(source) ⇒ Object
This method inserts a new image at the current position in a node.
Parameters
- source
-
Either a string containing the path and name of a file or a File object for the image file to be inserted.
Exceptions
- RTFError
-
Generated whenever an invalid or inaccessible file is specified or the image file type is not supported.
330 331 332 |
# File 'lib/rtf/node.rb', line 330 def image(source) self.store(ImageNode.new(self, source, root.get_id)) end |
#italic ⇒ Object
This method provides a short cut means of creating an italic command node. The method accepts a block that will be passed a single parameter which will be a reference to the italic node created. After the block is complete the italic node is appended to the end of the child nodes on the object that the method is call against.
385 386 387 388 389 390 391 392 393 |
# File 'lib/rtf/node.rb', line 385 def italic style = CharacterStyle.new style.italic = true if block_given? apply(style) {|node| yield node} else apply(style) end end |
#line_break ⇒ Object
This method provides a short cut means of creating a line break command node. This command node does not take a block and may possess no other content.
302 303 304 305 |
# File 'lib/rtf/node.rb', line 302 def line_break self.store(CommandNode.new(self, '\line', nil, false)) nil end |
#paragraph(style = nil) {|node| ... } ⇒ Object
This method provides a short cut means of creating a paragraph command node. The method accepts a block that will be passed a single parameter which will be a reference to the paragraph node created. After the block is complete the paragraph node is appended to the end of the child nodes on the object that the method is called against.
Parameters
- style
-
A reference to a ParagraphStyle object that defines the style for the new paragraph. Defaults to nil to indicate that the currently applied paragraph styling should be used.
288 289 290 291 292 293 294 295 296 297 |
# File 'lib/rtf/node.rb', line 288 def paragraph(style=nil) # Create the node prefix. text = StringIO.new text << '\pard' text << style.prefix(nil, nil) if style != nil node = CommandNode.new(self, text.string, '\par') yield node if block_given? self.store(node) end |
#superscript ⇒ Object
This method provides a short cut means of creating a superscript command node. The method accepts a block that will be passed a single parameter which will be a reference to the superscript node created. After the block is complete the superscript node is appended to the end of the child nodes on the object that the method is call against.
415 416 417 418 419 420 421 422 423 |
# File 'lib/rtf/node.rb', line 415 def superscript style = CharacterStyle.new style.superscript = true if block_given? apply(style) {|node| yield node} else apply(style) end end |
#table(rows, columns, *widths) {|node| ... } ⇒ Object
This method creates a new table node and returns it. The method accepts a block that will be passed the table as a parameter. The node is added to the node the method is called upon after the block is complete.
Parameters
- rows
-
The number of rows that the table contains.
- columns
-
The number of columns that the table contains.
- *widths
-
One or more integers representing the widths for the table columns.
520 521 522 523 524 525 |
# File 'lib/rtf/node.rb', line 520 def table(rows, columns, *widths) node = TableNode.new(self, rows, columns, *widths) yield node if block_given? store(node) node end |
#to_rtf ⇒ Object
This method generates the RTF text for a CommandNode object.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/rtf/node.rb', line 261 def to_rtf text = StringIO.new separator = split? ? "\n" : " " line = (separator == " ") text << "{#{@prefix}" text << separator if self.size > 0 self.each do |entry| text << "\n" if line line = true text << "#{entry.to_rtf}" end text << "\n" if split? text << "#{@suffix}}" text.string end |
#underline ⇒ Object
This method provides a short cut means of creating an underline command node. The method accepts a block that will be passed a single parameter which will be a reference to the underline node created. After the block is complete the underline node is appended to the end of the child nodes on the object that the method is call against.
400 401 402 403 404 405 406 407 408 |
# File 'lib/rtf/node.rb', line 400 def underline style = CharacterStyle.new style.underline = true if block_given? apply(style) {|node| yield node} else apply(style) end end |