Module: RRTF::SectionFormatting

Included in:
SectionStyle
Defined in:
lib/rrtf/formatting.rb

Overview

Section formatting attributes and methods.

Author:

  • Wesley Hileman

Since:

  • 1.0.0

Constant Summary

SECTION_ATTRIBUTES =

Formatting attributes that can be applied to an RTF document section.

Returns:

  • (Hash<String, Hash>)

    a hash mapping each attribute to a hash that describes (1) the attribute's default value, (2) how to parse the attribute from the user, and (3) how to convert the attribute to an RTF sequence.

Since:

  • 1.0.0

{
  "columns" => {
    "default" => nil,
    "to_rtf" => lambda{ |value| "\\cols#{value}" unless value.nil? }
  },
  "column_spacing" => {
    "default" => nil,
    "from_user" => lambda{ |value| RRTF::Utilities.value2twips(value) },
    "to_rtf" => lambda{ |value| "\\colsx#{value}" unless value.nil? }
  },
  "mirror_margins" => {
    "default" => nil,
    "to_rtf" => lambda{ |value| "\\margmirrorsxn" if value }
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Generates attribute accessors for all section attributes when the module is included in another module or class.

Since:

  • 1.0.0



928
929
930
931
932
933
934
935
# File 'lib/rrtf/formatting.rb', line 928

def self.included(base)
  # define accessors in base for document attributes
  base.class_eval do
    SECTION_ATTRIBUTES.each do |key, options|
      attr_accessor :#{key}"
    end # each
  end # class_eval
end

Instance Method Details

#initialize_section_formatting(options = {}) ⇒ Object

Initializes section formatting attributes.

Parameters:

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

    the section formatting options.

Options Hash (options):

  • "columns" (Integer) — default: nil

    the number of columns in the section.

  • "column_spacing" (String, Integer) — default: nil

    the column spacing in twips (can also be a string, see Utilities.value2twips).

  • "mirror_margins" (Boolean) — default: nil

    whether or not to enable mirrored margins (when facing pages is enabled) in the document.

Since:

  • 1.0.0



943
944
945
946
947
948
949
950
# File 'lib/rrtf/formatting.rb', line 943

def initialize_section_formatting(options = {})
  # load default attribute values
  SECTION_ATTRIBUTES.each do |key, options|
    send("#{key}=", options["default"])
  end # each
  # overwrite default attribute values with given values
  set_section_formatting_from_hashmap(options)
end

#section_formatting_to_rtfString

Generates an RTF string representing all applied section formatting.

Returns:

  • (String)

    RTF string.

Since:

  • 1.0.0



972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'lib/rrtf/formatting.rb', line 972

def section_formatting_to_rtf
  text = StringIO.new

  # accumulate RTF representations of section attributes
  SECTION_ATTRIBUTES.each do |key, options|
    if options.has_key?("to_rtf")
      rtf = options["to_rtf"].call(send(key))
      text << rtf unless rtf.nil?
    end # if
  end # each

  text.string
end

#set_section_formatting_from_hashmap(hash) ⇒ Object

Sets section formatting attributes according to the supplied hashmap.

See Also:

Since:

  • 1.0.0



954
955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/rrtf/formatting.rb', line 954

def set_section_formatting_from_hashmap(hash)
  hash.each do |attribute, value|
    # skip unreconized attributes
    next unless(SECTION_ATTRIBUTES.keys.include?(attribute))
    # preprocess value if nessesary
    if SECTION_ATTRIBUTES[attribute].has_key?("from_user")
      value = SECTION_ATTRIBUTES[attribute]["from_user"].call(value)
    elsif SECTION_ATTRIBUTES[attribute].has_key?("dictionary") && value.is_a?(String)
      value = SECTION_ATTRIBUTES[attribute]["dictionary"][value]
    end # if
    # set attribute value
    send("#{attribute}=", value)
  end # each
end