class DocxGenerator::DSL::Paragraph

Represent a paragraph with formatting options

Public Class Methods

new(options = {}) { |self| ... } click to toggle source

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). @param options [Hash] Formatting options. @option options [Boolean] alignment The alignment of the paragraph. See the specification for the complete list. @option options [Hash] spacing Various spacing options for the paragraph. See the specification for more details. @option options [Hash] indentation Various indentation properties for the paragraph. See the specification for more details. @option options [Array] tabs Custom tabulation stops. See the specification for more details.

# File lib/docx_generator/dsl/paragraph.rb, line 12
def initialize(options = {}, &block)
  @objects = []
  @options = options
  yield self if block
end

Public Instance Methods

add(*objects) click to toggle source

Add other objects to the paragraph. @param objects [Object] Objects (like text fragments). @return [DocxGenerator::DSL::Paragraph] The paragraph object.

# File lib/docx_generator/dsl/paragraph.rb, line 81
def add(*objects)
  objects.each do |object|
    @objects << object
  end
  self
end
alignment(value) click to toggle source

Set the alignment of the paragraph. See the specification for more details. @param value [String] The alignment of the paragraph. See the specification for the complete list.

# File lib/docx_generator/dsl/paragraph.rb, line 20
def alignment(value)
  @options[:alignment] = value
end
generate() click to toggle source

Generate the XML element objects. @return [DocxGenerator::Word::Paragraph] A Word::Paragraph object representing the paragraph.

# 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) click to toggle source

Set various indentation properties for the paragraph. See the specification for more details. @param properties [Hash] Various indentation properties for the paragraph. See the specification for more details.

# File lib/docx_generator/dsl/paragraph.rb, line 32
def indentation(properties)
  @options[:indentation] = properties
end
newline() click to toggle source

Add a newline

# File lib/docx_generator/dsl/paragraph.rb, line 48
def newline
  @objects << DocxGenerator::Word::Extensions::Newline.new
end
no_space() click to toggle source

Prevent the addition of a space between two text fragments.

# File lib/docx_generator/dsl/paragraph.rb, line 43
def no_space
  @objects << DocxGenerator::Word::Extensions::NoSpace.new
end
spacing(options) click to toggle source

Set various spacing options for the paragraph. See the specification for more details. @param options [Hash] Various spacing options for the paragraph. See the specification for more details.

# File lib/docx_generator/dsl/paragraph.rb, line 26
def spacing(options)
  @options[:spacing] = options
end
tab() click to toggle source

Add a tabulation

# File lib/docx_generator/dsl/paragraph.rb, line 53
def tab
  @objects << DocxGenerator::Word::Tab.new
end
tabs(tab_stops) click to toggle source

Set custom tabulation stops. See the specification for more details. @param tab_stops [Array] Custom tabulation stops.

# File lib/docx_generator/dsl/paragraph.rb, line 38
def tabs(tab_stops)
  @options[:tabs] = tab_stops
end
text(text_fragment, options = {}) { |text_object| ... } click to toggle source

Add a new text fragment to the paragraph. @param text_fragment [String] The text fragment. @param options [Hash] Formatting options for the text fragment. See the full list in DocxGenerator::DSL::Text.

# 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

Private Instance Methods

generate_paragraph_options() click to toggle source
# File lib/docx_generator/dsl/paragraph.rb, line 105
def generate_paragraph_options
  unless @options.empty?
    parsed_options = []
    @options.each do |option, value|
      parsed_options << parse_paragraph_option(option, value)
    end
    Word::ParagraphProperties.new(parsed_options)
  end
end
generate_text_fragments() click to toggle source
# File lib/docx_generator/dsl/paragraph.rb, line 89
def generate_text_fragments
  text_fragments = []
  @objects.each do |object|
    if object.class == DocxGenerator::Word::Extensions::NoSpace
      text_fragments.pop
    elsif object.class == DocxGenerator::Word::Extensions::Newline
      text_fragments.pop
      text_fragments << object.generate
    elsif object.respond_to?(:generate)
      text_fragments << object.generate << Word::Extensions.space
    end
  end
  text_fragments.pop if text_fragments.last.to_s == Word::Extensions.space.to_s # In order to remove the last space added
  text_fragments
end
parse_paragraph_option(option, value) click to toggle source
# File lib/docx_generator/dsl/paragraph.rb, line 115
def parse_paragraph_option(option, value)
  case option
    when :alignment then Word::Alignment.new(value)
    when :spacing then Word::Spacing.new(value)
    when :indentation then Word::Indentation.new(value)
    when :tabs then Word::Tabs.new(value)
  end
end