Class: DocxGenerator::DSL::Paragraph

Inherits:
Object
  • Object
show all
Defined in:
lib/docx_generator/dsl/paragraph.rb

Overview

Represent a paragraph with formatting options

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Paragraph

Create a new paragraph with the formatting options specified. The formatting properties can be passed with a Hash or they could be set by calling the methods on the object (either in the block or not).

Parameters:

  • options (Hash) (defaults to: {})

    Formatting options.

Options Hash (options):

  • alignment (Boolean)

    The alignment of the paragraph. See the specification for the complete list.

  • spacing (Hash)

    Various spacing options for the paragraph. See the specification for more details.

  • indentation (Hash)

    Various indentation properties for the paragraph. See the specification for more details.

  • tabs (Array)

    Custom tabulation stops. See the specification for more details.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
# File 'lib/docx_generator/dsl/paragraph.rb', line 12

def initialize(options = {}, &block)
  @objects = []
  @options = options
  yield self if block
end

Instance Method Details

#add(*objects) ⇒ DocxGenerator::DSL::Paragraph

Add other objects to the paragraph.

Parameters:

  • objects (Object)

    Objects (like text fragments).

Returns:



81
82
83
84
85
86
# File 'lib/docx_generator/dsl/paragraph.rb', line 81

def add(*objects)
  objects.each do |object|
    @objects << object
  end
  self
end

#alignment(value) ⇒ Object

Set the alignment of the paragraph. See the specification for more details.

Parameters:

  • value (String)

    The alignment of the paragraph. See the specification for the complete list.



20
21
22
# File 'lib/docx_generator/dsl/paragraph.rb', line 20

def alignment(value)
  @options[:alignment] = value
end

#generateDocxGenerator::Word::Paragraph

Generate the XML element objects.

Returns:



68
69
70
71
72
73
74
75
76
# File 'lib/docx_generator/dsl/paragraph.rb', line 68

def generate
  text_fragments = generate_text_fragments
  options = generate_paragraph_options
  if options
    Word::Paragraph.new({}, text_fragments.unshift(options))
  else
    Word::Paragraph.new({}, text_fragments)
  end
end

#indentation(properties) ⇒ Object

Set various indentation properties for the paragraph. See the specification for more details.

Parameters:

  • properties (Hash)

    Various indentation properties for the paragraph. See the specification for more details.



32
33
34
# File 'lib/docx_generator/dsl/paragraph.rb', line 32

def indentation(properties)
  @options[:indentation] = properties
end

#newlineObject

Add a newline



48
49
50
# File 'lib/docx_generator/dsl/paragraph.rb', line 48

def newline
  @objects << DocxGenerator::Word::Extensions::Newline.new
end

#no_spaceObject

Prevent the addition of a space between two text fragments.



43
44
45
# File 'lib/docx_generator/dsl/paragraph.rb', line 43

def no_space
  @objects << DocxGenerator::Word::Extensions::NoSpace.new
end

#spacing(options) ⇒ Object

Set various spacing options for the paragraph. See the specification for more details.

Parameters:

  • options (Hash)

    Various spacing options for the paragraph. See the specification for more details.



26
27
28
# File 'lib/docx_generator/dsl/paragraph.rb', line 26

def spacing(options)
  @options[:spacing] = options
end

#tabObject

Add a tabulation



53
54
55
# File 'lib/docx_generator/dsl/paragraph.rb', line 53

def tab
  @objects << DocxGenerator::Word::Tab.new
end

#tabs(tab_stops) ⇒ Object

Set custom tabulation stops. See the specification for more details.

Parameters:

  • tab_stops (Array)

    Custom tabulation stops.



38
39
40
# File 'lib/docx_generator/dsl/paragraph.rb', line 38

def tabs(tab_stops)
  @options[:tabs] = tab_stops
end

#text(text_fragment, options = {}) {|text_object| ... } ⇒ Object

Add a new text fragment to the paragraph.

Parameters:

  • text_fragment (String)

    The text fragment.

  • options (Hash) (defaults to: {})

    Formatting options for the text fragment. See the full list in DocxGenerator::DSL::Text.

Yields:

  • (text_object)


60
61
62
63
64
# File 'lib/docx_generator/dsl/paragraph.rb', line 60

def text(text_fragment, options = {}, &block)
  text_object = DocxGenerator::DSL::Text.new(text_fragment, options)
  yield text_object if block
  @objects << text_object
end