class DocxGenerator::DSL::Text

Represent a text fragment with formatting options

Public Class Methods

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

Create a new text fragment with the text 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 text_fragment [String] The text fragment. @param options [Hash] Formatting options. @option options [Boolean] bold If the text should be in bold. @option options [Boolean] italics If the text should be in italics. @option options [Hash] underline The style of the underline and other options. See the specification for more details. @option options [Integer] size The size of the text (in points). @option options [Boolean] superscript If the text should be in superscript. @option options [Boolean] subscript If the text should be in subscript. @option options [Boolean] caps If the text should be displayed in capital letters. @option options [Boolean] small_caps If the text should be displayed in small capital letters. @option options [String] font The name of the font.

# File lib/docx_generator/dsl/text.rb, line 18
def initialize(text_fragment, options = {}, &block)
  @text_fragment = text_fragment
  @options = options
  yield self if block
end

Public Instance Methods

bold(value) click to toggle source

Set whether the text should be in bold or not. @param value [Boolean] Whether the text should be in bold or not.

# File lib/docx_generator/dsl/text.rb, line 26
def bold(value)
  @options[:bold] = value
end
caps(value) click to toggle source

Set whether the text should be displayed in capital letters. @param value [Boolean] Whether the text should be displayed in capital letters.

# File lib/docx_generator/dsl/text.rb, line 62
def caps(value)
  @options[:caps] = value
end
font(value) click to toggle source

Set the name of the font. @param value [String] The name of the font

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

Generate the XML element objects. @return [DocxGenerator::Word::Run] A Word::Run object representing the text fragment.

# File lib/docx_generator/dsl/text.rb, line 80
def generate
  options = generate_text_options
  text = Word::Text.new({}, [@text_fragment])
  if options
    Word::Run.new({}, [options, text])
  else
    Word::Run.new({}, [text])
  end
end
italics(value) click to toggle source

Set whether the text should be in italics or not. @param value [Boolean] Whether the text should be in italics or not.

# File lib/docx_generator/dsl/text.rb, line 32
def italics(value)
  @options[:italics] = value
end
size(value) click to toggle source

Set the size of the text (in points). @param value [Integer] The size of the text (in points).

# File lib/docx_generator/dsl/text.rb, line 44
def size(value)
  @options[:size] = value
end
small_caps(value) click to toggle source

Set whether the text should be displayed in small capital letters. @param value [Boolean] Whether the text should be displayed in small capital letters.

# File lib/docx_generator/dsl/text.rb, line 68
def small_caps(value)
  @options[:small_caps] = value
end
subscript(value) click to toggle source

Set whether the text should be in subscript. @param value [Boolean] Whether the text should be in subscript.

# File lib/docx_generator/dsl/text.rb, line 56
def subscript(value)
  @options[:subscript] = value
end
superscript(value) click to toggle source

Set whether the text should be in superscript. @param value [Boolean] Whether the text should be in superscript.

# File lib/docx_generator/dsl/text.rb, line 50
def superscript(value)
  @options[:superscript] = value
end
to_s() click to toggle source

Generate the XML representation of the text fragment

# File lib/docx_generator/dsl/text.rb, line 91
def to_s
  generate.to_s
end
underline(value) click to toggle source

Set the style of the underline and other options. See the specification for more details. @param value [Hash] The style of the underline and other options. See the specification for more details.

# File lib/docx_generator/dsl/text.rb, line 38
def underline(value)
  @options[:underline] = value
end

Private Instance Methods

generate_text_options() click to toggle source
# File lib/docx_generator/dsl/text.rb, line 96
def generate_text_options
  unless @options.empty?
    parsed_options = []
    @options.each do |option, value|
      parsed_options << parse_text_option(option, value)
    end
    Word::RunProperties.new(parsed_options)
  end
end
parse_text_option(option, value) click to toggle source
# File lib/docx_generator/dsl/text.rb, line 106
def parse_text_option(option, value)
  case option
    when :bold then Word::Bold.new(value)
    when :italics then Word::Italics.new(value)
    when :underline then Word::Underline.new(value)
    when :size then Word::Size.new(value)
    when :superscript then Word::VerticalAlign.new("superscript")
    when :subscript then Word::VerticalAlign.new("subscript")
    when :caps then Word::CapitalLetters.new(value)
    when :small_caps then Word::SmallCapitalLetters.new(value)
    when :font then Word::Font.new(value)
  end
end