Class: RRTF::CommandNode

Inherits:
ContainerNode show all
Defined in:
lib/rrtf/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.

Instance Attribute Summary collapse

Attributes inherited from ContainerNode

#children

Attributes inherited from Node

#parent

Instance Method Summary collapse

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, wrap = 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.

wrap

A boolean to indicate whether the prefix and suffix should be wrapped in curly braces. Defaults to true.



234
235
236
237
238
239
240
# File 'lib/rrtf/node.rb', line 234

def initialize(parent, prefix, suffix=nil, split=true, wrap=true)
   super(parent)
   @prefix = prefix
   @suffix = suffix
   @split  = split
   @wrap   = wrap
end

Instance Attribute Details

#prefixObject

String containing the prefix text for the command



211
212
213
# File 'lib/rrtf/node.rb', line 211

def prefix
  @prefix
end

#splitObject 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



217
218
219
# File 'lib/rrtf/node.rb', line 217

def split
  @split
end

#suffixObject

String containing the suffix text for the command



213
214
215
# File 'lib/rrtf/node.rb', line 213

def suffix
  @suffix
end

#wrapObject Also known as: wrap?

A boolean to indicate whether the prefix and suffix should be wrapped in curly braces. Defaults to true.



220
221
222
# File 'lib/rrtf/node.rb', line 220

def wrap
  @wrap
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.



248
249
250
251
252
253
254
# File 'lib/rrtf/node.rb', line 248

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.

Yields:

  • (node)


371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/rrtf/node.rb', line 371

def apply(style)
   # Check the input style.
   if !style.is_character_style?
      RTFError.fire("Non-character style specified to the "\
                    "CommandNode#apply() method.")
   end

   # Store fonts and colours.
   style.push_colours(root.colours)
   style.push_fonts(root.fonts)

   # Generate the command node.
   node = CommandNode.new(self, style.prefix(root))
   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.



530
531
532
533
534
535
536
537
538
539
# File 'lib/rrtf/node.rb', line 530

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

#boldObject

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.



393
394
395
396
397
398
399
400
401
# File 'lib/rrtf/node.rb', line 393

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.



551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/rrtf/node.rb', line 551

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.



489
490
491
492
493
494
495
496
497
498
499
# File 'lib/rrtf/node.rb', line 489

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.



335
336
337
338
339
340
341
342
343
# File 'lib/rrtf/node.rb', line 335

def footnote(text)
   if !text.nil? and 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.



510
511
512
513
514
515
516
517
518
519
# File 'lib/rrtf/node.rb', line 510

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.



354
355
356
# File 'lib/rrtf/node.rb', line 354

def image(source)
   self.store(ImageNode.new(self, source, root.get_id))
end

#italicObject

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.



408
409
410
411
412
413
414
415
416
# File 'lib/rrtf/node.rb', line 408

def italic
   style        = CharacterStyle.new
   style.italic = true
   if block_given?
      apply(style) {|node| yield node}
   else
      apply(style)
   end
end

#line_breakObject

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.



326
327
328
329
# File 'lib/rrtf/node.rb', line 326

def line_break
   self.store(CommandNode.new(self, '\line', nil, false))
   nil
end

Yields:

  • (node)


316
317
318
319
320
321
# File 'lib/rrtf/node.rb', line 316

def link(url, text=nil)
  node = LinkNode.new(self, url)
  node << text if text
  yield node   if block_given?
  self.store(node)
end

#list(kind = :bullets) {|node.list(kind)| ... } ⇒ Object

This method provides a short cut means of creating a new ordered or unordered list. The method requires a block that will be passed a single parameter that'll be a reference to the first level of the list. See the ListLevelNode doc for more information.

Example usage:

rtf.list do |level1|
  level1.item do |li|
    li << 'some text'
    li.apply(some_style) {|x| x << 'some styled text'}
  end

  level1.list(:decimal) do |level2|
    level2.item {|li| li << 'some other text in a decimal list'}
    level2.item {|li| li << 'and here we go'}
  end
end

Yields:



310
311
312
313
314
# File 'lib/rrtf/node.rb', line 310

def list(kind=:bullets)
  node = ListNode.new(self)
  yield node.list(kind)
  self.store(node)
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.

Yields:

  • (node)


285
286
287
288
289
# File 'lib/rrtf/node.rb', line 285

def paragraph(style=nil)
   node = ParagraphNode.new(self, style)
   yield node if block_given?
   self.store(node)
end

#strikeObject

This method provides a short cut means of creating a strike command node. The method accepts a block that will be passed a single parameter which will be a reference to the strike node created. After the block is complete the strike node is appended to the end of the child nodes on the object that the method is call against.



468
469
470
471
472
473
474
475
476
# File 'lib/rrtf/node.rb', line 468

def strike
   style        = CharacterStyle.new
   style.strike = true
   if block_given?
      apply(style) {|node| yield node}
   else
      apply(style)
   end
end

#subscriptObject

This method provides a short cut means of creating a subscript command node. The method accepts a block that will be passed a single parameter which will be a reference to the subscript node created. After the block is complete the subscript node is appended to the end of the child nodes on the object that the method is call against.



438
439
440
441
442
443
444
445
446
# File 'lib/rrtf/node.rb', line 438

def subscript
   style           = CharacterStyle.new
   style.subscript = true
   if block_given?
      apply(style) {|node| yield node}
   else
      apply(style)
   end
end

#superscriptObject

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.



453
454
455
456
457
458
459
460
461
# File 'lib/rrtf/node.rb', line 453

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.

Yields:

  • (node)


573
574
575
576
577
578
# File 'lib/rrtf/node.rb', line 573

def table(rows, columns, *widths)
   node = TableNode.new(self, rows, columns, *widths)
   yield node if block_given?
   store(node)
   node
end

#to_rtfObject

This method generates the RTF text for a CommandNode object.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/rrtf/node.rb', line 257

def to_rtf
   text = StringIO.new

   text << '{'       if wrap?
   text << @prefix   if @prefix

   self.each do |entry|
      text << "\n" if split?
      text << entry.to_rtf
   end

   text << "\n"    if split?
   text << @suffix if @suffix
   text << '}'     if wrap?

   text.string
end

#underlineObject

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.



423
424
425
426
427
428
429
430
431
# File 'lib/rrtf/node.rb', line 423

def underline
   style           = CharacterStyle.new
   style.underline = true
   if block_given?
      apply(style) {|node| yield node}
   else
      apply(style)
   end
end